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 4ed839d..374f034 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 @@ -1,5 +1,8 @@ package org.sonatype.cs.getmetrics.util; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonatype.cs.getmetrics.reports.Waivers; import org.sonatype.cs.getmetrics.service.PolicyIdsService; import org.sonatype.cs.getmetrics.service.UtilService; @@ -10,6 +13,7 @@ import javax.json.JsonObject; public class ParseReasons { + private static final Logger log = LoggerFactory.getLogger(Waivers.class); private ParseReasons() {} @@ -61,6 +65,10 @@ public static String getLicense(JsonArray reasons) { if (licenseFound.isEmpty()) { continue; } + if (!licenseFound.contains("(") || !licenseFound.contains(")")) { + log.debug("licenseFound string does not contain brackets: " + licenseFound); + continue; + } String license = licenseFound.substring( licenseFound.indexOf("(") + 1, licenseFound.indexOf(")")); 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 index e4a795a..b955a11 100644 --- 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 @@ -1,12 +1,39 @@ package org.sonatype.cs.getmetrics.util; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import ch.qos.logback.classic.LoggerContext; +import ch.qos.logback.core.read.ListAppender; + +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.slf4j.LoggerFactory; import javax.json.Json; import javax.json.JsonArray; public class ParseReasonsTest { + private ListAppender listAppender; + + @BeforeEach + public void setup() { + // Set up Logback configuration + LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); + listAppender = new ListAppender<>(); + listAppender.start(); + loggerContext.getLogger("root").addAppender(listAppender); + } + + @AfterEach + public void teardown() { + // Clean up the logger context + listAppender.stop(); + LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); + loggerContext.getLogger("root").detachAppender(listAppender); + } + @Test void testEmptyLicenseString() { JsonArray reasons = @@ -16,6 +43,22 @@ void testEmptyLicenseString() { Assertions.assertEquals("", actualReason); } + @Test + void testLicenseStringWithoutBrackets() { + JsonArray reasons = + Json.createArrayBuilder() + .add(Json.createObjectBuilder().add("reason", "some reason")) + .build(); + String actualReason = ParseReasons.getLicense(reasons); + + Assertions.assertEquals(1, listAppender.list.size()); + ch.qos.logback.classic.spi.ILoggingEvent loggingEvent = listAppender.list.get(0); + assertEquals( + "licenseFound string does not contain brackets: some reason", + loggingEvent.getMessage()); + Assertions.assertEquals("", actualReason); + } + @Test void testSingleLicenseString() { JsonArray reasons =