From c8c21d0ab864a83de1eb3a3ace6400396fdaf9fe Mon Sep 17 00:00:00 2001 From: brkbzn Date: Tue, 26 Mar 2019 19:03:54 +0300 Subject: [PATCH] eventapis-40 (#40) mark success for an operation --- .../kloia/eventapis/api/EventRepository.java | 2 + .../cassandra/CassandraEventRecorder.java | 28 +++++++++++- .../kloia/eventapis/common/EventRecorder.java | 2 + .../core/CompositeRepositoryImpl.java | 5 +++ .../core/CompositeRepositoryImplTest.java | 13 +++++- .../view/EntityFunctionSpecTest.java | 44 +++++++++++++++++++ 6 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 java-api/src/test/java/com/kloia/eventapis/view/EntityFunctionSpecTest.java diff --git a/java-api/src/main/java/com/kloia/eventapis/api/EventRepository.java b/java-api/src/main/java/com/kloia/eventapis/api/EventRepository.java index f93c654..2e7dabc 100644 --- a/java-api/src/main/java/com/kloia/eventapis/api/EventRepository.java +++ b/java-api/src/main/java/com/kloia/eventapis/api/EventRepository.java @@ -21,6 +21,8 @@ public interface EventRepository { List markFail(String opId); + List markSuccess(String opId); +

EventKey recordAndPublish(P publishedEvent) throws EventStoreException, ConcurrentEventException;

EventKey recordAndPublish(Entity entity, P publishedEvent) throws EventStoreException, ConcurrentEventException; diff --git a/java-api/src/main/java/com/kloia/eventapis/cassandra/CassandraEventRecorder.java b/java-api/src/main/java/com/kloia/eventapis/cassandra/CassandraEventRecorder.java index 9b0bb9f..2b69b3e 100644 --- a/java-api/src/main/java/com/kloia/eventapis/cassandra/CassandraEventRecorder.java +++ b/java-api/src/main/java/com/kloia/eventapis/cassandra/CassandraEventRecorder.java @@ -174,7 +174,7 @@ public List markFail(String key) { update.where(QueryBuilder.eq(ENTITY_ID, entityEvent.getEventKey().getEntityId())) .and(QueryBuilder.eq(VERSION, entityEvent.getEventKey().getVersion())) .ifExists(); - update.with(QueryBuilder.set(STATUS, "FAILED")); + update.with(QueryBuilder.set(STATUS, EventState.FAILED.name())); ResultSet execute = cassandraSession.execute(update); log.debug("Failure Mark Result:" + execute.toString() + " Update: " + update.toString()); return true; @@ -186,6 +186,32 @@ public List markFail(String key) { } + @Override + public List markSuccess(String key) { + Select select = QueryBuilder.select().from(tableNameByOps); + select.where(QueryBuilder.eq(OP_ID, key)); + List entityEventDatas = cassandraSession.execute(select, PagingIterable::all); + + return entityEventDatas.stream().map( + CassandraViewQuery::convertToEntityEvent + ).filter(entityEvent -> { + try { + Update update = QueryBuilder.update(tableName); + update.where(QueryBuilder.eq(ENTITY_ID, entityEvent.getEventKey().getEntityId())) + .and(QueryBuilder.eq(VERSION, entityEvent.getEventKey().getVersion())) + .ifExists(); + update.with(QueryBuilder.set(STATUS, EventState.CREATED.name())); + ResultSet execute = cassandraSession.execute(update); + log.debug("Success Mark Result:" + execute.toString() + " Update: " + update.toString()); + return true; + } catch (Exception e) { + log.warn(e.getMessage(), e); + return false; + } + }).collect(Collectors.toList()); + + } + @Override public String updateEvent(EventKey eventKey, @Nullable RecordedEvent newEventData, @Nullable EventState newEventState, @Nullable String newEventType) throws EventStoreException { diff --git a/java-api/src/main/java/com/kloia/eventapis/common/EventRecorder.java b/java-api/src/main/java/com/kloia/eventapis/common/EventRecorder.java index 75e6a9d..2d747f5 100644 --- a/java-api/src/main/java/com/kloia/eventapis/common/EventRecorder.java +++ b/java-api/src/main/java/com/kloia/eventapis/common/EventRecorder.java @@ -32,6 +32,8 @@ EventKey recordEntityEvent( List markFail(String key); + List markSuccess(String key); + String updateEvent(EventKey eventKey, @Nullable RecordedEvent newEventData, @Nullable EventState newEventState, @Nullable String newEventType) throws EventStoreException; String updateEvent(EventKey eventKey, RecordedEvent newEventData) throws EventStoreException; diff --git a/java-api/src/main/java/com/kloia/eventapis/core/CompositeRepositoryImpl.java b/java-api/src/main/java/com/kloia/eventapis/core/CompositeRepositoryImpl.java index 108428e..c385c9d 100644 --- a/java-api/src/main/java/com/kloia/eventapis/core/CompositeRepositoryImpl.java +++ b/java-api/src/main/java/com/kloia/eventapis/core/CompositeRepositoryImpl.java @@ -46,6 +46,11 @@ public List markFail(String opId) { return eventRecorder.markFail(opId); } + @Override + public List markSuccess(String opId) { + return eventRecorder.markSuccess(opId); + } + @Override public

EventKey recordAndPublish(P publishedEvent) throws EventStoreException, ConcurrentEventException { return recordAndPublishInternal(publishedEvent, Optional.empty(), entityEvent -> new DefaultConcurrencyResolver()); diff --git a/java-api/src/test/java/com/kloia/eventapis/core/CompositeRepositoryImplTest.java b/java-api/src/test/java/com/kloia/eventapis/core/CompositeRepositoryImplTest.java index be3a894..641f327 100644 --- a/java-api/src/test/java/com/kloia/eventapis/core/CompositeRepositoryImplTest.java +++ b/java-api/src/test/java/com/kloia/eventapis/core/CompositeRepositoryImplTest.java @@ -8,7 +8,6 @@ import com.kloia.eventapis.api.impl.UUIDCreationStrategy; import com.kloia.eventapis.cassandra.ConcurrencyResolver; import com.kloia.eventapis.cassandra.ConcurrentEventException; -import com.kloia.eventapis.cassandra.ConcurrentEventResolver; import com.kloia.eventapis.cassandra.DefaultConcurrencyResolver; import com.kloia.eventapis.cassandra.EntityEvent; import com.kloia.eventapis.common.EventKey; @@ -54,17 +53,22 @@ public class CompositeRepositoryImplTest { @Rule public ExpectedException expectedException = ExpectedException.none(); + @InjectMocks private CompositeRepositoryImpl compositeRepository; + @Mock private EventRecorder eventRecorder; + @Mock private ObjectMapper objectMapper; + @Mock private IOperationRepository operationRepository; @Mock private IdCreationStrategy idCreationStrategy = new UUIDCreationStrategy(); + @Captor private ArgumentCaptor>> concurrencyResolverFactoryCaptor; @@ -111,6 +115,13 @@ public void shouldMarkFail() { verify(eventRecorder).markFail("opId"); } + @Test + public void shouldMarkSuccess() { + compositeRepository.markSuccess("opId"); + + verify(eventRecorder).markSuccess("opId"); + } + private void mockCommon(PublishedEvent event) throws EventStoreException, ConcurrentEventException, JsonProcessingException { when(eventRecorder.recordEntityEvent(eq(event), anyLong(), previousEventKeyCaptor.capture(), concurrencyResolverFactoryCaptor.capture())).thenReturn(eventKey); when(objectWriter.writeValueAsString(event)).thenReturn("{" + event.getClass().getSimpleName() + "}"); diff --git a/java-api/src/test/java/com/kloia/eventapis/view/EntityFunctionSpecTest.java b/java-api/src/test/java/com/kloia/eventapis/view/EntityFunctionSpecTest.java new file mode 100644 index 0000000..b079303 --- /dev/null +++ b/java-api/src/test/java/com/kloia/eventapis/view/EntityFunctionSpecTest.java @@ -0,0 +1,44 @@ +package com.kloia.eventapis.view; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.runners.MockitoJUnitRunner; + +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; + +@RunWith(MockitoJUnitRunner.class) +public class EntityFunctionSpecTest { + + private EntityFunctionSpec entityFunctionSpec; + + private class EntityType { + } + + private class QueryType { + } + + @Before + public void setUp() throws Exception { + entityFunctionSpec = new EntityFunctionSpec((previous, event) -> null) { + @Override + public EntityFunction getEntityFunction() { + return super.getEntityFunction(); + } + }; + } + + @Test + public void shouldGetQueryType() throws Exception { + Class queryType = entityFunctionSpec.getQueryType(); + assertThat(queryType, equalTo(QueryType.class)); + } + + @Test + public void shouldGetEntityType() throws Exception { + Class entityType = entityFunctionSpec.getEntityType(); + assertThat(entityType, equalTo(EntityType.class)); + } + +} \ No newline at end of file