http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing-cassandra/src/main/java/org/apache/james/eventsourcing/cassandra/EventStoreDao.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing-cassandra/src/main/java/org/apache/james/eventsourcing/cassandra/EventStoreDao.java b/mailbox/plugin/quota-mailing-cassandra/src/main/java/org/apache/james/eventsourcing/cassandra/EventStoreDao.java deleted file mode 100644 index 2518f02..0000000 --- a/mailbox/plugin/quota-mailing-cassandra/src/main/java/org/apache/james/eventsourcing/cassandra/EventStoreDao.java +++ /dev/null @@ -1,122 +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.cassandra; - -import static com.datastax.driver.core.querybuilder.QueryBuilder.bindMarker; -import static com.datastax.driver.core.querybuilder.QueryBuilder.eq; -import static com.datastax.driver.core.querybuilder.QueryBuilder.insertInto; -import static com.datastax.driver.core.querybuilder.QueryBuilder.select; -import static org.apache.james.eventsourcing.cassandra.CassandraEventStoreTable.AGGREGATE_ID; -import static org.apache.james.eventsourcing.cassandra.CassandraEventStoreTable.EVENT; -import static org.apache.james.eventsourcing.cassandra.CassandraEventStoreTable.EVENTS_TABLE; -import static org.apache.james.eventsourcing.cassandra.CassandraEventStoreTable.EVENT_ID; - -import java.io.IOException; -import java.util.List; -import java.util.concurrent.CompletableFuture; - -import javax.inject.Inject; - -import org.apache.james.backends.cassandra.utils.CassandraAsyncExecutor; -import org.apache.james.backends.cassandra.utils.CassandraUtils; -import org.apache.james.eventsourcing.AggregateId; -import org.apache.james.eventsourcing.Event; -import org.apache.james.eventsourcing.EventStore; - -import com.datastax.driver.core.BatchStatement; -import com.datastax.driver.core.BoundStatement; -import com.datastax.driver.core.PreparedStatement; -import com.datastax.driver.core.ResultSet; -import com.datastax.driver.core.Row; -import com.datastax.driver.core.Session; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.github.steveash.guavate.Guavate; - -public class EventStoreDao { - private final CassandraUtils cassandraUtils; - private final CassandraAsyncExecutor cassandraAsyncExecutor; - private final PreparedStatement insert; - private final PreparedStatement select; - private final JsonEventSerializer jsonEventSerializer; - - @Inject - public EventStoreDao(Session session, CassandraUtils cassandraUtils, JsonEventSerializer jsonEventSerializer) { - this.cassandraUtils = cassandraUtils; - this.cassandraAsyncExecutor = new CassandraAsyncExecutor(session); - this.jsonEventSerializer = jsonEventSerializer; - this.insert = prepareInsert(session); - this.select = prepareSelect(session); - } - - private PreparedStatement prepareInsert(Session session) { - return session.prepare(insertInto(EVENTS_TABLE) - .value(AGGREGATE_ID, bindMarker(AGGREGATE_ID)) - .value(EVENT_ID, bindMarker(EVENT_ID)) - .value(EVENT, bindMarker(EVENT)) - .ifNotExists()); - } - - private PreparedStatement prepareSelect(Session session) { - return session.prepare(select() - .from(EVENTS_TABLE) - .where(eq(AGGREGATE_ID, bindMarker(AGGREGATE_ID)))); - } - - public CompletableFuture<Boolean> appendAll(List<Event> events) { - BatchStatement batch = new BatchStatement(); - events.forEach(event -> batch.add(insertEvent(event))); - return cassandraAsyncExecutor.executeReturnApplied(batch); - } - - private BoundStatement insertEvent(Event event) { - try { - return insert - .bind() - .setString(AGGREGATE_ID, event.getAggregateId().asAggregateKey()) - .setInt(EVENT_ID, event.eventId().serialize()) - .setString(EVENT, jsonEventSerializer.serialize(event)); - } catch (JsonProcessingException e) { - throw new RuntimeException(e); - } - } - - public EventStore.History getEventsOfAggregate(AggregateId aggregateId) { - return toHistory( - cassandraAsyncExecutor.execute( - select.bind() - .setString(AGGREGATE_ID, aggregateId.asAggregateKey())) - .join()); - } - - private EventStore.History toHistory(ResultSet resultSet) { - List<Event> events = cassandraUtils.convertToStream(resultSet) - .map(this::toEvent) - .collect(Guavate.toImmutableList()); - return EventStore.History.of(events); - } - - private Event toEvent(Row row) { - try { - return jsonEventSerializer.deserialize(row.getString(EVENT)); - } catch (IOException e) { - throw new RuntimeException(e); - } - } -}
http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing-cassandra/src/main/java/org/apache/james/eventsourcing/cassandra/JsonEventSerializer.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing-cassandra/src/main/java/org/apache/james/eventsourcing/cassandra/JsonEventSerializer.java b/mailbox/plugin/quota-mailing-cassandra/src/main/java/org/apache/james/eventsourcing/cassandra/JsonEventSerializer.java deleted file mode 100644 index bf3a5dc..0000000 --- a/mailbox/plugin/quota-mailing-cassandra/src/main/java/org/apache/james/eventsourcing/cassandra/JsonEventSerializer.java +++ /dev/null @@ -1,98 +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.cassandra; - -import java.io.IOException; -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.function.Function; - -import javax.inject.Inject; - -import org.apache.james.eventsourcing.Event; -import org.apache.james.eventsourcing.cassandra.dto.EventDTO; -import org.apache.james.eventsourcing.cassandra.dto.EventDTOModule; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; -import com.github.steveash.guavate.Guavate; -import com.google.common.collect.ImmutableSet; - -public class JsonEventSerializer { - public static class UnknownEventException extends RuntimeException { - public UnknownEventException(String message) { - super(message); - } - } - - private final Map<Class<? extends Event>, EventDTOModule> eventClassToModule; - private final Map<String, EventDTOModule> typeToModule; - private final ObjectMapper objectMapper; - - @Inject - public JsonEventSerializer(Set<EventDTOModule> modules) { - objectMapper = new ObjectMapper(); - objectMapper.registerModule(new Jdk8Module()); - objectMapper.setSerializationInclusion(JsonInclude.Include.NON_ABSENT); - - typeToModule = modules.stream() - .collect(Guavate.toImmutableMap( - EventDTOModule::getType, - Function.identity())); - - eventClassToModule = modules.stream() - .collect(Guavate.toImmutableMap( - EventDTOModule::getEventClass, - Function.identity())); - } - - public JsonEventSerializer(EventDTOModule... modules) { - this(ImmutableSet.copyOf(modules)); - } - - public String serialize(Event event) throws JsonProcessingException { - Object dto = Optional.ofNullable(eventClassToModule.get(event.getClass())) - .orElseThrow(() -> new UnknownEventException("unknown event class " + event.getClass())) - .toDTO(event); - return objectMapper.writeValueAsString(dto); - } - - public Event deserialize(String value) throws IOException { - JsonNode jsonNode = objectMapper.readTree(value); - - String type = jsonNode.path("type").asText(); - - EventDTO dto = objectMapper.readValue( - objectMapper.treeAsTokens(jsonNode), - retrieveDTOClass(type)); - return dto.toEvent(); - } - - public Class<? extends EventDTO> retrieveDTOClass(String type) { - return Optional.ofNullable(typeToModule.get(type)) - .map(EventDTOModule::getDTOClass) - .orElseThrow(() -> new UnknownEventException("unknown event type " + type)); - } - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing-cassandra/src/main/java/org/apache/james/eventsourcing/cassandra/dto/EventDTO.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing-cassandra/src/main/java/org/apache/james/eventsourcing/cassandra/dto/EventDTO.java b/mailbox/plugin/quota-mailing-cassandra/src/main/java/org/apache/james/eventsourcing/cassandra/dto/EventDTO.java deleted file mode 100644 index 4616248..0000000 --- a/mailbox/plugin/quota-mailing-cassandra/src/main/java/org/apache/james/eventsourcing/cassandra/dto/EventDTO.java +++ /dev/null @@ -1,26 +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.cassandra.dto; - -import org.apache.james.eventsourcing.Event; - -public interface EventDTO { - Event toEvent(); -} http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing-cassandra/src/main/java/org/apache/james/eventsourcing/cassandra/dto/EventDTOModule.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing-cassandra/src/main/java/org/apache/james/eventsourcing/cassandra/dto/EventDTOModule.java b/mailbox/plugin/quota-mailing-cassandra/src/main/java/org/apache/james/eventsourcing/cassandra/dto/EventDTOModule.java deleted file mode 100644 index d86d1d7..0000000 --- a/mailbox/plugin/quota-mailing-cassandra/src/main/java/org/apache/james/eventsourcing/cassandra/dto/EventDTOModule.java +++ /dev/null @@ -1,32 +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.cassandra.dto; - -import org.apache.james.eventsourcing.Event; - -public interface EventDTOModule { - String getType(); - - Class<? extends EventDTO> getDTOClass(); - - Class<? extends Event> getEventClass(); - - EventDTO toDTO(Event event); -} http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing-cassandra/src/main/java/org/apache/james/mailbox/quota/cassandra/dto/QuotaThresholdChangedEventDTO.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing-cassandra/src/main/java/org/apache/james/mailbox/quota/cassandra/dto/QuotaThresholdChangedEventDTO.java b/mailbox/plugin/quota-mailing-cassandra/src/main/java/org/apache/james/mailbox/quota/cassandra/dto/QuotaThresholdChangedEventDTO.java index 80eae8d..f229e2c 100644 --- a/mailbox/plugin/quota-mailing-cassandra/src/main/java/org/apache/james/mailbox/quota/cassandra/dto/QuotaThresholdChangedEventDTO.java +++ b/mailbox/plugin/quota-mailing-cassandra/src/main/java/org/apache/james/mailbox/quota/cassandra/dto/QuotaThresholdChangedEventDTO.java @@ -21,7 +21,7 @@ package org.apache.james.mailbox.quota.cassandra.dto; import org.apache.james.core.User; import org.apache.james.eventsourcing.EventId; -import org.apache.james.eventsourcing.cassandra.dto.EventDTO; +import org.apache.james.eventsourcing.eventstore.cassandra.dto.EventDTO; import org.apache.james.mailbox.quota.mailing.aggregates.UserQuotaThresholds; import org.apache.james.mailbox.quota.mailing.events.QuotaThresholdChangedEvent; http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing-cassandra/src/main/java/org/apache/james/mailbox/quota/cassandra/dto/QuotaThresholdChangedEventDTOModule.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing-cassandra/src/main/java/org/apache/james/mailbox/quota/cassandra/dto/QuotaThresholdChangedEventDTOModule.java b/mailbox/plugin/quota-mailing-cassandra/src/main/java/org/apache/james/mailbox/quota/cassandra/dto/QuotaThresholdChangedEventDTOModule.java index 328cb09..7c37b7f 100644 --- a/mailbox/plugin/quota-mailing-cassandra/src/main/java/org/apache/james/mailbox/quota/cassandra/dto/QuotaThresholdChangedEventDTOModule.java +++ b/mailbox/plugin/quota-mailing-cassandra/src/main/java/org/apache/james/mailbox/quota/cassandra/dto/QuotaThresholdChangedEventDTOModule.java @@ -20,8 +20,8 @@ package org.apache.james.mailbox.quota.cassandra.dto; import org.apache.james.eventsourcing.Event; -import org.apache.james.eventsourcing.cassandra.dto.EventDTO; -import org.apache.james.eventsourcing.cassandra.dto.EventDTOModule; +import org.apache.james.eventsourcing.eventstore.cassandra.dto.EventDTO; +import org.apache.james.eventsourcing.eventstore.cassandra.dto.EventDTOModule; import org.apache.james.mailbox.quota.mailing.events.QuotaThresholdChangedEvent; import com.google.common.base.Preconditions; http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/CassandraEventSourcingSystemTest.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/CassandraEventSourcingSystemTest.java b/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/CassandraEventSourcingSystemTest.java deleted file mode 100644 index bee6e88..0000000 --- a/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/CassandraEventSourcingSystemTest.java +++ /dev/null @@ -1,28 +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.cassandra; - -import org.apache.james.eventsourcing.EventSourcingSystemTest; -import org.junit.jupiter.api.extension.ExtendWith; - -@ExtendWith(CassandraEventStoreExtension.class) -public class CassandraEventSourcingSystemTest implements EventSourcingSystemTest { - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/CassandraEventStoreExtension.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/CassandraEventStoreExtension.java b/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/CassandraEventStoreExtension.java deleted file mode 100644 index 1b0066f..0000000 --- a/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/CassandraEventStoreExtension.java +++ /dev/null @@ -1,86 +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.cassandra; - -import org.apache.james.backends.cassandra.CassandraCluster; -import org.apache.james.backends.cassandra.DockerCassandraExtension; -import org.apache.james.backends.cassandra.DockerCassandraExtension.DockerCassandra; -import org.apache.james.backends.cassandra.utils.CassandraUtils; -import org.apache.james.eventsourcing.EventStore; -import org.apache.james.eventsourcing.cassandra.dto.TestEventDTOModule; -import org.apache.james.mailbox.quota.cassandra.dto.QuotaThresholdChangedEventDTOModule; -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 final DockerCassandraExtension dockerCassandraExtension; - private CassandraCluster cassandra; - private DockerCassandra dockerCassandra; - private EventStoreDao eventStoreDao; - - public CassandraEventStoreExtension() { - dockerCassandraExtension = new DockerCassandraExtension(); - } - - @Override - public void beforeAll(ExtensionContext context) throws Exception { - dockerCassandraExtension.beforeAll(context); - dockerCassandra = dockerCassandraExtension.getDockerCassandra(); - } - - @Override - public void afterAll(ExtensionContext context) throws Exception { - dockerCassandraExtension.afterAll(context); - } - - @Override - public void beforeEach(ExtensionContext context) { - cassandra = CassandraCluster.create( - new CassandraEventStoreModule(), dockerCassandra.getIp(), dockerCassandra.getBindingPort()); - - JsonEventSerializer jsonEventSerializer = new JsonEventSerializer( - new QuotaThresholdChangedEventDTOModule(), - new TestEventDTOModule()); - - eventStoreDao = new EventStoreDao(cassandra.getConf(), CassandraUtils.WITH_DEFAULT_CONFIGURATION, - jsonEventSerializer); - } - - @Override - public void afterEach(ExtensionContext context) { - cassandra.close(); - } - - @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); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/CassandraEventStoreTest.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/CassandraEventStoreTest.java b/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/CassandraEventStoreTest.java deleted file mode 100644 index 314b185..0000000 --- a/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/CassandraEventStoreTest.java +++ /dev/null @@ -1,28 +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.cassandra; - -import org.apache.james.eventsourcing.EventStoreTest; -import org.junit.jupiter.api.extension.ExtendWith; - -@ExtendWith(CassandraEventStoreExtension.class) -class CassandraEventStoreTest implements EventStoreTest { - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/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 deleted file mode 100644 index 89770bb..0000000 --- a/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/JsonEventSerializerTest.java +++ /dev/null @@ -1,102 +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.cassandra; - -import static net.javacrumbs.jsonunit.fluent.JsonFluentAssert.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.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 String OTHER_EVENT_JSON = "{\"type\":\"other-type\",\"data\":1,\"eventId\":0,\"aggregate\":1}"; - - @Test - void shouldDeserializeKnownEvent() throws Exception { - assertThat(new JsonEventSerializer(new TestEventDTOModule()) - .deserialize(TEST_EVENT_JSON)) - .isEqualTo(TEST_EVENT); - } - - @Test - void shouldThrowWhenDeserializeUnknownEvent() { - assertThatThrownBy(() -> new JsonEventSerializer() - .deserialize(TEST_EVENT_JSON)) - .isInstanceOf(JsonEventSerializer.UnknownEventException.class); - } - - @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)) - .isEqualTo(TEST_EVENT_JSON); - } - - @Test - void shouldThrowWhenSerializeUnknownEvent() { - assertThatThrownBy(() -> new JsonEventSerializer() - .serialize(TEST_EVENT)) - .isInstanceOf(JsonEventSerializer.UnknownEventException.class); - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/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 deleted file mode 100644 index 4e74720..0000000 --- a/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/dto/OtherEvent.java +++ /dev/null @@ -1,50 +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.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/189490a4/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 deleted file mode 100644 index 4f35c12..0000000 --- a/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/dto/OtherTestEventDTO.java +++ /dev/null @@ -1,72 +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.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/189490a4/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 deleted file mode 100644 index deb9a02..0000000 --- a/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/dto/OtherTestEventDTOModule.java +++ /dev/null @@ -1,55 +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.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()); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/dto/TestEventDTO.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/dto/TestEventDTO.java b/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/dto/TestEventDTO.java deleted file mode 100644 index 529d8a9..0000000 --- a/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/dto/TestEventDTO.java +++ /dev/null @@ -1,73 +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.cassandra.dto; - -import org.apache.james.eventsourcing.Event; -import org.apache.james.eventsourcing.EventId; -import org.apache.james.eventsourcing.TestAggregateId; -import org.apache.james.eventsourcing.TestEvent; - -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonIgnore; -import com.fasterxml.jackson.annotation.JsonProperty; - -public class TestEventDTO implements EventDTO { - private final String type; - private final String data; - private final int eventId; - private final int aggregate; - - @JsonCreator - public TestEventDTO( - @JsonProperty("type") String type, - @JsonProperty("data") String 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 String getData() { - return data; - } - - public long getEventId() { - return eventId; - } - - public int getAggregate() { - return aggregate; - } - - @JsonIgnore - @Override - public Event toEvent() { - return new TestEvent( - EventId.fromSerialized(eventId), - TestAggregateId.testId(aggregate), - data); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/dto/TestEventDTOModule.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/dto/TestEventDTOModule.java b/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/dto/TestEventDTOModule.java deleted file mode 100644 index 6b46268..0000000 --- a/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/eventsourcing/cassandra/dto/TestEventDTOModule.java +++ /dev/null @@ -1,56 +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.cassandra.dto; - -import org.apache.james.eventsourcing.Event; -import org.apache.james.eventsourcing.TestEvent; -import org.testcontainers.shaded.com.google.common.base.Preconditions; - -public class TestEventDTOModule implements EventDTOModule { - - public static final String TEST_TYPE = "TestType"; - - @Override - public String getType() { - return TEST_TYPE; - } - - @Override - public Class<? extends EventDTO> getDTOClass() { - return TestEventDTO.class; - } - - @Override - public Class<? extends Event> getEventClass() { - return TestEvent.class; - } - - @Override - public EventDTO toDTO(Event event) { - Preconditions.checkArgument(event instanceof TestEvent); - - TestEvent testEvent = (TestEvent) event; - return new TestEventDTO( - TEST_TYPE, - testEvent.getData(), - testEvent.eventId().serialize(), - testEvent.getAggregateId().getId()); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/mailbox/quota/cassandra/dto/DTOTest.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/mailbox/quota/cassandra/dto/DTOTest.java b/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/mailbox/quota/cassandra/dto/DTOTest.java index 2f48a55..ca063d9 100644 --- a/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/mailbox/quota/cassandra/dto/DTOTest.java +++ b/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/mailbox/quota/cassandra/dto/DTOTest.java @@ -28,7 +28,7 @@ import java.time.Instant; import org.apache.james.core.User; import org.apache.james.eventsourcing.EventId; -import org.apache.james.eventsourcing.cassandra.JsonEventSerializer; +import org.apache.james.eventsourcing.eventstore.cassandra.JsonEventSerializer; import org.apache.james.mailbox.model.Quota; import org.apache.james.mailbox.quota.QuotaCount; import org.apache.james.mailbox.quota.QuotaSize; @@ -46,24 +46,24 @@ import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; class DTOTest { - public static final Quota<QuotaSize> SIZE_QUOTA = Quota.<QuotaSize>builder().used(QuotaSize.size(23)).computedLimit(QuotaSize.size(33)).build(); - public static final Quota<QuotaCount> COUNT_QUOTA = Quota.<QuotaCount>builder().used(QuotaCount.count(12)).computedLimit(QuotaCount.count(45)).build(); - public static final Instant INSTANT = Instant.ofEpochMilli(45554); - public static final QuotaThresholdChangedEvent EVENT = new QuotaThresholdChangedEvent( + static final Quota<QuotaSize> SIZE_QUOTA = Quota.<QuotaSize>builder().used(QuotaSize.size(23)).computedLimit(QuotaSize.size(33)).build(); + static final Quota<QuotaCount> COUNT_QUOTA = Quota.<QuotaCount>builder().used(QuotaCount.count(12)).computedLimit(QuotaCount.count(45)).build(); + static final Instant INSTANT = Instant.ofEpochMilli(45554); + static final QuotaThresholdChangedEvent EVENT = new QuotaThresholdChangedEvent( EventId.first(), HistoryEvolution.noChanges(), HistoryEvolution.noChanges(), SIZE_QUOTA, COUNT_QUOTA, UserQuotaThresholds.Id.from(User.fromUsername("f...@bar.com"))); - public static final QuotaThresholdChangedEvent EVENT_2 = new QuotaThresholdChangedEvent( + static final QuotaThresholdChangedEvent EVENT_2 = new QuotaThresholdChangedEvent( EventId.first(), HistoryEvolution.lowerThresholdReached(new QuotaThresholdChange(_75, INSTANT)), HistoryEvolution.noChanges(), SIZE_QUOTA, Quota.<QuotaCount>builder().used(QuotaCount.count(12)).computedLimit(QuotaCount.unlimited()).build(), UserQuotaThresholds.Id.from(User.fromUsername("f...@bar.com"))); - public static final QuotaThresholdChangedEvent EVENT_3 = new QuotaThresholdChangedEvent( + static final QuotaThresholdChangedEvent EVENT_3 = new QuotaThresholdChangedEvent( EventId.first(), HistoryEvolution.lowerThresholdReached(new QuotaThresholdChange(_75, INSTANT)), HistoryEvolution.higherThresholdReached(new QuotaThresholdChange(_80, INSTANT), @@ -71,7 +71,7 @@ class DTOTest { SIZE_QUOTA, Quota.<QuotaCount>builder().used(QuotaCount.count(12)).computedLimit(QuotaCount.unlimited()).build(), UserQuotaThresholds.Id.from(User.fromUsername("f...@bar.com"))); - public static final QuotaThresholdChangedEvent EVENT_4 = new QuotaThresholdChangedEvent( + static final QuotaThresholdChangedEvent EVENT_4 = new QuotaThresholdChangedEvent( EventId.first(), HistoryEvolution.lowerThresholdReached(new QuotaThresholdChange(_75, INSTANT)), HistoryEvolution.higherThresholdReached(new QuotaThresholdChange(_80, INSTANT), @@ -80,24 +80,24 @@ class DTOTest { Quota.<QuotaCount>builder().used(QuotaCount.count(12)).computedLimit(QuotaCount.unlimited()).build(), UserQuotaThresholds.Id.from(User.fromUsername("f...@bar.com"))); - public static final String EVENT_JSON = ClassLoaderUtils.getSystemResourceAsString("json/event.json"); - public static final String EVENT_JSON_2 = ClassLoaderUtils.getSystemResourceAsString("json/event2.json"); - public static final String EVENT_JSON_3 = ClassLoaderUtils.getSystemResourceAsString("json/event3.json"); - public static final String EVENT_JSON_4 = ClassLoaderUtils.getSystemResourceAsString("json/event4.json"); + static final String EVENT_JSON = ClassLoaderUtils.getSystemResourceAsString("json/event.json"); + static final String EVENT_JSON_2 = ClassLoaderUtils.getSystemResourceAsString("json/event2.json"); + static final String EVENT_JSON_3 = ClassLoaderUtils.getSystemResourceAsString("json/event3.json"); + static final String EVENT_JSON_4 = ClassLoaderUtils.getSystemResourceAsString("json/event4.json"); - public static final String COUNT_QUOTA_JSON = "{" + + static final String COUNT_QUOTA_JSON = "{" + " \"used\": 12," + " \"limit\": 45" + " }"; - public static final String NO_CHANGES_JSON = "{" + + static final String NO_CHANGES_JSON = "{" + " \"change\":\"NoChange\"" + "}"; private ObjectMapper objectMapper; @BeforeEach - public void setUp() { + void setUp() { objectMapper = new ObjectMapper(); objectMapper.registerModule(new Jdk8Module()); objectMapper.setSerializationInclusion(JsonInclude.Include.NON_ABSENT); http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/mailbox/quota/cassandra/listeners/CassandraEventStoreExtension.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/mailbox/quota/cassandra/listeners/CassandraEventStoreExtension.java b/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/mailbox/quota/cassandra/listeners/CassandraEventStoreExtension.java new file mode 100644 index 0000000..f46a605 --- /dev/null +++ b/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/mailbox/quota/cassandra/listeners/CassandraEventStoreExtension.java @@ -0,0 +1,32 @@ +/**************************************************************** + * 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.mailbox.quota.cassandra.listeners; + +import org.apache.james.eventsourcing.eventstore.cassandra.CassandraGenericEventStoreExtension; +import org.apache.james.mailbox.quota.cassandra.dto.QuotaThresholdChangedEventDTOModule; + +import com.google.common.collect.ImmutableSet; + +public class CassandraEventStoreExtension extends CassandraGenericEventStoreExtension { + public CassandraEventStoreExtension() { + super(ImmutableSet.of( + new QuotaThresholdChangedEventDTOModule())); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/mailbox/quota/cassandra/listeners/CassandraQuotaMailingListenersIntegrationTest.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/mailbox/quota/cassandra/listeners/CassandraQuotaMailingListenersIntegrationTest.java b/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/mailbox/quota/cassandra/listeners/CassandraQuotaMailingListenersIntegrationTest.java index 3ea097a..7835898 100644 --- a/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/mailbox/quota/cassandra/listeners/CassandraQuotaMailingListenersIntegrationTest.java +++ b/mailbox/plugin/quota-mailing-cassandra/src/test/java/org/apache/james/mailbox/quota/cassandra/listeners/CassandraQuotaMailingListenersIntegrationTest.java @@ -19,7 +19,6 @@ package org.apache.james.mailbox.quota.cassandra.listeners; -import org.apache.james.eventsourcing.cassandra.CassandraEventStoreExtension; import org.apache.james.mailbox.quota.mailing.listeners.QuotaThresholdMailingIntegrationTest; import org.junit.jupiter.api.extension.ExtendWith; http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing-memory/pom.xml ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing-memory/pom.xml b/mailbox/plugin/quota-mailing-memory/pom.xml index ad3a70f..aa28165 100644 --- a/mailbox/plugin/quota-mailing-memory/pom.xml +++ b/mailbox/plugin/quota-mailing-memory/pom.xml @@ -36,6 +36,27 @@ <dependencies> <dependency> <groupId>${project.groupId}</groupId> + <artifactId>event-sourcing-core</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>event-sourcing-event-store-api</artifactId> + <type>test-jar</type> + <scope>test</scope> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>event-sourcing-event-store-memory</artifactId> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>event-sourcing-event-store-memory</artifactId> + <type>test-jar</type> + <scope>test</scope> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> <artifactId>apache-james-mailbox-api</artifactId> <type>test-jar</type> <scope>test</scope> http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing-memory/src/main/java/org/apache/james/eventsource/InMemoryEventStore.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing-memory/src/main/java/org/apache/james/eventsource/InMemoryEventStore.java b/mailbox/plugin/quota-mailing-memory/src/main/java/org/apache/james/eventsource/InMemoryEventStore.java deleted file mode 100644 index 11c8e46..0000000 --- a/mailbox/plugin/quota-mailing-memory/src/main/java/org/apache/james/eventsource/InMemoryEventStore.java +++ /dev/null @@ -1,97 +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.eventsource; - -import java.util.List; -import java.util.Optional; -import java.util.concurrent.ConcurrentHashMap; - -import org.apache.james.eventsourcing.AggregateId; -import org.apache.james.eventsourcing.Event; -import org.apache.james.eventsourcing.EventStore; - -import com.google.common.base.Preconditions; -import com.google.common.collect.ImmutableList; - -public class InMemoryEventStore implements EventStore { - - private final ConcurrentHashMap<AggregateId, History> store; - - public InMemoryEventStore() { - this.store = new ConcurrentHashMap<>(); - } - - @Override - public void appendAll(List<Event> events) { - if (events.isEmpty()) { - return; - } - AggregateId aggregateId = getAggregateId(events); - - if (!store.containsKey(aggregateId)) { - appendToEmptyHistory(aggregateId, events); - } else { - appendToExistingHistory(aggregateId, events); - } - } - - private AggregateId getAggregateId(List<? extends Event> events) { - Preconditions.checkArgument(!events.isEmpty()); - Preconditions.checkArgument(Event.belongsToSameAggregate(events)); - - return events.stream() - .map(Event::getAggregateId) - .findFirst() - .get(); - } - - private void appendToEmptyHistory(AggregateId aggregateId, List<Event> events) { - History newHistory = History.of(events); - - History previousHistory = store.putIfAbsent(aggregateId, newHistory); - if (previousHistory != null) { - throw new EventStore.EventStoreFailedException(); - } - } - - private void appendToExistingHistory(AggregateId aggregateId, List<? extends Event> events) { - History currentHistory = store.get(aggregateId); - List<Event> updatedEvents = updatedEvents(currentHistory, events); - History updatedHistory = History.of(updatedEvents); - - boolean isReplaced = store.replace(aggregateId, currentHistory, updatedHistory); - if (!isReplaced) { - throw new EventStore.EventStoreFailedException(); - } - } - - private List<Event> updatedEvents(History currentHistory, List<? extends Event> newEvents) { - return ImmutableList.<Event>builder() - .addAll(currentHistory.getEvents()) - .addAll(newEvents) - .build(); - } - - @Override - public History getEventsOfAggregate(AggregateId aggregateId) { - return Optional.ofNullable(store.get(aggregateId)) - .orElse(History.empty()); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing-memory/src/test/java/org/apache/james/eventsourcing/InMemoryEventSourcingSystemTest.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing-memory/src/test/java/org/apache/james/eventsourcing/InMemoryEventSourcingSystemTest.java b/mailbox/plugin/quota-mailing-memory/src/test/java/org/apache/james/eventsourcing/InMemoryEventSourcingSystemTest.java deleted file mode 100644 index c2f9935..0000000 --- a/mailbox/plugin/quota-mailing-memory/src/test/java/org/apache/james/eventsourcing/InMemoryEventSourcingSystemTest.java +++ /dev/null @@ -1,27 +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 org.junit.jupiter.api.extension.ExtendWith; - -@ExtendWith(InMemoryEventStoreExtension.class) -public class InMemoryEventSourcingSystemTest implements EventSourcingSystemTest { - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing-memory/src/test/java/org/apache/james/eventsourcing/InMemoryEventStoreExtension.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing-memory/src/test/java/org/apache/james/eventsourcing/InMemoryEventStoreExtension.java b/mailbox/plugin/quota-mailing-memory/src/test/java/org/apache/james/eventsourcing/InMemoryEventStoreExtension.java deleted file mode 100644 index c4b345e..0000000 --- a/mailbox/plugin/quota-mailing-memory/src/test/java/org/apache/james/eventsourcing/InMemoryEventStoreExtension.java +++ /dev/null @@ -1,39 +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 org.apache.james.eventsource.InMemoryEventStore; -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 InMemoryEventStoreExtension implements ParameterResolver { - - @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 InMemoryEventStore(); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing-memory/src/test/java/org/apache/james/eventsourcing/InMemoryEventStoreTest.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing-memory/src/test/java/org/apache/james/eventsourcing/InMemoryEventStoreTest.java b/mailbox/plugin/quota-mailing-memory/src/test/java/org/apache/james/eventsourcing/InMemoryEventStoreTest.java deleted file mode 100644 index eddc2d0..0000000 --- a/mailbox/plugin/quota-mailing-memory/src/test/java/org/apache/james/eventsourcing/InMemoryEventStoreTest.java +++ /dev/null @@ -1,27 +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 org.junit.jupiter.api.extension.ExtendWith; - -@ExtendWith(InMemoryEventStoreExtension.class) -public class InMemoryEventStoreTest implements EventStoreTest { - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing-memory/src/test/java/org/apache/james/mailbox/quota/memory/listeners/InMemoryQuotaMailingListenersIntegrationTest.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing-memory/src/test/java/org/apache/james/mailbox/quota/memory/listeners/InMemoryQuotaMailingListenersIntegrationTest.java b/mailbox/plugin/quota-mailing-memory/src/test/java/org/apache/james/mailbox/quota/memory/listeners/InMemoryQuotaMailingListenersIntegrationTest.java index 4b17682..1a1ecb2 100644 --- a/mailbox/plugin/quota-mailing-memory/src/test/java/org/apache/james/mailbox/quota/memory/listeners/InMemoryQuotaMailingListenersIntegrationTest.java +++ b/mailbox/plugin/quota-mailing-memory/src/test/java/org/apache/james/mailbox/quota/memory/listeners/InMemoryQuotaMailingListenersIntegrationTest.java @@ -19,7 +19,7 @@ package org.apache.james.mailbox.quota.memory.listeners; -import org.apache.james.eventsourcing.InMemoryEventStoreExtension; +import org.apache.james.eventsourcing.eventstore.memory.InMemoryEventStoreExtension; import org.apache.james.mailbox.quota.mailing.listeners.QuotaThresholdMailingIntegrationTest; import org.junit.jupiter.api.extension.ExtendWith; http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing-memory/src/test/java/org/apache/james/mailbox/quota/memory/listeners/InMemoryQuotaThresholdConfigurationChangesTest.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing-memory/src/test/java/org/apache/james/mailbox/quota/memory/listeners/InMemoryQuotaThresholdConfigurationChangesTest.java b/mailbox/plugin/quota-mailing-memory/src/test/java/org/apache/james/mailbox/quota/memory/listeners/InMemoryQuotaThresholdConfigurationChangesTest.java index ba65405..409e726 100644 --- a/mailbox/plugin/quota-mailing-memory/src/test/java/org/apache/james/mailbox/quota/memory/listeners/InMemoryQuotaThresholdConfigurationChangesTest.java +++ b/mailbox/plugin/quota-mailing-memory/src/test/java/org/apache/james/mailbox/quota/memory/listeners/InMemoryQuotaThresholdConfigurationChangesTest.java @@ -19,7 +19,7 @@ package org.apache.james.mailbox.quota.memory.listeners; -import org.apache.james.eventsourcing.InMemoryEventStoreExtension; +import org.apache.james.eventsourcing.eventstore.memory.InMemoryEventStoreExtension; import org.apache.james.mailbox.quota.mailing.listeners.QuotaThresholdConfigurationChangesTest; import org.junit.jupiter.api.extension.ExtendWith; http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing/pom.xml ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing/pom.xml b/mailbox/plugin/quota-mailing/pom.xml index 8f45fb1..b10bd8e 100644 --- a/mailbox/plugin/quota-mailing/pom.xml +++ b/mailbox/plugin/quota-mailing/pom.xml @@ -36,6 +36,20 @@ <dependencies> <dependency> <groupId>${project.groupId}</groupId> + <artifactId>event-sourcing-core</artifactId> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>event-sourcing-event-store-api</artifactId> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>event-sourcing-event-store-api</artifactId> + <type>test-jar</type> + <scope>test</scope> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> <artifactId>apache-james-mailbox-api</artifactId> </dependency> <dependency> http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/AggregateId.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/AggregateId.java b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/AggregateId.java deleted file mode 100644 index 18c6224..0000000 --- a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/AggregateId.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 AggregateId { - String asAggregateKey(); -} http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/CommandDispatcher.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/CommandDispatcher.java b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/CommandDispatcher.java deleted file mode 100644 index de8b63f..0000000 --- a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/CommandDispatcher.java +++ /dev/null @@ -1,117 +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.Collection; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.function.Supplier; -import java.util.stream.IntStream; - -import javax.inject.Inject; - -import com.github.steveash.guavate.Guavate; - -public class CommandDispatcher { - - private static final int MAX_RETRY = 10; - - public interface Command { - } - - public class UnknownCommandException extends RuntimeException { - private final Command command; - - public UnknownCommandException(Command command) { - super(String.format("Unknown command %s", command)); - this.command = command; - } - - public Command getCommand() { - return command; - } - } - - public class TooManyRetries extends RuntimeException { - private final Command command; - private final int retries; - - - public TooManyRetries(Command command, int retries) { - super(String.format("Too much retries for command %s. Store failure after %d retries", command, retries)); - this.command = command; - this.retries = retries; - } - - - public Command getCommand() { - return command; - } - - public int getRetries() { - return retries; - } - } - - public interface CommandHandler<C extends Command> { - Class<C> handledClass(); - - List<? extends Event> handle(C c); - } - - private final EventBus eventBus; - @SuppressWarnings("rawtypes") - private final Map<Class, CommandHandler> handlers; - - @Inject - public CommandDispatcher(EventBus eventBus, Collection<CommandHandler<?>> handlers) { - this.eventBus = eventBus; - this.handlers = handlers.stream() - .collect(Guavate.toImmutableMap(CommandHandler::handledClass, handler -> handler)); - } - - public void dispatch(Command c) { - trySeveralTimes(() -> tryDispatch(c)) - .orElseThrow(() -> new TooManyRetries(c, MAX_RETRY)); - } - - public Optional<Integer> trySeveralTimes(Supplier<Boolean> singleTry) { - return IntStream.range(0, MAX_RETRY) - .boxed() - .filter(any -> singleTry.get()) - .findFirst(); - } - - @SuppressWarnings("unchecked") - private boolean tryDispatch(Command c) { - try { - List<Event> events = - Optional.ofNullable(handlers.get(c.getClass())) - .map(f -> f.handle(c)) - .orElseThrow(() -> new UnknownCommandException(c)); - - eventBus.publish(events); - return true; - } catch (EventStore.EventStoreFailedException e) { - return false; - } - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/Event.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/Event.java b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/Event.java deleted file mode 100644 index 2b31374..0000000 --- a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/Event.java +++ /dev/null @@ -1,43 +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.List; - -public interface Event extends Comparable<Event> { - - static boolean belongsToSameAggregate(List<? extends Event> events) { - return events.stream() - .map(Event::getAggregateId) - .distinct() - .limit(2) - .count() == 1; - } - - EventId eventId(); - - AggregateId getAggregateId(); - - @Override - default int compareTo(Event o) { - return eventId().compareTo(o.eventId()); - } - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/EventBus.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/EventBus.java b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/EventBus.java deleted file mode 100644 index 067d432..0000000 --- a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/EventBus.java +++ /dev/null @@ -1,61 +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.List; -import java.util.Set; - -import javax.inject.Inject; - -import org.apache.commons.lang3.tuple.Pair; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.collect.ImmutableSet; - -public class EventBus { - - public static final Logger LOGGER = LoggerFactory.getLogger(EventBus.class); - private final EventStore eventStore; - private final Set<Subscriber> subscribers; - - @Inject - public EventBus(EventStore eventStore, Set<Subscriber> subscribers) { - this.eventStore = eventStore; - this.subscribers = ImmutableSet.copyOf(subscribers); - } - - public void publish(List<Event> events) throws EventStore.EventStoreFailedException { - eventStore.appendAll(events); - events.stream() - .flatMap(event -> subscribers.stream().map(subscriber -> Pair.of(event, subscriber))) - .forEach(this::handle); - } - - public void handle(Pair<Event, Subscriber> pair) { - Subscriber subscriber = pair.getRight(); - Event event = pair.getLeft(); - try { - subscriber.handle(event); - } catch (Exception e) { - LOGGER.error("Error while calling {} for {}", subscriber, event, e); - } - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/189490a4/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/EventId.java ---------------------------------------------------------------------- diff --git a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/EventId.java b/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/EventId.java deleted file mode 100644 index cb5bd1e..0000000 --- a/mailbox/plugin/quota-mailing/src/main/java/org/apache/james/eventsourcing/EventId.java +++ /dev/null @@ -1,86 +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 java.util.Optional; - -import com.google.common.base.MoreObjects; -import com.google.common.base.Preconditions; - -public class EventId implements Comparable<EventId> { - - public static EventId fromSerialized(int value) { - return new EventId(value); - } - - public static EventId first() { - return new EventId(0); - } - - private final int value; - - private EventId(int value) { - Preconditions.checkArgument(value >= 0, "EventId can not be negative"); - this.value = value; - } - - public EventId next() { - return new EventId(value + 1); - } - - public Optional<EventId> previous() { - if (value > 0) { - return Optional.of(new EventId(value - 1)); - } - return Optional.empty(); - } - - @Override - public int compareTo(EventId o) { - return Long.compare(value, o.value); - } - - @Override - public final boolean equals(Object o) { - if (o instanceof EventId) { - EventId eventId = (EventId) o; - - return Objects.equals(this.value, eventId.value); - } - return false; - } - - @Override - public final int hashCode() { - return Objects.hash(value); - } - - @Override - public String toString() { - return MoreObjects.toStringHelper(this) - .add("value", value) - .toString(); - } - - public int serialize() { - return value; - } -} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org