You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by rc...@apache.org on 2019/12/20 07:30:47 UTC

[james-project] 03/15: JAMES-2993 Use JsonSerializationVerifier for Task manager event DTO tests

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

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

commit cf3700459cc0dc2993c8fdcf3e5550ecf79cfd6a
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Wed Dec 18 13:14:38 2019 +0700

    JAMES-2993 Use JsonSerializationVerifier for Task manager event DTO tests
---
 .../apache/james/JsonSerializationVerifier.java    | 28 ++++---
 .../james/JsonSerializationVerifierTest.java       |  5 +-
 server/task/task-distributed/pom.xml               |  6 ++
 .../distributed/TaskEventsSerializationTest.java   | 88 +++++++++-------------
 4 files changed, 60 insertions(+), 67 deletions(-)

diff --git a/json/src/test/java/org/apache/james/JsonSerializationVerifier.java b/json/src/test/java/org/apache/james/JsonSerializationVerifier.java
index 2d717e2..a842ca9 100644
--- a/json/src/test/java/org/apache/james/JsonSerializationVerifier.java
+++ b/json/src/test/java/org/apache/james/JsonSerializationVerifier.java
@@ -34,26 +34,34 @@ import com.github.fge.lambdas.Throwing;
 import com.google.common.collect.ImmutableList;
 
 public class JsonSerializationVerifier<T, U extends DTO> {
+
     @FunctionalInterface
     public interface RequireJson<T, U extends DTO> {
         JsonSerializationVerifier<T, U> json(String json);
     }
 
     public static <T, U extends DTO> JsonSerializationVerifier<T, U> dtoModule(DTOModule<T, U> dtoModule) {
-        return new JsonSerializationVerifier<>(dtoModule, ImmutableList.of());
+        return new JsonSerializationVerifier<>(JsonGenericSerializer
+            .forModules(dtoModule)
+            .withoutNestedType(),
+            ImmutableList.of());
+    }
+
+    public static <T, U extends DTO> JsonSerializationVerifier<T, U> serializer(JsonGenericSerializer<T, U> serializer) {
+        return new JsonSerializationVerifier<>(serializer, ImmutableList.of());
     }
 
-    private final DTOModule<T, U> dtoModule;
     private final List<Pair<String, T>> testValues;
+    private final JsonGenericSerializer<T, U> serializer;
 
-    private JsonSerializationVerifier(DTOModule<T, U> dtoModule, List<Pair<String, T>> testValues) {
-        this.dtoModule = dtoModule;
+    private JsonSerializationVerifier( JsonGenericSerializer<T, U> serializer, List<Pair<String, T>> testValues) {
         this.testValues = testValues;
+        this.serializer = serializer;
     }
 
     public RequireJson<T, U> bean(T bean) {
         return json -> new JsonSerializationVerifier<>(
-            dtoModule,
+            serializer,
             ImmutableList.<Pair<String, T>>builder()
                 .addAll(testValues)
                 .add(Pair.of(json, bean))
@@ -65,15 +73,11 @@ public class JsonSerializationVerifier<T, U extends DTO> {
     }
 
     private void verify(Pair<String, T> testValue) throws IOException {
-        JsonGenericSerializer<T, U> seriliazer = JsonGenericSerializer
-            .forModules(dtoModule)
-            .withoutNestedType();
-
-        assertThatJson(seriliazer.serialize(testValue.getRight()))
-            .describedAs("Serialization test")
+        assertThatJson(serializer.serialize(testValue.getRight()))
+            .describedAs("Serialization test [" + testValue.getRight() + "]")
             .isEqualTo(testValue.getLeft());
 
-        assertThat(seriliazer.deserialize(testValue.getLeft()))
+        assertThat(serializer.deserialize(testValue.getLeft()))
             .describedAs("Deserialization test [" + testValue.getRight() + "]")
             .isEqualToComparingFieldByFieldRecursively(testValue.getRight());
     }
diff --git a/json/src/test/java/org/apache/james/JsonSerializationVerifierTest.java b/json/src/test/java/org/apache/james/JsonSerializationVerifierTest.java
index 4758ab8..88f5737 100644
--- a/json/src/test/java/org/apache/james/JsonSerializationVerifierTest.java
+++ b/json/src/test/java/org/apache/james/JsonSerializationVerifierTest.java
@@ -48,7 +48,8 @@ class JsonSerializationVerifierTest {
                 .json(FIRST_JSON_BAD)
                 .verify())
             .isInstanceOf(AssertionFailedError.class)
-            .hasMessageContaining("[Serialization test] JSON documents are different:\n" +
-                "Different value found in node \"id\"");
+            .hasMessageContaining("[Serialization test [org.apache.james.dto.FirstDomainObject@7650497c]]")
+            .hasMessageContaining("JSON documents are different:")
+            .hasMessageContaining("Different value found in node \"id\"");
     }
 }
diff --git a/server/task/task-distributed/pom.xml b/server/task/task-distributed/pom.xml
index 2e590a6..40fab57 100644
--- a/server/task/task-distributed/pom.xml
+++ b/server/task/task-distributed/pom.xml
@@ -69,6 +69,12 @@
         </dependency>
         <dependency>
             <groupId>${james.groupId}</groupId>
+            <artifactId>james-json</artifactId>
+            <type>test-jar</type>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>${james.groupId}</groupId>
             <artifactId>james-server-lifecycle-api</artifactId>
         </dependency>
         <dependency>
diff --git a/server/task/task-distributed/src/test/java/org/apache/james/task/eventsourcing/distributed/TaskEventsSerializationTest.java b/server/task/task-distributed/src/test/java/org/apache/james/task/eventsourcing/distributed/TaskEventsSerializationTest.java
index bad55dd..8246466 100644
--- a/server/task/task-distributed/src/test/java/org/apache/james/task/eventsourcing/distributed/TaskEventsSerializationTest.java
+++ b/server/task/task-distributed/src/test/java/org/apache/james/task/eventsourcing/distributed/TaskEventsSerializationTest.java
@@ -19,17 +19,14 @@
 
 package org.apache.james.task.eventsourcing.distributed;
 
-import static org.assertj.core.api.Assertions.assertThat;
-
 import java.time.Instant;
 import java.util.Set;
-import java.util.stream.Stream;
 
+import org.apache.james.JsonSerializationVerifier;
 import org.apache.james.eventsourcing.EventId;
-import org.apache.james.eventsourcing.eventstore.cassandra.JsonEventSerializer;
 import org.apache.james.eventsourcing.eventstore.cassandra.dto.EventDTOModule;
 import org.apache.james.json.DTOConverter;
-import org.apache.james.server.task.json.JsonTaskAdditionalInformationSerializer;
+import org.apache.james.json.JsonGenericSerializer;
 import org.apache.james.server.task.json.JsonTaskSerializer;
 import org.apache.james.server.task.json.dto.AdditionalInformationDTO;
 import org.apache.james.server.task.json.dto.MemoryReferenceWithCounterTaskAdditionalInformationDTO;
@@ -49,19 +46,14 @@ import org.apache.james.task.eventsourcing.Created;
 import org.apache.james.task.eventsourcing.Failed;
 import org.apache.james.task.eventsourcing.Started;
 import org.apache.james.task.eventsourcing.TaskAggregateId;
-import org.apache.james.task.eventsourcing.TaskEvent;
-import org.junit.jupiter.params.ParameterizedTest;
-import org.junit.jupiter.params.provider.Arguments;
-import org.junit.jupiter.params.provider.MethodSource;
+import org.junit.jupiter.api.Test;
 
-import net.javacrumbs.jsonunit.assertj.JsonAssertions;
 import scala.Option;
 
 class TaskEventsSerializationTest {
     static final Instant TIMESTAMP = Instant.parse("2018-11-13T12:00:55Z");
     static final DTOConverter<TaskExecutionDetails.AdditionalInformation, AdditionalInformationDTO> ADDITIONAL_INFORMATION_CONVERTER = DTOConverter.of(MemoryReferenceWithCounterTaskAdditionalInformationDTO.SERIALIZATION_MODULE);
     static final DTOConverter<Task, TaskDTO> TASK_CONVERTER = DTOConverter.of(TestTaskDTOModules.COMPLETED_TASK_MODULE);
-    static final JsonTaskAdditionalInformationSerializer TASK_ADDITIONNAL_INFORMATION_SERIALIZER = JsonTaskAdditionalInformationSerializer.of(MemoryReferenceWithCounterTaskAdditionalInformationDTO.SERIALIZATION_MODULE);
     static final TaskAggregateId AGGREGATE_ID = new TaskAggregateId(TaskId.fromString("2c7f4081-aa30-11e9-bf6c-2d3b9e84aafd"));
     static final EventId EVENT_ID = EventId.fromSerialized(42);
     static final Task TASK = new CompletedTask();
@@ -75,47 +67,37 @@ class TaskEventsSerializationTest {
         ADDITIONAL_INFORMATION_CONVERTER,
         TASK_CONVERTER);
 
-    JsonEventSerializer serializer = JsonEventSerializer
-        .forModules(list)
-        .withNestedTypeModules(
-            MemoryReferenceWithCounterTaskAdditionalInformationDTO.SERIALIZATION_MODULE,
-            TestTaskDTOModules.COMPLETED_TASK_MODULE);
-
-    @ParameterizedTest
-    @MethodSource
-    void taskShouldBeSerializable(TaskEvent event, String serializedJson) throws Exception {
-        JsonAssertions.assertThatJson(serializer.serialize(event)).isEqualTo(serializedJson);
-    }
-
-    static Stream<Arguments> taskShouldBeSerializable() throws Exception {
-        return validTasks();
-    }
-
-    @ParameterizedTest
-    @MethodSource
-    void taskShouldBeDeserializable(TaskEvent event, String serializedJson) throws Exception {
-        assertThat(serializer.deserialize(serializedJson)).isEqualToComparingFieldByFieldRecursively(event);
+    @Test
+    void taskManagerEventsShouldBeSerializable() throws Exception {
+        JsonSerializationVerifier.serializer(JsonGenericSerializer
+            .forModules(list)
+            .withNestedTypeModules(
+                MemoryReferenceWithCounterTaskAdditionalInformationDTO.SERIALIZATION_MODULE,
+                TestTaskDTOModules.COMPLETED_TASK_MODULE))
+            .bean(new Created(AGGREGATE_ID, EVENT_ID, TASK, HOSTNAME))
+                .json("{\"task\":{\"type\":\"completed-task\"},\"type\":\"task-manager-created\",\"aggregate\":\"2c7f4081-aa30-11e9-bf6c-2d3b9e84aafd\",\"event\":42,\"hostname\":\"foo\"}")
+            .bean(new Started(AGGREGATE_ID, EVENT_ID, HOSTNAME))
+                .json("{\"aggregate\":\"2c7f4081-aa30-11e9-bf6c-2d3b9e84aafd\",\"event\":42,\"type\":\"task-manager-started\",\"hostname\":\"foo\"}")
+            .bean(new CancelRequested(AGGREGATE_ID, EVENT_ID, HOSTNAME))
+                .json("{\"type\":\"task-manager-cancel-requested\",\"aggregate\":\"2c7f4081-aa30-11e9-bf6c-2d3b9e84aafd\",\"event\":42,\"hostname\":\"foo\"}")
+            .bean(new Completed(AGGREGATE_ID, EVENT_ID, Task.Result.COMPLETED, Option.empty()))
+                .json("{\"result\":\"COMPLETED\",\"aggregate\":\"2c7f4081-aa30-11e9-bf6c-2d3b9e84aafd\",\"event\":42,\"type\":\"task-manager-completed\"}")
+            .bean(new Completed(AGGREGATE_ID, EVENT_ID, Task.Result.PARTIAL, Option.empty()))
+                .json("{\"result\":\"PARTIAL\",\"aggregate\":\"2c7f4081-aa30-11e9-bf6c-2d3b9e84aafd\",\"event\":42,\"type\":\"task-manager-completed\"}")
+            .bean(new Failed(AGGREGATE_ID, EVENT_ID, Option.empty(), Option.empty(), Option.empty()))
+                .json("{\"aggregate\":\"2c7f4081-aa30-11e9-bf6c-2d3b9e84aafd\",\"event\":42,\"type\":\"task-manager-failed\"}")
+            .bean(new Failed(AGGREGATE_ID, EVENT_ID, Option.empty(), Option.apply("contextual message"), Option.apply("my exception")))
+                .json("{\"aggregate\":\"2c7f4081-aa30-11e9-bf6c-2d3b9e84aafd\",\"event\":42,\"type\":\"task-manager-failed\", \"errorMessage\": \"contextual message\", \"exception\": \"my exception\"}")
+            .bean(new Cancelled(AGGREGATE_ID, EVENT_ID, Option.empty()))
+                .json("{\"aggregate\":\"2c7f4081-aa30-11e9-bf6c-2d3b9e84aafd\",\"event\":42,\"type\":\"task-manager-cancelled\"}")
+            .bean(new Completed(AGGREGATE_ID, EVENT_ID, Task.Result.COMPLETED, Option.apply(COUNTER_ADDITIONAL_INFORMATION)))
+                .json("{\"result\":\"COMPLETED\",\"aggregate\":\"2c7f4081-aa30-11e9-bf6c-2d3b9e84aafd\",\"event\":42,\"type\":\"task-manager-completed\",\"additionalInformation\":{\"type\":\"memory-reference-task-with-counter\",\"count\":3,\"timestamp\":\"2018-11-13T12:00:55Z\"}}")
+            .bean(new Completed(AGGREGATE_ID, EVENT_ID, Task.Result.PARTIAL, Option.apply(COUNTER_ADDITIONAL_INFORMATION)))
+                .json("{\"result\":\"PARTIAL\",\"aggregate\":\"2c7f4081-aa30-11e9-bf6c-2d3b9e84aafd\",\"event\":42,\"type\":\"task-manager-completed\",\"additionalInformation\":{\"type\":\"memory-reference-task-with-counter\",\"count\":3,\"timestamp\":\"2018-11-13T12:00:55Z\"}}")
+            .bean(new Failed(AGGREGATE_ID, EVENT_ID, Option.apply(COUNTER_ADDITIONAL_INFORMATION), Option.empty(), Option.empty()))
+                .json("{\"aggregate\":\"2c7f4081-aa30-11e9-bf6c-2d3b9e84aafd\",\"event\":42,\"type\":\"task-manager-failed\",\"additionalInformation\":{\"type\":\"memory-reference-task-with-counter\",\"count\":3,\"timestamp\":\"2018-11-13T12:00:55Z\"}}")
+            .bean(new Cancelled(AGGREGATE_ID, EVENT_ID, Option.apply(COUNTER_ADDITIONAL_INFORMATION)))
+                .json("{\"aggregate\":\"2c7f4081-aa30-11e9-bf6c-2d3b9e84aafd\",\"event\":42,\"type\":\"task-manager-cancelled\",\"additionalInformation\":{\"type\":\"memory-reference-task-with-counter\",\"count\":3,\"timestamp\":\"2018-11-13T12:00:55Z\"}}")
+            .verify();
     }
-
-    static Stream<Arguments> taskShouldBeDeserializable() throws Exception {
-        return validTasks();
-    }
-
-    static Stream<Arguments> validTasks() throws Exception {
-        return Stream.of(
-            Arguments.of(new Created(AGGREGATE_ID, EVENT_ID, TASK, HOSTNAME), "{\"task\":{\"type\":\"completed-task\"},\"type\":\"task-manager-created\",\"aggregate\":\"2c7f4081-aa30-11e9-bf6c-2d3b9e84aafd\",\"event\":42,\"hostname\":\"foo\"}\n"),
-            Arguments.of(new Started(AGGREGATE_ID, EVENT_ID, HOSTNAME), "{\"aggregate\":\"2c7f4081-aa30-11e9-bf6c-2d3b9e84aafd\",\"event\":42,\"type\":\"task-manager-started\",\"hostname\":\"foo\"}"),
-            Arguments.of(new CancelRequested(AGGREGATE_ID, EVENT_ID, HOSTNAME), "{\"type\":\"task-manager-cancel-requested\",\"aggregate\":\"2c7f4081-aa30-11e9-bf6c-2d3b9e84aafd\",\"event\":42,\"hostname\":\"foo\"}\n"),
-            Arguments.of(new Completed(AGGREGATE_ID, EVENT_ID, Task.Result.COMPLETED, Option.empty()), "{\"result\":\"COMPLETED\",\"aggregate\":\"2c7f4081-aa30-11e9-bf6c-2d3b9e84aafd\",\"event\":42,\"type\":\"task-manager-completed\"}"),
-            Arguments.of(new Completed(AGGREGATE_ID, EVENT_ID, Task.Result.PARTIAL, Option.empty()), "{\"result\":\"PARTIAL\",\"aggregate\":\"2c7f4081-aa30-11e9-bf6c-2d3b9e84aafd\",\"event\":42,\"type\":\"task-manager-completed\"}"),
-            Arguments.of(new Failed(AGGREGATE_ID, EVENT_ID, Option.empty(), Option.empty(), Option.empty()), "{\"aggregate\":\"2c7f4081-aa30-11e9-bf6c-2d3b9e84aafd\",\"event\":42,\"type\":\"task-manager-failed\"}"),
-            Arguments.of(new Failed(AGGREGATE_ID, EVENT_ID, Option.empty(), Option.apply("contextual message"), Option.apply("my exception")), "{\"aggregate\":\"2c7f4081-aa30-11e9-bf6c-2d3b9e84aafd\",\"event\":42,\"type\":\"task-manager-failed\", \"errorMessage\": \"contextual message\", \"exception\": \"my exception\"}"),
-            Arguments.of(new Cancelled(AGGREGATE_ID, EVENT_ID, Option.empty()), "{\"aggregate\":\"2c7f4081-aa30-11e9-bf6c-2d3b9e84aafd\",\"event\":42,\"type\":\"task-manager-cancelled\"}"),
-            Arguments.of(new Completed(AGGREGATE_ID, EVENT_ID, Task.Result.COMPLETED, Option.apply(COUNTER_ADDITIONAL_INFORMATION)), "{\"result\":\"COMPLETED\",\"aggregate\":\"2c7f4081-aa30-11e9-bf6c-2d3b9e84aafd\",\"event\":42,\"type\":\"task-manager-completed\",\"additionalInformation\":{\"type\":\"memory-reference-task-with-counter\",\"count\":3,\"timestamp\":\"2018-11-13T12:00:55Z\"}}"),
-            Arguments.of(new Completed(AGGREGATE_ID, EVENT_ID, Task.Result.PARTIAL, Option.apply(COUNTER_ADDITIONAL_INFORMATION)), "{\"result\":\"PARTIAL\",\"aggregate\":\"2c7f4081-aa30-11e9-bf6c-2d3b9e84aafd\",\"event\":42,\"type\":\"task-manager-completed\",\"additionalInformation\":{\"type\":\"memory-reference-task-with-counter\",\"count\":3,\"timestamp\":\"2018-11-13T12:00:55Z\"}}"),
-            Arguments.of(new Failed(AGGREGATE_ID, EVENT_ID, Option.apply(COUNTER_ADDITIONAL_INFORMATION), Option.empty(), Option.empty()), "{\"aggregate\":\"2c7f4081-aa30-11e9-bf6c-2d3b9e84aafd\",\"event\":42,\"type\":\"task-manager-failed\",\"additionalInformation\":{\"type\":\"memory-reference-task-with-counter\",\"count\":3,\"timestamp\":\"2018-11-13T12:00:55Z\"}}"),
-            Arguments.of(new Cancelled(AGGREGATE_ID, EVENT_ID, Option.apply(COUNTER_ADDITIONAL_INFORMATION)), "{\"aggregate\":\"2c7f4081-aa30-11e9-bf6c-2d3b9e84aafd\",\"event\":42,\"type\":\"task-manager-cancelled\",\"additionalInformation\":{\"type\":\"memory-reference-task-with-counter\",\"count\":3,\"timestamp\":\"2018-11-13T12:00:55Z\"}}")
-        );
-    }
-
 }
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org