|
| 1 | +package org.kohsuke.github; |
| 2 | + |
| 3 | +import org.junit.Assume; |
| 4 | +import org.junit.Before; |
| 5 | +import org.junit.Test; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 11 | +import static org.hamcrest.Matchers.not; |
| 12 | + |
| 13 | +/** |
| 14 | + * <p> |
| 15 | + * Note : As the code scanning alerts cannot be tailored as part of test setup, lot of the test cases are dependent on |
| 16 | + * manual setup of the mock repo. Assertions and verifications will often simply check that the values are non-null |
| 17 | + * rather than depending on hard-coded values, to prevent making the tests flimsy |
| 18 | + * </p> |
| 19 | + */ |
| 20 | +public class GHCodeScanningAlertInstanceTest extends AbstractGitHubWireMockTest { |
| 21 | + private static final String REPO_NAME = "Pixi"; |
| 22 | + private GHCodeScanningAlert alert; |
| 23 | + |
| 24 | + @Before |
| 25 | + public void setUp() throws Exception { |
| 26 | + GHRepository repo = gitHub.getRepository(GITHUB_API_TEST_ORG + "/" + REPO_NAME); |
| 27 | + alert = getAlertFromRepo(repo); |
| 28 | + } |
| 29 | + |
| 30 | + private GHCodeScanningAlert getAlertFromRepo(GHRepository repo) { |
| 31 | + List<GHCodeScanningAlert> dismissedAlerts = repo.listCodeScanningAlerts(GHCodeScanningAlertState.DISMISSED) |
| 32 | + ._iterator(1) |
| 33 | + .nextPage(); |
| 34 | + Assume.assumeThat(dismissedAlerts.size(), greaterThanOrEqualTo(1)); |
| 35 | + return dismissedAlerts.get(0); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testListAlertInstances() throws IOException { |
| 40 | + // Arrange |
| 41 | + |
| 42 | + // Act |
| 43 | + List<GHCodeScanningAlertInstance> results = alert.listAlertInstances().toList(); |
| 44 | + |
| 45 | + // Assert |
| 46 | + assertThat(results.size(), greaterThanOrEqualTo(1)); |
| 47 | + GHCodeScanningAlertInstance instance = results.get(0); |
| 48 | + // Can't assert on exact values with having to hardcode values from |
| 49 | + // json file, hence making the assertions generics |
| 50 | + assertThat(instance.getRef(), not((Object) null)); |
| 51 | + assertThat(instance.getCommitSha(), not((Object) null)); |
| 52 | + assertThat(instance.getState(), not((Object) null)); |
| 53 | + assertThat(instance.getMessage(), not((Object) null)); |
| 54 | + assertThat(instance.getLocation(), not((Object) null)); |
| 55 | + |
| 56 | + GHCodeScanningAlertInstance.Location location = instance.getLocation(); |
| 57 | + // Can't assert on exact values with having to hardcode values from |
| 58 | + // json file, hence making the assertions generics |
| 59 | + assertThat(location.getPath(), not((Object) null)); |
| 60 | + assertThat(location.getStartLine(), greaterThanOrEqualTo(0L)); |
| 61 | + assertThat(location.getEndLine(), greaterThanOrEqualTo(0L)); |
| 62 | + assertThat(location.getStartColumn(), greaterThanOrEqualTo(0L)); |
| 63 | + assertThat(location.getStartColumn(), greaterThanOrEqualTo(0L)); |
| 64 | + } |
| 65 | +} |
0 commit comments