From f96fbd3ea94dd931efbda9babd0af0ab40657dc6 Mon Sep 17 00:00:00 2001 From: jhaber Date: Tue, 26 Mar 2024 15:15:06 -0400 Subject: [PATCH] Add more tests --- .../datatype/protobuf/PropertyNamingTest.java | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/src/test/java/com/hubspot/jackson/datatype/protobuf/PropertyNamingTest.java b/src/test/java/com/hubspot/jackson/datatype/protobuf/PropertyNamingTest.java index f737510..5068372 100644 --- a/src/test/java/com/hubspot/jackson/datatype/protobuf/PropertyNamingTest.java +++ b/src/test/java/com/hubspot/jackson/datatype/protobuf/PropertyNamingTest.java @@ -13,6 +13,7 @@ import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; import com.fasterxml.jackson.databind.introspect.AnnotatedField; import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.ObjectNode; import com.hubspot.jackson.datatype.protobuf.util.CompileCustomProtobufs.MixedJsonName; import com.hubspot.jackson.datatype.protobuf.util.ObjectMapperHelper; @@ -422,6 +423,82 @@ public String translate(String propertyName) { assertThat(parsed).isEqualTo(expected); } + @Test + public void ensureSerializationBehavior() { + ObjectMapper original = new ObjectMapper().registerModules(new ProtobufModule()); + ObjectMapper custom = new ObjectMapper() + .registerModules(new ProtobufModule()) + .setPropertyNamingStrategy(new PropertyNamingStrategy() {}); + + PropertyNamingSnakeCased snakeCase = PropertyNamingSnakeCased + .newBuilder() + .setStringAttribute("value") + .build(); + PropertyNamingCamelCased camelCase = PropertyNamingCamelCased + .newBuilder() + .setStringAttribute("value") + .build(); + + assertThat(original.valueToTree(snakeCase)) + .isEqualTo(objectNode("stringAttribute", "value")); + assertThat(custom.valueToTree(snakeCase)) + .isEqualTo(objectNode("stringAttribute", "value")); + + assertThat(original.valueToTree(camelCase)) + .isEqualTo(objectNode("stringattribute", "value")); + assertThat(custom.valueToTree(camelCase)) + .isEqualTo(objectNode("stringattribute", "value")); + } + + @Test + public void ensureDeserializationBehavior() throws IOException { + ObjectMapper original = new ObjectMapper().registerModules(new ProtobufModule()); + ObjectMapper custom = new ObjectMapper() + .registerModules(new ProtobufModule()) + .setPropertyNamingStrategy(new PropertyNamingStrategy() {}); + + PropertyNamingSnakeCased snakeCase = PropertyNamingSnakeCased + .newBuilder() + .setStringAttribute("value") + .build(); + PropertyNamingCamelCased camelCase = PropertyNamingCamelCased + .newBuilder() + .setStringAttribute("value") + .build(); + + assertThat( + original.treeToValue( + objectNode("stringAttribute", "value"), + PropertyNamingSnakeCased.class + ) + ) + .isEqualTo(snakeCase); + + assertThat( + custom.treeToValue( + objectNode("stringAttribute", "value"), + PropertyNamingSnakeCased.class + ) + ) + .isEqualTo(snakeCase); + + assertThat( + original.treeToValue( + objectNode("stringattribute", "value"), + PropertyNamingCamelCased.class + ) + ) + .isEqualTo(camelCase); + + assertThat( + custom.treeToValue( + objectNode("stringattribute", "value"), + PropertyNamingCamelCased.class + ) + ) + .isEqualTo(camelCase); + } + private static PropertyNamingStrategy snakeCaseNamingBase() { return new NamingBase() { @Override @@ -430,4 +507,8 @@ public String translate(String propertyName) { } }; } + + private static ObjectNode objectNode(String field, String value) { + return JsonNodeFactory.instance.objectNode().put(field, value); + } }