Skip to content

Commit

Permalink
Added unit test for MaskingLokiJsonEncoder
Browse files Browse the repository at this point in the history
  • Loading branch information
gmsdelmundo committed Sep 28, 2023
1 parent a6e3e9a commit b6d1133
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void start() {
super.start();

maskingPatternLayout = new MaskingPatternLayout();
maskingPatternLayout.setContext(this.context);
maskingPatternLayout.setContext(context);
maskingPatternLayout.setPattern(message.pattern);
maskingPatternLayout.start();
}
Expand All @@ -34,10 +34,7 @@ public void setMessage(MessageCfg message) {
}

public static final class MessageCfg {
String pattern = "l=%level c=%logger{20} t=%thread | %msg %ex";

public MessageCfg() {
}
private String pattern = "l=%level c=%logger{20} t=%thread | %msg %ex";

public void setPattern(String pattern) {
this.pattern = pattern;
Expand Down
96 changes: 96 additions & 0 deletions src/test/java/com/uid2/shared/util/MaskingLokiJsonEncoderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.uid2.shared.util;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.pattern.FormattingConverter;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.junit.jupiter.MockitoExtension;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.*;

@ExtendWith(MockitoExtension.class)
public class MaskingLokiJsonEncoderTest {
private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(MaskingLokiJsonEncoderTest.class);
private static final MaskingLokiJsonEncoder MASKING_LOKI_JSON_ENCODER = new MaskingLokiJsonEncoder();
private static final String URL_WITHOUT_PROTOCOL = "myservice.s3.amazonaws.com/some/path?param1=value1&X-Amz-Security-Token=mysecurityToken&param3=value3";
private static final Map<String, String> MASKED_MESSAGES = Map.of(
"Error: " + URL_WITHOUT_PROTOCOL + " and something else", "Error: REDACTED - S3 and something else",
"https://" + URL_WITHOUT_PROTOCOL, "REDACTED - S3",
"http://" + URL_WITHOUT_PROTOCOL, "REDACTED - S3",
URL_WITHOUT_PROTOCOL, "REDACTED - S3",
"Should not be redacted", "Should not be redacted"
);

@BeforeAll
public static void setupAll() {
MaskingLokiJsonEncoder.MessageCfg message = new MaskingLokiJsonEncoder.MessageCfg();
message.setPattern("%msg %ex");
MASKING_LOKI_JSON_ENCODER.setMessage(message);
MASKING_LOKI_JSON_ENCODER.setContext(LOGGER.getLoggerContext());
MASKING_LOKI_JSON_ENCODER.start();
}

@AfterAll
public static void teardownAll() {
MASKING_LOKI_JSON_ENCODER.stop();
}

@ParameterizedTest
@MethodSource("maskedMessagesWithS3")
public void testMaskedMessagesWithS3(String message, String maskedMessage) {
String log = MASKING_LOKI_JSON_ENCODER.eventToMessage(getLoggingEvent(message));

assertEquals(maskedMessage, log.trim());
}

private static Set<Arguments> maskedMessagesWithS3() {
return MASKED_MESSAGES.entrySet().stream()
.map(entry -> Arguments.of(
entry.getKey(),
entry.getValue()
))
.collect(Collectors.toSet());
}

@ParameterizedTest
@MethodSource("maskedExceptionsWithS3")
public void testMaskedExceptionsWithS3(Exception ex, String maskedMessage, String stackTrace) {
String log = MASKING_LOKI_JSON_ENCODER.eventToMessage(getLoggingEvent(ex.getMessage(), ex));

assertAll(
"testMaskedExceptionsWithS3",
() -> assertEquals(log.split("\n")[0].trim(), maskedMessage),
() -> assertTrue(log.split("\n")[1].trim().startsWith(stackTrace))
);
}

private static Set<Arguments> maskedExceptionsWithS3() {
return MASKED_MESSAGES.entrySet().stream()
.map(entry -> Arguments.of(
new Exception(entry.getKey()),
entry.getValue() + " java.lang.Exception: " + entry.getValue(),
"at com.uid2.shared.util.MaskingPatternLayoutTest.lambda$maskedExceptionsWithS3"
))
.collect(Collectors.toSet());
}

private static ILoggingEvent getLoggingEvent(String message, Exception ex) {
return new LoggingEvent(FormattingConverter.class.getName(), LOGGER, Level.ERROR, message, ex, null);
}

private static ILoggingEvent getLoggingEvent(String message) {
return getLoggingEvent(message, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.pattern.FormattingConverter;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -39,6 +40,11 @@ public static void setupAll() {
MASKING_PATTERN_LAYOUT.start();
}

@AfterAll
public static void teardownAll() {
MASKING_PATTERN_LAYOUT.stop();
}

@ParameterizedTest
@MethodSource("maskedMessagesWithS3")
public void testMaskedMessagesWithS3(String message, String maskedMessage) {
Expand Down

0 comments on commit b6d1133

Please sign in to comment.