diff --git a/src/main/java/com/lpvs/LicensePreValidationService.java b/src/main/java/com/lpvs/LicensePreValidationService.java index 08f422db..f380c8cf 100644 --- a/src/main/java/com/lpvs/LicensePreValidationService.java +++ b/src/main/java/com/lpvs/LicensePreValidationService.java @@ -6,6 +6,7 @@ */ package com.lpvs; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -25,6 +26,7 @@ @SpringBootApplication(scanBasePackages = {"com.lpvs"}) @EnableAutoConfiguration @EnableAsync +@Slf4j public class LicensePreValidationService { /** @@ -56,9 +58,11 @@ public static void main(String[] args) { ApplicationContext applicationContext = SpringApplication.run(LicensePreValidationService.class, args); exitHandler = applicationContext.getBean(LPVSExitHandler.class); - } catch (IllegalArgumentException e) { - System.err.println("An IllegalArgumentException occurred: " + e.getMessage()); + } catch (IllegalArgumentException ex1) { + log.error("An IllegalArgumentException occurred: " + ex1.getMessage()); System.exit(1); + } catch (Exception ex2) { + log.info("LPVS application is being closed."); } } diff --git a/src/main/java/com/lpvs/service/LPVSGitHubService.java b/src/main/java/com/lpvs/service/LPVSGitHubService.java index 8aee6ba4..1df181fd 100644 --- a/src/main/java/com/lpvs/service/LPVSGitHubService.java +++ b/src/main/java/com/lpvs/service/LPVSGitHubService.java @@ -229,7 +229,7 @@ public void commentResults( GHPullRequest pullRequest = getPullRequest(webhookConfig, repository); if (pullRequest == null) { - log.error("Can't find pull request " + webhookConfig.getPullRequestUrl()); + log.error("Pull request is not found " + webhookConfig.getPullRequestUrl()); lpvsPullRequest.setStatus(LPVSPullRequestStatus.NO_ACCESS.toString()); pullRequestRepository.saveAndFlush(lpvsPullRequest); return; diff --git a/src/main/java/com/lpvs/service/scan/LPVSDetectService.java b/src/main/java/com/lpvs/service/scan/LPVSDetectService.java index da19e021..59462dc6 100644 --- a/src/main/java/com/lpvs/service/scan/LPVSDetectService.java +++ b/src/main/java/com/lpvs/service/scan/LPVSDetectService.java @@ -138,11 +138,13 @@ public void runOneScan() { String report = LPVSCommentUtil.reportCommentBuilder( webhookConfig, scanResult, detectedConflicts); - log.info(report); + if (report != null && !report.isEmpty()) { + log.info(report); + } } log.info("Single scan completed."); } catch (Exception ex) { - log.error("\n\n\n Single scan finished with errors \n\n\n"); + log.error("Single scan finished with errors."); log.error("Can't trigger single scan: " + ex.getMessage()); } SpringApplication.exit(ctx, () -> 0); @@ -174,7 +176,7 @@ public List runScan(LPVSQueue webhookConfig, String path) throws Excep } return files; } catch (IllegalArgumentException | NullPointerException ex) { - log.error(ex.getMessage()); + log.error("Exception occurred during running the scan."); return new ArrayList<>(); } } diff --git a/src/main/java/com/lpvs/service/scan/scanner/LPVSScanossDetectService.java b/src/main/java/com/lpvs/service/scan/scanner/LPVSScanossDetectService.java index 39c60c30..53a563a4 100644 --- a/src/main/java/com/lpvs/service/scan/scanner/LPVSScanossDetectService.java +++ b/src/main/java/com/lpvs/service/scan/scanner/LPVSScanossDetectService.java @@ -115,7 +115,10 @@ public void runScan(LPVSQueue webhookConfig, String path) throws Exception { LPVSPayloadUtil.createBufferReader( LPVSPayloadUtil.createInputStreamReader( process.getErrorStream())); - log.error(output.readLine()); + String line = output.readLine(); + if (line != null) { + log.error(line); + } throw new Exception( "Scanoss scanner terminated with non-zero code. Terminating."); } finally { diff --git a/src/test/java/com/lpvs/service/scan/LPVSDetectServiceTest.java b/src/test/java/com/lpvs/service/scan/LPVSDetectServiceTest.java index 7440e207..3f51f05c 100644 --- a/src/test/java/com/lpvs/service/scan/LPVSDetectServiceTest.java +++ b/src/test/java/com/lpvs/service/scan/LPVSDetectServiceTest.java @@ -353,6 +353,20 @@ void testCommentBuilder_ConflictFilePresent() throws Exception { assertNotNull(commentHTML); } + @Test + void testCommentBuilder_NoConflictNoLicense() { + List> expected = new ArrayList<>(); + LPVSQueue webhookConfig = new LPVSQueue(); + List scanResults = new ArrayList<>(); + String commentGitHub = + LPVSCommentUtil.reportCommentBuilder(webhookConfig, scanResults, expected); + String commentHTML = + LPVSCommentUtil.buildHTMLComment(webhookConfig, scanResults, expected); + + assertEquals(commentGitHub, ""); + assertEquals(commentHTML, ""); + } + @Test public void testGetPathByPullRequest() { diff --git a/src/test/java/com/lpvs/service/scan/scanner/LPVSScanossDetectServiceTest.java b/src/test/java/com/lpvs/service/scan/scanner/LPVSScanossDetectServiceTest.java index d70294cb..1e9a3728 100644 --- a/src/test/java/com/lpvs/service/scan/scanner/LPVSScanossDetectServiceTest.java +++ b/src/test/java/com/lpvs/service/scan/scanner/LPVSScanossDetectServiceTest.java @@ -177,6 +177,33 @@ public void testRunScan_StatusEqualsOne() throws Exception { } } + @Test + public void testRunScan_StatusEqualsOneNoErrorStream() throws Exception { + Mockito.when(lpvsQueue.getRepositoryUrl()).thenReturn("https://github.com/Samsung/LPVS"); + Mockito.when(lpvsQueue.getPullRequestUrl()) + .thenReturn("https://github.com/Samsung/LPVS/pull/1"); + Process process = Mockito.mock(Process.class); + try (MockedConstruction mockedPb = + Mockito.mockConstruction( + ProcessBuilder.class, + (mock, context) -> { + when(mock.inheritIO()).thenReturn(mock); + when(mock.start()).thenReturn(process); + when(process.getErrorStream()).thenReturn(null); + when(process.waitFor()).thenReturn(1); + })) { + Exception exception = + assertThrows( + Exception.class, () -> scanossDetectService.runScan(lpvsQueue, "path")); + + // Verify that the method throws an exception when the status is 1 + assertEquals(null, exception.getMessage()); + + verify(mockedPb.constructed().get(0)).start(); + verify(process, times(1)).waitFor(); + } + } + @Test public void testRunScan_StatusEqualsZero() throws Exception { Process process = Mockito.mock(Process.class); diff --git a/src/test/java/com/lpvs/util/LPVSCommentUtilTest.java b/src/test/java/com/lpvs/util/LPVSCommentUtilTest.java index 8bb5dfd9..f5e11ce6 100644 --- a/src/test/java/com/lpvs/util/LPVSCommentUtilTest.java +++ b/src/test/java/com/lpvs/util/LPVSCommentUtilTest.java @@ -137,6 +137,22 @@ void testReportCommentBuilder() { assertNotNull(comment); } + @Test + void testReportCommentBuilder_HubLink() { + LPVSQueue webhookConfig = new LPVSQueue(); + List scanResults = new ArrayList<>(); + scanResults.add(createSampleFile("testPath1", "test1")); + LPVSLicenseService.Conflict conflict_1 = + new LPVSLicenseService.Conflict<>("MIT", "Apache-2.0"); + List> conflicts = + List.of(conflict_1, conflict_1); + webhookConfig.setHubLink("any_link"); + String comment = + LPVSCommentUtil.reportCommentBuilder(webhookConfig, scanResults, conflicts); + + assertNotNull(comment); + } + @Test void testBuildHTMLComment() { LPVSQueue webhookConfig = new LPVSQueue(); @@ -150,6 +166,22 @@ void testBuildHTMLComment() { assertNotNull(htmlComment); } + @Test + void testBuildHTMLComment_HubLink() { + LPVSQueue webhookConfig = new LPVSQueue(); + List scanResults = new ArrayList<>(); + scanResults.add(createSampleFile("testPath1", "test1")); + LPVSLicenseService.Conflict conflict_1 = + new LPVSLicenseService.Conflict<>("MIT", "Apache-2.0"); + List> conflicts = + List.of(conflict_1, conflict_1); + webhookConfig.setHubLink("some_link"); + String htmlComment = + LPVSCommentUtil.buildHTMLComment(webhookConfig, scanResults, conflicts); + + assertNotNull(htmlComment); + } + @Test void testSaveHTMLToFile() throws IOException { String htmlContent = "

Test HTML

";