@Override public void execute(Consumer<AR> invocation) { wrappedAggregate.execute(invocation); }
@Override public ResultValidator<T> expectState(Consumer<T> aggregateStateValidator) { DefaultUnitOfWork<Message<?>> uow = DefaultUnitOfWork.startAndGet(null); try { state.get().execute(aggregateStateValidator); } finally { uow.rollback(); } return this; }
@CommandHandler public void handleStubAggregateLooping(LoopingCommand command) { repository.load(command.getAggregateId().toString()) .execute(StubAggregate::makeALoopingChange); }
@CommandHandler public void handleProblematicCommand(ProblematicCommand command) { repository.load(command.getAggregateId().toString(), command.getAggregateVersion()) .execute(StubAggregate::causeTrouble); }
@CommandHandler public void handleStubAggregateUpdated(UpdateStubAggregateCommand command) { repository.load(command.getAggregateId().toString(), command.getAggregateVersion()) .execute(StubAggregate::makeAChange); }
@CommandHandler public void handleStubAggregateUpdatedWithExtraEvent(UpdateStubAggregateWithExtraEventCommand command, UnitOfWork<CommandMessage<?>> unitOfWork) { Aggregate<StubAggregate> aggregate = repository.load(command.getAggregateId().toString()); aggregate.execute(StubAggregate::makeAChange); eventBus.publish(new GenericEventMessage<>(new MyEvent())); aggregate.execute(StubAggregate::makeAChange); }
@Test public void testDeleteAnAggregate() { JpaAggregate agg = new JpaAggregate("First message"); entityManager.persist(agg); entityManager.flush(); entityManager.clear(); assertEquals((Long) 0L, agg.getVersion()); UnitOfWork<?> uow = startAndGetUnitOfWork(); Aggregate<JpaAggregate> aggregate = repository.load(agg.getIdentifier()); aggregate.execute(r -> r.setMessage("And again")); aggregate.execute(r -> r.setMessage("And more")); aggregate.execute(JpaAggregate::delete); uow.commit(); entityManager.flush(); entityManager.clear(); assertEquals(2, capturedEvents.size()); assertNull(entityManager.find(JpaAggregate.class, aggregate.identifierAsString())); }
@Test public void testUpdateAnAggregate() { JpaAggregate agg = new JpaAggregate("First message"); entityManager.persist(agg); entityManager.flush(); entityManager.clear(); UnitOfWork<?> uow = startAndGetUnitOfWork(); Aggregate<JpaAggregate> aggregate = repository.load(agg.getIdentifier()); aggregate.execute(r -> r.setMessage("And again")); aggregate.execute(r -> r.setMessage("And more")); uow.commit(); assertEquals((Long) 1L, aggregate.version()); assertEquals(2, capturedEvents.size()); assertNotNull(entityManager.find(JpaAggregate.class, aggregate.identifierAsString())); }
@CommandHandler @SuppressWarnings("unchecked") public void changeCounter(ChangeCounterCommand command) { repository.load(command.getAggregateId()).execute(r -> r.setCounter(command.getNewValue())); } }
@CommandHandler public MyAggregate handle(MyCommand command) throws Exception { Aggregate<MyAggregate> aggregate; try { aggregate = repository.load(command.getAggregateIdentifier()); } catch (AggregateNotFoundException e) { aggregate = repository.newInstance(() -> new MyAggregate(command.getAggregateIdentifier())); } aggregate.execute(a -> a.handle(command, commandGateway)); return aggregate.invoke(Function.identity()); } }
@Override public void execute(Consumer<AR> invocation) { wrappedAggregate.execute(invocation); }