CommandResult commandResult = command.run();
command.setAggregateId(AGGREGATE_ID); command.setIdempotentCreate(true); CommandResult commandResult = command.run();
command.run();
@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()); }
@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); }
@Test(expected = InvalidCommandException.class) @SuppressWarnings("unchecked") public void conflictingVersionsGiveInvalidCommand() { PowerMockito.mockStatic(ExpectedVersion.class); PowerMockito.when(ExpectedVersion.merge(any(), any())) .thenThrow(new ConflictingExpectedVersionsException( "error", null, null)); Operation operation = new OperationHandlerOperation( (x, y) -> null, true, false, ExpectedVersion.exactly(42)); DefaultCommand command = new DefaultCommand(repository, operation); command.setAggregateId(AGGREGATE_ID); command.run(); }
@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()); }