DefaultCommand command = new DefaultCommand(repository, operation); command.setAggregateId(AGGREGATE_ID); command.setIdempotentCreate(true); CommandResult commandResult = 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 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()); }