Pergunta

I'm learning CQRS recently, so I started a sample project with axon-framework(A java CRQS framework).

According to the quick start, I got this below:

public class CreditEntryUnitTests {

    private FixtureConfiguration fixture;

    @Before
    public void setUp() throws Exception {
        fixture = Fixtures.newGivenWhenThenFixture(CreditEntry.class);
    }

    @Test
    public void creditEntryCreated() throws Throwable {
        final Long entryId = 1L;
        final int amount = 100;

        fixture.given().when(new CreateCreditEntryCommand(entryId, amount))
            .expectEvents(new CreditEntryCreatedEvent(entryId, amount));
    }

    @Test
    public void creditEntryMadeEffective() throws Throwable {
        final Long entryId = 1L;
        final int amount = 100;
        final Date start = nov(2011, 12);
        final Date end = nov(2012, 12);// a year effective period

        fixture.given(new CreditEntryCreatedEvent(entryId, amount))
            .when(new MakeCreditEntryEffectiveCommand(entryId, start, end))
            .expectEvents(new CreditEntryMadeEffectiveEvent(entryId, start, end));
    }

    //omitted support methods
}

public class CreditEntry extends AbstractAnnotatedAggregateRoot {

    @AggregateIdentifier
    private Long id;
    private int amount;
    private Date effectiveDateRangeStart;
    private Date effectiveDateRangeEnd;
    private Status status;

    @CommandHandler
    public CreditEntry(CreateCreditEntryCommand command) {
        apply(new CreditEntryCreatedEvent(
            command.getEntryId(), command.getAmount()));
    }

    @EventHandler
    public void on(CreditEntryCreatedEvent event) {
        this.id = event.getEntryId();
        this.amount = event.getAmount();
        this.status = Status.NEW;
    }

    @CommandHandler
    public void markCompleted(MakeCreditEntryEffectiveCommand command) {
        apply(new CreditEntryMadeEffectiveEvent(
            command.getEntryId(), command.getStart(), command.getEnd()));
    }

    @EventHandler
    public void on(CreditEntryMadeEffectiveEvent event) {
        this.effectiveDateRangeStart = event.getStart();
        this.effectiveDateRangeEnd = event.getEnd();
        this.status = Status.EFFECTIVE;
    }

    public CreditEntry() {}

    public enum Status {
        NEW, EFFECTIVE, EXPIRED
    }
}

The test code drives me written the domain model and integration code with axon-framework but it doesn't cover what side effect the event made. Where did I test them? e.g. when made effective the credit entry's status should be effective. Should I create a CreditEntry instance in other test methods and test by calling specific on(...Event event) method?

And one more question is: where should I put business validation logic? In command handler method? Assuming if the CreditEntry can not be made effective again given it is effective already.

@CommandHandler
public void markCompleted(MakeCreditEntryEffectiveCommand command) {
    if (is(NEW)) {
        apply(new CreditEntryMadeEffectiveEvent(
            command.getEntryId(), command.getStart(), command.getEnd()));
    } else {
        throw new IllegalStateException(.......);
    }
}

Any idea is appreciate, thank you.

Foi útil?

Solução

On your first question: Do you mean by side effect the internal state of your aggregate object? The Given-When-Then fixture test treat the aggregate as a kind of black box. So indeed, there is no real need to test the internal state. It is only important that the right events are applied.

So for example, you might even end up with aggregates without any fields (expect the ID) as your decision logic does not depend on any internal state. As a rule of thumb, I only save data transported in an event in the aggregate object if I need it later to decide which events to apply or if it changes the data applied in an event.

If you keep that in mind, you don't really have to test the internal state. You simply configure an aggregate with specific events in the given clause (build up some state) and then apply a command. If the correct events come out... you're done.

On your second question: Business validation should go in the command handler. So everything should be validated before the applymethod is called. One reason for this: Imagine a system in which the validation logic changes over the life time, but you have to deal with old data which was entered when the system was introduced. If the validation would be in the event handler and the validation is not the same as when the event was first introduced, loading your aggregate from the events might fail as the "old" data does not match to the current validation logic.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top