Search in sources :

Example 1 with EventStoreException

use of com.kloia.eventapis.exception.EventStoreException in project eventapis by kloiasoft.

the class CompositeRepositoryImplTest method shouldRecordAndPublishWithPreviousEventKeyAndPublishedEventAndConcurrencyResolverFactory.

@Test
public void shouldRecordAndPublishWithPreviousEventKeyAndPublishedEventAndConcurrencyResolverFactory() throws JsonProcessingException, EventStoreException, ConcurrentEventException {
    mockCommon(intermediateEvent);
    EventKey previousEntityEventKey = new EventKey();
    ConcurrencyResolver concurrencyResolver = mock(ConcurrencyResolver.class);
    Function<EntityEvent, ConcurrencyResolver<ConcurrentEventException>> factory = entityEvent -> concurrencyResolver;
    EventKey actual = compositeRepository.recordAndPublish(previousEntityEventKey, intermediateEvent, factory);
    assertCommon(intermediateEvent);
    assertThat(actual, equalTo(eventKey));
    assertThat(previousEventKeyCaptor.getValue().isPresent(), equalTo(true));
    assertThat(previousEventKeyCaptor.getValue().get(), equalTo(previousEntityEventKey));
    assertThat(concurrencyResolverFactoryCaptor.getValue(), equalTo(factory));
}
Also used : ConcurrencyResolver(com.kloia.eventapis.cassandra.ConcurrencyResolver) SerializableConsumer(com.kloia.eventapis.kafka.SerializableConsumer) IdCreationStrategy(com.kloia.eventapis.api.IdCreationStrategy) Mock(org.mockito.Mock) EventKey(com.kloia.eventapis.common.EventKey) Entity(com.kloia.eventapis.view.Entity) DefaultConcurrencyResolver(com.kloia.eventapis.cassandra.DefaultConcurrencyResolver) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) EventRecorder(com.kloia.eventapis.common.EventRecorder) IOperationRepository(com.kloia.eventapis.kafka.IOperationRepository) Captor(org.mockito.Captor) Function(java.util.function.Function) Assert.assertThat(org.junit.Assert.assertThat) Mockito.doThrow(org.mockito.Mockito.doThrow) ArgumentCaptor(org.mockito.ArgumentCaptor) Matchers.eq(org.mockito.Matchers.eq) Map(java.util.Map) Matchers.anyLong(org.mockito.Matchers.anyLong) UUIDCreationStrategy(com.kloia.eventapis.api.impl.UUIDCreationStrategy) ExpectedException(org.junit.rules.ExpectedException) EventStoreException(com.kloia.eventapis.exception.EventStoreException) Before(org.junit.Before) InjectMocks(org.mockito.InjectMocks) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) PublishedEvent(com.kloia.eventapis.common.PublishedEvent) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ConcurrentEventException(com.kloia.eventapis.cassandra.ConcurrentEventException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.Test) EventState(com.kloia.eventapis.pojos.EventState) Mockito.when(org.mockito.Mockito.when) EventType(com.kloia.eventapis.common.EventType) Mockito.verify(org.mockito.Mockito.verify) Rule(org.junit.Rule) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) Views(com.kloia.eventapis.api.Views) Matchers.equalTo(org.hamcrest.Matchers.equalTo) EntityEvent(com.kloia.eventapis.cassandra.EntityEvent) Event(com.kloia.eventapis.pojos.Event) Optional(java.util.Optional) Mockito.mock(org.mockito.Mockito.mock) ConcurrencyResolver(com.kloia.eventapis.cassandra.ConcurrencyResolver) DefaultConcurrencyResolver(com.kloia.eventapis.cassandra.DefaultConcurrencyResolver) EntityEvent(com.kloia.eventapis.cassandra.EntityEvent) EventKey(com.kloia.eventapis.common.EventKey) Test(org.junit.Test)

Example 2 with EventStoreException

use of com.kloia.eventapis.exception.EventStoreException in project eventapis by kloiasoft.

the class ReturnPaymentCommand method execute.

@RequestMapping(value = "/payment/{paymentId}/return", method = RequestMethod.POST)
@Command
public EventKey execute(@PathVariable("paymentId") String paymentId, @RequestBody @Valid ReturnPaymentCommandDto dto) throws Exception {
    dto.setPaymentId(paymentId);
    Payment payment = paymentViewQuery.queryEntity(dto.getPaymentId());
    if (payment.getState() == PaymentState.PAID) {
        PaymentReturnedEvent paymentReturnedEvent = new PaymentReturnedEvent(payment.getOrderId(), payment.getAmount());
        return eventRepository.recordAndPublish(payment, paymentReturnedEvent);
    } else
        throw new EventStoreException("Payment state is not valid for this Operation: " + dto);
}
Also used : PaymentReturnedEvent(com.kloia.sample.dto.event.PaymentReturnedEvent) EventStoreException(com.kloia.eventapis.exception.EventStoreException) Payment(com.kloia.sample.model.Payment) Command(com.kloia.eventapis.api.Command) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with EventStoreException

