From 16b82148ad94b4aff9dadde1f82c7fa48b861f09 Mon Sep 17 00:00:00 2001 From: Oleg Kopysov Date: Thu, 1 Aug 2024 13:17:21 +0300 Subject: [PATCH] fix: Improve code coverage Signed-off-by: Oleg Kopysov --- .../service/scan/LPVSDetectServiceTest.java | 57 ++++++++++++++++++- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/lpvs/service/scan/LPVSDetectServiceTest.java b/src/test/java/com/lpvs/service/scan/LPVSDetectServiceTest.java index eccd1ef4..6fd0c860 100644 --- a/src/test/java/com/lpvs/service/scan/LPVSDetectServiceTest.java +++ b/src/test/java/com/lpvs/service/scan/LPVSDetectServiceTest.java @@ -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 { @@ -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()); @@ -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 conflict_1 = + new LPVSLicenseService.Conflict<>("MIT", "Apache-2.0"); + + List> 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 conflict_1 = new LPVSLicenseService.Conflict<>("MIT", "Apache-2.0");