Hi,
I have a problem when trying to write a fixture for a command handler
dealing with (well, "loading" actually) more than one AR.
Let's say I have a StartSellingProductCommand, consisting of a Shop
id, a Product id, and a price.
The CommandHandler will load the two AR from their repositories, and
then invoke something like shop.startSelling(product, price).
So, Shop will collaborate with Product to check that it's actually ok
to sell it (just a simple business rule). Nothing guarantees me that
the Product instance being used is really the latest version
available, since it's another AR and therefore outside of the Shop's
consistency boundaries. But let's assume it's okay for me.
Btw, we see that the only AR really being manipulated (and ultimately
raising events, here the StartedSellingProductEvent) is the Shop.
Ok, I want to make a fixture to test that.
So I'm doing something like :
@Test
public void testStartSellingProduct () {
StartSellingProductCommand cmd = new StartSellingProductCommand();
cmd.shopId = fixture.getAggregateIdentifier().asString();
cmd.productId = "PRODUCT-1";
cmd.price = 500;
fixture.given(new ShopRegisterdEvent("Paris shop"),
new ProductLaunchedEvent("My great product"))
.when(cmd)
.expectEvents(new StartedSellingProductEvent("PRODUCT-1", 500));
}
Thing is... when instanciating those events, I never need to (can ?)
give the ID of the AR which have generated them.
Here, I would need to say "OK, the ProductLaunchedEvent was generated
by the AR with the Id 'PRODUCT-1'", so that this AR could be actually
loaded from the repo.
When it comes to the "main" AR being manipulated by the handler tested
by the fixture (Shop), it's okay : it's the fixture that chooses the
AR Id, and then I can retrieve it using
fixture.getAggregateIdentifier().
But what about the other ARs which need to be loaded from their
repositories by the Command Handler ?
Who should choose their IDs ? And put them into the events ?
Or all this is just an invalid use case of a fixture ?
Not sure I'm being clear here. But any help is welcome
Thanx,
Eric