From 73a3d20f31455ea6b4169bb929c730f4b3938c0b Mon Sep 17 00:00:00 2001 From: Richard Panman <95854467+rpanman-sonatype@users.noreply.github.com> Date: Mon, 26 Feb 2024 13:13:42 +0000 Subject: [PATCH] Merge develop to main for release 104 (#123) Fix StringIndexOutOfBoundsException for license #122 --- .../cs/getmetrics/util/ParseReasons.java | 18 +++++--- .../cs/getmetrics/util/ParseReasonsTest.java | 41 +++++++++++++++++++ 2 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 get-metrics/src/test/java/org/sonatype/cs/getmetrics/util/ParseReasonsTest.java diff --git a/get-metrics/src/main/java/org/sonatype/cs/getmetrics/util/ParseReasons.java b/get-metrics/src/main/java/org/sonatype/cs/getmetrics/util/ParseReasons.java index d32aba6..4ed839d 100644 --- a/get-metrics/src/main/java/org/sonatype/cs/getmetrics/util/ParseReasons.java +++ b/get-metrics/src/main/java/org/sonatype/cs/getmetrics/util/ParseReasons.java @@ -52,13 +52,15 @@ private static String getCVE(JsonArray reasons) { return cveList; } - private static String getLicense(JsonArray reasons) { + public static String getLicense(JsonArray reasons) { String licenseList = ""; List licenses = new ArrayList<>(); for (JsonObject reason : reasons.getValuesAs(JsonObject.class)) { String licenseFound = reason.getString("reason"); - + if (licenseFound.isEmpty()) { + continue; + } String license = licenseFound.substring( licenseFound.indexOf("(") + 1, licenseFound.indexOf(")")); @@ -69,11 +71,17 @@ private static String getLicense(JsonArray reasons) { } } - for (String l : licenses) { - licenseList = l + ":" + licenseList; + if (licenses.size() == 0) { + return ""; } - licenseList = UtilService.removeLastChar(licenseList); + for (String l : licenses) { + if (licenseList.isEmpty()) { + licenseList = l; + } else { + licenseList = licenseList + ":" + l; + } + } return licenseList; } diff --git a/get-metrics/src/test/java/org/sonatype/cs/getmetrics/util/ParseReasonsTest.java b/get-metrics/src/test/java/org/sonatype/cs/getmetrics/util/ParseReasonsTest.java new file mode 100644 index 0000000..e4a795a --- /dev/null +++ b/get-metrics/src/test/java/org/sonatype/cs/getmetrics/util/ParseReasonsTest.java @@ -0,0 +1,41 @@ +package org.sonatype.cs.getmetrics.util; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import javax.json.Json; +import javax.json.JsonArray; + +public class ParseReasonsTest { + @Test + void testEmptyLicenseString() { + JsonArray reasons = + Json.createArrayBuilder().add(Json.createObjectBuilder().add("reason", "")).build(); + + String actualReason = ParseReasons.getLicense(reasons); + Assertions.assertEquals("", actualReason); + } + + @Test + void testSingleLicenseString() { + JsonArray reasons = + Json.createArrayBuilder() + .add(Json.createObjectBuilder().add("reason", "(license)")) + .build(); + + String actualReason = ParseReasons.getLicense(reasons); + Assertions.assertEquals("license", actualReason); + } + + @Test + void testMultipltLicenseString() { + JsonArray reasons = + Json.createArrayBuilder() + .add(Json.createObjectBuilder().add("reason", "(license)")) + .add(Json.createObjectBuilder().add("reason", "(license2)")) + .build(); + + String actualReason = ParseReasons.getLicense(reasons); + Assertions.assertEquals("license:license2", actualReason); + } +}