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..5f36b770bc730d6c2d7b24313d529b4472345c5d 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,8 +9,10 @@ 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 lombok.RequiredArgsConstructor; import net.devh.boot.grpc.client.inject.GrpcClient; @@ -74,7 +76,17 @@ public class GrpcOzgCloudCommandService implements OzgCloudCommandService { return response.getCommandList().stream().map(mapper::fromGrpc).toList(); } + public boolean hasPendingCommands(OzgCloudVorgangId vorgangId) { + var response = getCommandServiceStub().existsPendingCommands(buildHasPendingCommandRequest(vorgangId)); + return response.getExistsPendingCommands(); + } + + GrpcExistsPendingCommandsRequest buildHasPendingCommandRequest(OzgCloudVorgangId id) { + return GrpcExistsPendingCommandsRequest.newBuilder().setVorgangId(id.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/GrpcOzgCloudCommandServiceTest.java b/api-lib-core/src/test/java/de/ozgcloud/apilib/common/command/grpc/GrpcOzgCloudCommandServiceTest.java index 83f1e3eed2402fa8ec9c6ae8d6849101aa0fde36..03b06ebcacc7c319974a509b9f3641a000f04c83 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,14 @@ 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; class GrpcOzgCloudCommandServiceTest { @@ -200,4 +205,68 @@ class GrpcOzgCloudCommandServiceTest { return service.addSubCommands(OzgCloudCreateSubCommandsRequestTestFactory.create()); } } + + @Nested + class TestHasPendingCommands { + + private final GrpcExistsPendingCommandsRequest request = GrpcExistsPendingCommandsRequestTestFactory.create(); + + @Mock + private CommandServiceBlockingStub serviceStubWithInterceptor; + + @BeforeEach + void mock() { + doReturn(serviceStubWithInterceptor).when(service).getCommandServiceStub(); + doReturn(request).when(service).buildHasPendingCommandRequest(OzgCloudVorgangTestFactory.ID); + var response = GrpcExistsPendingCommandsResponse.newBuilder().setExistsPendingCommands(true).build(); + when(serviceStubWithInterceptor.existsPendingCommands(any())).thenReturn(response); + } + + @Test + void shouldCallBuildHasPendingCommandRequest() { + hasPendingCommands(); + + verify(service).buildHasPendingCommandRequest(OzgCloudVorgangTestFactory.ID); + } + + @Test + void shouldCallGetCommandServiceStub() { + hasPendingCommands(); + + verify(service).getCommandServiceStub(); + } + + @Test + void shouldCallServiceStubWithInterceptor() { + hasPendingCommands(); + + 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 = hasPendingCommands(); + + assertThat(result).isEqualTo(hasPendingCommands); + } + + private boolean hasPendingCommands() { + return service.hasPendingCommands(OzgCloudVorgangTestFactory.ID); + } + } + + @Nested + class TestBuildHasPendingCommandRequest { + + @Test + void shouldReturnRequest() { + var request = service.buildHasPendingCommandRequest(OzgCloudVorgangTestFactory.ID); + + assertThat(request).isEqualTo(GrpcExistsPendingCommandsRequestTestFactory.create()); + } + } } \ No newline at end of file