Skip to content

Commit

Permalink
Fix StringIndexOutOfBoundsException for license and add logging (#124)
Browse files Browse the repository at this point in the history
* Revert "Revert "Fix Windows Docker command""

* Updated get-metrics start time period (#115)

* Merge develop to main for release 102 (#119)

* Updated get-metrics start time period (#114)

* Improve scanning coverage calculation (#118)

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>

* Fix StringIndexOutOfBoundsException and add logging

* Removed unecessary lines from test

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
rpanman-sonatype and github-actions[bot] authored Feb 27, 2024
1 parent d8b05d1 commit 083137f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
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;

import java.util.ArrayList;
import java.util.List;

import javax.json.JsonArray;
import javax.json.JsonObject;

public class ParseReasons {

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

public static String getReason(String policyName, JsonArray reasons) {
Expand Down Expand Up @@ -61,6 +62,11 @@ 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,37 @@
package org.sonatype.cs.getmetrics.util;

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;
import static org.junit.jupiter.api.Assertions.assertEquals;

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 +41,19 @@ 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 083137f

Please sign in to comment.