From a53012756a5ab29fccd9d19eb8b69baf973a5bde Mon Sep 17 00:00:00 2001
From: OZGCloud <ozgcloud@mgm-tp.com>
Date: Tue, 24 Oct 2023 16:26:59 +0200
Subject: [PATCH] OZG-4392 [refactor] outsource command verification

---
 .../command/grpc/GrpcOzgCloudCommandService.java    | 10 +++++-----
 .../grpc/GrpcOzgCloudCommandServiceTest.java        | 13 ++++++-------
 2 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/api-lib-core/src/main/java/de/ozgcloud/apilib/common/command/grpc/GrpcOzgCloudCommandService.java b/api-lib-core/src/main/java/de/ozgcloud/apilib/common/command/grpc/GrpcOzgCloudCommandService.java
index 8e8c8c3..4a3d26f 100644
--- a/api-lib-core/src/main/java/de/ozgcloud/apilib/common/command/grpc/GrpcOzgCloudCommandService.java
+++ b/api-lib-core/src/main/java/de/ozgcloud/apilib/common/command/grpc/GrpcOzgCloudCommandService.java
@@ -50,9 +50,7 @@ public class GrpcOzgCloudCommandService implements OzgCloudCommandService {
 				}
 			}
 		}
-		if (hasErrorStatus(command)) {
-			throw new TechnicalException("Command (id=%s) failed: %s".formatted(command.getId(), command.getErrorMessage()));
-		}
+		verifyCommand(command);
 		return command;
 	}
 
@@ -65,7 +63,9 @@ public class GrpcOzgCloudCommandService implements OzgCloudCommandService {
 		return commandServiceStub.withInterceptors(new OzgCloudCallContextAttachingInterceptor(contextProvider));
 	}
 
-	boolean hasErrorStatus(OzgCloudCommand command) {
-		return command.getStatus() == OzgCloudCommandStatus.ERROR;
+	void verifyCommand(OzgCloudCommand command) {
+		if (command.getStatus() == OzgCloudCommandStatus.ERROR) {
+			throw new TechnicalException("Command (id=%s) failed: %s".formatted(command.getId(), command.getErrorMessage()));
+		}
 	}
 }
diff --git a/api-lib-core/src/test/java/de/ozgcloud/apilib/common/command/grpc/GrpcOzgCloudCommandServiceTest.java b/api-lib-core/src/test/java/de/ozgcloud/apilib/common/command/grpc/GrpcOzgCloudCommandServiceTest.java
index 23eac1e..f032df3 100644
--- a/api-lib-core/src/test/java/de/ozgcloud/apilib/common/command/grpc/GrpcOzgCloudCommandServiceTest.java
+++ b/api-lib-core/src/test/java/de/ozgcloud/apilib/common/command/grpc/GrpcOzgCloudCommandServiceTest.java
@@ -1,6 +1,7 @@
 package de.ozgcloud.apilib.common.command.grpc;
 
 import static org.assertj.core.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.*;
 import static org.mockito.ArgumentMatchers.*;
 import static org.mockito.Mockito.*;
 
@@ -68,10 +69,10 @@ class GrpcOzgCloudCommandServiceTest {
 		}
 
 		@Test
-		void shouldThrowException() {
-			var command = OzgCloudCommandTestFactory.createBuilder().status(OzgCloudCommandStatus.ERROR).build();
+		void shouldCallVerifyCommand() {
+			service.waitUntilDone(command);
 
-			Assertions.assertThrows(TechnicalException.class, () -> service.waitUntilDone(command));
+			verify(service).verifyCommand(command);
 		}
 
 		@Nested
@@ -123,12 +124,10 @@ class GrpcOzgCloudCommandServiceTest {
 	class TestStatus {
 
 		@Test
-		void shouldReturnTrueByError() {
+		void shouldThrowException() {
 			var command = OzgCloudCommandTestFactory.createBuilder().status(OzgCloudCommandStatus.ERROR).build();
 
-			var result = service.hasErrorStatus(command);
-
-			assertThat(result).isTrue();
+			assertThrows(TechnicalException.class, () ->  service.verifyCommand(command));
 		}
 
 	}
-- 
GitLab