Skip to content

Commit

Permalink
test: Add more unit tests and improve code testability
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 23, 2024
1 parent 9cee3b8 commit cdba8f8
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 12 deletions.
8 changes: 2 additions & 6 deletions src/main/java/com/lpvs/entity/report/LPVSReportBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,6 @@ private void addBlockOfTableForLicenseTypeHTML(
.append(type)
.append("</span>");
break;
default:
throw new IllegalStateException(
"Unexpected value for the license type: " + type);
}
htmlBuilder.append(" / ");
htmlBuilder.append(getExplanationForLicenseType(type));
Expand Down Expand Up @@ -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.";
}
}

Expand Down
92 changes: 86 additions & 6 deletions src/test/java/com/lpvs/entity/report/LPVSReportBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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<String, String> conflict1, conflict2;
LPVSReportBuilder reportBuilder;

@TempDir Path tempDir;

@BeforeEach
public void setUp() throws Exception {
licPermitted =
Expand Down Expand Up @@ -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<>() {
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");

Expand All @@ -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");
Expand Down Expand Up @@ -206,6 +266,18 @@ public void testGenerateHtmlReportSingleScan_WithPermittedLicenses() {
assertThat(actual).contains("No license conflicts detected");
}

@Test
public void testGenerateHtmlReportSingleScan_WithSimilarLicenses() {
List<LPVSFile> 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 = "<html><body><p>Test HTML</p></body></html>";
Expand All @@ -221,4 +293,12 @@ void testSaveHTMLToFile() throws IOException {
// Clean up: delete the created file
Files.deleteIfExists(path);
}

@Test
void saveHTMLToFile_CatchBlock_N() {
String htmlContent = "<html><body></body></html>";
Path invalidPath = tempDir.resolve("invalid/path/with/special/characters");
saveHTMLToFile(htmlContent, invalidPath.toString());
assertFalse(Files.exists(invalidPath));
}
}

0 comments on commit cdba8f8

Please sign in to comment.