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 1cecb578e467e518700b53eef8719f34ca7a0455..2beb35728ea3c1e4f5ede1a87d7d94ee67ebcce7 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 91e05f4c9af4d367f850c8f01b04f61f884a0c39..1094020177a08e5e41049cff1536f92b56a374f6 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 @@ -9,9 +9,12 @@ import de.ozgcloud.apilib.common.command.OzgCloudCommandId; import de.ozgcloud.apilib.common.command.OzgCloudCommandService; import de.ozgcloud.apilib.common.command.OzgCloudCommandStatus; import de.ozgcloud.apilib.common.command.OzgCloudCreateSubCommandsRequest; +import de.ozgcloud.apilib.vorgang.OzgCloudVorgangId; 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; @@ -74,7 +77,26 @@ public class GrpcOzgCloudCommandService implements OzgCloudCommandService { return response.getCommandList().stream().map(mapper::fromGrpc).toList(); } + public boolean existsPendingCommands(OzgCloudVorgangId vorgangId) { + var response = getCommandServiceStub().existsPendingCommands(buildExistsPendingCommandRequest(vorgangId)); + return response.getExistsPendingCommands(); + } + + 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/GrpcExistsPendingCommandsRequestTestFactory.java b/api-lib-core/src/test/java/de/ozgcloud/apilib/common/command/grpc/GrpcExistsPendingCommandsRequestTestFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..2d035c01365608141528d2a286dee46981236a58 --- /dev/null +++ b/api-lib-core/src/test/java/de/ozgcloud/apilib/common/command/grpc/GrpcExistsPendingCommandsRequestTestFactory.java @@ -0,0 +1,18 @@ +package de.ozgcloud.apilib.common.command.grpc; + +import de.ozgcloud.apilib.vorgang.OzgCloudVorgangTestFactory; +import de.ozgcloud.vorgang.grpc.command.GrpcExistsPendingCommandsRequest; +import de.ozgcloud.vorgang.grpc.command.GrpcExistsPendingCommandsRequest.Builder; + +public class GrpcExistsPendingCommandsRequestTestFactory { + public static final String VORGAND_ID = OzgCloudVorgangTestFactory.ID.toString(); + + public static GrpcExistsPendingCommandsRequest create() { + return createBuilder().build(); + } + + public static Builder createBuilder() { + return GrpcExistsPendingCommandsRequest.newBuilder() + .setVorgangId(VORGAND_ID); + } +} diff --git a/api-lib-core/src/test/java/de/ozgcloud/apilib/common/command/grpc/GrpcExistsPendingCommandsResponseTestFactory.java b/api-lib-core/src/test/java/de/ozgcloud/apilib/common/command/grpc/GrpcExistsPendingCommandsResponseTestFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..4a78217021a4f3848d7a2fc0075abc1bca4d77da --- /dev/null +++ b/api-lib-core/src/test/java/de/ozgcloud/apilib/common/command/grpc/GrpcExistsPendingCommandsResponseTestFactory.java @@ -0,0 +1,17 @@ +package de.ozgcloud.apilib.common.command.grpc; + +import de.ozgcloud.vorgang.grpc.command.GrpcExistsPendingCommandsResponse; +import de.ozgcloud.vorgang.grpc.command.GrpcExistsPendingCommandsResponse.Builder; + +public class GrpcExistsPendingCommandsResponseTestFactory { + + public static final boolean EXISTS_PENDING_COMMAND = true; + + public static GrpcExistsPendingCommandsResponse create() { + return createBuilder().build(); + } + + public static Builder createBuilder() { + return GrpcExistsPendingCommandsResponse.newBuilder().setExistsPendingCommands(EXISTS_PENDING_COMMAND); + } +} 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 0000000000000000000000000000000000000000..2f6f97b8bda07edf301e6f0b0d741f48de8cf7e2 --- /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/GrpcGetPendingCommandsResponseTestFactory.java b/api-lib-core/src/test/java/de/ozgcloud/apilib/common/command/grpc/GrpcGetPendingCommandsResponseTestFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..ac9726ecb62105e061e73e948428a84cac7ab9cd --- /dev/null +++ b/api-lib-core/src/test/java/de/ozgcloud/apilib/common/command/grpc/GrpcGetPendingCommandsResponseTestFactory.java @@ -0,0 +1,18 @@ +package de.ozgcloud.apilib.common.command.grpc; + +import de.ozgcloud.vorgang.grpc.command.GrpcCommand; +import de.ozgcloud.vorgang.grpc.command.GrpcGetPendingCommandsResponse; +import de.ozgcloud.vorgang.grpc.command.GrpcGetPendingCommandsResponse.Builder; + +public class GrpcGetPendingCommandsResponseTestFactory { + + public static final GrpcCommand GRPC_COMMAND = GrpcCommandTestFactory.create(); + + public static GrpcGetPendingCommandsResponse create() { + return createBuilder().build(); + } + + public static Builder createBuilder() { + return GrpcGetPendingCommandsResponse.newBuilder().addCommand(GRPC_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 83f1e3eed2402fa8ec9c6ae8d6849101aa0fde36..3d8eebcdc4fd51502389d94402bbed8e4fb00062 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 @@ -10,6 +10,8 @@ import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.mockito.Mock; import org.mockito.Mockito; @@ -17,11 +19,15 @@ import de.ozgcloud.apilib.common.callcontext.OzgCloudCallContextProvider; import de.ozgcloud.apilib.common.command.OzgCloudCommand; import de.ozgcloud.apilib.common.command.OzgCloudCommandStatus; import de.ozgcloud.apilib.common.command.OzgCloudCreateSubCommandsRequestTestFactory; +import de.ozgcloud.apilib.vorgang.OzgCloudVorgangTestFactory; import de.ozgcloud.common.errorhandling.TechnicalException; import de.ozgcloud.vorgang.grpc.command.CommandServiceGrpc.CommandServiceBlockingStub; import de.ozgcloud.vorgang.grpc.command.GrpcAddSubCommandsRequest; 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; class GrpcOzgCloudCommandServiceTest { @@ -200,4 +206,139 @@ class GrpcOzgCloudCommandServiceTest { return service.addSubCommands(OzgCloudCreateSubCommandsRequestTestFactory.create()); } } + + @Nested + class TestExistsPendingCommands { + + private final GrpcExistsPendingCommandsRequest request = GrpcExistsPendingCommandsRequestTestFactory.create(); + + @Mock + private CommandServiceBlockingStub serviceStubWithInterceptor; + + @BeforeEach + void mock() { + doReturn(serviceStubWithInterceptor).when(service).getCommandServiceStub(); + doReturn(request).when(service).buildExistsPendingCommandRequest(OzgCloudVorgangTestFactory.ID); + var response = GrpcExistsPendingCommandsResponseTestFactory.create(); + when(serviceStubWithInterceptor.existsPendingCommands(any())).thenReturn(response); + } + + @Test + void shouldCallBuildExistsPendingCommandRequest() { + existsPendingCommands(); + + verify(service).buildExistsPendingCommandRequest(OzgCloudVorgangTestFactory.ID); + } + + @Test + void shouldCallGetCommandServiceStub() { + existsPendingCommands(); + + verify(service).getCommandServiceStub(); + } + + @Test + void shouldCallServiceStubWithInterceptor() { + existsPendingCommands(); + + verify(serviceStubWithInterceptor).existsPendingCommands(request); + } + + @ParameterizedTest + @ValueSource(booleans = { true, false }) + void shouldReturnIfCommandsArePending(boolean hasPendingCommands) { + var response = GrpcExistsPendingCommandsResponse.newBuilder().setExistsPendingCommands(hasPendingCommands).build(); + when(serviceStubWithInterceptor.existsPendingCommands(any())).thenReturn(response); + + var result = existsPendingCommands(); + + assertThat(result).isEqualTo(hasPendingCommands); + } + + private boolean existsPendingCommands() { + return service.existsPendingCommands(OzgCloudVorgangTestFactory.ID); + } + } + + @Nested + class TestBuildExistsPendingCommandRequest { + + @Test + void shouldReturnRequest() { + 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 OzgCloudCommand command = OzgCloudCommandTestFactory.create(); + + @BeforeEach + void mock() { + doReturn(serviceStubWithInterceptor).when(service).getCommandServiceStub(); + doReturn(request).when(service).buildGetPendingCommandRequest(any()); + var response = GrpcGetPendingCommandsResponseTestFactory.create(); + 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(GrpcGetPendingCommandsResponseTestFactory.GRPC_COMMAND); + } + + @Test + void shouldReturnPendingCommands() { + when(mapper.fromGrpc(GrpcGetPendingCommandsResponseTestFactory.GRPC_COMMAND)).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