Skip to content

Commit

Permalink
fix: Improve code coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Oleg Kopysov <o.kopysov@samsung.com>
  • Loading branch information
o-kopysov committed Aug 1, 2024
1 parent 60ec69c commit 16b8214
Showing 1 changed file with 55 additions and 2 deletions.
57 changes: 55 additions & 2 deletions src/test/java/com/lpvs/service/scan/LPVSDetectServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,21 @@ void testRunOneScanBothPullRequestAndLocalFile()
assertDoesNotThrow(() -> lpvsDetectService.runSingleScan());
}

@Test
void testRunOneScanBothPullRequestAndLocalFile2()
throws NoSuchFieldException, IllegalAccessException {
lpvsDetectService =
spy(
new LPVSDetectService(
"scanoss", false, null, null, null, scanServiceFactory_mock));

setPrivateField(lpvsDetectService, "ctx", mockApplicationContext);
setPrivateField(lpvsDetectService, "trigger", "some-pull-request");
setPrivateField(lpvsDetectService, "localPath", "some-local-path");

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

@Test
void testRunOneScan_PullRequest_Default()
throws NoSuchFieldException, IllegalAccessException {
Expand All @@ -203,7 +218,6 @@ void testRunOneScan_PullRequest_Default()

setPrivateField(lpvsDetectService, "trigger", "fake-trigger-value");
setPrivateField(lpvsDetectService, "ctx", mockApplicationContext);
setPrivateField(lpvsDetectService, "licenseService", licenseservice_mock);
setPrivateField(lpvsDetectService, "gitHubService", githubservice_mock);

assertDoesNotThrow(() -> lpvsDetectService.runSingleScan());
Expand Down Expand Up @@ -602,7 +616,46 @@ void testRunOneScan_TriggerNotNull_Branch3() throws Exception {
}

@Test
void testCommentBuilder_ConflictFilePresent() throws Exception {
void testRunOneScan_TriggerNotNull_NoDirectory() 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, "htmlReport", "report/test.html");
setPrivateField(detectService, "ctx", mockApplicationContext);
setPrivateField(detectService, "scanService", scanoss_mock);

// 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(githubservice_mock.getInternalQueueByPullRequest(anyString()))
.thenReturn(webhookConfig);

// Set up expected values
String expectedPullRequestUrl = "https://example.com/pull/1";
when(mockRepository.getHtmlUrl()).thenReturn(new URL(expectedPullRequestUrl));
when(githubservice_mock.getPullRequestFiles(any()))
.thenReturn(
System.getProperty("user.home")
+ File.separator
+ "LPVS"
+ File.separator
+ "Projects");

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

@Test
void testCommentBuilder_ConflictFilePresent() {

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

0 comments on commit 16b8214

Please sign in to comment.