Skip to content

Commit

Permalink
Deserializers as singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
giulong committed Dec 9, 2023
1 parent 949e833 commit 3a6a342
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import io.github.giulong.spectrum.browsers.*;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

import static lombok.AccessLevel.PRIVATE;

@Slf4j
@NoArgsConstructor(access = PRIVATE)
public class BrowserDeserializer extends InterpolatedDeserializer<Browser<?, ?, ?>> {

private static final BrowserDeserializer INSTANCE = new BrowserDeserializer();

public static BrowserDeserializer getInstance() {
return INSTANCE;
}

@Override
public Browser<?, ?, ?> deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException {
final String value = jsonParser.getValueAsString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.time.Duration;

import static lombok.AccessLevel.PRIVATE;

@Slf4j
@NoArgsConstructor(access = PRIVATE)
public class DurationDeserializer extends JsonDeserializer<Duration> {

private static final DurationDeserializer INSTANCE = new DurationDeserializer();

public static DurationDeserializer getInstance() {
return INSTANCE;
}

@Override
public Duration deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException {
final int value = jsonParser.getValueAsInt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

import static lombok.AccessLevel.PRIVATE;

@Slf4j
@NoArgsConstructor(access = PRIVATE)
public class InterpolatedBooleanDeserializer extends InterpolatedDeserializer<Boolean> {

private static final InterpolatedBooleanDeserializer INSTANCE = new InterpolatedBooleanDeserializer();

public static InterpolatedBooleanDeserializer getInstance() {
return INSTANCE;
}

@Override
public Boolean deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException {
final String value = jsonParser.getValueAsString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,28 @@
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static io.github.giulong.spectrum.SpectrumSessionListener.VARS;
import static lombok.AccessLevel.PRIVATE;

@Slf4j
@NoArgsConstructor(access = PRIVATE)
public class InterpolatedObjectDeserializer extends JsonDeserializer<Object> {

private static final InterpolatedObjectDeserializer INSTANCE = new InterpolatedObjectDeserializer();
private static final Pattern INT_PATTERN = Pattern.compile("(?<placeholder>\\$<(?<varName>[\\w.]+)(:-(?<defaultValue>[\\w~.:/\\\\]*))?>)");
private static final Pattern NUMBER = Pattern.compile("-?\\d+(.\\d+|,\\d+)?");

public static InterpolatedObjectDeserializer getInstance() {
return INSTANCE;
}

@Override
public Object deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException {
final JsonNode jsonNode = jsonParser.readValueAsTree();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import lombok.NoArgsConstructor;

import java.io.IOException;

import static lombok.AccessLevel.PRIVATE;

@NoArgsConstructor(access = PRIVATE)
public class LogbackLogLevelDeserializer extends JsonDeserializer<Level> {

private static final LogbackLogLevelDeserializer INSTANCE = new LogbackLogLevelDeserializer();

public static LogbackLogLevelDeserializer getInstance() {
return INSTANCE;
}

@Override
public Level deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
return Level.toLevel(jsonParser.getValueAsString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import lombok.NoArgsConstructor;

import java.io.IOException;
import java.util.logging.Level;

import static lombok.AccessLevel.PRIVATE;

@NoArgsConstructor(access = PRIVATE)
public class UtilLogLevelDeserializer extends JsonDeserializer<Level> {

private static final UtilLogLevelDeserializer INSTANCE = new UtilLogLevelDeserializer();

public static UtilLogLevelDeserializer getInstance() {
return INSTANCE;
}

@Override
public Level deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
return Level.parse(jsonParser.getValueAsString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public static YamlUtils getInstance() {
.setDefaultMergeable(true)
.registerModules(
new JavaTimeModule(),
new SimpleModule().addDeserializer(Object.class, new InterpolatedObjectDeserializer()),
new SimpleModule().addDeserializer(Object.class, InterpolatedObjectDeserializer.getInstance()),
new SimpleModule().addDeserializer(String.class, InterpolatedStringDeserializer.getInstance()),
new SimpleModule().addDeserializer(boolean.class, new InterpolatedBooleanDeserializer()),
new SimpleModule().addDeserializer(java.util.logging.Level.class, new UtilLogLevelDeserializer()),
new SimpleModule().addDeserializer(Level.class, new LogbackLogLevelDeserializer()),
new SimpleModule().addDeserializer(Duration.class, new DurationDeserializer()),
new SimpleModule().addDeserializer(Browser.class, new BrowserDeserializer())
new SimpleModule().addDeserializer(boolean.class, InterpolatedBooleanDeserializer.getInstance()),
new SimpleModule().addDeserializer(java.util.logging.Level.class, UtilLogLevelDeserializer.getInstance()),
new SimpleModule().addDeserializer(Level.class, LogbackLogLevelDeserializer.getInstance()),
new SimpleModule().addDeserializer(Duration.class, DurationDeserializer.getInstance()),
new SimpleModule().addDeserializer(Browser.class, BrowserDeserializer.getInstance())
);

private final ObjectWriter writer = new YAMLMapper()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ public static void afterAll() {
VARS.clear();
}

@Test
@DisplayName("getInstance should return the singleton")
public void getInstance() {
//noinspection EqualsWithItself
assertSame(BrowserDeserializer.getInstance(), BrowserDeserializer.getInstance());
}

@DisplayName("deserialize should delegate to the parent method passing the string value")
@ParameterizedTest(name = "with value {0} we expect {1}")
@MethodSource("valuesProvider")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.time.Duration;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
Expand All @@ -28,6 +29,13 @@ class DurationDeserializerTest {
@InjectMocks
private DurationDeserializer durationDeserializer;

@Test
@DisplayName("getInstance should return the singleton")
public void getInstance() {
//noinspection EqualsWithItself
assertSame(DurationDeserializer.getInstance(), DurationDeserializer.getInstance());
}

@Test
@DisplayName("deserialize should return the duration in seconds from the provided string")
public void deserialize() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand All @@ -18,6 +19,7 @@

import static io.github.giulong.spectrum.SpectrumSessionListener.VARS;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -46,6 +48,13 @@ public static void afterAll() {
VARS.clear();
}

@Test
@DisplayName("getInstance should return the singleton")
public void getInstance() {
//noinspection EqualsWithItself
assertSame(InterpolatedBooleanDeserializer.getInstance(), InterpolatedBooleanDeserializer.getInstance());
}

@DisplayName("deserialize should delegate to the parent method passing the string value")
@ParameterizedTest(name = "with value {0} we expect {1}")
@MethodSource("valuesProvider")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static com.github.stefanbirkner.systemlambda.SystemLambda.withEnvironmentVariable;
import static io.github.giulong.spectrum.SpectrumSessionListener.VARS;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import static org.junit.jupiter.params.provider.EnumSource.Mode.EXCLUDE;
import static org.mockito.Mockito.mockStatic;
Expand Down Expand Up @@ -71,6 +72,13 @@ public static void afterAll() {
VARS.clear();
}

@Test
@DisplayName("getInstance should return the singleton")
public void getInstance() {
//noinspection EqualsWithItself
assertSame(InterpolatedObjectDeserializer.getInstance(), InterpolatedObjectDeserializer.getInstance());
}

@Test
@DisplayName("deserialize should delegate to the interpolatedStringDeserializer when the value is a string")
public void deserializeStrings() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import ch.qos.logback.classic.Level;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import io.github.giulong.spectrum.internals.jackson.deserializers.LogbackLogLevelDeserializer;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand All @@ -18,6 +18,7 @@

import static ch.qos.logback.classic.Level.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import static org.mockito.Mockito.when;

Expand All @@ -34,6 +35,13 @@ class LogbackLogLevelDeserializerTest {
@InjectMocks
private LogbackLogLevelDeserializer logbackLogLevelDeserializer;

@Test
@DisplayName("getInstance should return the singleton")
public void getInstance() {
//noinspection EqualsWithItself
assertSame(LogbackLogLevelDeserializer.getInstance(), LogbackLogLevelDeserializer.getInstance());
}

@DisplayName("deserialize should return the Logback level from the provided string")
@ParameterizedTest(name = "with value {0} we expect {1}")
@MethodSource("valuesProvider")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import static java.util.logging.Level.INFO;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
Expand All @@ -26,7 +27,14 @@ class UtilLogLevelDeserializerTest {
private DeserializationContext deserializationContext;

@InjectMocks
private io.github.giulong.spectrum.internals.jackson.deserializers.UtilLogLevelDeserializer UtilLogLevelDeserializer;
private UtilLogLevelDeserializer UtilLogLevelDeserializer;

@Test
@DisplayName("getInstance should return the singleton")
public void getInstance() {
//noinspection EqualsWithItself
assertSame(UtilLogLevelDeserializer.getInstance(), UtilLogLevelDeserializer.getInstance());
}

@Test
@DisplayName("deserialize should return the log level from the provided string")
Expand Down

0 comments on commit 3a6a342

Please sign in to comment.