From 292174d988e7d08bdfd412f969715d8ca953cf03 Mon Sep 17 00:00:00 2001 From: OZGCloud <ozgcloud@mgm-tp.com> Date: Tue, 19 Nov 2024 16:20:44 +0100 Subject: [PATCH] OZG-7037 add getPendingCommands endpoint --- .../command/OzgCloudCommandService.java | 6 ++ .../grpc/GrpcOzgCloudCommandService.java | 14 +++- ...cGetPendingCommandsRequestTestFactory.java | 20 +++++ .../grpc/GrpcOzgCloudCommandServiceTest.java | 84 +++++++++++++++++-- 4 files changed, 117 insertions(+), 7 deletions(-) create mode 100644 api-lib-core/src/test/java/de/ozgcloud/apilib/common/command/grpc/GrpcGetPendingCommandsRequestTestFactory.java diff --git a/api-lib-core/src/main/java/de/ozgcloud/apilib/common/command/OzgCloudCommandService.java b/api-lib-core/src/main/java/de/ozgcloud/apilib/common/command/OzgCloudCommandService.java index 1cecb57..2beb357 100644 --- a/api-lib-core/src/main/java/de/ozgcloud/apilib/common/command/OzgCloudCommandService.java +++ b/api-lib-core/src/main/java/de/ozgcloud/apilib/common/command/OzgCloudCommandService.java @@ -2,6 +2,8 @@ package de.ozgcloud.apilib.common.command; import java.util.List; +import de.ozgcloud.apilib.vorgang.OzgCloudVorgangId; + public interface OzgCloudCommandService { OzgCloudCommand create(OzgCloudCommand commandToCreate); @@ -9,4 +11,8 @@ public interface OzgCloudCommandService { OzgCloudCommand createAndWaitUntilDone(OzgCloudCommand commandToCreate); List<OzgCloudCommand> addSubCommands(OzgCloudCreateSubCommandsRequest request); + + boolean existsPendingCommands(OzgCloudVorgangId vorgangId); + + List<OzgCloudCommand> getPendingCommands(OzgCloudVorgangId vorgangId); } 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 7c8c8c3..1094020 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 @@ -14,6 +14,7 @@ import de.ozgcloud.common.errorhandling.TechnicalException; import de.ozgcloud.vorgang.grpc.command.CommandServiceGrpc.CommandServiceBlockingStub; import de.ozgcloud.vorgang.grpc.command.GrpcExistsPendingCommandsRequest; import de.ozgcloud.vorgang.grpc.command.GrpcGetCommandRequest; +import de.ozgcloud.vorgang.grpc.command.GrpcGetPendingCommandsRequest; import lombok.RequiredArgsConstructor; import net.devh.boot.grpc.client.inject.GrpcClient; @@ -77,14 +78,23 @@ public class GrpcOzgCloudCommandService implements OzgCloudCommandService { } public boolean existsPendingCommands(OzgCloudVorgangId vorgangId) { - var response = getCommandServiceStub().existsPendingCommands(buildHasPendingCommandRequest(vorgangId)); + var response = getCommandServiceStub().existsPendingCommands(buildExistsPendingCommandRequest(vorgangId)); return response.getExistsPendingCommands(); } - GrpcExistsPendingCommandsRequest buildHasPendingCommandRequest(OzgCloudVorgangId vorgangId) { + GrpcExistsPendingCommandsRequest buildExistsPendingCommandRequest(OzgCloudVorgangId vorgangId) { return GrpcExistsPendingCommandsRequest.newBuilder().setVorgangId(vorgangId.toString()).build(); } + public List<OzgCloudCommand> getPendingCommands(OzgCloudVorgangId vorgangId) { + var response = getCommandServiceStub().getPendingCommands(buildGetPendingCommandRequest(vorgangId)); + return response.getCommandList().stream().map(mapper::fromGrpc).toList(); + } + + GrpcGetPendingCommandsRequest buildGetPendingCommandRequest(OzgCloudVorgangId vorgangId) { + return GrpcGetPendingCommandsRequest.newBuilder().setVorgangId(vorgangId.toString()).build(); + } + CommandServiceBlockingStub getCommandServiceStub() { return commandServiceStub.withInterceptors(new OzgCloudCallContextAttachingInterceptor(contextProvider)); } diff --git a/api-lib-core/src/test/java/de/ozgcloud/apilib/common/command/grpc/GrpcGetPendingCommandsRequestTestFactory.java b/api-lib-core/src/test/java/de/ozgcloud/apilib/common/command/grpc/GrpcGetPendingCommandsRequestTestFactory.java new file mode 100644 index 0000000..2f6f97b --- /dev/null +++ b/api-lib-core/src/test/java/de/ozgcloud/apilib/common/command/grpc/GrpcGetPendingCommandsRequestTestFactory.java @@ -0,0 +1,20 @@ +package de.ozgcloud.apilib.common.command.grpc; + +import de.ozgcloud.apilib.vorgang.OzgCloudVorgangTestFactory; +import de.ozgcloud.vorgang.grpc.command.GrpcGetPendingCommandsRequest; +import de.ozgcloud.vorgang.grpc.command.GrpcGetPendingCommandsRequest.Builder; + +public class GrpcGetPendingCommandsRequestTestFactory { + + public static final String VORGAND_ID = OzgCloudVorgangTestFactory.ID.toString(); + + public static GrpcGetPendingCommandsRequest create() { + return createBuilder().build(); + } + + public static Builder createBuilder() { + return GrpcGetPendingCommandsRequest.newBuilder() + .setVorgangId(VORGAND_ID); + } + +} 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 281ae7a..fa44afb 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 @@ -27,6 +27,8 @@ import de.ozgcloud.vorgang.grpc.command.GrpcCommand; import de.ozgcloud.vorgang.grpc.command.GrpcCommandsResponse; import de.ozgcloud.vorgang.grpc.command.GrpcExistsPendingCommandsRequest; import de.ozgcloud.vorgang.grpc.command.GrpcExistsPendingCommandsResponse; +import de.ozgcloud.vorgang.grpc.command.GrpcGetPendingCommandsRequest; +import de.ozgcloud.vorgang.grpc.command.GrpcGetPendingCommandsResponse; class GrpcOzgCloudCommandServiceTest { @@ -217,16 +219,16 @@ class GrpcOzgCloudCommandServiceTest { @BeforeEach void mock() { doReturn(serviceStubWithInterceptor).when(service).getCommandServiceStub(); - doReturn(request).when(service).buildHasPendingCommandRequest(OzgCloudVorgangTestFactory.ID); + doReturn(request).when(service).buildExistsPendingCommandRequest(OzgCloudVorgangTestFactory.ID); var response = GrpcExistsPendingCommandsResponse.newBuilder().setExistsPendingCommands(true).build(); when(serviceStubWithInterceptor.existsPendingCommands(any())).thenReturn(response); } @Test - void shouldCallBuildHasPendingCommandRequest() { + void shouldCallBuildExistsPendingCommandRequest() { existsPendingCommands(); - verify(service).buildHasPendingCommandRequest(OzgCloudVorgangTestFactory.ID); + verify(service).buildExistsPendingCommandRequest(OzgCloudVorgangTestFactory.ID); } @Test @@ -260,13 +262,85 @@ class GrpcOzgCloudCommandServiceTest { } @Nested - class TestBuildHasPendingCommandRequest { + class TestBuildExistsPendingCommandRequest { @Test void shouldReturnRequest() { - var request = service.buildHasPendingCommandRequest(OzgCloudVorgangTestFactory.ID); + var request = service.buildExistsPendingCommandRequest(OzgCloudVorgangTestFactory.ID); assertThat(request).isEqualTo(GrpcExistsPendingCommandsRequestTestFactory.create()); } } + + @Nested + class TestGetPendingCommands { + + private final GrpcGetPendingCommandsRequest request = GrpcGetPendingCommandsRequestTestFactory.create(); + + @Mock + private CommandServiceBlockingStub serviceStubWithInterceptor; + + private final GrpcCommand grpcCommand = GrpcCommandTestFactory.create(); + private final OzgCloudCommand command = OzgCloudCommandTestFactory.create(); + + @BeforeEach + void mock() { + doReturn(serviceStubWithInterceptor).when(service).getCommandServiceStub(); + doReturn(request).when(service).buildGetPendingCommandRequest(any()); + var response = GrpcGetPendingCommandsResponse.newBuilder().addCommand(grpcCommand).build(); + when(serviceStubWithInterceptor.getPendingCommands(any())).thenReturn(response); + } + + @Test + void shouldCallBuildGetPendingCommandRequest() { + getPendingCommands(); + + verify(service).buildGetPendingCommandRequest(OzgCloudVorgangTestFactory.ID); + } + + @Test + void shouldCallGetCommandServiceStub() { + getPendingCommands(); + + verify(service).getCommandServiceStub(); + } + + @Test + void shouldCallServiceStubWithInterceptor() { + getPendingCommands(); + + verify(serviceStubWithInterceptor).getPendingCommands(request); + } + + @Test + void shouldCallMapper() { + getPendingCommands(); + + verify(mapper).fromGrpc(grpcCommand); + } + + @Test + void shouldReturnPendingCommands() { + when(mapper.fromGrpc(grpcCommand)).thenReturn(command); + + var result = getPendingCommands(); + + assertThat(result).containsExactly(command); + } + + private List<OzgCloudCommand> getPendingCommands() { + return service.getPendingCommands(OzgCloudVorgangTestFactory.ID); + } + } + + @Nested + class TestBuildGetPendingCommandRequest { + + @Test + void shouldReturnRequest() { + var request = service.buildGetPendingCommandRequest(OzgCloudVorgangTestFactory.ID); + + assertThat(request).isEqualTo(GrpcGetPendingCommandsRequestTestFactory.create()); + } + } } \ No newline at end of file -- GitLab