Skip to content
Snippets Groups Projects
Commit f67afbc4 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-5987 fix completing parent command

parent 46e98baf
Branches
Tags
No related merge requests found
...@@ -125,8 +125,7 @@ public class CommandService { ...@@ -125,8 +125,7 @@ public class CommandService {
} }
Optional.ofNullable(createdResource).ifPresentOrElse(resource -> repository.finishCommand(commandId, resource), Optional.ofNullable(createdResource).ifPresentOrElse(resource -> repository.finishCommand(commandId, resource),
() -> repository.finishCommand(commandId)); () -> repository.finishCommand(commandId));
// TODO complete parent command if flag set getCompletableParentId(command).ifPresent(this::publishCommandExecutedEvent);
getParentId(command).filter(parentId -> !repository.existsNotFinishedSubCommands(parentId)).ifPresent(this::publishCommandExecutedEvent);
} }
boolean isRevokeCommand(Command command) { boolean isRevokeCommand(Command command) {
...@@ -138,7 +137,8 @@ public class CommandService { ...@@ -138,7 +137,8 @@ public class CommandService {
} }
Optional<String> getCompletableParentId(Command command) { 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) { Optional<String> getParentId(Command command) {
......
...@@ -296,49 +296,32 @@ class CommandServiceTest { ...@@ -296,49 +296,32 @@ class CommandServiceTest {
} }
@Test @Test
void shouldCallGetParentId() { void shouldCallGetCompletableParentId() {
setCommandFinished(); setCommandFinished();
verify(service).getParentId(FINISHED_COMMAND); verify(service).getCompletableParentId(FINISHED_COMMAND);
} }
@Test @Test
void shouldCallPublishCommandExecutedEvent() { void shouldCallPublishCommandExecutedEvent() {
var parentId = "parent-id"; var parentId = "parent-id";
doReturn(Optional.of(parentId)).when(service).getParentId(any()); doReturn(Optional.of(parentId)).when(service).getCompletableParentId(any());
doReturn(true).when(service).isAllSubCommandsFinished(any());
doReturn(true).when(repository).isCompleteIfSubsCompleted(anyString());
setCommandFinished(); setCommandFinished();
verify(service).publishCommandExecutedEvent(parentId); verify(service).publishCommandExecutedEvent(parentId);
} }
@Nested @DisplayName("should not call publishCommandExecutedEvent when no completable parent id")
class TestNoCommandExecutedEvent {
@DisplayName("should not call publishCommandExecutedEvent when no parent id")
@Test @Test
void shouldNotPublishWhenNoParentId() { void shouldNotPublishWhenNoParentId() {
doReturn(Optional.empty()).when(service).getParentId(any()); doReturn(Optional.empty()).when(service).getCompletableParentId(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(); setCommandFinished();
verify(service, never()).publishCommandExecutedEvent(anyString()); verify(service, never()).publishCommandExecutedEvent(anyString());
} }
}
void setCommandFinished() { void setCommandFinished() {
service.setCommandFinished(CommandTestFactory.ID, CommandTestFactory.CREATED_RESOURCE); service.setCommandFinished(CommandTestFactory.ID, CommandTestFactory.CREATED_RESOURCE);
} }
...@@ -401,71 +384,36 @@ class CommandServiceTest { ...@@ -401,71 +384,36 @@ class CommandServiceTest {
} }
@Nested @Nested
class TestIsAllSubCommandsFinished { class TestGetCompletableParentId {
private static final Command COMMAND = CommandTestFactory.create(); private static final Command COMMAND = CommandTestFactory.create();
@Test @Test
void shouldCallGetParentId() { void shouldCallGetParentId() {
service.isAllSubCommandsFinished(COMMAND); service.getCompletableParentId(COMMAND);
verify(service).getParentId(COMMAND); verify(service).getParentId(COMMAND);
} }
@Test @Test
void shouldReturnFalseWhenNoParent() { void shouldCallIsCompleteIfSubsCompleted() {
doReturn(Optional.empty()).when(service).getParentId(any());
var result = isAllSubCommandsFinished();
assertThat(result).isTrue();
}
@Test
void shouldCallRepository() {
var parentId = "parent-id"; var parentId = "parent-id";
doReturn(Optional.of(parentId)).when(service).getParentId(any()); 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); service.getCompletableParentId(COMMAND);
verify(service).getParentId(COMMAND); verify(repository).isCompleteIfSubsCompleted(parentId);
} }
@Test @Test
void shouldCallRepository() { void shouldCallExistsNotFinishedSubCommands() {
var parentId = "parent-id"; var parentId = "parent-id";
doReturn(Optional.of(parentId)).when(service).getParentId(any()); doReturn(Optional.of(parentId)).when(service).getParentId(any());
when(repository.isCompleteIfSubsCompleted(anyString())).thenReturn(true);
service.getCompletableParentId(COMMAND); service.getCompletableParentId(COMMAND);
verify(repository).isCompleteIfSubsCompleted(parentId); verify(repository).existsNotFinishedSubCommands(parentId);
} }
@Test @Test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment