DefaultEventStore extends DefaultRecordBasedRepository<EventId, EEntity, Event> implements EventStore { @Override public void appendAll(Iterable<Event> events) { checkNotNull(events); ImmutableList<Event> eventList = Streams.stream(events) .filter(Objects::nonNull) .collect(toImmutableList()); if (eventList.isEmpty()) { return; } Event event = eventList.get(0); TenantAwareOperation op = new EventOperation(event) { @Override public void run() { if (isTenantSet()) { ensureSameTenant(eventList); } store(eventList); } }; op.execute(); log.stored(events); } DefaultEventStore(); @Override @SuppressWarnings({"OverridingMethodsMustInvokeSuper", "MissingSuperCall"}) // see impl. note void registerWith(BoundedContext context); @Override void append(Event event); @Override void appendAll(Iterable<Event> events); @Override void read(EventStreamQuery request, StreamObserver<Event> responseObserver);  }
@Test @DisplayName("do nothing when appending empty iterable") void processEmptyIterable() { eventStore.appendAll(Collections.emptySet()); }
@Test @DisplayName("fail to store events of different tenants in single operation") void rejectEventsFromDifferentTenants() { TenantId firstTenantId = TenantId .newBuilder() .setValue("abc") .buildPartial(); TenantId secondTenantId = TenantId .newBuilder() .setValue("xyz") .buildPartial(); ActorContext firstTenantActor = ActorContext .newBuilder() .setTenantId(firstTenantId) .buildPartial(); ActorContext secondTenantActor = ActorContext .newBuilder() .setTenantId(secondTenantId) .buildPartial(); Origin firstTenantOrigin = Origin .newBuilder() .setActorContext(firstTenantActor) .buildPartial(); Origin secondTenantOrigin = Origin .newBuilder() .setActorContext(secondTenantActor) .buildPartial(); EventContext firstTenantContext = EventContext .newBuilder() .setPastMessage(firstTenantOrigin) .buildPartial(); EventContext secondTenantContext = EventContext .newBuilder() .setPastMessage(secondTenantOrigin) .buildPartial(); Event firstTenantEvent = Event .newBuilder() .setContext(firstTenantContext) .buildPartial(); Event secondTenantEvent = Event .newBuilder() .setContext(secondTenantContext) .buildPartial(); Collection<Event> event = ImmutableSet.of(firstTenantEvent, secondTenantEvent); assertThrows(IllegalArgumentException.class, () -> eventStore.appendAll(event)); }