From b69b3017699b960ff5133b8ec34bff786bec0f77 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Sun, 23 Jun 2024 15:48:49 +0200 Subject: test: Add FormatterTest. Change-Id: Id627819583e8c9867b3950b7d8aac6830e5f3f80 --- .../googlecloud/jsonlogging/FormatterTest.java | 88 ++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 core/src/test/java/eu/mulk/quarkus/googlecloud/jsonlogging/FormatterTest.java (limited to 'core/src/test/java/eu') diff --git a/core/src/test/java/eu/mulk/quarkus/googlecloud/jsonlogging/FormatterTest.java b/core/src/test/java/eu/mulk/quarkus/googlecloud/jsonlogging/FormatterTest.java new file mode 100644 index 0000000..49cda39 --- /dev/null +++ b/core/src/test/java/eu/mulk/quarkus/googlecloud/jsonlogging/FormatterTest.java @@ -0,0 +1,88 @@ +package eu.mulk.quarkus.googlecloud.jsonlogging; + +import jakarta.json.Json; +import org.jboss.logmanager.ExtLogRecord; +import org.jboss.logmanager.Level; +import org.junit.jupiter.api.Test; + +import java.util.Collection; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertLinesMatch; + +class FormatterTest { + + @Test + void simpleRecord() { + var logRecord = new ExtLogRecord(Level.INFO, "Hello, world!", FormatterTest.class.getName()); + + var formatter = new Formatter(List.of(), List.of()); + var formattingResult = formatter.format(logRecord); + + assertLinesMatch( + List.of( + "\\{" + + "\"message\":\"Hello, world!\"," + + "\"severity\":\"INFO\"," + + "\"timestamp\":\\{\"seconds\":\\d+,\"nanos\":\\d+\\}," + + "\"logging.googleapis.com/sourceLocation\":" + + "\\{\"file\":\"ReflectionUtils.java\"," + + "\"line\":\"\\d+\"," + + "\"function\":\"org.junit.platform.commons.util.ReflectionUtils.invokeMethod\"" + + "\\}" + + "\\}\n"), + List.of(formattingResult)); + } + + @Test + void structuredRecord() { + var parameterProvider = + new StructuredParameterProvider() { + @Override + public StructuredParameter getParameter() { + var b = Json.createObjectBuilder(); + b.add("traceId", "39f9a49a9567a8bd7087b708f8932550"); + b.add("spanId", "c7431b14630b633d"); + return () -> b; + } + }; + + var labelProvider = + new LabelProvider() { + @Override + public Collection