MAILBOX-332 Demonstrate JsonEventSerializer polymorphism
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/c76a4ec9 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/c76a4ec9 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/c76a4ec9 Branch: refs/heads/master Commit: c76a4ec93a95c9cb24c57abae1d59b42103ad711 Parents: c3a1bda Author: benwa <btell...@linagora.com> Authored: Wed May 9 16:27:29 2018 +0700 Committer: benwa <btell...@linagora.com> Committed: Thu May 10 09:15:41 2018 +0700 ---------------------------------------------------------------------- .../cassandra/JsonEventSerializerTest.java | 43 ++++++++++-- .../eventsourcing/cassandra/dto/OtherEvent.java | 50 ++++++++++++++ .../cassandra/dto/OtherTestEventDTO.java | 72 ++++++++++++++++++++ .../cassandra/dto/OtherTestEventDTOModule.java | 55 +++++++++++++++ 4 files changed, 216 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/c76a4ec9/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/JsonEventSerializerTest.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/JsonEventSerializerTest.java b/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/JsonEventSerializerTest.java index 893647b..89770bb 100644 --- a/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/JsonEventSerializerTest.java +++ b/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/JsonEventSerializerTest.java @@ -26,15 +26,20 @@ 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.cassandra.dto.OtherEvent; +import org.apache.james.eventsourcing.cassandra.dto.OtherTestEventDTOModule; import org.apache.james.eventsourcing.cassandra.dto.TestEventDTOModule; import org.junit.jupiter.api.Test; class JsonEventSerializerTest { + public static final EventId EVENT_ID = EventId.fromSerialized(0); + public static final TestAggregateId AGGREGATE_ID = TestAggregateId.testId(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 TestEvent TEST_EVENT = new TestEvent( - EventId.fromSerialized(0), - TestAggregateId.testId(1), - "first"); + public static final String OTHER_EVENT_JSON = "{\"type\":\"other-type\",\"data\":1,\"eventId\":0,\"aggregate\":1}"; @Test void shouldDeserializeKnownEvent() throws Exception { @@ -51,6 +56,36 @@ class JsonEventSerializerTest { } @Test + void serializeShouldHandleAllKnownEvents() throws Exception { + JsonEventSerializer jsonEventSerializer = new JsonEventSerializer( + new TestEventDTOModule(), + new OtherTestEventDTOModule()); + + 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 = new JsonEventSerializer( + new TestEventDTOModule(), + new OtherTestEventDTOModule()); + + assertThatJson( + jsonEventSerializer.deserialize(OTHER_EVENT_JSON)) + .isEqualTo(OTHER_EVENT); + + assertThatJson( + jsonEventSerializer.deserialize(TEST_EVENT_JSON)) + .isEqualTo(TEST_EVENT); + } + + @Test void shouldSerializeKnownEvent() throws Exception { assertThatJson(new JsonEventSerializer(new TestEventDTOModule()) .serialize(TEST_EVENT)) http://git-wip-us.apache.org/repos/asf/james-project/blob/c76a4ec9/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/dto/OtherEvent.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/dto/OtherEvent.java b/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/dto/OtherEvent.java new file mode 100644 index 0000000..4e74720 --- /dev/null +++ b/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/dto/OtherEvent.java @@ -0,0 +1,50 @@ +/**************************************************************** + * 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.cassandra.dto; + +import org.apache.james.eventsourcing.Event; +import org.apache.james.eventsourcing.EventId; +import org.apache.james.eventsourcing.TestAggregateId; + +public class OtherEvent implements Event { + private final EventId eventId; + private final TestAggregateId aggregateId; + private final long payload; + + public OtherEvent(EventId eventId, TestAggregateId aggregateId, long payload) { + this.eventId = eventId; + this.aggregateId = aggregateId; + this.payload = payload; + } + + @Override + public EventId eventId() { + return eventId; + } + + @Override + public TestAggregateId getAggregateId() { + return aggregateId; + } + + public long getPayload() { + return payload; + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/c76a4ec9/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/dto/OtherTestEventDTO.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/dto/OtherTestEventDTO.java b/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/dto/OtherTestEventDTO.java new file mode 100644 index 0000000..4f35c12 --- /dev/null +++ b/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/dto/OtherTestEventDTO.java @@ -0,0 +1,72 @@ +/**************************************************************** + * 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.cassandra.dto; + +import org.apache.james.eventsourcing.Event; +import org.apache.james.eventsourcing.EventId; +import org.apache.james.eventsourcing.TestAggregateId; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; + +public class OtherTestEventDTO implements EventDTO { + private final String type; + private final long data; + private final int eventId; + private final int aggregate; + + @JsonCreator + public OtherTestEventDTO( + @JsonProperty("type") String type, + @JsonProperty("data") long data, + @JsonProperty("eventId") int eventId, + @JsonProperty("aggregate") int aggregate) { + this.type = type; + this.data = data; + this.eventId = eventId; + this.aggregate = aggregate; + } + + public String getType() { + return type; + } + + public long getData() { + return data; + } + + public long getEventId() { + return eventId; + } + + public int getAggregate() { + return aggregate; + } + + @JsonIgnore + @Override + public Event toEvent() { + return new OtherEvent( + EventId.fromSerialized(eventId), + TestAggregateId.testId(aggregate), + data); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/c76a4ec9/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/dto/OtherTestEventDTOModule.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/dto/OtherTestEventDTOModule.java b/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/dto/OtherTestEventDTOModule.java new file mode 100644 index 0000000..deb9a02 --- /dev/null +++ b/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/dto/OtherTestEventDTOModule.java @@ -0,0 +1,55 @@ +/**************************************************************** + * 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.cassandra.dto; + +import org.apache.james.eventsourcing.Event; +import org.testcontainers.shaded.com.google.common.base.Preconditions; + +public class OtherTestEventDTOModule implements EventDTOModule { + + public static final String OTHER_TYPE = "other-type"; + + @Override + public String getType() { + return OTHER_TYPE; + } + + @Override + public Class<? extends EventDTO> getDTOClass() { + return OtherTestEventDTO.class; + } + + @Override + public Class<? extends Event> getEventClass() { + return OtherEvent.class; + } + + @Override + public EventDTO toDTO(Event event) { + Preconditions.checkArgument(event instanceof OtherEvent); + OtherEvent otherEvent = (OtherEvent) event; + + return new OtherTestEventDTO( + OTHER_TYPE, + otherEvent.getPayload(), + otherEvent.eventId().serialize(), + otherEvent.getAggregateId().getId()); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org