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 ma...@apache.org on 2020/02/27 16:20:29 UTC
[james-project] 08/14: JAMES-3009 convert cassandra event store
test to scala
This is an automated email from the ASF dual-hosted git repository.
matthieu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit ff8062692a5d6160786d8001aaa191c4c136c7d2
Author: Rémi KOWALSKI <rk...@linagora.com>
AuthorDate: Wed Dec 11 15:47:09 2019 +0100
JAMES-3009 convert cassandra event store test to scala
---
.../cassandra/CassandraEventStoreExtension.java | 77 -------------
.../cassandra/JsonEventSerializerTest.java | 123 ---------------------
.../CassandraEventSourcingSystemTest.scala} | 16 +--
.../cassandra/CassandraEventStoreExtension.scala | 57 ++++++++++
...assandraEventStoreExtensionForTestEvents.scala} | 15 +--
.../cassandra/CassandraEventStoreTest.scala} | 16 +--
.../cassandra/JsonEventSerializerTest.scala | 95 ++++++++++++++++
7 files changed, 169 insertions(+), 230 deletions(-)
diff --git a/event-sourcing/event-store-cassandra/src/test/java/org/apache/james/eventsourcing/eventstore/cassandra/CassandraEventStoreExtension.java b/event-sourcing/event-store-cassandra/src/test/java/org/apache/james/eventsourcing/eventstore/cassandra/CassandraEventStoreExtension.java
deleted file mode 100644
index d319d8f..0000000
--- a/event-sourcing/event-store-cassandra/src/test/java/org/apache/james/eventsourcing/eventstore/cassandra/CassandraEventStoreExtension.java
+++ /dev/null
@@ -1,77 +0,0 @@
-/****************************************************************
- * 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.eventsourcing.eventstore.cassandra;
-
-import org.apache.james.backends.cassandra.CassandraClusterExtension;
-import org.apache.james.eventsourcing.eventstore.EventStore;
-import org.junit.jupiter.api.extension.AfterAllCallback;
-import org.junit.jupiter.api.extension.AfterEachCallback;
-import org.junit.jupiter.api.extension.BeforeAllCallback;
-import org.junit.jupiter.api.extension.BeforeEachCallback;
-import org.junit.jupiter.api.extension.ExtensionContext;
-import org.junit.jupiter.api.extension.ParameterContext;
-import org.junit.jupiter.api.extension.ParameterResolutionException;
-import org.junit.jupiter.api.extension.ParameterResolver;
-
-public class CassandraEventStoreExtension implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback, ParameterResolver {
-
- private CassandraClusterExtension cassandra;
- private final JsonEventSerializer eventSerializer;
- private EventStoreDao eventStoreDao;
-
- public CassandraEventStoreExtension(JsonEventSerializer eventSerializer) {
- this(new CassandraClusterExtension(CassandraEventStoreModule.MODULE()), eventSerializer);
- }
-
- public CassandraEventStoreExtension(CassandraClusterExtension cassandra, JsonEventSerializer eventSerializer) {
- this.cassandra = cassandra;
- this.eventSerializer = eventSerializer;
- }
-
- @Override
- public void beforeAll(ExtensionContext context) {
- cassandra.beforeAll(context);
- }
-
- @Override
- public void afterAll(ExtensionContext context) {
- cassandra.afterAll(context);
- }
-
- @Override
- public void beforeEach(ExtensionContext context) {
- eventStoreDao = new EventStoreDao(cassandra.getCassandraCluster().getConf(), eventSerializer);
- }
-
- @Override
- public void afterEach(ExtensionContext context) {
- cassandra.afterEach(context);
- }
-
- @Override
- public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
- return (parameterContext.getParameter().getType() == EventStore.class);
- }
-
- @Override
- public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
- return new CassandraEventStore(eventStoreDao);
- }
-}
diff --git a/event-sourcing/event-store-cassandra/src/test/java/org/apache/james/eventsourcing/eventstore/cassandra/JsonEventSerializerTest.java b/event-sourcing/event-store-cassandra/src/test/java/org/apache/james/eventsourcing/eventstore/cassandra/JsonEventSerializerTest.java
deleted file mode 100644
index aaad89e..0000000
--- a/event-sourcing/event-store-cassandra/src/test/java/org/apache/james/eventsourcing/eventstore/cassandra/JsonEventSerializerTest.java
+++ /dev/null
@@ -1,123 +0,0 @@
-/****************************************************************
- * 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.eventsourcing.eventstore.cassandra;
-
-import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
-
-import org.apache.james.eventsourcing.EventId;
-import org.apache.james.eventsourcing.TestAggregateId;
-import org.apache.james.eventsourcing.TestEvent;
-import org.apache.james.eventsourcing.eventstore.cassandra.dto.OtherEvent;
-import org.apache.james.eventsourcing.eventstore.cassandra.dto.TestEventDTOModules;
-import org.junit.jupiter.api.Test;
-
-class JsonEventSerializerTest {
- public static final EventId EVENT_ID = EventId.fromSerialized(0);
- public static final TestAggregateId AGGREGATE_ID = TestAggregateId.apply(1);
-
- public static final OtherEvent OTHER_EVENT = new OtherEvent(EVENT_ID, AGGREGATE_ID, 1);
- public static final TestEvent TEST_EVENT = new TestEvent(EVENT_ID, AGGREGATE_ID, "first");
-
- public static final String TEST_EVENT_JSON = "{\"type\":\"TestType\",\"data\":\"first\",\"eventId\":0,\"aggregate\":1}";
- public static final String OTHER_EVENT_JSON = "{\"type\":\"other-type\",\"data\":1,\"eventId\":0,\"aggregate\":1}";
- public static final String MISSING_TYPE_EVENT_JSON = "{\"data\":1,\"eventId\":0,\"aggregate\":1}";
- public static final String DUPLICATE_TYPE_EVENT_JSON = "{\"type\":\"TestType\", \"type\":\"other-type\",\"data\":1,\"eventId\":0,\"aggregate\":1}";
-
- @Test
- void shouldDeserializeKnownEvent() throws Exception {
- assertThat(JsonEventSerializer.forModules(TestEventDTOModules.TEST_TYPE()).withoutNestedType()
- .deserialize(TEST_EVENT_JSON))
- .isEqualTo(TEST_EVENT);
- }
-
- @Test
- void shouldThrowWhenDeserializeUnknownEvent() {
- assertThatThrownBy(() -> JsonEventSerializer.forModules().withoutNestedType()
- .deserialize(TEST_EVENT_JSON))
- .isInstanceOf(JsonEventSerializer.UnknownEventException.class);
- }
-
- @Test
- void serializeShouldHandleAllKnownEvents() throws Exception {
- JsonEventSerializer jsonEventSerializer = JsonEventSerializer
- .forModules(TestEventDTOModules.TEST_TYPE(), TestEventDTOModules.OTHER_TEST_TYPE())
- .withoutNestedType();
-
- assertThatJson(
- jsonEventSerializer.serialize(OTHER_EVENT))
- .isEqualTo(OTHER_EVENT_JSON);
-
- assertThatJson(
- jsonEventSerializer.serialize(TEST_EVENT))
- .isEqualTo(TEST_EVENT_JSON);
- }
-
- @Test
- void deserializeShouldHandleAllKnownEvents() throws Exception {
- JsonEventSerializer jsonEventSerializer = JsonEventSerializer
- .forModules(TestEventDTOModules.TEST_TYPE(), TestEventDTOModules.OTHER_TEST_TYPE())
- .withoutNestedType();
-
- assertThatJson(
- jsonEventSerializer.deserialize(OTHER_EVENT_JSON))
- .isEqualTo(OTHER_EVENT);
-
- assertThatJson(
- jsonEventSerializer.deserialize(TEST_EVENT_JSON))
- .isEqualTo(TEST_EVENT);
- }
-
-
- @Test
- void deserializeShouldThrowWhenEventWithDuplicatedTypes() {
- assertThatThrownBy(() -> JsonEventSerializer
- .forModules(TestEventDTOModules.TEST_TYPE(), TestEventDTOModules.OTHER_TEST_TYPE())
- .withoutNestedType()
- .deserialize(DUPLICATE_TYPE_EVENT_JSON))
- .isInstanceOf(JsonEventSerializer.InvalidEventException.class);
- }
-
- @Test
- void deserializeShouldThrowWhenEventWithMissingType() {
- assertThatThrownBy(() -> JsonEventSerializer
- .forModules(TestEventDTOModules.TEST_TYPE(), TestEventDTOModules.OTHER_TEST_TYPE())
- .withoutNestedType()
- .deserialize(MISSING_TYPE_EVENT_JSON))
- .isInstanceOf(JsonEventSerializer.InvalidEventException.class);
- }
-
- @Test
- void shouldSerializeKnownEvent() throws Exception {
- assertThatJson(JsonEventSerializer.forModules(TestEventDTOModules.TEST_TYPE()).withoutNestedType()
- .serialize(TEST_EVENT))
- .isEqualTo(TEST_EVENT_JSON);
- }
-
- @Test
- void shouldThrowWhenSerializeUnknownEvent() {
- assertThatThrownBy(() -> JsonEventSerializer.forModules().withoutNestedType()
- .serialize(TEST_EVENT))
- .isInstanceOf(JsonEventSerializer.UnknownEventException.class);
- }
-
-
-}
\ No newline at end of file
diff --git a/event-sourcing/event-store-cassandra/src/test/java/org/apache/james/eventsourcing/eventstore/cassandra/CassandraEventSourcingSystemTest.java b/event-sourcing/event-store-cassandra/src/test/scala/org/apache/james/eventsourcing/eventstore/cassandra/CassandraEventSourcingSystemTest.scala
similarity index 64%
rename from event-sourcing/event-store-cassandra/src/test/java/org/apache/james/eventsourcing/eventstore/cassandra/CassandraEventSourcingSystemTest.java
rename to event-sourcing/event-store-cassandra/src/test/scala/org/apache/james/eventsourcing/eventstore/cassandra/CassandraEventSourcingSystemTest.scala
index 4a20058..bc92f8f 100644
--- a/event-sourcing/event-store-cassandra/src/test/java/org/apache/james/eventsourcing/eventstore/cassandra/CassandraEventSourcingSystemTest.java
+++ b/event-sourcing/event-store-cassandra/src/test/scala/org/apache/james/eventsourcing/eventstore/cassandra/CassandraEventSourcingSystemTest.scala
@@ -1,4 +1,4 @@
-/****************************************************************
+ /***************************************************************
* 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 *
@@ -16,14 +16,10 @@
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/
+package org.apache.james.eventsourcing.eventstore.cassandra
-package org.apache.james.eventsourcing.eventstore.cassandra;
+import org.apache.james.eventsourcing.EventSourcingSystemTest
+import org.junit.jupiter.api.extension.ExtendWith
-import org.apache.james.eventsourcing.EventSourcingSystemTest;
-import org.apache.james.eventsourcing.eventstore.cassandra.dto.TestEventDTOModules;
-import org.junit.jupiter.api.extension.RegisterExtension;
-
-public class CassandraEventSourcingSystemTest implements EventSourcingSystemTest {
- @RegisterExtension
- static CassandraEventStoreExtension eventStoreExtension = new CassandraEventStoreExtension(JsonEventSerializer.forModules(TestEventDTOModules.TEST_TYPE()).withoutNestedType());
-}
+@ExtendWith(Array(classOf[CassandraEventStoreExtensionForTestEvents]))
+class CassandraEventSourcingSystemTest extends EventSourcingSystemTest
\ No newline at end of file
diff --git a/event-sourcing/event-store-cassandra/src/test/scala/org/apache/james/eventsourcing/eventstore/cassandra/CassandraEventStoreExtension.scala b/event-sourcing/event-store-cassandra/src/test/scala/org/apache/james/eventsourcing/eventstore/cassandra/CassandraEventStoreExtension.scala
new file mode 100644
index 0000000..dd8f7d8
--- /dev/null
+++ b/event-sourcing/event-store-cassandra/src/test/scala/org/apache/james/eventsourcing/eventstore/cassandra/CassandraEventStoreExtension.scala
@@ -0,0 +1,57 @@
+ /***************************************************************
+ * 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.eventsourcing.eventstore.cassandra
+
+import org.apache.james.backends.cassandra.CassandraClusterExtension
+import org.apache.james.eventsourcing.eventstore.EventStore
+import org.junit.jupiter.api.extension.AfterAllCallback
+import org.junit.jupiter.api.extension.AfterEachCallback
+import org.junit.jupiter.api.extension.BeforeAllCallback
+import org.junit.jupiter.api.extension.BeforeEachCallback
+import org.junit.jupiter.api.extension.ExtensionContext
+import org.junit.jupiter.api.extension.ParameterContext
+import org.junit.jupiter.api.extension.ParameterResolutionException
+import org.junit.jupiter.api.extension.ParameterResolver
+
+class CassandraEventStoreExtension(var cassandra: CassandraClusterExtension, val eventSerializer: JsonEventSerializer)
+ extends BeforeAllCallback with AfterAllCallback with BeforeEachCallback with AfterEachCallback with ParameterResolver {
+
+ private var eventStoreDao : Option[EventStoreDao] = None
+
+ def this(eventSerializer: JsonEventSerializer) {
+ this(new CassandraClusterExtension(CassandraEventStoreModule.MODULE), eventSerializer)
+ }
+
+ override def beforeAll(context: ExtensionContext): Unit = cassandra.beforeAll(context)
+
+ override def afterAll(context: ExtensionContext): Unit = cassandra.afterAll(context)
+
+ override def beforeEach(context: ExtensionContext): Unit = eventStoreDao =
+ Some(new EventStoreDao(cassandra.getCassandraCluster.getConf, eventSerializer))
+
+ override def afterEach(context: ExtensionContext): Unit = cassandra.afterEach(context)
+
+ @throws[ParameterResolutionException]
+ override def supportsParameter(parameterContext: ParameterContext, extensionContext: ExtensionContext): Boolean =
+ parameterContext.getParameter.getType eq classOf[EventStore]
+
+ @throws[ParameterResolutionException]
+ override def resolveParameter(parameterContext: ParameterContext, extensionContext: ExtensionContext): CassandraEventStore =
+ new CassandraEventStore(eventStoreDao.get)
+}
\ No newline at end of file
diff --git a/event-sourcing/event-store-cassandra/src/test/java/org/apache/james/eventsourcing/eventstore/cassandra/CassandraEventStoreTest.java b/event-sourcing/event-store-cassandra/src/test/scala/org/apache/james/eventsourcing/eventstore/cassandra/CassandraEventStoreExtensionForTestEvents.scala
similarity index 69%
copy from event-sourcing/event-store-cassandra/src/test/java/org/apache/james/eventsourcing/eventstore/cassandra/CassandraEventStoreTest.java
copy to event-sourcing/event-store-cassandra/src/test/scala/org/apache/james/eventsourcing/eventstore/cassandra/CassandraEventStoreExtensionForTestEvents.scala
index 316195c..9b1b7a6 100644
--- a/event-sourcing/event-store-cassandra/src/test/java/org/apache/james/eventsourcing/eventstore/cassandra/CassandraEventStoreTest.java
+++ b/event-sourcing/event-store-cassandra/src/test/scala/org/apache/james/eventsourcing/eventstore/cassandra/CassandraEventStoreExtensionForTestEvents.scala
@@ -1,4 +1,4 @@
-/****************************************************************
+ /***************************************************************
* 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 *
@@ -16,14 +16,9 @@
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/
+package org.apache.james.eventsourcing.eventstore.cassandra
-package org.apache.james.eventsourcing.eventstore.cassandra;
+import org.apache.james.eventsourcing.eventstore.cassandra.dto.TestEventDTOModules
+import org.junit.jupiter.api.extension.Extension
-import org.apache.james.eventsourcing.eventstore.EventStoreTest;
-import org.apache.james.eventsourcing.eventstore.cassandra.dto.TestEventDTOModules;
-import org.junit.jupiter.api.extension.RegisterExtension;
-
-class CassandraEventStoreTest implements EventStoreTest {
- @RegisterExtension
- static CassandraEventStoreExtension eventStoreExtension = new CassandraEventStoreExtension(JsonEventSerializer.forModules(TestEventDTOModules.TEST_TYPE()).withoutNestedType());
-}
+class CassandraEventStoreExtensionForTestEvents extends CassandraEventStoreExtension(JsonEventSerializer.forModules(TestEventDTOModules.TEST_TYPE).withoutNestedType) with Extension
diff --git a/event-sourcing/event-store-cassandra/src/test/java/org/apache/james/eventsourcing/eventstore/cassandra/CassandraEventStoreTest.java b/event-sourcing/event-store-cassandra/src/test/scala/org/apache/james/eventsourcing/eventstore/cassandra/CassandraEventStoreTest.scala
similarity index 65%
rename from event-sourcing/event-store-cassandra/src/test/java/org/apache/james/eventsourcing/eventstore/cassandra/CassandraEventStoreTest.java
rename to event-sourcing/event-store-cassandra/src/test/scala/org/apache/james/eventsourcing/eventstore/cassandra/CassandraEventStoreTest.scala
index 316195c..47c266e 100644
--- a/event-sourcing/event-store-cassandra/src/test/java/org/apache/james/eventsourcing/eventstore/cassandra/CassandraEventStoreTest.java
+++ b/event-sourcing/event-store-cassandra/src/test/scala/org/apache/james/eventsourcing/eventstore/cassandra/CassandraEventStoreTest.scala
@@ -1,4 +1,4 @@
-/****************************************************************
+ /***************************************************************
* 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 *
@@ -16,14 +16,10 @@
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/
+package org.apache.james.eventsourcing.eventstore.cassandra
-package org.apache.james.eventsourcing.eventstore.cassandra;
+import org.apache.james.eventsourcing.eventstore.EventStoreContract
+import org.junit.jupiter.api.extension.ExtendWith
-import org.apache.james.eventsourcing.eventstore.EventStoreTest;
-import org.apache.james.eventsourcing.eventstore.cassandra.dto.TestEventDTOModules;
-import org.junit.jupiter.api.extension.RegisterExtension;
-
-class CassandraEventStoreTest implements EventStoreTest {
- @RegisterExtension
- static CassandraEventStoreExtension eventStoreExtension = new CassandraEventStoreExtension(JsonEventSerializer.forModules(TestEventDTOModules.TEST_TYPE()).withoutNestedType());
-}
+@ExtendWith(Array(classOf[CassandraEventStoreExtensionForTestEvents]))
+class CassandraEventStoreTest extends EventStoreContract
\ No newline at end of file
diff --git a/event-sourcing/event-store-cassandra/src/test/scala/org/apache/james/eventsourcing/eventstore/cassandra/JsonEventSerializerTest.scala b/event-sourcing/event-store-cassandra/src/test/scala/org/apache/james/eventsourcing/eventstore/cassandra/JsonEventSerializerTest.scala
new file mode 100644
index 0000000..cde5dc4
--- /dev/null
+++ b/event-sourcing/event-store-cassandra/src/test/scala/org/apache/james/eventsourcing/eventstore/cassandra/JsonEventSerializerTest.scala
@@ -0,0 +1,95 @@
+ /***************************************************************
+ * 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.eventsourcing.eventstore.cassandra
+
+import net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson
+import org.apache.james.eventsourcing.eventstore.cassandra.dto.{OtherEvent, TestEventDTOModules}
+import org.apache.james.eventsourcing.{EventId, TestAggregateId, TestEvent}
+import org.assertj.core.api.Assertions.{assertThat, assertThatThrownBy}
+import org.junit.jupiter.api.Test
+
+object JsonEventSerializerTest {
+ val EVENT_ID: EventId = EventId.fromSerialized(0)
+ val AGGREGATE_ID: TestAggregateId = TestAggregateId(1)
+ val OTHER_EVENT: OtherEvent = OtherEvent(EVENT_ID, AGGREGATE_ID, 1)
+ val TEST_EVENT: TestEvent = TestEvent(EVENT_ID, AGGREGATE_ID, "first")
+ val TEST_EVENT_JSON: String = "{\"type\":\"TestType\",\"data\":\"first\",\"eventId\":0,\"aggregate\":1}"
+ val OTHER_EVENT_JSON: String = "{\"type\":\"other-type\",\"data\":1,\"eventId\":0,\"aggregate\":1}"
+ val MISSING_TYPE_EVENT_JSON: String = "{\"data\":1,\"eventId\":0,\"aggregate\":1}"
+ val DUPLICATE_TYPE_EVENT_JSON: String = "{\"type\":\"TestType\", \"type\":\"other-type\",\"data\":1,\"eventId\":0,\"aggregate\":1}"
+}
+
+class JsonEventSerializerTest {
+ @Test
+ def shouldDeserializeKnownEvent(): Unit =
+ assertThat(JsonEventSerializer.forModules(TestEventDTOModules.TEST_TYPE)
+ .withoutNestedType.deserialize(JsonEventSerializerTest.TEST_EVENT_JSON))
+ .isEqualTo(JsonEventSerializerTest.TEST_EVENT)
+
+ @Test
+ def shouldThrowWhenDeserializeUnknownEvent() =
+ assertThatThrownBy(() =>
+ JsonEventSerializer
+ .forModules()
+ .withoutNestedType.deserialize(JsonEventSerializerTest.TEST_EVENT_JSON))
+ .isInstanceOf(classOf[JsonEventSerializer.UnknownEventException])
+
+ @Test
+ def serializeShouldHandleAllKnownEvents(): Unit = {
+ val jsonEventSerializer = JsonEventSerializer.forModules(TestEventDTOModules.TEST_TYPE, TestEventDTOModules.OTHER_TEST_TYPE).withoutNestedType
+ assertThatJson(jsonEventSerializer.serialize(JsonEventSerializerTest.OTHER_EVENT)).isEqualTo(JsonEventSerializerTest.OTHER_EVENT_JSON)
+ assertThatJson(jsonEventSerializer.serialize(JsonEventSerializerTest.TEST_EVENT)).isEqualTo(JsonEventSerializerTest.TEST_EVENT_JSON)
+ }
+
+ @Test
+ def deserializeShouldHandleAllKnownEvents(): Unit = {
+ val jsonEventSerializer = JsonEventSerializer.forModules(TestEventDTOModules.TEST_TYPE, TestEventDTOModules.OTHER_TEST_TYPE).withoutNestedType
+ assertThatJson(jsonEventSerializer.deserialize(JsonEventSerializerTest.OTHER_EVENT_JSON)).isEqualTo(JsonEventSerializerTest.OTHER_EVENT)
+ assertThatJson(jsonEventSerializer.deserialize(JsonEventSerializerTest.TEST_EVENT_JSON)).isEqualTo(JsonEventSerializerTest.TEST_EVENT)
+ }
+
+ @Test
+ def deserializeShouldThrowWhenEventWithDuplicatedTypes(): Unit =
+ assertThatThrownBy(() =>
+ JsonEventSerializer
+ .forModules(TestEventDTOModules.TEST_TYPE, TestEventDTOModules.OTHER_TEST_TYPE)
+ .withoutNestedType.deserialize(JsonEventSerializerTest.DUPLICATE_TYPE_EVENT_JSON))
+ .isInstanceOf(classOf[JsonEventSerializer.InvalidEventException])
+
+ @Test
+ def deserializeShouldThrowWhenEventWithMissingType(): Unit =
+ assertThatThrownBy(() =>
+ JsonEventSerializer
+ .forModules(TestEventDTOModules.TEST_TYPE, TestEventDTOModules.OTHER_TEST_TYPE)
+ .withoutNestedType.deserialize(JsonEventSerializerTest.MISSING_TYPE_EVENT_JSON))
+ .isInstanceOf(classOf[JsonEventSerializer.InvalidEventException])
+
+ @Test
+ def shouldSerializeKnownEvent(): Unit =
+ assertThatJson(JsonEventSerializer.forModules(TestEventDTOModules.TEST_TYPE)
+ .withoutNestedType.serialize(JsonEventSerializerTest.TEST_EVENT))
+ .isEqualTo(JsonEventSerializerTest.TEST_EVENT_JSON)
+
+ @Test
+ def shouldThrowWhenSerializeUnknownEvent(): Unit =
+ assertThatThrownBy(() =>
+ JsonEventSerializer.forModules()
+ .withoutNestedType.serialize(JsonEventSerializerTest.TEST_EVENT))
+ .isInstanceOf(classOf[JsonEventSerializer.UnknownEventException])
+}
\ 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