@Test public void testLoadAndSaveAggregate() { UUID identifier = UUID.randomUUID(); DomainEventMessage event1 = new GenericDomainEventMessage<String>( identifier, (long) 1, "Mock contents", MetaData.emptyInstance()); DomainEventMessage event2 = new GenericDomainEventMessage<String>( identifier, (long) 2, "Mock contents", MetaData.emptyInstance()); when(mockEventStore.readEvents("test", identifier)) .thenReturn(new SimpleDomainEventStream(event1, event2)); TestAggregate aggregate = testSubject.load(identifier, null); assertEquals(0, aggregate.getUncommittedEventCount()); assertEquals(2, aggregate.getHandledEvents().size()); assertSame(event1, aggregate.getHandledEvents().get(0)); assertSame(event2, aggregate.getHandledEvents().get(1)); // now the aggregate is loaded (and hopefully correctly locked) StubDomainEvent event3 = new StubDomainEvent(); aggregate.apply(event3); CurrentUnitOfWork.commit(); verify(mockEventBus).publish(isA(DomainEventMessage.class)); verify(mockEventBus, never()).publish(event1); verify(mockEventBus, never()).publish(event2); verify(mockEventStore, times(1)).appendEvents(eq("test"), isA(DomainEventStream.class)); assertEquals(0, aggregate.getUncommittedEventCount()); }
@Test public void testLoadEventsWithDecorators() { UUID identifier = UUID.randomUUID(); SpyEventPreprocessor decorator1 = new SpyEventPreprocessor(); SpyEventPreprocessor decorator2 = new SpyEventPreprocessor(); testSubject.setEventStreamDecorators(Arrays.asList(decorator1, decorator2)); when(mockEventStore.readEvents("test", identifier)) .thenReturn( new SimpleDomainEventStream( new GenericDomainEventMessage<String>( identifier, (long) 1, "Mock contents", MetaData.emptyInstance()), new GenericDomainEventMessage<String>( identifier, (long) 2, "Mock contents", MetaData.emptyInstance()), new GenericDomainEventMessage<String>( identifier, (long) 3, "Mock contents", MetaData.emptyInstance()))); TestAggregate aggregate = testSubject.load(identifier); // loading them in... InOrder inOrder = Mockito.inOrder(decorator1.lastSpy, decorator2.lastSpy); inOrder.verify(decorator2.lastSpy).next(); inOrder.verify(decorator1.lastSpy).next(); inOrder.verify(decorator2.lastSpy).next(); inOrder.verify(decorator1.lastSpy).next(); inOrder.verify(decorator2.lastSpy).next(); inOrder.verify(decorator1.lastSpy).next(); aggregate.apply(new StubDomainEvent()); aggregate.apply(new StubDomainEvent()); }
@Test public void testLoadAndSaveWithConflictingChanges() { ConflictResolver conflictResolver = mock(ConflictResolver.class); UUID identifier = UUID.randomUUID(); DomainEventMessage event2 = new GenericDomainEventMessage<String>( identifier, (long) 2, "Mock contents", MetaData.emptyInstance()); DomainEventMessage event3 = new GenericDomainEventMessage<String>( identifier, (long) 3, "Mock contents", MetaData.emptyInstance()); when(mockEventStore.readEvents("test", identifier)) .thenReturn( new SimpleDomainEventStream( new GenericDomainEventMessage<String>( identifier, (long) 1, "Mock contents", MetaData.emptyInstance()), event2, event3)); testSubject.setConflictResolver(conflictResolver); TestAggregate actual = testSubject.load(identifier, 1L); verify(conflictResolver, never()) .resolveConflicts(anyListOf(DomainEventMessage.class), anyListOf(DomainEventMessage.class)); final StubDomainEvent appliedEvent = new StubDomainEvent(); actual.apply(appliedEvent); CurrentUnitOfWork.commit(); verify(conflictResolver) .resolveConflicts(payloadsEqual(appliedEvent), eq(Arrays.asList(event2, event3))); }
@Test public void testSaveEventsWithDecorators() { SpyEventPreprocessor decorator1 = new SpyEventPreprocessor(); SpyEventPreprocessor decorator2 = new SpyEventPreprocessor(); testSubject.setEventStreamDecorators(Arrays.asList(decorator1, decorator2)); testSubject.setEventStore( new EventStore() { @Override public void appendEvents(String type, DomainEventStream events) { while (events.hasNext()) { events.next(); } } @Override public DomainEventStream readEvents(String type, Object identifier) { return mockEventStore.readEvents(type, identifier); } }); UUID identifier = UUID.randomUUID(); when(mockEventStore.readEvents("test", identifier)) .thenReturn( new SimpleDomainEventStream( new GenericDomainEventMessage<String>( identifier, (long) 3, "Mock contents", MetaData.emptyInstance()))); TestAggregate aggregate = testSubject.load(identifier); aggregate.apply(new StubDomainEvent()); aggregate.apply(new StubDomainEvent()); CurrentUnitOfWork.commit(); InOrder inOrder = Mockito.inOrder(decorator1.lastSpy, decorator2.lastSpy); inOrder.verify(decorator1.lastSpy).next(); inOrder.verify(decorator2.lastSpy).next(); inOrder.verify(decorator1.lastSpy).next(); inOrder.verify(decorator2.lastSpy).next(); }