This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 774e96cadd4c5b3ad05db0b7561b976f31a6703f
Author: Benoit Tellier <[email protected]>
AuthorDate: Mon Dec 16 18:16:52 2019 +0700

    JAMES-2993 Provide a JsonSerializationVerifier
---
 .../apache/james/JsonGenericSerializerTest.java    | 30 ++++------
 .../apache/james/JsonSerializationVerifier.java    | 69 ++++++++++++++++++++++
 .../james/JsonSerializationVerifierTest.java       | 54 +++++++++++++++++
 .../org/apache/james/SerializationFixture.java     | 47 +++++++++++++++
 .../java/org/apache/james/dto/TestModules.java     |  2 +-
 5 files changed, 181 insertions(+), 21 deletions(-)

diff --git a/json/src/test/java/org/apache/james/JsonGenericSerializerTest.java 
b/json/src/test/java/org/apache/james/JsonGenericSerializerTest.java
index 6b179ba..14e528a 100644
--- a/json/src/test/java/org/apache/james/JsonGenericSerializerTest.java
+++ b/json/src/test/java/org/apache/james/JsonGenericSerializerTest.java
@@ -20,20 +20,22 @@
 package org.apache.james;
 
 import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
+import static org.apache.james.SerializationFixture.DUPLICATE_TYPE_JSON;
+import static org.apache.james.SerializationFixture.FIRST;
+import static org.apache.james.SerializationFixture.FIRST_JSON;
+import static org.apache.james.SerializationFixture.FIRST_JSON_WITH_NESTED;
+import static org.apache.james.SerializationFixture.FIRST_WITH_NESTED;
+import static org.apache.james.SerializationFixture.MISSING_TYPE_JSON;
+import static org.apache.james.SerializationFixture.SECOND;
+import static org.apache.james.SerializationFixture.SECOND_JSON;
+import static org.apache.james.SerializationFixture.SECOND_WITH_NESTED;
+import static org.apache.james.SerializationFixture.SECOND_WITH_NESTED_JSON;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
-import java.time.ZonedDateTime;
-import java.util.Optional;
-import java.util.UUID;
 import java.util.stream.Stream;
 
 import org.apache.james.dto.BaseType;
-import org.apache.james.dto.FirstDomainObject;
-import org.apache.james.dto.FirstNestedType;
-import org.apache.james.dto.NestedType;
-import org.apache.james.dto.SecondDomainObject;
-import org.apache.james.dto.SecondNestedType;
 import org.apache.james.dto.TestModules;
 import org.apache.james.json.DTO;
 import org.apache.james.json.JsonGenericSerializer;
