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 edec474e148cd8a7a4f1c0287328a7d8451d799c Author: Rémi KOWALSKI <rkowal...@linagora.com> AuthorDate: Tue Dec 10 15:03:01 2019 +0100 JAMES-3009 make code in event sourcing pojo more idiomatic to scala --- .../eventsourcing/EventSourcingSystemTest.java | 2 +- .../org/apache/james/eventsourcing/Command.scala | 2 +- .../org/apache/james/eventsourcing/Event.scala | 2 +- .../org/apache/james/eventsourcing/EventId.scala | 19 +++-- .../james/eventsourcing/TestAggregateId.java | 69 ------------------ .../org/apache/james/eventsourcing/TestEvent.java | 84 ---------------------- .../james/eventsourcing/TestAggregateId.scala} | 6 +- .../apache/james/eventsourcing/TestEvent.scala} | 12 +++- .../eventsourcing/eventstore/EventStoreTest.java | 5 +- .../eventsourcing/eventstore/HistoryTest.java | 10 +-- .../cassandra/JsonEventSerializerTest.java | 2 +- .../cassandra/dto/OtherTestEventDTO.scala | 9 +-- .../eventstore/cassandra/dto/TestEventDTO.scala | 2 +- 13 files changed, 42 insertions(+), 182 deletions(-) diff --git a/event-sourcing/event-sourcing-core/src/test/java/org/apache/james/eventsourcing/EventSourcingSystemTest.java b/event-sourcing/event-sourcing-core/src/test/java/org/apache/james/eventsourcing/EventSourcingSystemTest.java index c247186..5e431d6 100644 --- a/event-sourcing/event-sourcing-core/src/test/java/org/apache/james/eventsourcing/EventSourcingSystemTest.java +++ b/event-sourcing/event-sourcing-core/src/test/java/org/apache/james/eventsourcing/EventSourcingSystemTest.java @@ -43,7 +43,7 @@ public interface EventSourcingSystemTest { String PAYLOAD_1 = "payload1"; String PAYLOAD_2 = "payload2"; - TestAggregateId AGGREGATE_ID = TestAggregateId.testId(42); + TestAggregateId AGGREGATE_ID = TestAggregateId.apply(42); class MyCommand implements Command { private final String payload; diff --git a/event-sourcing/event-sourcing-pojo/src/main/scala/org/apache/james/eventsourcing/Command.scala b/event-sourcing/event-sourcing-pojo/src/main/scala/org/apache/james/eventsourcing/Command.scala index 4976e1a..209dbc5 100644 --- a/event-sourcing/event-sourcing-pojo/src/main/scala/org/apache/james/eventsourcing/Command.scala +++ b/event-sourcing/event-sourcing-pojo/src/main/scala/org/apache/james/eventsourcing/Command.scala @@ -18,4 +18,4 @@ * ***************************************************************/ package org.apache.james.eventsourcing -trait Command {} \ No newline at end of file +trait Command \ No newline at end of file diff --git a/event-sourcing/event-sourcing-pojo/src/main/scala/org/apache/james/eventsourcing/Event.scala b/event-sourcing/event-sourcing-pojo/src/main/scala/org/apache/james/eventsourcing/Event.scala index c530413..79b80bc 100644 --- a/event-sourcing/event-sourcing-pojo/src/main/scala/org/apache/james/eventsourcing/Event.scala +++ b/event-sourcing/event-sourcing-pojo/src/main/scala/org/apache/james/eventsourcing/Event.scala @@ -19,7 +19,7 @@ package org.apache.james.eventsourcing object Event { - def belongsToSameAggregate(events: List[_ <: Event]) = events + def belongsToSameAggregate(events: List[_ <: Event]): Boolean = events .view .map(event => event.getAggregateId) .distinct diff --git a/event-sourcing/event-sourcing-pojo/src/main/scala/org/apache/james/eventsourcing/EventId.scala b/event-sourcing/event-sourcing-pojo/src/main/scala/org/apache/james/eventsourcing/EventId.scala index 077aae1..9b41870 100644 --- a/event-sourcing/event-sourcing-pojo/src/main/scala/org/apache/james/eventsourcing/EventId.scala +++ b/event-sourcing/event-sourcing-pojo/src/main/scala/org/apache/james/eventsourcing/EventId.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 * @@ -7,7 +7,7 @@ * "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 * + * 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 * @@ -15,26 +15,25 @@ * KIND, either express or implied. See the License for the * * specific language governing permissions and limitations * * under the License. * - * ***************************************************************/ + ****************************************************************/ package org.apache.james.eventsourcing import com.google.common.base.Preconditions object EventId { - def fromSerialized(value: Int) = new EventId(value) + def fromSerialized(value: Int): EventId = { + Preconditions.checkArgument(value >= 0, "EventId can not be negative".asInstanceOf[Object]) + new EventId(value) + } - def first = new EventId(0) + def first: EventId = new EventId(0) } final case class EventId private(value: Int) extends Comparable[EventId] { - Preconditions.checkArgument(value >= 0, "EventId can not be negative".asInstanceOf[Object]) def next = new EventId(value + 1) - def previous: Option[EventId] = { - if (value > 0) return Some(new EventId(value - 1)) - None - } + def previous: Option[EventId] = Some(value).filter(_ > 0).map(value => EventId(value - 1)) override def compareTo(o: EventId): Int = value.compareTo(o.value) diff --git a/event-sourcing/event-sourcing-pojo/src/test/java/org/apache/james/eventsourcing/TestAggregateId.java b/event-sourcing/event-sourcing-pojo/src/test/java/org/apache/james/eventsourcing/TestAggregateId.java deleted file mode 100644 index 2c53628..0000000 --- a/event-sourcing/event-sourcing-pojo/src/test/java/org/apache/james/eventsourcing/TestAggregateId.java +++ /dev/null @@ -1,69 +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; - -import java.util.Objects; - -import com.google.common.base.MoreObjects; -import org.apache.james.eventsourcing.AggregateId; - -public class TestAggregateId implements AggregateId { - - public static TestAggregateId testId(int id) { - return new TestAggregateId(id); - } - - private final int id; - - private TestAggregateId(int id) { - this.id = id; - } - - @Override - public String asAggregateKey() { - return "TestAggregateId-" + id; - } - - public int getId() { - return id; - } - - @Override - public final boolean equals(Object o) { - if (o instanceof TestAggregateId) { - TestAggregateId that = (TestAggregateId) o; - - return Objects.equals(this.id, that.id); - } - return false; - } - - @Override - public int hashCode() { - return Objects.hash(id); - } - - @Override - public String toString() { - return MoreObjects.toStringHelper(this) - .add("id", id) - .toString(); - } -} diff --git a/event-sourcing/event-sourcing-pojo/src/test/java/org/apache/james/eventsourcing/TestEvent.java b/event-sourcing/event-sourcing-pojo/src/test/java/org/apache/james/eventsourcing/TestEvent.java deleted file mode 100644 index c65b96d..0000000 --- a/event-sourcing/event-sourcing-pojo/src/test/java/org/apache/james/eventsourcing/TestEvent.java +++ /dev/null @@ -1,84 +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; - -import java.util.Comparator; -import java.util.Objects; - -import com.google.common.base.MoreObjects; -import org.apache.james.eventsourcing.Event; -import org.apache.james.eventsourcing.EventId; - -public class TestEvent implements Event { - private final EventId id; - private final TestAggregateId aggregateId; - private final String data; - - public TestEvent(EventId id, TestAggregateId aggregateId, String data) { - this.id = id; - this.aggregateId = aggregateId; - this.data = data; - } - - @Override - public EventId eventId() { - return id; - } - - @Override - public TestAggregateId getAggregateId() { - return aggregateId; - } - - public String getData() { - return data; - } - - @Override - public int compareTo(Event o) { - return Comparator.<EventId>naturalOrder().compare(id, o.eventId()); - } - - @Override - public final boolean equals(Object o) { - if (o instanceof TestEvent) { - TestEvent testEvent = (TestEvent) o; - - return Objects.equals(this.id, testEvent.id) - && Objects.equals(this.aggregateId, testEvent.aggregateId) - && Objects.equals(this.data, testEvent.data); - } - return false; - } - - @Override - public int hashCode() { - return Objects.hash(id, aggregateId, data); - } - - @Override - public String toString() { - return MoreObjects.toStringHelper(this) - .add("id", id) - .add("aggregateId", aggregateId) - .add("data", data) - .toString(); - } -} diff --git a/event-sourcing/event-sourcing-pojo/src/main/scala/org/apache/james/eventsourcing/Command.scala b/event-sourcing/event-sourcing-pojo/src/test/scala/org/apache/james/eventsourcing/TestAggregateId.scala similarity index 88% copy from event-sourcing/event-sourcing-pojo/src/main/scala/org/apache/james/eventsourcing/Command.scala copy to event-sourcing/event-sourcing-pojo/src/test/scala/org/apache/james/eventsourcing/TestAggregateId.scala index 4976e1a..85015be 100644 --- a/event-sourcing/event-sourcing-pojo/src/main/scala/org/apache/james/eventsourcing/Command.scala +++ b/event-sourcing/event-sourcing-pojo/src/test/scala/org/apache/james/eventsourcing/TestAggregateId.scala @@ -18,4 +18,8 @@ * ***************************************************************/ package org.apache.james.eventsourcing -trait Command {} \ No newline at end of file +final case class TestAggregateId(id: Int) extends AggregateId { + override def asAggregateKey: String = "TestAggregateId-" + id + + def getId: Int = id +} diff --git a/event-sourcing/event-sourcing-pojo/src/main/scala/org/apache/james/eventsourcing/Command.scala b/event-sourcing/event-sourcing-pojo/src/test/scala/org/apache/james/eventsourcing/TestEvent.scala similarity index 77% copy from event-sourcing/event-sourcing-pojo/src/main/scala/org/apache/james/eventsourcing/Command.scala copy to event-sourcing/event-sourcing-pojo/src/test/scala/org/apache/james/eventsourcing/TestEvent.scala index 4976e1a..ff4fd0a 100644 --- a/event-sourcing/event-sourcing-pojo/src/main/scala/org/apache/james/eventsourcing/Command.scala +++ b/event-sourcing/event-sourcing-pojo/src/test/scala/org/apache/james/eventsourcing/TestEvent.scala @@ -18,4 +18,14 @@ * ***************************************************************/ package org.apache.james.eventsourcing -trait Command {} \ No newline at end of file +import java.util.Comparator + +final case class TestEvent(id: EventId, aggregateId: TestAggregateId, data: String) extends Event { + override def eventId: EventId = id + + override def getAggregateId: TestAggregateId = aggregateId + + def getData: String = data + + override def compareTo(o: Event): Int = Comparator.naturalOrder[EventId].compare(id, o.eventId) +} \ No newline at end of file diff --git a/event-sourcing/event-store-api/src/test/java/org/apache/james/eventsourcing/eventstore/EventStoreTest.java b/event-sourcing/event-store-api/src/test/java/org/apache/james/eventsourcing/eventstore/EventStoreTest.java index 4638a44..b4b37ba 100644 --- a/event-sourcing/event-store-api/src/test/java/org/apache/james/eventsourcing/eventstore/EventStoreTest.java +++ b/event-sourcing/event-store-api/src/test/java/org/apache/james/eventsourcing/eventstore/EventStoreTest.java @@ -19,7 +19,6 @@ package org.apache.james.eventsourcing.eventstore; -import static org.apache.james.eventsourcing.TestAggregateId.testId; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -31,8 +30,8 @@ import org.junit.jupiter.api.Test; public interface EventStoreTest { - TestAggregateId AGGREGATE_1 = testId(1); - TestAggregateId AGGREGATE_2 = testId(2); + TestAggregateId AGGREGATE_1 = TestAggregateId.apply(1); + TestAggregateId AGGREGATE_2 = TestAggregateId.apply(2); @Test default void getEventsOfAggregateShouldThrowOnNullAggregateId(EventStore testee) { diff --git a/event-sourcing/event-store-api/src/test/java/org/apache/james/eventsourcing/eventstore/HistoryTest.java b/event-sourcing/event-store-api/src/test/java/org/apache/james/eventsourcing/eventstore/HistoryTest.java index 98558c7..adec969 100644 --- a/event-sourcing/event-store-api/src/test/java/org/apache/james/eventsourcing/eventstore/HistoryTest.java +++ b/event-sourcing/event-store-api/src/test/java/org/apache/james/eventsourcing/eventstore/HistoryTest.java @@ -48,7 +48,7 @@ class HistoryTest { void getVersionShouldReturnSingleEventIdWhenSingleEvent() { assertThat(OptionConverters.toJava(History .of(new TestEvent(EventId.first(), - TestAggregateId.testId(42), + TestAggregateId.apply(42), "any")) .getVersion())) .contains(EventId.first()); @@ -57,10 +57,10 @@ class HistoryTest { @Test void getVersionShouldReturnHighestEventId() { TestEvent event1 = new TestEvent(EventId.first(), - TestAggregateId.testId(42), + TestAggregateId.apply(42), "any"); TestEvent event2 = new TestEvent(event1.eventId().next(), - TestAggregateId.testId(42), + TestAggregateId.apply(42), "any"); assertThat(OptionConverters.toJava(History.of(event1, event2) @@ -71,10 +71,10 @@ class HistoryTest { @Test void duplicateHistoryShouldThrow() { TestEvent event1 = new TestEvent(EventId.first(), - TestAggregateId.testId(42), + TestAggregateId.apply(42), "any"); TestEvent event2 = new TestEvent(EventId.first(), - TestAggregateId.testId(42), + TestAggregateId.apply(42), "any"); assertThatThrownBy(() -> History.of(event1, event2)) 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 index 45b7b61..aaad89e 100644 --- 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 @@ -32,7 +32,7 @@ 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 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"); diff --git a/event-sourcing/event-store-cassandra/src/test/scala/org/apache/james/eventsourcing/eventstore/cassandra/dto/OtherTestEventDTO.scala b/event-sourcing/event-store-cassandra/src/test/scala/org/apache/james/eventsourcing/eventstore/cassandra/dto/OtherTestEventDTO.scala index 9515670..59ce723 100644 --- a/event-sourcing/event-store-cassandra/src/test/scala/org/apache/james/eventsourcing/eventstore/cassandra/dto/OtherTestEventDTO.scala +++ b/event-sourcing/event-store-cassandra/src/test/scala/org/apache/james/eventsourcing/eventstore/cassandra/dto/OtherTestEventDTO.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 * @@ -7,7 +7,7 @@ * "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 * + * 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 * @@ -15,7 +15,7 @@ * 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.dto import com.fasterxml.jackson.annotation.{JsonCreator, JsonIgnore, JsonProperty} @@ -32,6 +32,7 @@ case class OtherTestEventDTO @JsonCreator()( def getAggregate: Int = aggregate - @JsonIgnore def toEvent: OtherEvent = OtherEvent(EventId.fromSerialized(eventId), TestAggregateId.testId(aggregate), data) + @JsonIgnore + def toEvent: OtherEvent = OtherEvent(EventId.fromSerialized(eventId), TestAggregateId(aggregate), data) } \ No newline at end of file diff --git a/event-sourcing/event-store-cassandra/src/test/scala/org/apache/james/eventsourcing/eventstore/cassandra/dto/TestEventDTO.scala b/event-sourcing/event-store-cassandra/src/test/scala/org/apache/james/eventsourcing/eventstore/cassandra/dto/TestEventDTO.scala index 761f68a..c3fa627 100644 --- a/event-sourcing/event-store-cassandra/src/test/scala/org/apache/james/eventsourcing/eventstore/cassandra/dto/TestEventDTO.scala +++ b/event-sourcing/event-store-cassandra/src/test/scala/org/apache/james/eventsourcing/eventstore/cassandra/dto/TestEventDTO.scala @@ -34,5 +34,5 @@ final case class TestEventDTO @JsonCreator() ( @JsonProperty("type") `type`: Str def getAggregate: Int = aggregate - @JsonIgnore def toEvent: TestEvent = new TestEvent(EventId.fromSerialized (eventId), TestAggregateId.testId (aggregate), data) + @JsonIgnore def toEvent: TestEvent = new TestEvent(EventId.fromSerialized (eventId), TestAggregateId(aggregate), data) } \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org