Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jhaber committed Mar 26, 2024
1 parent f6366df commit f96fbd3
Showing 1 changed file with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.<JsonNode>valueToTree(snakeCase))
.isEqualTo(objectNode("stringAttribute", "value"));
assertThat(custom.<JsonNode>valueToTree(snakeCase))
.isEqualTo(objectNode("stringAttribute", "value"));

assertThat(original.<JsonNode>valueToTree(camelCase))
.isEqualTo(objectNode("stringattribute", "value"));
assertThat(custom.<JsonNode>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
Expand All @@ -430,4 +507,8 @@ public String translate(String propertyName) {
}
};
}

private static ObjectNode objectNode(String field, String value) {
return JsonNodeFactory.instance.objectNode().put(field, value);
}
}

0 comments on commit f96fbd3

Please sign in to comment.