From f67afbc4dba59a06859ee180b3f41e1bbe1d4403 Mon Sep 17 00:00:00 2001
From: OZGCloud <ozgcloud@mgm-tp.com>
Date: Mon, 15 Jul 2024 06:06:36 +0200
Subject: [PATCH] OZG-5987 fix completing parent command

---
 .../vorgang/command/CommandService.java       |  6 +-
 .../vorgang/command/CommandServiceTest.java   | 84 ++++---------------
 2 files changed, 19 insertions(+), 71 deletions(-)

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 c1261858c..9f0582495 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 66338250b..0ad28f9b0 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
-- 
GitLab