|
| 1 | +package org.keycloak.gh.bot; |
| 2 | + |
| 3 | +import io.quarkiverse.githubapp.GitHubEvent; |
| 4 | +import io.vertx.core.json.JsonObject; |
| 5 | +import org.junit.jupiter.api.AfterEach; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.kohsuke.github.GHIssue; |
| 8 | +import org.kohsuke.github.GHRepository; |
| 9 | +import org.kohsuke.github.GitHub; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.InputStream; |
| 13 | +import java.nio.charset.StandardCharsets; |
| 14 | + |
| 15 | +import static org.mockito.Mockito.mock; |
| 16 | +import static org.mockito.Mockito.verify; |
| 17 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 18 | +import static org.mockito.Mockito.when; |
| 19 | + |
| 20 | +public class SyncKindLabelsTest { |
| 21 | + |
| 22 | + private JsonObject payload; |
| 23 | + private GitHub gitHub; |
| 24 | + private GHRepository repository; |
| 25 | + private GitHubEvent event; |
| 26 | + private GHIssue issue; |
| 27 | + |
| 28 | + private void runEvent(boolean typed, String payloadResource) throws IOException { |
| 29 | + payload = new JsonObject(getResource(payloadResource)); |
| 30 | + |
| 31 | + gitHub = mock(GitHub.class); |
| 32 | + event = mock(GitHubEvent.class); |
| 33 | + repository = mock(GHRepository.class); |
| 34 | + issue = mock(GHIssue.class); |
| 35 | + |
| 36 | + when(event.getParsedPayload()).thenReturn(payload); |
| 37 | + when(event.getRepositoryOrThrow()).thenReturn("myorg/myrep"); |
| 38 | + when(gitHub.getRepository("myorg/myrep")).thenReturn(repository); |
| 39 | + when(repository.getIssue(1234)).thenReturn(issue); |
| 40 | + |
| 41 | + SyncKindLabels syncKindLabels = new SyncKindLabels(); |
| 42 | + if (typed) { |
| 43 | + syncKindLabels.onTyped(event, gitHub); |
| 44 | + } else { |
| 45 | + syncKindLabels.onUntyped(event, gitHub); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @AfterEach |
| 50 | + public void after() { |
| 51 | + verifyNoMoreInteractions(issue); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testTypedLabelMissing() throws IOException { |
| 56 | + runEvent(true, "typed-label-missing.json"); |
| 57 | + verify(issue).addLabels("kind/cve"); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testTypedLabelMissingHasAnother() throws IOException { |
| 62 | + runEvent(true, "typed-label-missing-has-another.json"); |
| 63 | + verify(issue).addLabels("kind/cve"); |
| 64 | + verify(issue).removeLabels("kind/bug"); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testTypedLabelMatches() throws IOException { |
| 69 | + runEvent(true, "typed-label-matches.json"); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testTypedLabelMatchesHasAnother() throws IOException { |
| 74 | + runEvent(true, "typed-label-matches-has-another.json"); |
| 75 | + verify(issue).removeLabels("kind/bug"); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testUntypedWithLabels() throws IOException { |
| 80 | + runEvent(false,"untyped-with-labels.json"); |
| 81 | + verify(issue).removeLabels("kind/bug", "kind/cve"); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testUntypedWithoutLabels() throws IOException { |
| 86 | + runEvent(false,"untyped-without-labels.json"); |
| 87 | + } |
| 88 | + |
| 89 | + private String getResource(String name) throws IOException { |
| 90 | + try (InputStream is = getClass().getResourceAsStream(name)) { |
| 91 | + if (is == null) { |
| 92 | + throw new RuntimeException("Resource not found " + name); |
| 93 | + } |
| 94 | + return new String(is.readAllBytes(), StandardCharsets.UTF_8); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments