Skip to content

Commit

Permalink
test: Improve LPVSDetectService branch coverage (#367)
Browse files Browse the repository at this point in the history
* getGitHubWebhookConfig method covered in full

Signed-off-by: Vasyl Kerimov <[email protected]>

* more branches in DetectService covered

Signed-off-by: Vasyl Kerimov <[email protected]>

* Style fix

Signed-off-by: Vasyl Kerimov <[email protected]>

---------

Signed-off-by: Vasyl Kerimov <[email protected]>
  • Loading branch information
v-kerimov authored Dec 20, 2023
1 parent bb1405c commit 6e68ffa
Showing 1 changed file with 166 additions and 0 deletions.
166 changes: 166 additions & 0 deletions src/test/java/com/lpvs/service/LPVSDetectServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,21 @@ void setUp() throws IOException {
when(github_mock.connectToGitHubApi()).thenReturn(mockGitHub);
}

@Test
void testRunOneScanWithNullTriger() throws NoSuchFieldException, IllegalAccessException {
lpvsDetectService =
spy(new LPVSDetectService("scanoss", null, scanossDetectService, null));

setPrivateField(lpvsDetectService, "trigger", null);
setPrivateField(lpvsDetectService, "eventPublisher", mockEventPublisher);

assertDoesNotThrow(() -> lpvsDetectService.runOneScan());

setPrivateField(lpvsDetectService, "trigger", "");

assertDoesNotThrow(() -> lpvsDetectService.runOneScan());
}

@Test
void testRunOneScan_Default() throws NoSuchFieldException, IllegalAccessException {

Expand All @@ -137,6 +152,84 @@ void testRunOneScan_Default() throws NoSuchFieldException, IllegalAccessExceptio
assertDoesNotThrow(() -> lpvsDetectService.runOneScan());
}

@Test
void testRunOneScan_Branch2()
throws NoSuchFieldException, IllegalAccessException, IOException {
LPVSLicenseService.Conflict<String, String> conflict_1 =
new LPVSLicenseService.Conflict<>("MIT", "Apache-2.0");

List<LPVSLicenseService.Conflict<String, String>> expected =
List.of(conflict_1, conflict_1);

lpvsDetectService =
spy(new LPVSDetectService("scanoss", null, scanossDetectService, null));

// Mock the necessary GitHub objects for LPVSQueue
when(mockGitHub.getRepository(any())).thenReturn(mockRepository);
when(mockRepository.getPullRequest(anyInt())).thenReturn(mockPullRequest);
when(mockRepository.getPullRequest(anyInt())).thenReturn(mockPullRequest);
when(mockPullRequest.getHead()).thenReturn(mockCommitPointer);
when(licenseservice_mock.findConflicts(webhookConfig, null)).thenReturn(expected);
when(mockCommitPointer.getRepository()).thenReturn(mockHeadRepository);
when(mockHeadRepository.getHtmlUrl())
.thenReturn(new URL("https://example.com/repo/files"));

// Set up expected values
String expectedPullRequestUrl = "https://example.com/pull/1";
when(mockPullRequest.getHtmlUrl()).thenReturn(new URL(expectedPullRequestUrl));

setPrivateField(detectService, "trigger", "github/owner/repo/branch/123");
setPrivateField(detectService, "scannerType", "scanoss");
setPrivateField(detectService, "eventPublisher", mockEventPublisher);
setPrivateField(detectService, "htmlReport", null);

doNothing().when(mockEventPublisher).publishEvent(any());

detectService.runOneScan();

setPrivateField(detectService, "htmlReport", "");

detectService.runOneScan();
}

@Test
void testRunOneScan_Branch3()
throws NoSuchFieldException, IllegalAccessException, IOException {
LPVSLicenseService.Conflict<String, String> conflict_1 =
new LPVSLicenseService.Conflict<>("MIT", "Apache-2.0");

List<LPVSLicenseService.Conflict<String, String>> expected =
List.of(conflict_1, conflict_1);

doNothing().when(mockEventPublisher).publishEvent(any());

lpvsDetectService =
spy(new LPVSDetectService("scanoss", null, scanossDetectService, null));

setPrivateField(detectService, "trigger", "github/owner/repo/branch/123");
setPrivateField(detectService, "scannerType", "scanoss");
setPrivateField(detectService, "htmlReport", "build");
setPrivateField(detectService, "eventPublisher", mockEventPublisher);

// Mock the necessary GitHub objects for LPVSQueue
when(mockGitHub.getRepository(any())).thenReturn(mockRepository);
when(mockRepository.getPullRequest(anyInt())).thenReturn(mockPullRequest);
when(mockRepository.getPullRequest(anyInt())).thenReturn(mockPullRequest);
when(mockPullRequest.getHead()).thenReturn(mockCommitPointer);
when(licenseservice_mock.findConflicts(webhookConfig, null)).thenReturn(expected);
when(mockCommitPointer.getRepository()).thenReturn(mockHeadRepository);
when(mockHeadRepository.getHtmlUrl())
.thenReturn(new URL("https://example.com/repo/files"));

// Set up expected values
String expectedPullRequestUrl = "https://example.com/pull/1";
when(mockPullRequest.getHtmlUrl()).thenReturn(new URL(expectedPullRequestUrl));

detectService.runOneScan();

assertDoesNotThrow(() -> detectService.runOneScan());
}

@Test
void testRunOneScan_trigerInternalQueueException()
throws NoSuchFieldException, IllegalAccessException {
Expand Down Expand Up @@ -181,6 +274,73 @@ void testRunOneScan_TriggerNotNull() throws Exception {
assertDoesNotThrow(() -> detectService.runOneScan());
}

@Test
void testRunOneScan_TriggerNotNull_Branch2() throws Exception {

LPVSLicenseService.Conflict<String, String> conflict_1 =
new LPVSLicenseService.Conflict<>("MIT", "Apache-2.0");

List<LPVSLicenseService.Conflict<String, String>> expected =
List.of(conflict_1, conflict_1);

setPrivateField(detectService, "trigger", "github/owner/repo/branch/123");
setPrivateField(detectService, "scannerType", "scanoss");
setPrivateField(detectService, "htmlReport", "build/report/test.html");
setPrivateField(detectService, "eventPublisher", mockEventPublisher);

// Mock the necessary GitHub objects for LPVSQueue
when(mockGitHub.getRepository(any())).thenReturn(mockRepository);
when(mockRepository.getPullRequest(anyInt())).thenReturn(mockPullRequest);
when(mockRepository.getPullRequest(anyInt())).thenReturn(mockPullRequest);
when(mockPullRequest.getHead()).thenReturn(mockCommitPointer);
when(licenseservice_mock.findConflicts(webhookConfig, null)).thenReturn(expected);
when(mockCommitPointer.getRepository()).thenReturn(null);
when(mockHeadRepository.getHtmlUrl())
.thenReturn(new URL("https://example.com/repo/files"));

// Set up expected values
String expectedPullRequestUrl = "https://example.com/pull/1";
when(mockPullRequest.getHtmlUrl()).thenReturn(new URL(expectedPullRequestUrl));

detectService.runOneScan();

assertDoesNotThrow(() -> detectService.runOneScan());
}

@Test
void testRunOneScan_TriggerNotNull_Branch3() throws Exception {
GHRepository mockHeadRepository2 = mock(GHRepository.class);

LPVSLicenseService.Conflict<String, String> conflict_1 =
new LPVSLicenseService.Conflict<>("MIT", "Apache-2.0");

List<LPVSLicenseService.Conflict<String, String>> expected =
List.of(conflict_1, conflict_1);

setPrivateField(detectService, "trigger", "github/owner/repo/branch/123");
setPrivateField(detectService, "scannerType", "scanoss");
setPrivateField(detectService, "htmlReport", "build/report/test.html");
setPrivateField(detectService, "eventPublisher", mockEventPublisher);

// Mock the necessary GitHub objects for LPVSQueue
when(mockGitHub.getRepository(any())).thenReturn(mockRepository);
when(mockRepository.getPullRequest(anyInt())).thenReturn(mockPullRequest);
when(mockRepository.getPullRequest(anyInt())).thenReturn(mockPullRequest);
when(mockPullRequest.getHead()).thenReturn(mockCommitPointer);
when(licenseservice_mock.findConflicts(webhookConfig, null)).thenReturn(expected);
when(mockCommitPointer.getRepository()).thenReturn(mockHeadRepository2);
when(mockHeadRepository2.getHtmlUrl()).thenReturn(null);

// Set up expected values
String expectedPullRequestUrl = "https://example.com/pull/1";
when(mockRepository.getHtmlUrl()).thenReturn(new URL(expectedPullRequestUrl));
// setPrivateField(mockPullRequest, "url", expectedPullRequestUrl);

detectService.runOneScan();

assertDoesNotThrow(() -> detectService.runOneScan());
}

@Test
void testCommentBuilder_ConflictFilePresent() throws Exception {

Expand All @@ -201,6 +361,12 @@ void testCommentBuilder_ConflictFilePresent() throws Exception {
assertNotNull(commentHTML);
}

@Test
void testGetInternalQueueByPullRequestWithNull() throws IOException {
LPVSQueue result = lpvsDetectService.getInternalQueueByPullRequest(null);
assertNull(result);
}

@Test
void testGetInternalQueueByPullRequest() throws IOException {
String pullRequest = "github/owner/repo/branch/123";
Expand Down

0 comments on commit 6e68ffa

Please sign in to comment.