Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
rpanman-sonatype committed Feb 27, 2024
2 parents fb61f40 + 5532d1c commit 93dd3e8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -10,6 +13,7 @@
import javax.json.JsonObject;

public class ParseReasons {
private static final Logger log = LoggerFactory.getLogger(Waivers.class);

private ParseReasons() {}

Expand Down Expand Up @@ -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(")"));
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ch.qos.logback.classic.spi.ILoggingEvent> 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 =
Expand All @@ -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 =
Expand Down

0 comments on commit 93dd3e8

Please sign in to comment.