@Test @SuppressWarnings("unchecked") public void aggregateReadWhenStateRequired() { Operation operation = new OperationHandlerOperation( (x, y) -> ImmutableList.of(new TestEvent("test")), true, false, ExpectedVersion.any()); DefaultCommand command = new DefaultCommand(repository, operation); when(repository.load(any())) .thenReturn(DefaultImmutableAggregate.fromExisting( mock(AggregateProjection.class), AGGREGATE_ID, 42, new TestState("test"))); command.setAggregateId(AGGREGATE_ID); command.run(); verify(repository).load(AGGREGATE_ID); }
when(repository.load(any())) .thenReturn(DefaultImmutableAggregate.fromExisting( mock(AggregateProjection.class),
DefaultImmutableAggregate sourceAggregate = DefaultImmutableAggregate.createNew(projection, AGGREGATE_ID); when(repository.load(any())).thenReturn(sourceAggregate); when(repository.append(any(), any(), any(), any())).thenReturn(newVersion); DefaultCommand command = new DefaultCommand(repository, operation);
when(repository.load(any())) .thenReturn(DefaultImmutableAggregate.fromExisting( mock(AggregateProjection.class),
@Test @SuppressWarnings("unchecked") public void idempotentCreateWorksIfConflictOnSave() { Operation operation = new OperationHandlerOperation( (x, y) -> ImmutableList.of("hello"), false, false, ExpectedVersion.notCreated()); when(repository.load(any())) .thenReturn(DefaultImmutableAggregate.fromExisting( mock(AggregateProjection.class), AGGREGATE_ID, 42, new TestState("test"))); DefaultCommand command = new DefaultCommand(repository, operation); command.setAggregateId(AGGREGATE_ID); command.setIdempotentCreate(true); command.setAtomic(false); when(repository.append(any(), any(), any(), any())) .thenThrow(new UnexpectedVersionException(42, ExpectedVersion.notCreated())); CommandResult commandResult = command.run(); Assert.assertTrue(commandResult.getEvents().isEmpty()); }
@NotNull private ImmutableAggregate<TState, TEvent> readAndValidateAggregate( final ExpectedVersion effectiveExpectedVersion) { ImmutableAggregate<TState, TEvent> aggregate = repository.load(aggregateId);
@NotNull private ImmutableAggregate<TState, TEvent> readAndValidateAggregate( final ExpectedVersion effectiveExpectedVersion) { ImmutableAggregate<TState, TEvent> aggregate = repository.load(aggregateId);
@Test @SuppressWarnings("unchecked") public void idempotentCreateMeansNoOpIfExisting() { Operation operation = new OperationHandlerOperation( (x, y) -> ImmutableList.of(new TestEvent("xxx")), false, false, ExpectedVersion.notCreated()); when(repository.load(any())) .thenReturn(DefaultImmutableAggregate.fromExisting( mock(AggregateProjection.class), AGGREGATE_ID, 42, new TestState("test"))); DefaultCommand command = new DefaultCommand(repository, operation); command.setAggregateId(AGGREGATE_ID); command.setIdempotentCreate(true); CommandResult commandResult = command.run(); Assert.assertTrue(commandResult.getEvents().isEmpty()); }