Skip to content

Commit

Permalink
Merge pull request #12 from dm-drogeriemarkt/duration-test
Browse files Browse the repository at this point in the history
add missing test, serialize remaining java.time classes
  • Loading branch information
dupps authored Apr 16, 2021
2 parents 7fac66e + 6ff50d9 commit c95674b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ For consuming logs:

If you use maven, add this to your pom.xml:

```pom.xml
```xml
<dependency>
<groupId>de.dm.infrastructure</groupId>
<artifactId>structured-logging</artifactId>
<version>2.0.3</version>
<version>2.0.5</version>
<scope>test</scope>
</dependency>
```
Expand Down Expand Up @@ -289,6 +289,14 @@ public class TimeMachine {

## Changes

### 2.0.5

* **New Feature**: Added correct serialization of remaining java.time types (Year, YearMonth and MonthDay)

### 2.0.4

* **New Feature**: Added correct serialization of Duration

### 2.0.3

* **Bugfix**: The Task Decorator threw a NullPointerException when resetting MDC contents if the target thread's MDC was empty
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/de/dm/prom/structuredlogging/MdcContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.MonthDay;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Period;
import java.time.Year;
import java.time.YearMonth;
import java.time.ZonedDateTime;

/**
Expand All @@ -42,6 +45,9 @@ public final class MdcContext implements java.io.Closeable {
module.addSerializer(ZonedDateTime.class, new ToStringSerializer());
module.addSerializer(LocalTime.class, new ToStringSerializer());
module.addSerializer(Duration.class, new ToStringSerializer());
module.addSerializer(MonthDay.class, new ToStringSerializer());
module.addSerializer(Year.class, new ToStringSerializer());
module.addSerializer(YearMonth.class, new ToStringSerializer());

OBJECT_MAPPER.registerModule(module);
}
Expand Down
22 changes: 21 additions & 1 deletion src/test/java/de/dm/prom/structuredlogging/ExampleBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@
import lombok.Builder;
import lombok.Data;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.MonthDay;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Period;
import java.time.Year;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

import static java.time.DayOfWeek.MONDAY;
import static java.time.Month.JANUARY;

@Data
@Builder
class ExampleBean {
Expand All @@ -28,9 +36,15 @@ class ExampleBean {
private Period period;
private ZonedDateTime zonedDateTime;
private LocalTime localTime;
private Duration duration;
private DayOfWeek dayOfWeek;
private Month month;
private MonthDay monthDay;
private Year year;
private YearMonth yearMonth;

static ExampleBean getExample() {
LocalDateTime importantTime = LocalDateTime.of(2019, Month.JANUARY, 1, 13, 37);
LocalDateTime importantTime = LocalDateTime.of(2019, JANUARY, 1, 13, 37);

return ExampleBean.builder()
.name("John Doe")
Expand All @@ -43,6 +57,12 @@ static ExampleBean getExample() {
.period(Period.ofDays(42))
.zonedDateTime(ZonedDateTime.of(importantTime, ZoneId.of("UTC")))
.localTime(LocalTime.of(13, 37))
.duration(Duration.ofMinutes(42))
.dayOfWeek(MONDAY)
.month(JANUARY)
.monthDay(MonthDay.of(12, 24))
.year(Year.of(1984))
.yearMonth(YearMonth.of(2000, 8))
.build();
}
}
17 changes: 13 additions & 4 deletions src/test/java/de/dm/prom/structuredlogging/MdcContextUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ class MdcContextUnitTest {
"\"offsetTime\":\"13:37+01:00\"," +
"\"period\":\"P42D\"," +
"\"zonedDateTime\":\"2019-01-01T13:37Z[UTC]\"," +
"\"localTime\":\"13:37\"" +
"\"localTime\":\"13:37\"," +
"\"duration\":\"PT42M\"," +
"\"dayOfWeek\":\"MONDAY\"," +
"\"month\":\"JANUARY\"," +
"\"monthDay\":\"--12-24\"," +
"\"year\":\"1984\"," +
"\"yearMonth\":\"2000-08\"" +
"}";

@RegisterExtension
Expand Down Expand Up @@ -61,9 +67,12 @@ private void assertMdcFieldContentIsCorrect(String mdcFieldName, String expected
JsonNode sampleBeanTree = objectMapper.readTree(expectedJson);

assertThat(jsonStringFromMdc).startsWith(JSON_PREFIX);
JsonNode treeFromMDC = objectMapper.readTree(jsonStringFromMdc.replaceFirst(JSON_PREFIX, ""));
String actualJson = jsonStringFromMdc.replaceFirst(JSON_PREFIX, "");
JsonNode treeFromMDC = objectMapper.readTree(actualJson);

assertThat(treeFromMDC).isEqualTo(sampleBeanTree);
assertThat(treeFromMDC).as("Expecting:\n<%s>\nto be equal to:\n<%s>\nbut was not.\n\n\n",
treeFromMDC.toPrettyString(), sampleBeanTree.toPrettyString())
.isEqualTo(sampleBeanTree);
}

@Test
Expand Down Expand Up @@ -110,7 +119,7 @@ void withSimpleNameAsKey() throws Exception {
}

@Test
void failedUpdate() throws Exception {
void failedUpdate() {
MdcContext.update(ExampleBean.getExample());

logCapture.assertLogged(WARN, "^Cannot update content of MDC key ExampleBean in .*\\.failedUpdate\\(MdcContextUnitTest.java:[0-9]+\\) because it does not exist.$");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ class StructuredMdcJsonProviderUnitTest {
"\"offsetTime\":\"13:37+01:00\"," +
"\"period\":\"P42D\"," +
"\"localTime\":\"13:37\"," +
"\"duration\":\"PT42M\"," +
"\"dayOfWeek\":\"MONDAY\"," +
"\"month\":\"JANUARY\"," +
"\"monthDay\":\"--12-24\"," +
"\"year\":\"1984\"," +
"\"yearMonth\":\"2000-08\"," +
"\"zonedDateTime\":\"2019-01-01T13:37Z[UTC]\"}}";

private Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
Expand Down

0 comments on commit c95674b

Please sign in to comment.