JAMES-2578 NumericNode is too vague Our integer/long AttributeSerializer should reject float values.
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/77f4ecb8 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/77f4ecb8 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/77f4ecb8 Branch: refs/heads/master Commit: 77f4ecb87a9928f8941ec8a80f535367324897b9 Parents: 94df1ac Author: Benoit Tellier <btell...@linagora.com> Authored: Tue Oct 30 14:17:17 2018 +0700 Committer: Benoit Tellier <btell...@linagora.com> Committed: Thu Nov 1 11:03:38 2018 +0700 ---------------------------------------------------------------------- .../main/java/org/apache/mailet/Serializer.java | 13 ++++-- .../org/apache/mailet/AttributeValueTest.java | 49 ++++++++++++++++++++ 2 files changed, 57 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/77f4ecb8/mailet/api/src/main/java/org/apache/mailet/Serializer.java ---------------------------------------------------------------------- diff --git a/mailet/api/src/main/java/org/apache/mailet/Serializer.java b/mailet/api/src/main/java/org/apache/mailet/Serializer.java index ffa0069..192d132 100644 --- a/mailet/api/src/main/java/org/apache/mailet/Serializer.java +++ b/mailet/api/src/main/java/org/apache/mailet/Serializer.java @@ -48,7 +48,6 @@ import com.fasterxml.jackson.databind.node.IntNode; import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.LongNode; import com.fasterxml.jackson.databind.node.NullNode; -import com.fasterxml.jackson.databind.node.NumericNode; import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.TextNode; import com.google.common.collect.ImmutableList; @@ -157,7 +156,7 @@ public interface Serializer<T> { @Override public Optional<Integer> deserialize(JsonNode json) { - if (json instanceof NumericNode) { + if (json instanceof IntNode) { return Optional.of(json.asInt()); } else { return Optional.empty(); @@ -185,8 +184,10 @@ public interface Serializer<T> { @Override public Optional<Long> deserialize(JsonNode json) { - if (json instanceof NumericNode) { + if (json instanceof LongNode) { return Optional.of(json.asLong()); + } else if (json instanceof IntNode) { + return Optional.of(Long.valueOf(json.asInt())); } else { return Optional.empty(); } @@ -213,7 +214,9 @@ public interface Serializer<T> { @Override public Optional<Float> deserialize(JsonNode json) { - if (json instanceof NumericNode) { + if (json instanceof FloatNode) { + return Optional.of(json.floatValue()); + } else if (json instanceof DoubleNode) { return Optional.of(json.floatValue()); } else { return Optional.empty(); @@ -241,7 +244,7 @@ public interface Serializer<T> { @Override public Optional<Double> deserialize(JsonNode json) { - if (json instanceof NumericNode) { + if (json instanceof DoubleNode || json instanceof FloatNode) { return Optional.of(json.asDouble()); } else { return Optional.empty(); http://git-wip-us.apache.org/repos/asf/james-project/blob/77f4ecb8/mailet/api/src/test/java/org/apache/mailet/AttributeValueTest.java ---------------------------------------------------------------------- diff --git a/mailet/api/src/test/java/org/apache/mailet/AttributeValueTest.java b/mailet/api/src/test/java/org/apache/mailet/AttributeValueTest.java index 95cb0c2..d1189d1 100644 --- a/mailet/api/src/test/java/org/apache/mailet/AttributeValueTest.java +++ b/mailet/api/src/test/java/org/apache/mailet/AttributeValueTest.java @@ -172,6 +172,19 @@ class AttributeValueTest { assertThatIllegalStateException() .isThrownBy(() -> AttributeValue.fromJsonString("{\"serializer\":\"IntSerializer\",\"value\": []}")); } + + @Test + void fromJsonStringShouldThrowOnFloatJson() { + assertThatIllegalStateException() + .isThrownBy(() -> AttributeValue.fromJsonString("{\"serializer\":\"IntSerializer\",\"value\": 42.0}")); + } + + @Test + void fromJsonStringShouldThrowOnLongJson() { + assertThatIllegalStateException() + .isThrownBy(() -> AttributeValue.fromJsonString("{\"serializer\":\"IntSerializer\",\"value\": 2147483648}")); + // Int.MAX_VALUE + 1 + } } @Nested @@ -185,6 +198,15 @@ class AttributeValueTest { assertThat(actual).isEqualTo(expected); } + @Test + void longShouldBeSerializedAndBackForLongMaxValue() { + AttributeValue<Long> expected = AttributeValue.of(Long.MAX_VALUE); + + JsonNode json = expected.toJson(); + AttributeValue<?> actual = AttributeValue.fromJson(json); + + assertThat(actual).isEqualTo(expected); + } @Test void nullLongShouldThrowAnException() { @@ -206,6 +228,12 @@ class AttributeValueTest { assertThatIllegalStateException() .isThrownBy(() -> AttributeValue.fromJsonString("{\"serializer\":\"LongSerializer\",\"value\": []}")); } + + @Test + void fromJsonStringShouldThrowOnFloatJson() { + assertThatIllegalStateException() + .isThrownBy(() -> AttributeValue.fromJsonString("{\"serializer\":\"LongSerializer\",\"value\": 42.0}")); + } } @Nested @@ -240,6 +268,12 @@ class AttributeValueTest { assertThatIllegalStateException() .isThrownBy(() -> AttributeValue.fromJsonString("{\"serializer\":\"FloatSerializer\",\"value\": []}")); } + + @Test + void fromJsonStringShouldThrowOnIntNode() { + assertThatIllegalStateException() + .isThrownBy(() -> AttributeValue.fromJsonString("{\"serializer\":\"FloatSerializer\",\"value\": 1}")); + } } @Nested @@ -253,6 +287,15 @@ class AttributeValueTest { assertThat(actual).isEqualTo(expected); } + @Test + void doubleShouldBeSerializedAndBackForMaxValue() { + AttributeValue<Double> expected = AttributeValue.of(Double.MAX_VALUE); + + JsonNode json = expected.toJson(); + AttributeValue<?> actual = AttributeValue.fromJson(json); + + assertThat(actual).isEqualTo(expected); + } @Test void nullDoubleShouldThrowAnException() { @@ -274,6 +317,12 @@ class AttributeValueTest { assertThatIllegalStateException() .isThrownBy(() -> AttributeValue.fromJsonString("{\"serializer\":\"DoubleSerializer\",\"value\": []}")); } + + @Test + void fromJsonStringShouldThrowOnIntNode() { + assertThatIllegalStateException() + .isThrownBy(() -> AttributeValue.fromJsonString("{\"serializer\":\"DoubleSerializer\",\"value\": 1}")); + } } @Nested --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org