Adding a uniform configuration for all json/yaml mapper use cases: part 1

This commit is contained in:
Sergey Skrobotov
2023-02-23 16:11:05 -08:00
parent 6ee9c6ad46
commit b9b4e3fdd8
38 changed files with 250 additions and 121 deletions

View File

@@ -0,0 +1,99 @@
/*
* Copyright 2023 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.whispersystems.textsecuregcm.util;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Optional;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
class SystemMapperTest {
private static final ObjectMapper MAPPER = SystemMapper.configureMapper(new ObjectMapper());
private static final String JSON_NO_FIELD = """
{}
""".trim();
private static final String JSON_NULL_FIELD = """
{"name":null}
""".trim();
private static final String JSON_WITH_FIELD = """
{"name":"value"}
""".trim();
interface Data {
Optional<String> name();
}
@JsonInclude(JsonInclude.Include.NON_ABSENT)
public record DataRecord(Optional<String> name) implements Data {
}
public static class DataClass implements Data {
@JsonProperty
private Optional<String> name = Optional.empty();
public DataClass() {
}
public DataClass(final Optional<String> name) {
this.name = name;
}
@Override
public Optional<String> name() {
return name;
}
}
@JsonInclude(JsonInclude.Include.NON_ABSENT)
public static class DataClass2 extends DataClass {
public DataClass2(final Optional<String> name) {
super(name);
}
}
@ParameterizedTest
@ValueSource(classes = {DataClass.class, DataRecord.class})
public void testOptionalField(final Class<? extends Data> clazz) throws Exception {
assertTrue(MAPPER.readValue(JSON_NO_FIELD, clazz).name().isEmpty());
assertTrue(MAPPER.readValue(JSON_NULL_FIELD, clazz).name().isEmpty());
assertEquals("value", MAPPER.readValue(JSON_WITH_FIELD, clazz).name().orElseThrow());
}
@ParameterizedTest
@MethodSource("provideStringsForIsBlank")
public void testSerialization(final Data data, final String expectedJson) throws Exception {
assertEquals(expectedJson, MAPPER.writeValueAsString(data));
}
private static Stream<Arguments> provideStringsForIsBlank() {
return Stream.of(
Arguments.of(new DataClass(Optional.of("value")), JSON_WITH_FIELD),
Arguments.of(new DataClass(Optional.empty()), JSON_NULL_FIELD),
Arguments.of(new DataClass(null), JSON_NULL_FIELD),
Arguments.of(new DataClass2(Optional.of("value")), JSON_WITH_FIELD),
Arguments.of(new DataClass2(Optional.of("value")), JSON_WITH_FIELD),
Arguments.of(new DataClass2(Optional.empty()), JSON_NO_FIELD),
Arguments.of(new DataRecord(Optional.of("value")), JSON_WITH_FIELD),
Arguments.of(new DataRecord(Optional.empty()), JSON_NO_FIELD),
Arguments.of(new DataRecord(null), JSON_NO_FIELD)
);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2021 Signal Messenger, LLC
* Copyright 2013 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
@@ -15,7 +15,6 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.net.HttpHeaders;
import io.dropwizard.jersey.DropwizardResourceConfig;
import io.dropwizard.jersey.jackson.JacksonMessageBodyProvider;
@@ -43,6 +42,7 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.slf4j.Logger;
import org.whispersystems.textsecuregcm.util.SystemMapper;
import org.whispersystems.websocket.WebSocketResourceProvider;
import org.whispersystems.websocket.auth.WebsocketAuthValueFactoryProvider;
import org.whispersystems.websocket.logging.WebsocketRequestLog;
@@ -129,7 +129,7 @@ class LoggingUnhandledExceptionMapperTest {
resourceConfig.register(new TestController());
resourceConfig.register(new WebSocketSessionContextValueFactoryProvider.Binder());
resourceConfig.register(new WebsocketAuthValueFactoryProvider.Binder<>(TestPrincipal.class));
resourceConfig.register(new JacksonMessageBodyProvider(new ObjectMapper()));
resourceConfig.register(new JacksonMessageBodyProvider(SystemMapper.jsonMapper()));
ApplicationHandler applicationHandler = new ApplicationHandler(resourceConfig);
WebsocketRequestLog requestLog = mock(WebsocketRequestLog.class);