diff --git a/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/command/CommandService.java b/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/command/CommandService.java index c1261858c43912ce4d72e92b6a4a4ca1e0949254..9f0582495ee6494c9afff898e57792162eb0c8aa 100644 --- a/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/command/CommandService.java +++ b/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/command/CommandService.java @@ -125,8 +125,7 @@ public class CommandService { } Optional.ofNullable(createdResource).ifPresentOrElse(resource -> repository.finishCommand(commandId, resource), () -> repository.finishCommand(commandId)); - // TODO complete parent command if flag set - getParentId(command).filter(parentId -> !repository.existsNotFinishedSubCommands(parentId)).ifPresent(this::publishCommandExecutedEvent); + getCompletableParentId(command).ifPresent(this::publishCommandExecutedEvent); } boolean isRevokeCommand(Command command) { @@ -138,7 +137,8 @@ public class CommandService { } Optional<String> getCompletableParentId(Command command) { - return getParentId(command).filter(repository::isCompleteIfSubsCompleted); + return getParentId(command).filter(repository::isCompleteIfSubsCompleted) + .filter(parentId -> !repository.existsNotFinishedSubCommands(parentId)); } Optional<String> getParentId(Command command) { diff --git a/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/command/CommandServiceTest.java b/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/command/CommandServiceTest.java index 66338250b079af1e482c5393aae048be8f535fa0..0ad28f9b06d19e17b21bdd613e4e010cac0c32b1 100644 --- a/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/command/CommandServiceTest.java +++ b/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/command/CommandServiceTest.java @@ -296,47 +296,30 @@ class CommandServiceTest { } @Test - void shouldCallGetParentId() { + void shouldCallGetCompletableParentId() { setCommandFinished(); - verify(service).getParentId(FINISHED_COMMAND); + verify(service).getCompletableParentId(FINISHED_COMMAND); } @Test void shouldCallPublishCommandExecutedEvent() { var parentId = "parent-id"; - doReturn(Optional.of(parentId)).when(service).getParentId(any()); - doReturn(true).when(service).isAllSubCommandsFinished(any()); - doReturn(true).when(repository).isCompleteIfSubsCompleted(anyString()); + doReturn(Optional.of(parentId)).when(service).getCompletableParentId(any()); setCommandFinished(); verify(service).publishCommandExecutedEvent(parentId); } - @Nested - class TestNoCommandExecutedEvent { - - @DisplayName("should not call publishCommandExecutedEvent when no parent id") - @Test - void shouldNotPublishWhenNoParentId() { - doReturn(Optional.empty()).when(service).getParentId(any()); - - setCommandFinished(); - - verify(service, never()).publishCommandExecutedEvent(anyString()); - } - - @DisplayName("should not publish CommandExecutedEvent when parent command not completable by sub commands") - @Test - void shouldNotPublishWhenNotCompletable() { - doReturn(Optional.of("parent-id")).when(service).getParentId(any()); - - setCommandFinished(); + @DisplayName("should not call publishCommandExecutedEvent when no completable parent id") + @Test + void shouldNotPublishWhenNoParentId() { + doReturn(Optional.empty()).when(service).getCompletableParentId(any()); - verify(service, never()).publishCommandExecutedEvent(anyString()); - } + setCommandFinished(); + verify(service, never()).publishCommandExecutedEvent(anyString()); } void setCommandFinished() { @@ -401,71 +384,36 @@ class CommandServiceTest { } @Nested - class TestIsAllSubCommandsFinished { + class TestGetCompletableParentId { private static final Command COMMAND = CommandTestFactory.create(); @Test void shouldCallGetParentId() { - service.isAllSubCommandsFinished(COMMAND); + service.getCompletableParentId(COMMAND); verify(service).getParentId(COMMAND); } @Test - void shouldReturnFalseWhenNoParent() { - doReturn(Optional.empty()).when(service).getParentId(any()); - - var result = isAllSubCommandsFinished(); - - assertThat(result).isTrue(); - } - - @Test - void shouldCallRepository() { + void shouldCallIsCompleteIfSubsCompleted() { var parentId = "parent-id"; doReturn(Optional.of(parentId)).when(service).getParentId(any()); - isAllSubCommandsFinished(); - - verify(repository).existsNotFinishedSubCommands(parentId); - } - - @Test - void shouldReturnResult() { - doReturn(Optional.of("parent-id")).when(service).getParentId(any()); - when(repository.existsNotFinishedSubCommands(any())).thenReturn(true); - - var result = isAllSubCommandsFinished(); - - assertThat(result).isFalse(); - } - - boolean isAllSubCommandsFinished() { - return service.isAllSubCommandsFinished(COMMAND); - } - } - - @Nested - class TestGetCompletableParentId { - - private static final Command COMMAND = CommandTestFactory.create(); - - @Test - void shouldCallGetParentId() { service.getCompletableParentId(COMMAND); - verify(service).getParentId(COMMAND); + verify(repository).isCompleteIfSubsCompleted(parentId); } @Test - void shouldCallRepository() { + void shouldCallExistsNotFinishedSubCommands() { var parentId = "parent-id"; doReturn(Optional.of(parentId)).when(service).getParentId(any()); + when(repository.isCompleteIfSubsCompleted(anyString())).thenReturn(true); service.getCompletableParentId(COMMAND); - verify(repository).isCompleteIfSubsCompleted(parentId); + verify(repository).existsNotFinishedSubCommands(parentId); } @Test