use of com.kloia.eventapis.exception.EventStoreException in project eventapis by kloiasoft.

the class ProcessOrderCommand method execute.

@Override
public EventKey execute(@RequestBody ProcessOrderCommandDto dto) throws Exception {
    Orders order = orderQuery.queryEntity(dto.getOrderId());
    if (order.getState() == OrderState.INITIAL) {
        ReserveStockEvent reserveStockEvent = new ReserveStockEvent(order.getStockId(), order.getOrderAmount(), dto.getPaymentInformation());
        log.info("Template account saved: " + dto);
        return eventRepository.recordAndPublish(order, reserveStockEvent);
    } else
        throw new EventStoreException("Order state is not valid for this Operation: " + dto);
}
Also used : Orders(com.kloia.sample.model.Orders) EventStoreException(com.kloia.eventapis.exception.EventStoreException) ReserveStockEvent(com.kloia.sample.dto.event.ReserveStockEvent)

Example 4 with EventStoreException

use of com.kloia.eventapis.exception.EventStoreException in project eventapis by kloiasoft.

the class ProcessOrderCommand method process.

@RequestMapping(value = "/order/{orderId}/process", method = RequestMethod.POST)
@Command
public EventKey process(@PathVariable("orderId") String orderId, @RequestBody @Valid ProcessOrderCommandDto dto) throws Exception {
    dto.setOrderId(orderId);
    Orders order = orderQuery.queryEntity(dto.getOrderId());
    if (order.getState() == OrderState.INITIAL) {
        ReserveStockEvent reserveStockEvent = new ReserveStockEvent(order.getStockId(), order.getOrderAmount(), dto.getPaymentInformation());
        log.info("Template account saved: " + dto);
        return eventRepository.recordAndPublish(order, reserveStockEvent);
    } else
        throw new EventStoreException("Order state is not valid for this Operation: " + dto);
}
Also used : Orders(com.kloia.sample.model.Orders) EventStoreException(com.kloia.eventapis.exception.EventStoreException) ReserveStockEvent(com.kloia.sample.dto.event.ReserveStockEvent) Command(com.kloia.eventapis.api.Command) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with EventStoreException

use of com.kloia.eventapis.exception.EventStoreException in project eventapis by kloiasoft.

the class CompositeRepositoryImpl method recordAndPublishInternal.

private <P extends PublishedEvent, T extends Exception> EventKey recordAndPublishInternal(P publishedEvent, Optional<EventKey> previousEventKey, Function<EntityEvent, ConcurrencyResolver<T>> concurrencyResolverFactory) throws EventStoreException, T {
    long opDate = System.currentTimeMillis();
    EventKey eventKey = eventRecorder.recordEntityEvent(publishedEvent, opDate, previousEventKey, concurrencyResolverFactory);
    publishedEvent.setSender(eventKey);
    String event;
    try {
        event = objectMapper.writerWithView(Views.PublishedOnly.class).writeValueAsString(publishedEvent);
    } catch (JsonProcessingException e) {
        throw new EventStoreException(e.getMessage(), e);
    }
    operationRepository.publishEvent(publishedEvent.getClass().getSimpleName(), event, opDate);
    checkOperationFinalStates(publishedEvent);
    return publishedEvent.getSender();
}
Also used : EventStoreException(com.kloia.eventapis.exception.EventStoreException) Views(com.kloia.eventapis.api.Views) EventKey(com.kloia.eventapis.common.EventKey) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

EventStoreException (com.kloia.eventapis.exception.EventStoreException)10 EventKey (com.kloia.eventapis.common.EventKey)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 Views (com.kloia.eventapis.api.Views)4 Orders (com.kloia.sample.model.Orders)4 Row (com.datastax.driver.core.Row)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)2 Command (com.kloia.eventapis.api.Command)2 IdCreationStrategy (com.kloia.eventapis.api.IdCreationStrategy)2 UUIDCreationStrategy (com.kloia.eventapis.api.impl.UUIDCreationStrategy)2 ConcurrencyResolver (com.kloia.eventapis.cassandra.ConcurrencyResolver)2 ConcurrentEventException (com.kloia.eventapis.cassandra.ConcurrentEventException)2 DefaultConcurrencyResolver (com.kloia.eventapis.cassandra.DefaultConcurrencyResolver)2 EntityEvent (com.kloia.eventapis.cassandra.EntityEvent)2 EventRecorder (com.kloia.eventapis.common.EventRecorder)2 EventType (com.kloia.eventapis.common.EventType)2 PublishedEvent (com.kloia.eventapis.common.PublishedEvent)2 IOperationRepository (com.kloia.eventapis.kafka.IOperationRepository)2 SerializableConsumer (com.kloia.eventapis.kafka.SerializableConsumer)2