Skip to content

Commit

Permalink
fix: improve exception handling and test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
remisbaima committed Apr 15, 2022
1 parent 08b9479 commit 3bc9be7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
18 changes: 11 additions & 7 deletions src/main/java/org/remis/cyclonedx/LicenseChecker.java
Expand Up @@ -94,14 +94,18 @@ protected Bom parseBom(Map<String, String> cycloneDxConfig, String bomDir) throw

protected List<String> parseJson(String jsonFileUri, String jsonPath, File destFile)
throws IOException {
File file;
if (isUrl(jsonFileUri)) {
file = destFile;
FileUtils.copyURLToFile(new URL(jsonFileUri), file, TIMEOUT_MILLIS, TIMEOUT_MILLIS);
} else {
file = new File(jsonFileUri);
try {
File file;
if (isUrl(jsonFileUri)) {
file = destFile;
FileUtils.copyURLToFile(new URL(jsonFileUri), file, TIMEOUT_MILLIS, TIMEOUT_MILLIS);
} else {
file = new File(jsonFileUri);
}
return JsonPath.read(file, jsonPath);
} catch (Exception e) {
throw new IOException(e);
}
return JsonPath.read(file, jsonPath);
}

protected Set<String> lowercaseList(List<String> list) {
Expand Down
17 changes: 8 additions & 9 deletions src/test/java/org/remis/cyclonedx/LicenseCheckerTest.java
Expand Up @@ -2,7 +2,6 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.params.provider.Arguments.arguments;

import java.io.File;
Expand All @@ -17,13 +16,14 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;

public class LicenseCheckerTest {
private static final URL JSON_FILE_URL =
LicenseCheckerTest.class.getResource("/complex-project/allowedLicenses.json");
private static final String JSON_PATH = "$[?(@.License_Conflicts=='No')].License_SPDX";
private static final File TEMP_FILE = new File("target/licensechecker.json");

private final LicenseChecker licenseChecker = new LicenseChecker();

Expand All @@ -39,11 +39,11 @@ void lowercaseAndRemoveWhitespacesFromList() {
}

@ParameterizedTest
@ValueSource(strings = {"", " ", "invalid-path"})
void parseJsonThrowsIOException(String path) {
// @ValueSource(strings = {" ", "invalid-path", "http://abc.com"})
@CsvSource({"'',''", "' ',' '", "invalid-path,''", "http://abc.com,''", "' '," + JSON_PATH})
void parseJsonThrowsIOException(String jsonFileUri, String jsonPath) {
// WHEN + THEN
Exception e = assertThrows(IOException.class, () -> licenseChecker.parseJson(path, null, null));
assertTrue(e.getMessage().startsWith(path));
assertThrows(IOException.class, () -> licenseChecker.parseJson(jsonFileUri, jsonPath, null));
}

@ParameterizedTest
Expand All @@ -58,12 +58,11 @@ void parseJson(String jsonFileUri, String jsonPath, File destFile, List<String>

static Stream<Arguments> parseJson() {
List<String> expected = Arrays.asList("Apache-2.0", "BSD-4-Clause");
File destFile = new File("target/licensechecker.json");
return Stream.of(
// json from local file
arguments(JSON_FILE_URL.getFile(), JSON_PATH, null, expected),
arguments(JSON_FILE_URL.getFile(), JSON_PATH, TEMP_FILE, expected),
// json from URL
arguments(JSON_FILE_URL.toString(), JSON_PATH, destFile, expected));
arguments(JSON_FILE_URL.toString(), JSON_PATH, TEMP_FILE, expected));
}

@ParameterizedTest
Expand Down

0 comments on commit 3bc9be7

Please sign in to comment.