@@ -43,18 +45,6 @@ import org.junit.jupiter.params.provider.Arguments;
 import org.junit.jupiter.params.provider.MethodSource;
 
 class JsonGenericSerializerTest {
-    private static final Optional<NestedType> NO_CHILD = Optional.empty();
-    private static final BaseType FIRST = new 
FirstDomainObject(Optional.of(1L), 
ZonedDateTime.parse("2016-04-03T02:01+07:00[Asia/Vientiane]"), "first payload", 
NO_CHILD);
-    private static final BaseType SECOND = new 
SecondDomainObject(UUID.fromString("4a2c853f-7ffc-4ce3-9410-a47e85b3b741"), 
"second payload", NO_CHILD);
-    private static final BaseType SECOND_WITH_NESTED = new 
SecondDomainObject(UUID.fromString("4a2c853f-7ffc-4ce3-9410-a47e85b3b741"), 
"second payload", Optional.of(new FirstNestedType(12)));
-    private static final BaseType FIRST_WITH_NESTED = new 
FirstDomainObject(Optional.of(1L), 
ZonedDateTime.parse("2016-04-03T02:01+07:00[Asia/Vientiane]"), "payload", 
Optional.of(new SecondNestedType("bar")));
-
-    private static final String MISSING_TYPE_JSON = 
"{\"id\":1,\"time\":\"2016-04-03T02:01+07:00[Asia/Vientiane]\",\"payload\":\"first
 payload\"}";
-    private static final String DUPLICATE_TYPE_JSON = "{\"type\":\"first\", 
\"type\":\"second\", 
\"id\":1,\"time\":\"2016-04-03T02:01+07:00[Asia/Vientiane]\",\"payload\":\"first
 payload\"}";
-    private static final String FIRST_JSON = 
"{\"type\":\"first\",\"id\":1,\"time\":\"2016-04-03T02:01+07:00[Asia/Vientiane]\",\"payload\":\"first
 payload\"}";
-    private static final String FIRST_JSON_WITH_NESTED = 
"{\"type\":\"first\",\"id\":1,\"time\":\"2016-04-03T02:01+07:00[Asia/Vientiane]\",\"payload\":\"payload\",
 \"child\": {\"bar\": \"bar\", \"type\": \"second-nested\"}}";
-    private static final String SECOND_JSON = 
"{\"type\":\"second\",\"id\":\"4a2c853f-7ffc-4ce3-9410-a47e85b3b741\",\"payload\":\"second
 payload\"}";
-    private static final String SECOND_WITH_NESTED_JSON = 
"{\"type\":\"second\",\"id\":\"4a2c853f-7ffc-4ce3-9410-a47e85b3b741\",\"payload\":\"second
 payload\", \"child\": {\"foo\": 12, \"type\": \"first-nested\"}}";
 
     @Test
     void shouldDeserializeKnownType() throws Exception {
diff --git a/json/src/test/java/org/apache/james/JsonSerializationVerifier.java 
b/json/src/test/java/org/apache/james/JsonSerializationVerifier.java
new file mode 100644
index 0000000..31eee87
--- /dev/null
+++ b/json/src/test/java/org/apache/james/JsonSerializationVerifier.java
@@ -0,0 +1,69 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james;
+
+import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.IOException;
+
+import org.apache.james.json.DTO;
+import org.apache.james.json.DTOModule;
+import org.apache.james.json.JsonGenericSerializer;
+
+public class JsonSerializationVerifier<T, U extends DTO> {
+    @FunctionalInterface
+    public interface RequireBean<T, U extends DTO> {
+        RequireJson<T, U> bean(T bean);
+    }
+
+    @FunctionalInterface
+    public interface RequireJson<T, U extends DTO> {
+        JsonSerializationVerifier<T, U> json(String json);
+    }
+
+    public static <T, U extends DTO> RequireBean<T, U> dtoModule(DTOModule<T, 
U> dtoModule) {
+        return bean -> json -> new JsonSerializationVerifier<>(dtoModule, 
json, bean);
+    }
+
+    private final DTOModule<T, U> dtoModule;
+    private final String json;
+    private final T bean;
+
+    private JsonSerializationVerifier(DTOModule<T, U> dtoModule, String json, 
T bean) {
+        this.dtoModule = dtoModule;
+        this.json = json;
+        this.bean = bean;
+    }
+
+    public void verify() throws IOException {
+        JsonGenericSerializer<T, U> seriliazer = JsonGenericSerializer
+            .forModules(dtoModule)
+            .withoutNestedType();
+
+        assertThatJson(seriliazer.serialize(bean))
+            .describedAs("Serialization test")
+            .isEqualTo(json);
+
+        assertThat(seriliazer.deserialize(json))
+            .describedAs("Deserialization test")
+            .isEqualTo(bean);
+    }
+}
diff --git 
a/json/src/test/java/org/apache/james/JsonSerializationVerifierTest.java 
b/json/src/test/java/org/apache/james/JsonSerializationVerifierTest.java
new file mode 100644
index 0000000..4758ab8
--- /dev/null
+++ b/json/src/test/java/org/apache/james/JsonSerializationVerifierTest.java
@@ -0,0 +1,54 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james;
+
+import static org.apache.james.SerializationFixture.FIRST;
+import static org.apache.james.SerializationFixture.FIRST_JSON;
+import static org.apache.james.SerializationFixture.FIRST_JSON_BAD;
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import org.apache.james.dto.TestModules;
+import org.junit.jupiter.api.Test;
+import org.opentest4j.AssertionFailedError;
+
+class JsonSerializationVerifierTest {
+    @Test
+    void verifyShouldNotThrowWhenValid() {
+        assertThatCode(() ->
+            JsonSerializationVerifier.dtoModule(TestModules.FIRST_TYPE)
+                .bean(FIRST)
+                .json(FIRST_JSON)
+                .verify())
+            .doesNotThrowAnyException();
+    }
+
+    @Test
+    void verifyShouldThrowOnUnexpectedJson() {
+        assertThatThrownBy(() ->
+            JsonSerializationVerifier.dtoModule(TestModules.FIRST_TYPE)
+                .bean(FIRST)
+                .json(FIRST_JSON_BAD)
+                .verify())
+            .isInstanceOf(AssertionFailedError.class)
+            .hasMessageContaining("[Serialization test] JSON documents are 
different:\n" +
+                "Different value found in node \"id\"");
+    }
+}
diff --git a/json/src/test/java/org/apache/james/SerializationFixture.java 
b/json/src/test/java/org/apache/james/SerializationFixture.java
new file mode 100644
index 0000000..0a8eff0
--- /dev/null
+++ b/json/src/test/java/org/apache/james/SerializationFixture.java
@@ -0,0 +1,47 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james;
+
+import java.time.ZonedDateTime;
+import java.util.Optional;
+import java.util.UUID;
+
+import org.apache.james.dto.BaseType;
+import org.apache.james.dto.FirstDomainObject;
+import org.apache.james.dto.FirstNestedType;
+import org.apache.james.dto.NestedType;
+import org.apache.james.dto.SecondDomainObject;
+import org.apache.james.dto.SecondNestedType;
+
+public interface SerializationFixture {
+    Optional<NestedType> NO_CHILD = Optional.empty();
+    FirstDomainObject FIRST = new FirstDomainObject(Optional.of(1L), 
ZonedDateTime.parse("2016-04-03T02:01+07:00[Asia/Vientiane]"), "first payload", 
NO_CHILD);
+    BaseType SECOND = new 
SecondDomainObject(UUID.fromString("4a2c853f-7ffc-4ce3-9410-a47e85b3b741"), 
"second payload", NO_CHILD);
+    BaseType SECOND_WITH_NESTED = new 
SecondDomainObject(UUID.fromString("4a2c853f-7ffc-4ce3-9410-a47e85b3b741"), 
"second payload", Optional.of(new FirstNestedType(12)));
+    BaseType FIRST_WITH_NESTED = new FirstDomainObject(Optional.of(1L), 
ZonedDateTime.parse("2016-04-03T02:01+07:00[Asia/Vientiane]"), "payload", 
Optional.of(new SecondNestedType("bar")));
+
+    String MISSING_TYPE_JSON = 
"{\"id\":1,\"time\":\"2016-04-03T02:01+07:00[Asia/Vientiane]\",\"payload\":\"first
 payload\"}";
+    String DUPLICATE_TYPE_JSON = "{\"type\":\"first\", \"type\":\"second\", 
\"id\":1,\"time\":\"2016-04-03T02:01+07:00[Asia/Vientiane]\",\"payload\":\"first
 payload\"}";
+    String FIRST_JSON = 
"{\"type\":\"first\",\"id\":1,\"time\":\"2016-04-03T02:01+07:00[Asia/Vientiane]\",\"payload\":\"first
 payload\"}";
+    String FIRST_JSON_BAD = 
"{\"type\":\"first\",\"id\":2,\"time\":\"2016-04-03T02:01+07:00[Asia/Vientiane]\",\"payload\":\"first
 payload\"}";
+    String FIRST_JSON_WITH_NESTED = 
"{\"type\":\"first\",\"id\":1,\"time\":\"2016-04-03T02:01+07:00[Asia/Vientiane]\",\"payload\":\"payload\",
 \"child\": {\"bar\": \"bar\", \"type\": \"second-nested\"}}";
+    String SECOND_JSON = 
"{\"type\":\"second\",\"id\":\"4a2c853f-7ffc-4ce3-9410-a47e85b3b741\",\"payload\":\"second
 payload\"}";
+    String SECOND_WITH_NESTED_JSON = 
"{\"type\":\"second\",\"id\":\"4a2c853f-7ffc-4ce3-9410-a47e85b3b741\",\"payload\":\"second
 payload\", \"child\": {\"foo\": 12, \"type\": \"first-nested\"}}";
+}
diff --git a/json/src/test/java/org/apache/james/dto/TestModules.java 
b/json/src/test/java/org/apache/james/dto/TestModules.java
index 88dbf79..802573b 100644
--- a/json/src/test/java/org/apache/james/dto/TestModules.java
+++ b/json/src/test/java/org/apache/james/dto/TestModules.java
@@ -46,7 +46,7 @@ public interface TestModules {
 
     DTOConverter<NestedType, DTO> NESTED_CONVERTERS = 
DTOConverter.of(FIRST_NESTED, SECOND_NESTED);
 
-    TestModule<?, ?> FIRST_TYPE = DTOModule
+    TestModule<FirstDomainObject, FirstDTO> FIRST_TYPE = DTOModule
         .forDomainObject(FirstDomainObject.class)
         .convertToDTO(FirstDTO.class)
         .toDomainObjectConverter(dto -> dto.toDomainObject(NESTED_CONVERTERS))


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to