Skip to content
Snippets Groups Projects
Commit 8b717de7 authored by OZGCloud's avatar OZGCloud
Browse files

Merge pull request 'OZG-7037 add hasPendingCommands to...

Merge pull request 'OZG-7037 add hasPendingCommands to GrpcOzgCloudCommandService' (#38) from OZG-7037-pending-commands into master

Reviewed-on: https://git.ozg-sh.de/ozgcloud-lib/api-lib/pulls/38


Reviewed-by: default avatarOZGCloud <ozgcloud@mgm-tp.com>
parents 40a3710a eaa5d36d
Branches
Tags
No related merge requests found
Showing with 242 additions and 0 deletions
......@@ -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);
}
......@@ -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));
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment