diff --git a/src/main/java/com/lpvs/entity/LPVSFile.java b/src/main/java/com/lpvs/entity/LPVSFile.java index ee4657c0..6c6cbf6d 100644 --- a/src/main/java/com/lpvs/entity/LPVSFile.java +++ b/src/main/java/com/lpvs/entity/LPVSFile.java @@ -16,7 +16,10 @@ import java.io.IOException; import java.io.RandomAccessFile; import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; /** * Represents a file in the LPVS system. @@ -105,45 +108,51 @@ public class LPVSFile { * @return A formatted string representing the licenses. */ public String convertLicensesToString(LPVSVcs vcs) { - String licenseNames = ""; - for (LPVSLicense license : this.licenses) { - String licSpdxId = license.getSpdxId(); - // Check if the license SPDX ID has scanner-specific name - if (licSpdxId.startsWith("LicenseRef")) { + StringBuilder licenseNames = new StringBuilder(); + Map> groupedLicenses = + licenses.stream().collect(Collectors.groupingBy(LPVSLicense::getAccess)); + + for (Map.Entry> entry : groupedLicenses.entrySet()) { + String accessType = entry.getKey(); + List licensesWithAccessType = entry.getValue(); + licenseNames.append("\n").append("- ").append(accessType.toUpperCase()).append(":\n"); + + for (LPVSLicense license : licensesWithAccessType) { + String licSpdxId = license.getSpdxId(); + // Check if the license SPDX ID has scanner-specific name. // Change the name of the license that will be displayed in PR comment to // scanner-independent licSpdxId = - "UNREVIEWED LICENSE : " - + licSpdxId - .replaceAll("LicenseRef-scancode-", "") - .replaceAll("LicenseRef-scanoss-", ""); - } - if (vcs != null && vcs.equals(LPVSVcs.GITHUB)) { - licenseNames += - (license.getChecklistUrl() != null - ? "" - : "") - + licSpdxId - + (license.getChecklistUrl() != null ? "" : "") - + " (" - + license.getAccess().toLowerCase() - + "), "; - } else { - licenseNames += licSpdxId - + (license.getChecklistUrl() != null - ? " (" + license.getChecklistUrl() + ")" - : "") - + " - " - + license.getAccess().toLowerCase() - + ", "; + .replaceAll("LicenseRef-scancode-", "") + .replaceAll("LicenseRef-scanoss-", ""); + + if (vcs != null && vcs.equals(LPVSVcs.GITHUB)) { + licenseNames + .append(" : ") + .append( + license.getChecklistUrl() != null + ? "" + : "") + .append(licSpdxId) + .append(license.getChecklistUrl() != null ? "" : "") + .append("\n"); + } else { + licenseNames + .append(" : ") + .append(licSpdxId) + .append( + license.getChecklistUrl() != null + ? " (" + license.getChecklistUrl() + ")" + : "") + .append("\n"); + } } } - if (licenseNames.endsWith(", ")) - licenseNames = licenseNames.substring(0, licenseNames.length() - 2); - return licenseNames; + + return licenseNames.toString(); } /** @@ -183,7 +192,7 @@ public String convertBytesToLinesNumbers() { while (byteCounter < endByte) { String line = sourceFile.readLine(); if (line == null) { - if (startLine > 0 && endLine == 0) endLine = currentLine; + if (startLine > 0) endLine = currentLine; break; } byteCounter += line.getBytes(StandardCharsets.UTF_8).length + 1; @@ -198,7 +207,7 @@ public String convertBytesToLinesNumbers() { } // Construct the string representing start and end line numbers in the range - if (result.length() > 0) { + if (!result.isEmpty()) { result.append(","); } result.append(startLine); diff --git a/src/main/java/com/lpvs/entity/report/LPVSReportBuilder.java b/src/main/java/com/lpvs/entity/report/LPVSReportBuilder.java index ef212b17..ae24e0c3 100644 --- a/src/main/java/com/lpvs/entity/report/LPVSReportBuilder.java +++ b/src/main/java/com/lpvs/entity/report/LPVSReportBuilder.java @@ -71,6 +71,8 @@ public LPVSReportBuilder(TemplateEngine templateEngine) { private final String restricted = "RESTRICTED"; private final String prohibited = "PROHIBITED"; private final String unreviewed = "UNREVIEWED"; + private final String boldStart = "\033[1m"; + private final String boldEnd = "\033[0m"; /** * A class representing a group of elements with a count. @@ -147,6 +149,88 @@ public String generateHtmlReportSingleScan( return templateEngine.process("report_single_scan", context); } + /** + * Generates a formatted string for an LPVS command line comment. + * + * @param path The path to the source folder for scan or pull request URL + * @param scanResults the results of the license scan + * @param conflicts a list of license conflicts found during the scan + * @return A string containing scan results in command line friendly format. + */ + public String generateCommandLineComment( + String path, + List scanResults, + List> conflicts) { + StringBuilder commentBuilder = new StringBuilder(); + String date = sdf.format(new Date()); + commentBuilder.append("\n"); + commentBuilder.append(boldStart + "Scan date: " + boldEnd + date + "\n"); + commentBuilder.append(boldStart + "Source code location: " + boldEnd + path + "\n"); + commentBuilder.append(boldStart + "Used scanner: " + boldEnd + scannerType + "\n"); + commentBuilder.append(boldStart + "Version of LPVS: " + boldEnd + lpvsVersion + "\n\n"); + + commentBuilder.append(boldStart + "Detected Licenses" + boldEnd + "\n\n"); + + Map> detectedLicenseInfo = + groupScanResultsForLicenseTable(scanResults); + long prohibitedLicenses = getDetectedLicenseCountByType(detectedLicenseInfo, prohibited); + long restrictedLicenses = getDetectedLicenseCountByType(detectedLicenseInfo, restricted); + long unreviewedLicenses = getDetectedLicenseCountByType(detectedLicenseInfo, unreviewed); + long licenseDetected = prohibitedLicenses + restrictedLicenses + unreviewedLicenses; + + if (licenseDetected > 0) { + commentBuilder.append( + "Potential license problem(s) detected: " + licenseDetected + "\n"); + if (prohibitedLicenses > 0) { + commentBuilder.append(" - Prohibited license(s): " + prohibitedLicenses + "\n"); + } + if (restrictedLicenses > 0) { + commentBuilder.append(" - Restricted license(s): " + restrictedLicenses + "\n"); + } + if (unreviewedLicenses > 0) { + commentBuilder.append(" - Unreviewed license(s): " + unreviewedLicenses + "\n"); + } + } else { + commentBuilder.append("No license problems detected.\n"); + } + + if (scanResults != null && !scanResults.isEmpty()) { + commentBuilder.append("\n"); + for (LPVSFile file : scanResults) { + commentBuilder.append(boldStart + "File: " + boldEnd); + commentBuilder.append(file.getFilePath()); + commentBuilder.append("\n"); + commentBuilder.append(boldStart + "License(s): " + boldEnd); + commentBuilder.append(file.convertLicensesToString(null)); + commentBuilder.append(boldStart + "Component: " + boldEnd); + commentBuilder.append(file.getComponentName()); + commentBuilder.append(" ("); + commentBuilder.append(file.getComponentFilePath()); + commentBuilder.append(")\n"); + commentBuilder.append(boldStart + "Matched Lines: " + boldEnd); + commentBuilder.append(file.getMatchedLines()); + commentBuilder.append("\n"); + commentBuilder.append(boldStart + "Snippet Match: " + boldEnd); + commentBuilder.append(file.getSnippetMatch()); + commentBuilder.append("\n\n"); + } + } + + commentBuilder.append("\n"); + commentBuilder.append(boldStart + "Detected License Conflicts" + boldEnd + "\n\n"); + if (conflicts != null && !conflicts.isEmpty()) { + commentBuilder.append( + "Potential license conflict(s) detected: " + conflicts.size() + "\n"); + for (LPVSLicenseService.Conflict conflict : conflicts) { + commentBuilder.append(" - " + conflict.l1 + " and " + conflict.l2 + "\n"); + } + } else { + commentBuilder.append("No license conflicts detected.\n"); + } + + return commentBuilder.toString(); + } + /** * Saves HTML report to given location. * diff --git a/src/main/java/com/lpvs/service/LPVSGitHubService.java b/src/main/java/com/lpvs/service/LPVSGitHubService.java index 6ec1a77f..f5bd5563 100644 --- a/src/main/java/com/lpvs/service/LPVSGitHubService.java +++ b/src/main/java/com/lpvs/service/LPVSGitHubService.java @@ -255,8 +255,7 @@ public void commentResults( commitComment = "**Detected licenses:**\n\n\n"; for (LPVSFile file : scanResults) { commitComment += "**File:** " + file.getFilePath() + "\n"; - commitComment += - "**License(s):** " + file.convertLicensesToString(LPVSVcs.GITHUB) + "\n"; + commitComment += "**License(s):** " + file.convertLicensesToString(LPVSVcs.GITHUB); commitComment += "**Component:** " + file.getComponentName() diff --git a/src/main/java/com/lpvs/service/scan/LPVSDetectService.java b/src/main/java/com/lpvs/service/scan/LPVSDetectService.java index 6e2ae929..796174c4 100644 --- a/src/main/java/com/lpvs/service/scan/LPVSDetectService.java +++ b/src/main/java/com/lpvs/service/scan/LPVSDetectService.java @@ -29,7 +29,6 @@ import com.lpvs.entity.LPVSFile; import com.lpvs.entity.LPVSQueue; -import com.lpvs.util.LPVSCommentUtil; import lombok.extern.slf4j.Slf4j; @@ -211,8 +210,7 @@ public void runSingleScan() { } else if (generateReport) { // 2. Command line output String report = - LPVSCommentUtil.reportCommentBuilder( - webhookConfig, scanResult, detectedConflicts); + reportBuilder.generateCommandLineComment(path, scanResult, detectedConflicts); if (!report.isEmpty()) { log.info(report); } diff --git a/src/main/java/com/lpvs/util/LPVSCommentUtil.java b/src/main/java/com/lpvs/util/LPVSCommentUtil.java index 8e650ef5..3c848c6c 100644 --- a/src/main/java/com/lpvs/util/LPVSCommentUtil.java +++ b/src/main/java/com/lpvs/util/LPVSCommentUtil.java @@ -6,13 +6,9 @@ */ package com.lpvs.util; -import java.util.List; - -import com.lpvs.entity.LPVSDetectedLicense; import com.lpvs.entity.LPVSFile; import com.lpvs.entity.LPVSQueue; import com.lpvs.entity.enums.LPVSVcs; -import com.lpvs.service.LPVSLicenseService; import lombok.extern.slf4j.Slf4j; @@ -78,62 +74,4 @@ public static String getMatchedLinesAsLink( log.debug("MatchedLines: " + matchedLines); return matchedLines; } - - /** - * Generates a formatted string for an LPVS GitHub comment. - * - * @param webhookConfig The {@link LPVSQueue} configuration for the webhook. - * @param scanResults List containing preformatted scan results. - * @param conflicts List of conflicts, containing license conflict information. - * @return A string containing scan results in GitHub-friendly format. - */ - public static String reportCommentBuilder( - LPVSQueue webhookConfig, - List scanResults, - List> conflicts) { - - StringBuilder commitCommentBuilder = new StringBuilder(); - - if (scanResults != null && scanResults.size() != 0) { - commitCommentBuilder.append("**Detected licenses:**\n\n\n"); - for (LPVSFile file : scanResults) { - commitCommentBuilder.append("**File:** "); - commitCommentBuilder.append(file.getFilePath()); - commitCommentBuilder.append("\n"); - commitCommentBuilder.append("**License(s):** "); - commitCommentBuilder.append(file.convertLicensesToString(LPVSVcs.GITHUB)); - commitCommentBuilder.append("\n"); - commitCommentBuilder.append("**Component:** "); - commitCommentBuilder.append(file.getComponentName()); - commitCommentBuilder.append(" ("); - commitCommentBuilder.append(file.getComponentFilePath()); - commitCommentBuilder.append(")\n"); - commitCommentBuilder.append("**Matched Lines:** "); - commitCommentBuilder.append( - LPVSCommentUtil.getMatchedLinesAsLink(webhookConfig, file, LPVSVcs.GITHUB)); - commitCommentBuilder.append("\n"); - commitCommentBuilder.append("**Snippet Match:** "); - commitCommentBuilder.append(file.getSnippetMatch()); - commitCommentBuilder.append("\n\n\n\n"); - } - } - - if (conflicts != null && conflicts.size() > 0) { - commitCommentBuilder.append("**Detected license conflicts:**\n\n\n"); - commitCommentBuilder.append("
    "); - for (LPVSLicenseService.Conflict conflict : conflicts) { - commitCommentBuilder.append("
  • " + conflict.l1 + " and " + conflict.l2 + "
  • "); - LPVSDetectedLicense detectedIssue = new LPVSDetectedLicense(); - detectedIssue.setIssue(true); - } - commitCommentBuilder.append("
"); - if (null != webhookConfig.getHubLink()) { - commitCommentBuilder.append("("); - commitCommentBuilder.append(webhookConfig.getHubLink()); - commitCommentBuilder.append(")"); - } - } - - return commitCommentBuilder.toString(); - } } diff --git a/src/test/java/com/lpvs/entity/LPVSFileTest.java b/src/test/java/com/lpvs/entity/LPVSFileTest.java index de254c67..ec297d79 100644 --- a/src/test/java/com/lpvs/entity/LPVSFileTest.java +++ b/src/test/java/com/lpvs/entity/LPVSFileTest.java @@ -156,7 +156,7 @@ public void convertLicensesToStringBaseTest() { lpvsFile.setLicenses(licenses); assertEquals( lpvsFile.convertLicensesToString(LPVSVcs.GITHUB), - "spdxId (access)"); + "\n- ACCESS:\n : spdxId\n"); } @Test @@ -189,7 +189,7 @@ public void convertLicenseToStringCheckListUrlNullTest() { lpvsFile.setLicenses(licenses); assertEquals( lpvsFile.convertLicensesToString(LPVSVcs.GITHUB), - "spdxId (access), spdxId (access)"); + "\n- ACCESS:\n : spdxId\n : spdxId\n"); } @Test @@ -211,9 +211,7 @@ public void convertLicenseToStringCheckListUrlLicenseRef() { Set licenses = new HashSet<>(Arrays.asList(lpvsLicense3)); lpvsFile.setLicenses(licenses); - assertEquals( - lpvsFile.convertLicensesToString(LPVSVcs.GITHUB), - "UNREVIEWED LICENSE : spdxId (access)"); + assertEquals(lpvsFile.convertLicensesToString(LPVSVcs.GITHUB), "\n- ACCESS:\n : spdxId\n"); licenses = new HashSet<>(); lpvsFile.setLicenses(licenses); @@ -251,7 +249,7 @@ public void convertLicenseToStringCheckListUrlTwoTest() { lpvsFile.setLicenses(licenses); assertEquals( lpvsFile.convertLicensesToString(LPVSVcs.GITHUB), - "spdxId (access), spdxId (access)"); + "\n- ACCESS:\n : spdxId\n : spdxId\n"); } @Test diff --git a/src/test/java/com/lpvs/entity/report/LPVSReportBuilderTest.java b/src/test/java/com/lpvs/entity/report/LPVSReportBuilderTest.java index a53ac20c..54c0808c 100644 --- a/src/test/java/com/lpvs/entity/report/LPVSReportBuilderTest.java +++ b/src/test/java/com/lpvs/entity/report/LPVSReportBuilderTest.java @@ -301,4 +301,71 @@ void saveHTMLToFile_CatchBlock_N() { saveHTMLToFile(htmlContent, invalidPath.toString()); assertFalse(Files.exists(invalidPath)); } + + private LPVSFile createSampleFile(String filePath, String matchedLines, String baseAccess) { + final Long baseLicenseId = 1234567890L; + final String baseLicenseName = "licenseName"; + final String baseSpdxId = "spdxId"; + final String baseAlternativeName = "licenseNameAlternative"; + + LPVSLicense lpvsLicense = + new LPVSLicense( + baseLicenseId, + baseLicenseName, + baseSpdxId, + baseAccess, + baseAlternativeName, + null); + + Set licenses = new HashSet<>(List.of(lpvsLicense)); + LPVSFile file = new LPVSFile(); + file.setFilePath(filePath); + file.setMatchedLines(matchedLines); + file.setLicenses(licenses); + return file; + } + + @Test + void testReportCommentBuilder_LicenseDetectedNoConflicts() { + LPVSReportBuilder reportBuilder = new LPVSReportBuilder(null); + List scanResults = new ArrayList<>(); + List> conflicts = new ArrayList<>(); + + scanResults.add(createSampleFile("testPath1", "test1", "UNREVIEWED")); + String comment = + reportBuilder.generateCommandLineComment("testPath1", scanResults, conflicts); + assertNotNull(comment); + + scanResults.add(createSampleFile("testPath1", "test1", "PROHIBITED")); + comment = reportBuilder.generateCommandLineComment("testPath1", scanResults, conflicts); + assertNotNull(comment); + + scanResults.add(createSampleFile("testPath1", "test1", "RESTRICTED")); + comment = reportBuilder.generateCommandLineComment("testPath1", scanResults, conflicts); + assertNotNull(comment); + + scanResults.add(createSampleFile("testPath1", "test1", "PERMITTED")); + comment = reportBuilder.generateCommandLineComment("testPath1", scanResults, conflicts); + assertNotNull(comment); + } + + @Test + void testReportCommentBuilder_LicenseDetectedConflictsDetected() { + LPVSReportBuilder reportBuilder = new LPVSReportBuilder(null); + List scanResults = new ArrayList<>(); + scanResults.add(createSampleFile("testPath1", "test1", "PROHIBITED")); + LPVSLicenseService.Conflict conflict_1 = + new LPVSLicenseService.Conflict<>("MIT", "Apache-2.0"); + List> conflicts = + List.of(conflict_1, conflict_1); + String comment = reportBuilder.generateCommandLineComment("testPath1", null, null); + assertNotNull(comment); + } + + @Test + void testReportCommentBuilder_NoLicenseDetectedNoConflicts() { + LPVSReportBuilder reportBuilder = new LPVSReportBuilder(null); + String comment = reportBuilder.generateCommandLineComment("testPath1", null, null); + assertNotNull(comment); + } } diff --git a/src/test/java/com/lpvs/service/LPVSGitHubServiceTest.java b/src/test/java/com/lpvs/service/LPVSGitHubServiceTest.java index 42012194..8d5854d0 100644 --- a/src/test/java/com/lpvs/service/LPVSGitHubServiceTest.java +++ b/src/test/java/com/lpvs/service/LPVSGitHubServiceTest.java @@ -2172,7 +2172,9 @@ class TestCommentResults__ProhibitedPresentConflictsPresent { "**\\[License Pre-Validation Service\\]** Potential license problem(s) detected \n\n" + "**Detected licenses:**\n\n\n" + "**File:** src/main/java/com/lpvs/service/LPVSGitHubService.java\n" - + "**License(s):** MIT (prohibited)\n" + + "**License(s):** \n" + + "- PROHIBITED:\n" + + " : MIT\n" + "**Component:** LPVS::Services (src/main/java/com/lpvs/service/LPVSGitHubService.java)\n" + "**Matched Lines:** 1-6 \n" + "**Snippet Match:** /**\n" @@ -2339,7 +2341,7 @@ public void testCommentResults__ProhibitedPresentConflictsPresentLicensePresent( + LPVSPayloadUtil.getRepositoryName(webhookConfig)); } catch (IOException e) { log.error( - "TestCommentResults__ProhibitedPresentConflictsPresent.testCommentResults__ProhibitedPresentConflictsPresent() error " + "TestCommentResults__ProhibitedPresentConflictsPresent.testCommentResults__ProhibitedPresentConflictsPresentLicensePresent() error " + e); fail(); } @@ -2357,7 +2359,7 @@ public void testCommentResults__ProhibitedPresentConflictsPresentLicensePresent( "[License Pre-Validation Service]"); } catch (IOException e) { log.error( - "TestCommentResults__ProhibitedPresentConflictsPresent.testCommentResults__ProhibitedPresentConflictsPresent() error " + "TestCommentResults__ProhibitedPresentConflictsPresent.testCommentResults__ProhibitedPresentConflictsPresentLicensePresent() error " + e); fail(); } @@ -2400,7 +2402,7 @@ public void testCommentResults__ProhibitedPresentConflictsPresentLicensePresentA + LPVSPayloadUtil.getRepositoryName(webhookConfig)); } catch (IOException e) { log.error( - "TestCommentResults__ProhibitedPresentConflictsPresent.testCommentResults__ProhibitedPresentConflictsPresent() error " + "TestCommentResults__ProhibitedPresentConflictsPresent.testCommentResults__ProhibitedPresentConflictsPresentLicensePresentAlt() error " + e); fail(); } @@ -2418,7 +2420,7 @@ public void testCommentResults__ProhibitedPresentConflictsPresentLicensePresentA "[License Pre-Validation Service]"); } catch (IOException e) { log.error( - "TestCommentResults__ProhibitedPresentConflictsPresent.testCommentResults__ProhibitedPresentConflictsPresent() error " + "TestCommentResults__ProhibitedPresentConflictsPresent.testCommentResults__ProhibitedPresentConflictsPresentLicensePresentAlt() error " + e); fail(); } @@ -2504,7 +2506,9 @@ class TestCommentResults__EmptyPresentConflictsPresent { "**\\[License Pre-Validation Service\\]** Potential license problem(s) detected \n\n" + "**Detected licenses:**\n\n\n" + "**File:** src/main/java/com/lpvs/service/LPVSGitHubService.java\n" - + "**License(s):** MIT ()\n" + + "**License(s):** \n" + + "- :\n" + + " : MIT\n" + "**Component:** LPVS::Services (src/main/java/com/lpvs/service/LPVSGitHubService.java)\n" + "**Matched Lines:** 1-6 \n" + "**Snippet Match:** /**\n" @@ -2835,7 +2839,9 @@ class TestCommentResults__UnreviewedPresentConflictsPresent { "**\\[License Pre-Validation Service\\]** Potential license problem(s) detected \n\n" + "**Detected licenses:**\n\n\n" + "**File:** src/main/java/com/lpvs/service/LPVSGitHubService.java\n" - + "**License(s):** MIT (unreviewed)\n" + + "**License(s):** \n" + + "- UNREVIEWED:\n" + + " : MIT\n" + "**Component:** LPVS::Services (src/main/java/com/lpvs/service/LPVSGitHubService.java)\n" + "**Matched Lines:** 1-6 \n" + "**Snippet Match:** /**\n" @@ -3168,7 +3174,9 @@ class TestCommentResults__RestrictedPresentConflictsPresent { "**\\[License Pre-Validation Service\\]** Potential license problem(s) detected \n\n" + "**Detected licenses:**\n\n\n" + "**File:** src/main/java/com/lpvs/service/LPVSGitHubService.java\n" - + "**License(s):** MIT (restricted)\n" + + "**License(s):** \n" + + "- RESTRICTED:\n" + + " : MIT\n" + "**Component:** LPVS::Services (src/main/java/com/lpvs/service/LPVSGitHubService.java)\n" + "**Matched Lines:** 1-6 \n" + "**Snippet Match:** /**\n" @@ -3486,7 +3494,9 @@ class TestCommentResults__ProhibitedAbsentConflictsAbsent { "**\\[License Pre-Validation Service\\]** No license issue detected \n\n" + "**Detected licenses:**\n\n\n" + "**File:** src/main/java/com/lpvs/service/LPVSGitHubService.java\n" - + "**License(s):** MIT (permitted)\n" + + "**License(s):** \n" + + "- PERMITTED:\n" + + " : MIT\n" + "**Component:** LPVS::Services (src/main/java/com/lpvs/service/LPVSGitHubService.java)\n" + "**Matched Lines:** 1-6 \n" + "**Snippet Match:** /**\n" diff --git a/src/test/java/com/lpvs/service/scan/LPVSDetectServiceTest.java b/src/test/java/com/lpvs/service/scan/LPVSDetectServiceTest.java index c9f72e26..174fe738 100644 --- a/src/test/java/com/lpvs/service/scan/LPVSDetectServiceTest.java +++ b/src/test/java/com/lpvs/service/scan/LPVSDetectServiceTest.java @@ -14,7 +14,6 @@ import com.lpvs.service.LPVSGitHubService; import com.lpvs.service.LPVSLicenseService; import com.lpvs.service.scan.scanner.LPVSScanossDetectService; -import com.lpvs.util.LPVSCommentUtil; import com.lpvs.util.LPVSFileUtil; import lombok.extern.slf4j.Slf4j; @@ -39,10 +38,7 @@ import java.util.HashSet; import java.util.List; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.*; @@ -274,7 +270,7 @@ void testRunOneScan_PullRequest_Branch2() null, null, scanServiceFactory_mock, - null)); + reportBuilder_mock)); // Mock the necessary GitHub objects for LPVSQueue when(mockGitHub.getRepository(any())).thenReturn(mockRepository); @@ -298,6 +294,8 @@ void testRunOneScan_PullRequest_Branch2() + "LPVS" + File.separator + "Projects"); + when(reportBuilder_mock.generateCommandLineComment(anyString(), anyList(), anyList())) + .thenReturn("Sample report"); setPrivateField(detectService, "trigger", "github/owner/repo/branch/123"); setPrivateField(detectService, "htmlReport", null); @@ -376,17 +374,6 @@ void testRunOneScan_LocalFiles_WithConsoleReport() List> expected = List.of(conflict_1, conflict_1); - lpvsDetectService = - spy( - new LPVSDetectService( - "scanoss", - false, - null, - null, - null, - scanServiceFactory_mock, - null)); - File sourceDir = Files.createTempDirectory("source").toFile(); File sourceFile1 = new File(sourceDir, "file1.txt"); sourceFile1.createNewFile(); @@ -417,6 +404,8 @@ void testRunOneScan_LocalFiles_WithConsoleReport() + "LPVS" + File.separator + "Projects"); + when(reportBuilder_mock.generateCommandLineComment(anyString(), anyList(), anyList())) + .thenReturn("Sample report"); assertDoesNotThrow(() -> detectService.runSingleScan()); @@ -432,17 +421,6 @@ void testRunOneScan_LocalFiles_WithHtmlReport() List> expected = List.of(conflict_1, conflict_1); - lpvsDetectService = - spy( - new LPVSDetectService( - "scanoss", - false, - null, - null, - null, - scanServiceFactory_mock, - null)); - File sourceDir = Files.createTempDirectory("source").toFile(); File sourceFile1 = new File(sourceDir, "file1.txt"); File sourceFile2 = new File(sourceDir, "file2.txt"); @@ -497,17 +475,6 @@ void testRunOneScan_LocalFiles_NoFile() List> expected = List.of(conflict_1, conflict_1); - lpvsDetectService = - spy( - new LPVSDetectService( - "scanoss", - false, - null, - null, - null, - scanServiceFactory_mock, - null)); - File sourceDir = Files.createTempDirectory("source").toFile(); File sourceFile1 = new File(sourceDir, "file1.txt"); @@ -549,6 +516,9 @@ void testRunOneScan_trigerInternalQueueException() setPrivateField(detectService, "trigger", "fake-trigger-value"); setPrivateField(detectService, "ctx", mockApplicationContext); + setPrivateField(detectService, "reportBuilder", reportBuilder_mock); + when(reportBuilder_mock.generateCommandLineComment(anyString(), anyList(), anyList())) + .thenReturn("Sample report"); assertDoesNotThrow(() -> detectService.runSingleScan()); } @@ -737,30 +707,33 @@ void testRunOneScan_TriggerNotNull_NoDirectory() throws Exception { @Test void testCommentBuilder_ConflictFilePresent() { - + LPVSReportBuilder reportBuilder = new LPVSReportBuilder(null); LPVSLicenseService.Conflict conflict_1 = new LPVSLicenseService.Conflict<>("MIT", "Apache-2.0"); List> expected = List.of(conflict_1, conflict_1); - LPVSQueue webhookConfig = new LPVSQueue(); List scanResults = new ArrayList<>(); String commentGitHub = - LPVSCommentUtil.reportCommentBuilder(webhookConfig, scanResults, expected); + reportBuilder.generateCommandLineComment( + "/some/path/to/file", scanResults, expected); assertNotNull(commentGitHub); } @Test void testCommentBuilder_NoConflictNoLicense() { + LPVSReportBuilder reportBuilder = new LPVSReportBuilder(null); List> expected = new ArrayList<>(); - LPVSQueue webhookConfig = new LPVSQueue(); List scanResults = new ArrayList<>(); String commentGitHub = - LPVSCommentUtil.reportCommentBuilder(webhookConfig, scanResults, expected); + reportBuilder.generateCommandLineComment( + "/some/path/to/file", scanResults, expected); - assertEquals(commentGitHub, ""); + assertNotNull(commentGitHub); + assertTrue(commentGitHub.contains("No license problems detected.")); + assertTrue(commentGitHub.contains("No license conflicts detected.")); } @Test diff --git a/src/test/java/com/lpvs/util/LPVSCommentUtilTest.java b/src/test/java/com/lpvs/util/LPVSCommentUtilTest.java index 0c7a2490..b5b710de 100644 --- a/src/test/java/com/lpvs/util/LPVSCommentUtilTest.java +++ b/src/test/java/com/lpvs/util/LPVSCommentUtilTest.java @@ -7,10 +7,8 @@ package com.lpvs.util; import com.lpvs.entity.LPVSFile; -import com.lpvs.entity.LPVSLicense; import com.lpvs.entity.LPVSQueue; import com.lpvs.entity.enums.LPVSVcs; -import com.lpvs.service.LPVSLicenseService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -19,13 +17,6 @@ import org.mockito.MockitoAnnotations; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import java.util.Set; public class LPVSCommentUtilTest { @@ -36,33 +27,6 @@ public void setUp() { MockitoAnnotations.openMocks(this); } - private LPVSFile createSampleFile(String filePath, String matchedLines) { - final Long baseLicenseId = 1234567890L; - final String baseLicenseName = "licenseName"; - final String baseSpdxId = "spdxId"; - final String baseAccess = "access"; - final String baseAlternativeName = "licenseNameAlternative"; - final String baseChecklistUrl = "checklistUrl"; - List baseIncompatibleWith = - Arrays.asList("incompatibleWith1", "incompatibleWith2", "incompatibleWith3"); - - LPVSLicense lpvsLicense3 = - new LPVSLicense( - baseLicenseId, - baseLicenseName, - "LicenseRef-scancode-" + baseSpdxId, - baseAccess, - baseAlternativeName, - null); - - Set licenses = new HashSet<>(Arrays.asList(lpvsLicense3)); - LPVSFile file = new LPVSFile(); - file.setFilePath(filePath); - file.setMatchedLines(matchedLines); - file.setLicenses(licenses); - return file; - } - @Test public void testGetMatchedLinesAsLinkAll() { LPVSFile file = new LPVSFile(); @@ -120,32 +84,4 @@ public void testGetMatchedLinesAsLinkWithNonGitHubVcsMultipleLines() { + "9-12 (https://gerrit.org/repo/blob/headCommitSHA/exampleFile.txt#L9L12) ", result); } - - @Test - void testReportCommentBuilder() { - LPVSQueue webhookConfig = new LPVSQueue(); - List scanResults = new ArrayList<>(); - scanResults.add(createSampleFile("testPath1", "test1")); - List> conflicts = new ArrayList<>(); - String comment = - LPVSCommentUtil.reportCommentBuilder(webhookConfig, scanResults, conflicts); - - 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); - } }