Hello, Allard,
Thanks for that pointer. This may have pointed me to discover a bug while testing this method of testing.
however, When I do:
@Before
public void setUp() {
GenericEventMessage.clock = Clock.fixed(Instant.ofEpochSecond(0L), ZoneId.systemDefault());
fixture = new AnnotatedSagaTestFixture<>(Clazz.class);
}
@After
public void tearDown() {
GenericEventMessage.clock = Clock.systemUTC();
}
The timestamps of the messages in the Saga (same when testing an aggregate) will still use the system clock. However, when I use the static message factory method, the time is set by the new clock:
EventMessage message = GenericEventMessage.asEventMessage(Boolean.FALSE);
log.info(“Message time : {}”, message.getTimestamp());
logs:
19:39:51.862 INFO o.o.a.command.account.AccountTest setUp 86 - Message time : 1970-01-01T00:00:00Z
Even, if I manually create the events with the factory class …
EventMessage message2 = GenericEventMessage.asEventMessage(event2);
The call
fixture.given(message2)…
Results in the @Timestamp in the aggregate event handler to return the current system time.
Maybe I have found a bug here…
I think this finally ends up to in:
public class GivenWhenThenTestFixture implements FixtureConfiguration, TestExecutor {
@Override
public TestExecutor given(List<?> domainEvents) {
ensureRepositoryConfiguration();
clearGivenWhenState();
try {
for (Object event : domainEvents) {
Object payload = event;
MetaData metaData = null;
if (event instanceof Message) {
payload = ((Message) event).getPayload();
metaData = ((Message) event).getMetaData();
}
this.givenEvents.add(new GenericDomainEventMessage<>(
aggregateType.getSimpleName(), aggregateIdentifier, sequenceNumber++, payload, metaData));
}
} catch (Exception e) {
FixtureResourceParameterResolverFactory.clear();
}
return this;
}
Here you can see, that the timestamp is ignored regardless of if this is instanceof Message or not. The constructor (new GenericDomainEventMessage<>() isthen calling Instant.now() to set the timesamp.
In this method should proably read something like this:
public class GivenWhenThenTestFixture implements FixtureConfiguration, TestExecutor {
@Override
public TestExecutor given(List<?> domainEvents) {
ensureRepositoryConfiguration();
clearGivenWhenState();
try {
for (Object event : domainEvents) {
Object payload = event;
MetaData metaData = null;
Instant timestamp = null;
if (event instanceof Message) {
payload = ((Message) event).getPayload();
metaData = ((Message) event).getMetaData();
timestamp= ((Message) event).getTimestamp();
} else {
timestamp = Instant.now()
}
this.givenEvents.add(new GenericDomainEventMessage<>(
aggregateType.getSimpleName(), aggregateIdentifier, sequenceNumber++, payload, metaData,timestamp));
}
} catch (Exception e) {
FixtureResourceParameterResolverFactory.clear();
}
return this;
}
Right ? (Sorry for my lack of making nicely formatted sourcecode here)
If so, We would be able to do eactly what I previously requested and generate individual events with different timestamps manually to model the time dependent history of an aggregate for testing. Neato!
Thanks again.
Dominic