From edfa3bd57506e1a4b3f7cb07ca0dbd0a4461a0d2 Mon Sep 17 00:00:00 2001
From: OZGCloud <ozgcloud@mgm-tp.com>
Date: Thu, 19 Oct 2023 17:29:41 +0200
Subject: [PATCH] OZG-4392 throw exception when command finished with error

---
 .../grpc/GrpcOzgCloudCommandService.java      |  5 +++
 .../grpc/GrpcOzgCloudCommandServiceTest.java  | 31 ++++++++++++-------
 2 files changed, 25 insertions(+), 11 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 37eed86..30e3171 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
@@ -1,5 +1,6 @@
 package de.ozgcloud.apilib.common.command.grpc;
 
+import de.itvsh.kop.common.errorhandling.TechnicalException;
 import de.itvsh.ozg.pluto.grpc.command.CommandServiceGrpc.CommandServiceBlockingStub;
 import de.itvsh.ozg.pluto.grpc.command.GrpcGetCommandRequest;
 import de.ozgcloud.apilib.common.callcontext.OzgCloudCallContextAttachingInterceptor;
@@ -7,6 +8,7 @@ import de.ozgcloud.apilib.common.callcontext.OzgCloudCallContextProvider;
 import de.ozgcloud.apilib.common.command.OzgCloudCommand;
 import de.ozgcloud.apilib.common.command.OzgCloudCommandId;
 import de.ozgcloud.apilib.common.command.OzgCloudCommandService;
+import de.ozgcloud.apilib.common.command.OzgCloudCommandStatus;
 import lombok.RequiredArgsConstructor;
 import net.devh.boot.grpc.client.inject.GrpcClient;
 
@@ -48,6 +50,9 @@ public class GrpcOzgCloudCommandService implements OzgCloudCommandService {
 				}
 			}
 		}
+		if (command.getStatus() == OzgCloudCommandStatus.ERROR) {
+			throw new TechnicalException("Command (id=%s) failed: %s".formatted(command.getId(), command.getErrorMessage()));
+		}
 		return command;
 	}
 
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 5b8713e..5ca125e 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
@@ -4,12 +4,14 @@ import static org.assertj.core.api.Assertions.*;
 import static org.mockito.ArgumentMatchers.*;
 import static org.mockito.Mockito.*;
 
+import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Nested;
 import org.junit.jupiter.api.Test;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 
+import de.itvsh.kop.common.errorhandling.TechnicalException;
 import de.itvsh.ozg.pluto.grpc.command.CommandServiceGrpc.CommandServiceBlockingStub;
 import de.ozgcloud.apilib.common.callcontext.OzgCloudCallContextProvider;
 import de.ozgcloud.apilib.common.command.OzgCloudCommand;
@@ -55,6 +57,23 @@ class GrpcOzgCloudCommandServiceTest {
 			verify(service).waitUntilDone(created);
 		}
 
+		@Test
+		void shouldTerminateByTimeout() {
+			var command = OzgCloudCommandTestFactory.createBuilder().status(OzgCloudCommandStatus.PENDING).build();
+			doReturn(command).when(service).reloadCommand(any());
+
+			service.waitUntilDone(command);
+
+			Mockito.verify(service, Mockito.times(2)).reloadCommand(command.getId());
+		}
+
+		@Test
+		void shouldThrowException() {
+			var command = OzgCloudCommandTestFactory.createBuilder().status(OzgCloudCommandStatus.ERROR).build();
+
+			Assertions.assertThrows(TechnicalException.class, () -> service.waitUntilDone(command));
+		}
+
 		@Nested
 		class Create {
 
@@ -100,14 +119,4 @@ class GrpcOzgCloudCommandServiceTest {
 		}
 	}
 
-	@Test
-	void shouldTerminateByTimeout() {
-		var command = OzgCloudCommandTestFactory.createBuilder().status(OzgCloudCommandStatus.PENDING).build();
-		doReturn(command).when(service).reloadCommand(any());
-
-		service.waitUntilDone(command);
-
-		Mockito.verify(service, Mockito.times(2)).reloadCommand(command.getId());
-	}
-
-}
+}
\ No newline at end of file
-- 
GitLab