diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/pom.xml | 18 | ||||
| -rw-r--r-- | core/src/test/java/eu/mulk/quarkus/googlecloud/jsonlogging/FormatterTest.java | 88 | 
2 files changed, 106 insertions, 0 deletions
| diff --git a/core/pom.xml b/core/pom.xml index cb61c20..a75acd5 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -42,6 +42,19 @@ SPDX-License-Identifier: LGPL-3.0-or-later        <artifactId>parsson</artifactId>        <version>1.1.6</version>      </dependency> + +    <dependency> +      <groupId>org.junit.jupiter</groupId> +      <artifactId>junit-jupiter-engine</artifactId> +      <version>5.10.2</version> +      <scope>test</scope> +    </dependency> +    <dependency> +      <groupId>org.junit.jupiter</groupId> +      <artifactId>junit-jupiter-api</artifactId> +      <version>5.10.2</version> +      <scope>test</scope> +    </dependency>    </dependencies>    <build> @@ -58,6 +71,11 @@ SPDX-License-Identifier: LGPL-3.0-or-later          </configuration>        </plugin> +      <plugin> +        <artifactId>maven-surefire-plugin</artifactId> +        <version>3.2.5</version> +      </plugin> +      </plugins>    </build> 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<Label> getLabels() { +            return List.of(Label.of("requestId", "123")); +          } +        }; + +    var logRecord = new ExtLogRecord(Level.INFO, "Hello, world!", FormatterTest.class.getName()); +    logRecord.setParameters( +        new Object[] { +          (StructuredParameter) +              () -> Json.createObjectBuilder().add("one", 1).add("two", 2.0).add("yes", true), +          Label.of("a", "b") +        }); + +    var formatter = new Formatter(List.of(parameterProvider), List.of(labelProvider)); +    var formattingResult = formatter.format(logRecord); +    assertLinesMatch( +        List.of( +            "\\{" +                + "\"logging.googleapis.com/labels\":\\{\"a\":\"b\",\"requestId\":\"123\"\\}," +                + "\"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\"" +                + "\\}," +                + "\"traceId\":\"39f9a49a9567a8bd7087b708f8932550\"," +                + "\"spanId\":\"c7431b14630b633d\"," +                + "\"one\":1," +                + "\"two\":2.0," +                + "\"yes\":true" +                + "\\}\n"), +        List.of(formattingResult)); +  } +} | 
