@Test
  public void testDomainEventEntry_WrapEventsCorrectly() {
    Serializer serializer = new XStreamSerializer();

    String payload = "Payload";
    MetaData metaData = new MetaData(Collections.singletonMap("Key", "Value"));
    DomainEventMessage<String> event =
        new GenericDomainEventMessage<>(
            "type",
            randomUUID().toString(),
            2L,
            payload,
            metaData,
            randomUUID().toString(),
            Instant.now());

    DomainEventEntry eventEntry = new DomainEventEntry(event, serializer);

    assertEquals(event.getAggregateIdentifier(), eventEntry.getAggregateIdentifier());
    assertEquals(event.getSequenceNumber(), eventEntry.getSequenceNumber());
    assertEquals(event.getTimestamp(), eventEntry.getTimestamp());
    assertEquals(payload, serializer.deserialize(eventEntry.getPayload()));
    assertEquals(metaData, serializer.deserialize(eventEntry.getMetaData()));
    assertEquals(byte[].class, eventEntry.getPayload().getContentType());
  }
 /**
  * Constructor used to create a new event entry to store in Mongo.
  *
  * @param eventSerializer Serializer to use for the event to store
  * @param events The events contained in this commit
  */
 private CommitEntry(Serializer eventSerializer, List<DomainEventMessage<?>> events) {
   this.aggregateIdentifier = events.get(0).getAggregateIdentifier();
   this.firstSequenceNumber = events.get(0).getSequenceNumber();
   this.firstTimestamp = events.get(0).getTimestamp().toEpochMilli();
   final DomainEventMessage lastEvent = events.get(events.size() - 1);
   this.lastTimestamp = lastEvent.getTimestamp().toEpochMilli();
   this.lastSequenceNumber = lastEvent.getSequenceNumber();
   eventEntries = new EventEntry[events.size()];
   for (int i = 0, eventsLength = events.size(); i < eventsLength; i++) {
     DomainEventMessage event = events.get(i);
     eventEntries[i] = new EventEntry(eventSerializer, event);
   }
 }
    private EventEntry(Serializer serializer, DomainEventMessage event) {
      this.eventIdentifier = event.getIdentifier();
      Class<?> serializationTarget = String.class;
      if (serializer.canSerializeTo(DBObject.class)) {
        serializationTarget = DBObject.class;
      }
      SerializedObject serializedPayloadObject =
          serializePayload(event, serializer, serializationTarget);
      SerializedObject serializedMetaDataObject =
          serializeMetaData(event, serializer, serializationTarget);

      this.serializedPayload = serializedPayloadObject.getData();
      this.payloadType = serializedPayloadObject.getType().getName();
      this.payloadRevision = serializedPayloadObject.getType().getRevision();
      this.serializedMetaData = serializedMetaDataObject.getData();
      this.sequenceNumber = event.getSequenceNumber();
      this.timestamp = event.getTimestamp().toEpochMilli();
    }
  @Test
  public void testAggregateIdentifier_IdentifierAutomaticallyDeducted() {
    fixture.registerAggregateFactory(mockAggregateFactory);
    fixture.registerAnnotatedCommandHandler(
        new MyCommandHandler(fixture.getRepository(), fixture.getEventBus()));
    fixture
        .given(new MyEvent("AggregateId", 1), new MyEvent("AggregateId", 2))
        .when(new TestCommand("AggregateId"))
        .expectEvents(new MyEvent("AggregateId", 3));

    DomainEventStream events = fixture.getEventStore().readEvents("AggregateId");
    for (int t = 0; t < 3; t++) {
      assertTrue(events.hasNext());
      DomainEventMessage next = events.next();
      assertEquals("AggregateId", next.getAggregateIdentifier());
      assertEquals(t, next.getSequenceNumber());
    }
  }