EventStore { public Optional<AggregateRootType> findById(String identifier) { Optional<Snapshot<AggregateRootType>> retrievedSnapshot = retrieveSnapshot(identifier); if (retrievedSnapshot.isPresent()) { Snapshot<AggregateRootType> snapshot = retrievedSnapshot.get(); Collection<DomainEvent> allEvents = eventRepository.findAllByIdAndAfterRevision(identifier, snapshot.getRevision()); return Optional.ofNullable(objectConstructor.applyDomainEvents(snapshot.getAggregateRoot(), allEvents)); } else { Collection<DomainEvent> allEvents = eventRepository.findAllById(identifier); return Optional.ofNullable(objectConstructor.construct(allEvents)); } } EventStore(Class<AggregateRootType> type, EventRepository eventRepository); Optional<AggregateRootType> findById(String identifier); void setSnapshotRepository(SnapshotRepository<AggregateRootType> snapshotRepository);  }
@Test public void whenNotFindingAnyEvents_shouldReturnNull() throws Exception { Optional<MyAggregateRoot> aggregateRoot = eventStore.findById("nonExisting"); assertThat(aggregateRoot.orElse(null), nullValue()); }
@Test public void whenCallingFind_shouldReturnAggregateIfEventsAreFound() throws Exception { MyAggregateRootCreatedEvent createdEvent = new MyAggregateRootCreatedEvent("id", "name"); inMemoryEventRepository.save(createdEvent); Optional<MyAggregateRoot> aggregate = eventStore.findById(createdEvent.getAggregateRootIdentifier()); assertThat(aggregate.get().name, equalTo(createdEvent.getName())); }