Repository: james-project Updated Branches: refs/heads/master c2b85a004 -> 189490a4e
http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/EventSourcingSystem.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/EventSourcingSystem.java b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/EventSourcingSystem.java deleted file mode 100644 index 22a2788..0000000 --- a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/EventSourcingSystem.java +++ /dev/null @@ -1,36 +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.Set; - -public class EventSourcingSystem { - private final EventBus eventBus; - private final CommandDispatcher commandDispatcher; - - public EventSourcingSystem(Set<CommandDispatcher.CommandHandler<?>> handlers, Set<Subscriber> subscribers, EventStore eventStore) { - this.eventBus = new EventBus(eventStore, subscribers); - this.commandDispatcher = new CommandDispatcher(eventBus, handlers); - } - - public void dispatch(CommandDispatcher.Command c) { - commandDispatcher.dispatch(c); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/EventStore.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/EventStore.java b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/EventStore.java deleted file mode 100644 index 1ba8028..0000000 --- a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/EventStore.java +++ /dev/null @@ -1,114 +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.List; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; - -import com.github.steveash.guavate.Guavate; -import com.google.common.collect.ImmutableList; - -public interface EventStore { - - class EventStoreFailedException extends RuntimeException { - - } - - class History { - public static History empty() { - return new History(ImmutableList.of()); - } - - public static History of(List<Event> events) { - return new History(ImmutableList.copyOf(events)); - } - - public static History of(Event... events) { - return of(ImmutableList.copyOf(events)); - } - - private final List<Event> events; - - private History(List<Event> events) { - if (hasEventIdDuplicates(events)) { - throw new EventStoreFailedException(); - } - this.events = events; - } - - public boolean hasEventIdDuplicates(List<Event> events) { - Set<EventId> eventIds = events.stream() - .map(Event::eventId) - .collect(Guavate.toImmutableSet()); - - return eventIds.size() != events.size(); - } - - public Optional<EventId> getVersion() { - return events.stream() - .map(Event::eventId) - .max(Comparator.naturalOrder()); - } - - public List<Event> getEvents() { - return events; - } - - public EventId getNextEventId() { - return getVersion() - .map(EventId::next) - .orElse(EventId.first()); - } - - @Override - public final boolean equals(Object o) { - if (o instanceof History) { - History history = (History) o; - - return Objects.equals(this.events, history.events); - } - return false; - } - - @Override - public final int hashCode() { - return Objects.hash(events); - } - } - - default void append(Event event) { - appendAll(ImmutableList.of(event)); - } - - default void appendAll(Event... events) { - appendAll(ImmutableList.copyOf(events)); - } - - /** - * This method should check that no input event has an id already stored and throw otherwise - * It should also check that all events belong to the same aggregate - */ - void appendAll(List<Event> events); - - History getEventsOfAggregate(AggregateId aggregateId); -} http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/Subscriber.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/Subscriber.java b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/Subscriber.java deleted file mode 100644 index 42a804d..0000000 --- a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/Subscriber.java +++ /dev/null @@ -1,24 +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; - -public interface Subscriber { - void handle(Event event); -} http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/aggregates/UserQuotaThresholds.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/aggregates/UserQuotaThresholds.java b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/aggregates/UserQuotaThresholds.java index 8290baf..ae67d30 100644 --- a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/aggregates/UserQuotaThresholds.java +++ b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/aggregates/UserQuotaThresholds.java @@ -27,7 +27,7 @@ import java.util.stream.Collectors; import org.apache.james.core.User; import org.apache.james.eventsourcing.AggregateId; -import org.apache.james.eventsourcing.EventStore; +import org.apache.james.eventsourcing.eventstore.History; import org.apache.james.mailbox.model.Quota; import org.apache.james.mailbox.quota.QuotaCount; import org.apache.james.mailbox.quota.QuotaSize; @@ -82,15 +82,15 @@ public class UserQuotaThresholds { } } - public static UserQuotaThresholds fromEvents(Id aggregateId, EventStore.History history) { + public static UserQuotaThresholds fromEvents(Id aggregateId, History history) { return new UserQuotaThresholds(aggregateId, history); } private final Id aggregateId; - private final EventStore.History history; + private final History history; private final List<QuotaThresholdChangedEvent> events; - private UserQuotaThresholds(Id aggregateId, EventStore.History history) { + private UserQuotaThresholds(Id aggregateId, History history) { this.aggregateId = aggregateId; this.history = history; this.events = history.getEvents().stream() http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/commands/DetectThresholdCrossing.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/commands/DetectThresholdCrossing.java b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/commands/DetectThresholdCrossing.java index 0de0023..1128c0b 100644 --- a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/commands/DetectThresholdCrossing.java +++ b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/commands/DetectThresholdCrossing.java @@ -23,12 +23,12 @@ import java.time.Instant; import java.util.Objects; import org.apache.james.core.User; -import org.apache.james.eventsourcing.CommandDispatcher; +import org.apache.james.eventsourcing.Command; import org.apache.james.mailbox.model.Quota; import org.apache.james.mailbox.quota.QuotaCount; import org.apache.james.mailbox.quota.QuotaSize; -public class DetectThresholdCrossing implements CommandDispatcher.Command { +public class DetectThresholdCrossing implements Command { private final User user; private final Quota<QuotaCount> countQuota; http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/commands/DetectThresholdCrossingHandler.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/commands/DetectThresholdCrossingHandler.java b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/commands/DetectThresholdCrossingHandler.java index 39b2013..bbf480e 100644 --- a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/commands/DetectThresholdCrossingHandler.java +++ b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/mailbox/quota/mailing/commands/DetectThresholdCrossingHandler.java @@ -23,13 +23,14 @@ import java.util.List; import javax.inject.Inject; -import org.apache.james.eventsourcing.CommandDispatcher; +import org.apache.james.eventsourcing.CommandHandler; import org.apache.james.eventsourcing.Event; -import org.apache.james.eventsourcing.EventStore; +import org.apache.james.eventsourcing.eventstore.EventStore; +import org.apache.james.eventsourcing.eventstore.History; import org.apache.james.mailbox.quota.mailing.QuotaMailingListenerConfiguration; import org.apache.james.mailbox.quota.mailing.aggregates.UserQuotaThresholds; -public class DetectThresholdCrossingHandler implements CommandDispatcher.CommandHandler<DetectThresholdCrossing> { +public class DetectThresholdCrossingHandler implements CommandHandler<DetectThresholdCrossing> { private final EventStore eventStore; private final QuotaMailingListenerConfiguration quotaMailingListenerConfiguration; @@ -48,7 +49,7 @@ public class DetectThresholdCrossingHandler implements CommandDispatcher.Command private UserQuotaThresholds loadAggregate(DetectThresholdCrossing command) { UserQuotaThresholds.Id aggregateId = UserQuotaThresholds.Id.from(command.getUser()); - EventStore.History history = eventStore.getEventsOfAggregate(aggregateId); + History history = eventStore.getEventsOfAggregate(aggregateId); return UserQuotaThresholds.fromEvents(aggregateId, history); } http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/DataCollectorSubscriber.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/DataCollectorSubscriber.java b/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/DataCollectorSubscriber.java deleted file mode 100644 index 85fccd1..0000000 --- a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/DataCollectorSubscriber.java +++ /dev/null @@ -1,46 +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.ArrayList; -import java.util.List; - -import com.google.common.collect.ImmutableList; - -public class DataCollectorSubscriber implements Subscriber { - - private final List<String> data; - - public DataCollectorSubscriber() { - data = new ArrayList<>(); - } - - @Override - public void handle(Event event) { - if (event instanceof TestEvent) { - data.add(((TestEvent) event).getData()); - } - } - - - public List<String> getData() { - return ImmutableList.copyOf(data); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/EventIdTest.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/EventIdTest.java b/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/EventIdTest.java deleted file mode 100644 index ffdc5f2..0000000 --- a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/EventIdTest.java +++ /dev/null @@ -1,85 +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 static org.assertj.core.api.Assertions.assertThat; - -import org.junit.jupiter.api.Test; - -import nl.jqno.equalsverifier.EqualsVerifier; - -public class EventIdTest { - - @Test - public void shouldMatchBeanContract() { - EqualsVerifier.forClass(EventStore.History.class) - .allFieldsShouldBeUsed() - .verify(); - } - - @Test - public void firstShouldReturnAConstant() { - assertThat(EventId.first()) - .isEqualTo(EventId.first()); - } - - @Test - public void previousShouldReturnEmptyWhenBeforeFirst() { - assertThat(EventId.first().previous()) - .isEmpty(); - } - - @Test - public void compareToShouldReturnNegativeWhenComparedToNext() { - assertThat(EventId.first()) - .isLessThan(EventId.first().next()); - } - - @Test - public void compareToShouldReturnNegativeWhenComparedToPrevious() { - assertThat(EventId.first().next()) - .isGreaterThan(EventId.first()); - } - - @Test - public void nextShouldAlwaysHaveTheSameIncrement() { - assertThat(EventId.first().next()) - .isEqualTo(EventId.first().next()); - } - - @Test - public void previousShouldRevertNext() { - assertThat(EventId.first().next().previous()) - .contains(EventId.first()); - } - - @Test - public void compareToShouldReturnNegativeWhenComparedToNextWithPreviousCall() { - assertThat(EventId.first().next().previous().get()) - .isLessThan(EventId.first().next()); - } - - @Test - public void compareToShouldReturnNegativeWhenComparedToPreviousWithPreviousCall() { - assertThat(EventId.first().next()) - .isGreaterThan(EventId.first().next().previous().get()); - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/EventSourcingSystemTest.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/EventSourcingSystemTest.java b/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/EventSourcingSystemTest.java deleted file mode 100644 index 19c3925..0000000 --- a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/EventSourcingSystemTest.java +++ /dev/null @@ -1,250 +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 static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyListOf; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.util.List; - -import org.junit.jupiter.api.Test; - -import com.github.steveash.guavate.Guavate; -import com.google.common.base.Splitter; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; - -public interface EventSourcingSystemTest { - - String PAYLOAD_1 = "payload1"; - String PAYLOAD_2 = "payload2"; - TestAggregateId AGGREGATE_ID = TestAggregateId.testId(42); - - class MyCommand implements CommandDispatcher.Command { - private final String payload; - - public MyCommand(String payload) { - this.payload = payload; - } - - public String getPayload() { - return payload; - } - } - - @Test - default void dispatchShouldApplyCommandHandlerThenCallSubscribers(EventStore eventStore) { - DataCollectorSubscriber subscriber = new DataCollectorSubscriber(); - EventSourcingSystem eventSourcingSystem = new EventSourcingSystem( - ImmutableSet.of(simpleDispatcher(eventStore)), - ImmutableSet.of(subscriber), - eventStore); - - eventSourcingSystem.dispatch(new MyCommand(PAYLOAD_1)); - - assertThat(subscriber.getData()).containsExactly(PAYLOAD_1); - } - - @Test - default void throwingSubscribersShouldNotAbortSubscriberChain(EventStore eventStore) { - DataCollectorSubscriber subscriber = new DataCollectorSubscriber(); - EventSourcingSystem eventSourcingSystem = new EventSourcingSystem( - ImmutableSet.of(simpleDispatcher(eventStore)), - ImmutableSet.of( - events -> { - throw new RuntimeException(); - }, - subscriber), - eventStore); - - eventSourcingSystem.dispatch(new MyCommand(PAYLOAD_1)); - - assertThat(subscriber.getData()).containsExactly(PAYLOAD_1); - } - - @Test - default void throwingStoreShouldNotLeadToPusblishing() { - EventStore eventStore = mock(EventStore.class); - doThrow(new RuntimeException()).when(eventStore).appendAll(anyListOf(Event.class)); - when(eventStore.getEventsOfAggregate(any())).thenReturn(EventStore.History.empty()); - - DataCollectorSubscriber subscriber = new DataCollectorSubscriber(); - EventSourcingSystem eventSourcingSystem = new EventSourcingSystem( - ImmutableSet.of(simpleDispatcher(eventStore)), - ImmutableSet.of( - events -> { - throw new RuntimeException(); - }, - subscriber), - eventStore); - - assertThatThrownBy(() -> eventSourcingSystem.dispatch(new MyCommand(PAYLOAD_1))) - .isInstanceOf(RuntimeException.class); - - assertThat(subscriber.getData()).isEmpty(); - } - - @Test - default void dispatchShouldApplyCommandHandlerThenStoreGeneratedEvents(EventStore eventStore) { - DataCollectorSubscriber subscriber = new DataCollectorSubscriber(); - EventSourcingSystem eventSourcingSystem = new EventSourcingSystem( - ImmutableSet.of(simpleDispatcher(eventStore)), - ImmutableSet.of(subscriber), - eventStore); - - eventSourcingSystem.dispatch(new MyCommand(PAYLOAD_1)); - - TestEvent expectedEvent = new TestEvent(EventId.first(), AGGREGATE_ID, PAYLOAD_1); - assertThat(eventStore.getEventsOfAggregate(AGGREGATE_ID).getEvents()) - .containsOnly(expectedEvent); - } - - @Test - default void dispatchShouldCallSubscriberForSubsequentCommands(EventStore eventStore) { - DataCollectorSubscriber subscriber = new DataCollectorSubscriber(); - EventSourcingSystem eventSourcingSystem = new EventSourcingSystem( - ImmutableSet.of(simpleDispatcher(eventStore)), - ImmutableSet.of(subscriber), - eventStore); - - eventSourcingSystem.dispatch(new MyCommand(PAYLOAD_1)); - eventSourcingSystem.dispatch(new MyCommand(PAYLOAD_2)); - - assertThat(subscriber.getData()).containsExactly(PAYLOAD_1, PAYLOAD_2); - } - - @Test - default void dispatchShouldStoreEventsForSubsequentCommands(EventStore eventStore) { - DataCollectorSubscriber subscriber = new DataCollectorSubscriber(); - EventSourcingSystem eventSourcingSystem = new EventSourcingSystem( - ImmutableSet.of(simpleDispatcher(eventStore)), - ImmutableSet.of(subscriber), - eventStore); - - eventSourcingSystem.dispatch(new MyCommand(PAYLOAD_1)); - eventSourcingSystem.dispatch(new MyCommand(PAYLOAD_2)); - - TestEvent expectedEvent1 = new TestEvent(EventId.first(), AGGREGATE_ID, PAYLOAD_1); - TestEvent expectedEvent2 = new TestEvent(expectedEvent1.eventId().next(), AGGREGATE_ID, PAYLOAD_2); - assertThat(eventStore.getEventsOfAggregate(AGGREGATE_ID).getEvents()) - .containsOnly(expectedEvent1, expectedEvent2); - } - - @Test - default void dispatcherShouldBeAbleToReturnSeveralEvents(EventStore eventStore) { - DataCollectorSubscriber subscriber = new DataCollectorSubscriber(); - EventSourcingSystem eventSourcingSystem = new EventSourcingSystem( - ImmutableSet.of(wordCuttingDispatcher(eventStore)), - ImmutableSet.of(subscriber), - eventStore); - - eventSourcingSystem.dispatch(new MyCommand("This is a test")); - - assertThat(subscriber.getData()).containsExactly("This", "is", "a", "test"); - } - - @Test - default void unknownCommandsShouldBeIgnored(EventStore eventStore) { - DataCollectorSubscriber subscriber = new DataCollectorSubscriber(); - EventSourcingSystem eventSourcingSystem = new EventSourcingSystem( - ImmutableSet.of(wordCuttingDispatcher(eventStore)), - ImmutableSet.of(subscriber), - eventStore); - - assertThatThrownBy(() -> eventSourcingSystem.dispatch(new CommandDispatcher.Command() {})) - .isInstanceOf(CommandDispatcher.UnknownCommandException.class); - } - - @Test - default void constructorShouldThrowWhenSeveralHandlersForTheSameCommand(EventStore eventStore) { - DataCollectorSubscriber subscriber = new DataCollectorSubscriber(); - - assertThatThrownBy(() -> - new EventSourcingSystem( - ImmutableSet.of(wordCuttingDispatcher(eventStore), - simpleDispatcher(eventStore)), - ImmutableSet.of(subscriber), - eventStore)) - .isInstanceOf(IllegalArgumentException.class); - } - - default CommandDispatcher.CommandHandler<MyCommand> simpleDispatcher(EventStore eventStore) { - return new CommandDispatcher.CommandHandler<MyCommand>() { - @Override - public Class<MyCommand> handledClass() { - return MyCommand.class; - } - - @Override - public List<? extends Event> handle(MyCommand myCommand) { - EventStore.History history = eventStore.getEventsOfAggregate(AGGREGATE_ID); - - return ImmutableList.of(new TestEvent( - history.getNextEventId(), - AGGREGATE_ID, - myCommand.getPayload())); - } - }; - } - - default CommandDispatcher.CommandHandler<MyCommand> wordCuttingDispatcher(EventStore eventStore) { - return new CommandDispatcher.CommandHandler<MyCommand>() { - @Override - public Class<MyCommand> handledClass() { - return MyCommand.class; - } - - @Override - public List<? extends Event> handle(MyCommand myCommand) { - EventStore.History history = eventStore.getEventsOfAggregate(AGGREGATE_ID); - - EventIdIncrementer eventIdIncrementer = new EventIdIncrementer(history.getNextEventId()); - - return Splitter.on(" ") - .splitToList(myCommand.getPayload()) - .stream() - .map(word -> new TestEvent( - eventIdIncrementer.next(), - AGGREGATE_ID, - word)) - .collect(Guavate.toImmutableList()); - } - }; - } - - class EventIdIncrementer { - private EventId currentEventId; - - public EventIdIncrementer(EventId base) { - this.currentEventId = base; - } - - public EventId next() { - currentEventId = currentEventId.next(); - return currentEventId; - } - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/EventStoreTest.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/EventStoreTest.java b/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/EventStoreTest.java deleted file mode 100644 index aad8daa..0000000 --- a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/EventStoreTest.java +++ /dev/null @@ -1,75 +0,0 @@ -package org.apache.james.eventsourcing; - -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; - -import org.junit.jupiter.api.Test; - -import com.google.common.collect.ImmutableList; - -import nl.jqno.equalsverifier.EqualsVerifier; - -public interface EventStoreTest { - - TestAggregateId AGGREGATE_1 = testId(1); - TestAggregateId AGGREGATE_2 = testId(2); - - @Test - default void historyShouldMatchBeanContract() { - EqualsVerifier.forClass(EventStore.History.class) - .allFieldsShouldBeUsed() - .verify(); - } - - @Test - default void getEventsOfAggregateShouldThrowOnNullAggregateId(EventStore testee) { - assertThatThrownBy(() -> testee.getEventsOfAggregate(null)) - .isInstanceOf(NullPointerException.class); - } - - @Test - default void appendShouldThrowWhenEventFromSeveralAggregates(EventStore testee) { - TestEvent event1 = new TestEvent(EventId.first(), AGGREGATE_1, "first"); - TestEvent event2 = new TestEvent(event1.eventId().next(), AGGREGATE_2, "second"); - assertThatThrownBy(() -> testee.appendAll(event1, event2)).isInstanceOf(IllegalArgumentException.class); - } - - @Test - default void appendShouldDoNothingOnEmptyEventList(EventStore testee) { - assertThatCode(testee::appendAll).doesNotThrowAnyException(); - } - - @Test - default void appendShouldThrowWhenTryingToRewriteHistory(EventStore testee) { - TestEvent event1 = new TestEvent(EventId.first(), AGGREGATE_1, "first"); - testee.append(event1); - TestEvent event2 = new TestEvent(EventId.first(), AGGREGATE_1, "second"); - assertThatThrownBy(() -> testee.append(event2)).isInstanceOf(EventStore.EventStoreFailedException.class); - } - - @Test - default void getEventsOfAggregateShouldReturnEmptyHistoryWhenUnknown(EventStore testee) { - assertThat(testee.getEventsOfAggregate(AGGREGATE_1)).isEqualTo(EventStore.History.empty()); - } - - @Test - default void getEventsOfAggregateShouldReturnAppendedEvent(EventStore testee) { - TestEvent event = new TestEvent(EventId.first(), AGGREGATE_1, "first"); - testee.append(event); - assertThat(testee.getEventsOfAggregate(AGGREGATE_1)) - .isEqualTo(EventStore.History.of(ImmutableList.of(event))); - } - - @Test - default void getEventsOfAggregateShouldReturnAppendedEvents(EventStore testee) { - TestEvent event1 = new TestEvent(EventId.first(), AGGREGATE_1, "first"); - TestEvent event2 = new TestEvent(event1.eventId().next(), AGGREGATE_1, "second"); - testee.append(event1); - testee.append(event2); - assertThat(testee.getEventsOfAggregate(AGGREGATE_1)) - .isEqualTo(EventStore.History.of(ImmutableList.of(event1, event2))); - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/HistoryTest.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/HistoryTest.java b/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/HistoryTest.java deleted file mode 100644 index 9419087..0000000 --- a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/HistoryTest.java +++ /dev/null @@ -1,87 +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 static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -import org.junit.jupiter.api.Test; - -import nl.jqno.equalsverifier.EqualsVerifier; - -public class HistoryTest { - - @Test - public void shouldMatchBeanContract() { - EqualsVerifier.forClass(EventStore.History.class) - .verify(); - } - - @Test - public void emptyShouldGenerateAnEmptyHistory() { - assertThat(EventStore.History.empty()) - .isEqualTo(EventStore.History.of()); - } - - @Test - public void getVersionShouldReturnEmptyWhenEmpty() { - assertThat(EventStore.History.empty() - .getVersion()) - .isEmpty(); - } - - @Test - public void getVersionShouldReturnSingleEventIdWhenSingleEvent() { - assertThat(EventStore.History - .of(new TestEvent(EventId.first(), - TestAggregateId.testId(42), - "any")) - .getVersion()) - .contains(EventId.first()); - } - - @Test - public void getVersionShouldReturnHighestEventId() { - TestEvent event1 = new TestEvent(EventId.first(), - TestAggregateId.testId(42), - "any"); - TestEvent event2 = new TestEvent(event1.eventId().next(), - TestAggregateId.testId(42), - "any"); - - assertThat(EventStore.History.of(event1, event2) - .getVersion()) - .contains(event2.eventId()); - } - - @Test - public void duplicateHistoryShouldThrow() { - TestEvent event1 = new TestEvent(EventId.first(), - TestAggregateId.testId(42), - "any"); - TestEvent event2 = new TestEvent(EventId.first(), - TestAggregateId.testId(42), - "any"); - - assertThatThrownBy(() -> EventStore.History.of(event1, event2)) - .isInstanceOf(EventStore.EventStoreFailedException.class); - } - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/TestAggregateId.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/TestAggregateId.java b/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/TestAggregateId.java deleted file mode 100644 index b3ae78c..0000000 --- a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/TestAggregateId.java +++ /dev/null @@ -1,68 +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; - -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(); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/TestEvent.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/TestEvent.java b/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/TestEvent.java deleted file mode 100644 index c46f804..0000000 --- a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/eventsourcing/TestEvent.java +++ /dev/null @@ -1,82 +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; - -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(); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/listeners/QuotaThresholdConfigurationChangesTest.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/listeners/QuotaThresholdConfigurationChangesTest.java b/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/listeners/QuotaThresholdConfigurationChangesTest.java index fe57929..166d86c 100644 --- a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/listeners/QuotaThresholdConfigurationChangesTest.java +++ b/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/listeners/QuotaThresholdConfigurationChangesTest.java @@ -28,7 +28,7 @@ import static org.apache.james.mailbox.quota.model.QuotaThresholdFixture._75; import static org.apache.james.mailbox.quota.model.QuotaThresholdFixture.mailetContext; import static org.assertj.core.api.Assertions.assertThat; -import org.apache.james.eventsourcing.EventStore; +import org.apache.james.eventsourcing.eventstore.EventStore; import org.apache.james.mailbox.MailboxListener.QuotaUsageUpdatedEvent; import org.apache.james.mailbox.quota.mailing.QuotaMailingListenerConfiguration; import org.apache.james.mailbox.quota.model.QuotaThresholdFixture.Quotas.Counts; http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/listeners/QuotaThresholdListenersTestSystem.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/listeners/QuotaThresholdListenersTestSystem.java b/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/listeners/QuotaThresholdListenersTestSystem.java index bb1dce8..0b279dd 100644 --- a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/listeners/QuotaThresholdListenersTestSystem.java +++ b/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/listeners/QuotaThresholdListenersTestSystem.java @@ -20,7 +20,7 @@ package org.apache.james.mailbox.quota.mailing.listeners; import org.apache.james.eventsourcing.EventSourcingSystem; -import org.apache.james.eventsourcing.EventStore; +import org.apache.james.eventsourcing.eventstore.EventStore; import org.apache.james.filesystem.api.FileSystem; import org.apache.james.mailbox.Event; import org.apache.james.mailbox.exception.MailboxException; http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/listeners/QuotaThresholdMailingIntegrationTest.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/listeners/QuotaThresholdMailingIntegrationTest.java b/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/listeners/QuotaThresholdMailingIntegrationTest.java index 604d891..362246f 100644 --- a/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/listeners/QuotaThresholdMailingIntegrationTest.java +++ b/mailbox/plugin/quota-mailing/src/test/java/org/apache/james/mailbox/quota/mailing/listeners/QuotaThresholdMailingIntegrationTest.java @@ -37,7 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat; import java.util.concurrent.TimeUnit; -import org.apache.james.eventsourcing.EventStore; +import org.apache.james.eventsourcing.eventstore.EventStore; import org.apache.james.mailbox.MailboxListener.QuotaUsageUpdatedEvent; import org.apache.james.mailbox.quota.mailing.QuotaMailingListenerConfiguration; import org.apache.james.mailbox.quota.model.QuotaThresholdFixture.Quotas.Counts; http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index 4a41df0..16beb29 100644 --- a/pom.xml +++ b/pom.xml @@ -526,6 +526,7 @@ <modules> <module>backends-common</module> <module>core</module> + <module>event-sourcing</module> <module>javax-mail-extension</module> <module>mailbox</module> <module>mailet</module> @@ -1001,6 +1002,66 @@ <artifactId>blob-cassandra</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>event-sourcing-core</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>event-sourcing-core</artifactId> + <version>${project.version}</version> + <type>test-jar</type> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>event-sourcing-event-store-api</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>event-sourcing-event-store-api</artifactId> + <version>${project.version}</version> + <type>test-jar</type> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>event-sourcing-event-store-cassandra</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>event-sourcing-event-store-cassandra</artifactId> + <version>${project.version}</version> + <type>test-jar</type> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>event-sourcing-event-store-memory</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>event-sourcing-event-store-memory</artifactId> + <version>${project.version}</version> + <type>test-jar</type> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>event-sourcing-memory</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>event-sourcing-pojo</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>event-sourcing-pojo</artifactId> + <version>${project.version}</version> + <type>test-jar</type> + </dependency> <dependency> <groupId>${project.groupId}</groupId> <artifactId>james-server-cassandra-guice</artifactId> --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org