diff --git a/src/main/java/com/lpvs/entity/report/LPVSReportBuilder.java b/src/main/java/com/lpvs/entity/report/LPVSReportBuilder.java index e951c29e..f7c7354c 100644 --- a/src/main/java/com/lpvs/entity/report/LPVSReportBuilder.java +++ b/src/main/java/com/lpvs/entity/report/LPVSReportBuilder.java @@ -300,9 +300,6 @@ private void addBlockOfTableForLicenseTypeHTML( .append(type) .append(""); break; - default: - throw new IllegalStateException( - "Unexpected value for the license type: " + type); } htmlBuilder.append(" / "); htmlBuilder.append(getExplanationForLicenseType(type)); @@ -396,12 +393,11 @@ private String getExplanationForLicenseType(String type) { return "This license prohibits the use of the licensed code in certain contexts, such as commercial software development."; case restricted: return "This license required compliance with specific obligations. It is crucial to carefully review and adhere to these obligations before using the licensed code."; - case unreviewed: - return "This license has not been reviewed thoroughly and may contain unknown risks or limitations. It is recommended to review these licenses carefully before using the licensed code."; case permitted: return "This license permits free usage, modification, and distribution of the licensed code without any restrictions."; + case unreviewed: default: - throw new IllegalStateException("Unexpected value for the license type: " + type); + return "This license has not been reviewed thoroughly and may contain unknown risks or limitations. It is recommended to review these licenses carefully before using the licensed code."; } } diff --git a/src/test/java/com/lpvs/entity/report/LPVSReportBuilderTest.java b/src/test/java/com/lpvs/entity/report/LPVSReportBuilderTest.java index aeeeabd2..a53ac20c 100644 --- a/src/test/java/com/lpvs/entity/report/LPVSReportBuilderTest.java +++ b/src/test/java/com/lpvs/entity/report/LPVSReportBuilderTest.java @@ -13,6 +13,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.io.TempDir; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration; @@ -29,8 +30,7 @@ import static com.lpvs.entity.report.LPVSReportBuilder.saveHTMLToFile; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; @ExtendWith(SpringExtension.class) @ImportAutoConfiguration(ThymeleafAutoConfiguration.class) @@ -43,11 +43,20 @@ public class LPVSReportBuilderTest { LPVSFile fileLicPermitted, fileLicProhibitedRestricted, fileLicUnreviewed_1, - fileLicUnreviewed_2; - LPVSLicense licPermitted, licProhibited, licRestricted, licUnreviewed; + fileLicUnreviewed_2, + fileLicUnreviewed_3, + fileLicIncorrect; + LPVSLicense licPermitted, + licProhibited, + licRestricted, + licUnreviewed, + licUnreviewed_2, + licIncorrect; LPVSLicenseService.Conflict conflict1, conflict2; LPVSReportBuilder reportBuilder; + @TempDir Path tempDir; + @BeforeEach public void setUp() throws Exception { licPermitted = @@ -83,6 +92,24 @@ public void setUp() throws Exception { } }; + licUnreviewed_2 = + new LPVSLicense() { + { + setLicenseName("BSD 3-Clause \"New\" or \"Revised\" License"); + setAccess("UNREVIEWED"); + setSpdxId("BSD-3-Clause"); + } + }; + + licIncorrect = + new LPVSLicense() { + { + setLicenseName("Some incorrect license"); + setAccess("INCORRECT"); + setSpdxId("Some-license"); + } + }; + fileLicPermitted = new LPVSFile(); fileLicPermitted.setLicenses( new HashSet<>() { @@ -121,7 +148,7 @@ public void setUp() throws Exception { fileLicUnreviewed_1.setLicenses( new HashSet<>() { { - add(licUnreviewed); + add(licUnreviewed_2); } }); fileLicUnreviewed_1.setFilePath("local_file_path_3"); @@ -150,6 +177,38 @@ public void setUp() throws Exception { fileLicUnreviewed_2.setSnippetMatch("50%"); fileLicUnreviewed_2.setMatchedLines("5-10"); + fileLicUnreviewed_3 = new LPVSFile(); + fileLicUnreviewed_3.setLicenses( + new HashSet<>() { + { + add(licUnreviewed); + } + }); + fileLicUnreviewed_3.setFilePath("local_file_path_4"); + fileLicUnreviewed_3.setComponentFilePath("component_file_path_4"); + fileLicUnreviewed_3.setComponentName("component_name_4"); + fileLicUnreviewed_3.setComponentUrl("http://component_name_4/url"); + fileLicUnreviewed_3.setComponentVersion("v4.0.0"); + fileLicUnreviewed_3.setComponentVendor("component_vendor_4"); + fileLicUnreviewed_3.setSnippetMatch("40%"); + fileLicUnreviewed_3.setMatchedLines("1-10"); + + fileLicIncorrect = new LPVSFile(); + fileLicIncorrect.setLicenses( + new HashSet<>() { + { + add(licIncorrect); + } + }); + fileLicIncorrect.setFilePath("local_file_path_4"); + fileLicIncorrect.setComponentFilePath("component_file_path_4"); + fileLicIncorrect.setComponentName("component_name_4"); + fileLicIncorrect.setComponentUrl("http://component_name_4/url"); + fileLicIncorrect.setComponentVersion("v4.0.0"); + fileLicIncorrect.setComponentVendor("component_vendor_4"); + fileLicIncorrect.setSnippetMatch("50%"); + fileLicIncorrect.setMatchedLines("5-10"); + conflict1 = new LPVSLicenseService.Conflict<>("GPL-3.0-only", "Apache-2.0"); conflict2 = new LPVSLicenseService.Conflict<>("LGPL-2.0-or-later", "MIT"); @@ -161,7 +220,8 @@ public void setUp() throws Exception { @Test public void testGenerateHtmlReportSingleScan_Empty() { String actual = - reportBuilder.generateHtmlReportSingleScan("some/path", null, null, null, null); + reportBuilder.generateHtmlReportSingleScan( + "some/path", new ArrayList<>(), null, null, null); assertThat(actual).contains(sdf.format(new Date())); // check title and scanDate assertThat(actual).contains("No license problems detected"); assertThat(actual).contains("No license conflicts detected"); @@ -206,6 +266,18 @@ public void testGenerateHtmlReportSingleScan_WithPermittedLicenses() { assertThat(actual).contains("No license conflicts detected"); } + @Test + public void testGenerateHtmlReportSingleScan_WithSimilarLicenses() { + List scanResults = + List.of(fileLicUnreviewed_1, fileLicUnreviewed_2, fileLicUnreviewed_3); + String actual = + reportBuilder.generateHtmlReportSingleScan( + "some/path", scanResults, new ArrayList<>(), null, null); + assertThat(actual).contains(sdf.format(new Date())); + assertThat(actual).doesNotContain("No license problems detected"); + assertThat(actual).contains("No license conflicts detected"); + } + @Test void testSaveHTMLToFile() throws IOException { String htmlContent = "

Test HTML

"; @@ -221,4 +293,12 @@ void testSaveHTMLToFile() throws IOException { // Clean up: delete the created file Files.deleteIfExists(path); } + + @Test + void saveHTMLToFile_CatchBlock_N() { + String htmlContent = ""; + Path invalidPath = tempDir.resolve("invalid/path/with/special/characters"); + saveHTMLToFile(htmlContent, invalidPath.toString()); + assertFalse(Files.exists(invalidPath)); + } }