diff --git a/goofy-server/src/main/java/de/itvsh/goofy/common/command/CommandController.java b/goofy-server/src/main/java/de/itvsh/goofy/common/command/CommandController.java index e165bcd3730206ee8eef8171a591188d8fc6420d..091c8d73a88facd49e1ca39b577e7ba90d64ab26 100644 --- a/goofy-server/src/main/java/de/itvsh/goofy/common/command/CommandController.java +++ b/goofy-server/src/main/java/de/itvsh/goofy/common/command/CommandController.java @@ -2,10 +2,8 @@ package de.itvsh.goofy.common.command; import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*; -import java.util.Collection; -import java.util.Collections; - import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.hateoas.CollectionModel; import org.springframework.hateoas.EntityModel; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -26,8 +24,8 @@ public class CommandController { static final String COMMANDS_PATH = "/api/commands"; // NOSONAR - private static final String PARAM_PENDING = "pending"; - private static final String PARAM_VORGANG_ID = "vorgangId"; + static final String PARAM_PENDING = "pending"; + static final String PARAM_VORGANG_ID = "vorgangId"; @Autowired private CommandService service; @@ -54,8 +52,8 @@ public class CommandController { } @GetMapping(params = { PARAM_PENDING, PARAM_VORGANG_ID }) - public Collection<EntityModel<Command>> getPendingCommands(@RequestParam boolean pending, @RequestParam String vorgangId) { - return Collections.emptyList(); + public CollectionModel<EntityModel<Command>> getPendingCommands(@RequestParam boolean pending, @RequestParam String vorgangId) { + return modelAssembler.toCollectionModel(service.getPendingCommands(vorgangId)); } @RestController diff --git a/goofy-server/src/main/java/de/itvsh/goofy/common/command/CommandModelAssembler.java b/goofy-server/src/main/java/de/itvsh/goofy/common/command/CommandModelAssembler.java index 1ce3facb0a10778556b81c177ed080e4a8e07bff..b2df07127483fd0d22895b44ba9925a3ee494404 100644 --- a/goofy-server/src/main/java/de/itvsh/goofy/common/command/CommandModelAssembler.java +++ b/goofy-server/src/main/java/de/itvsh/goofy/common/command/CommandModelAssembler.java @@ -1,11 +1,13 @@ package de.itvsh.goofy.common.command; -import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo; -import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn; +import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*; import java.util.Optional; import java.util.function.Predicate; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.springframework.hateoas.CollectionModel; import org.springframework.hateoas.EntityModel; import org.springframework.hateoas.Link; import org.springframework.hateoas.LinkRelation; @@ -61,4 +63,9 @@ class CommandModelAssembler implements RepresentationModelAssembler<Command, Ent private EntityModel<Command> addIf(Predicate<Command> condition, EntityModel<Command> model, Link link) { return Optional.ofNullable(model.getContent()).filter(condition).map(command -> model.add(link)).orElse(model); } + + public CollectionModel<EntityModel<Command>> toCollectionModel(Stream<Command> entities) { + return CollectionModel.of(entities.map(this::toModel).collect(Collectors.toList()), + linkTo(CommandController.class).withSelfRel()); + } } \ No newline at end of file diff --git a/goofy-server/src/main/java/de/itvsh/goofy/common/command/CommandRemoteService.java b/goofy-server/src/main/java/de/itvsh/goofy/common/command/CommandRemoteService.java index 944205716d874165aff515de16427ce43804183d..9c5d8a62b421e7a9cc078e04b315178a531589dc 100644 --- a/goofy-server/src/main/java/de/itvsh/goofy/common/command/CommandRemoteService.java +++ b/goofy-server/src/main/java/de/itvsh/goofy/common/command/CommandRemoteService.java @@ -1,5 +1,7 @@ package de.itvsh.goofy.common.command; +import java.util.stream.Stream; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -9,6 +11,8 @@ import de.itvsh.ozg.pluto.grpc.command.GrpcCreateCommandRequest; import de.itvsh.ozg.pluto.grpc.command.GrpcExistsPendingCommandsRequest; import de.itvsh.ozg.pluto.grpc.command.GrpcExistsPendingCommandsResponse; import de.itvsh.ozg.pluto.grpc.command.GrpcGetCommandRequest; +import de.itvsh.ozg.pluto.grpc.command.GrpcGetPendingCommandsRequest; +import de.itvsh.ozg.pluto.grpc.command.GrpcGetPendingCommandsResponse; import de.itvsh.ozg.pluto.grpc.command.GrpcOrder; import de.itvsh.ozg.pluto.grpc.command.GrpcRevokeCommandRequest; import net.devh.boot.grpc.client.inject.GrpcClient; @@ -55,6 +59,7 @@ class CommandRemoteService { public Command getCommand(String commandId) { var response = commandServiceStub.getCommand(createGetCommandRequest(commandId)); + return mapper.toCommand(response); } @@ -67,10 +72,27 @@ class CommandRemoteService { public boolean existsPendingCommands(String relationId) { GrpcExistsPendingCommandsResponse respone = commandServiceStub.existsPendingCommands(buildGrpcExistsPendingCommandsResponse(relationId)); + return respone.getExistsPendingCommands(); } private GrpcExistsPendingCommandsRequest buildGrpcExistsPendingCommandsResponse(String relationId) { - return GrpcExistsPendingCommandsRequest.newBuilder().setVorgangId(relationId).build(); + return GrpcExistsPendingCommandsRequest.newBuilder() + .setContext(contextService.createCallContext()) + .setVorgangId(relationId) + .build(); + } + + public Stream<Command> getPendingCommands(String vorgangId) { + GrpcGetPendingCommandsResponse response = commandServiceStub.getPendingCommands(buildGrpcGetPendingCommandsRequest(vorgangId)); + + return response.getCommandList().stream().map(mapper::toCommand); + } + + private GrpcGetPendingCommandsRequest buildGrpcGetPendingCommandsRequest(String vorgangId) { + return GrpcGetPendingCommandsRequest.newBuilder() + .setContext(contextService.createCallContext()) + .setVorgangId(vorgangId) + .build(); } } \ No newline at end of file diff --git a/goofy-server/src/main/java/de/itvsh/goofy/common/command/CommandService.java b/goofy-server/src/main/java/de/itvsh/goofy/common/command/CommandService.java index 9865ce87fb84412888775609ba5183952dd26780..d408eddc161f5dfa908b6fca69a410e8834b97f8 100644 --- a/goofy-server/src/main/java/de/itvsh/goofy/common/command/CommandService.java +++ b/goofy-server/src/main/java/de/itvsh/goofy/common/command/CommandService.java @@ -1,5 +1,7 @@ package de.itvsh.goofy.common.command; +import java.util.stream.Stream; + import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; @@ -29,4 +31,8 @@ class CommandService { public boolean existsPendingCommands(String relationId) { return commandRemoteService.existsPendingCommands(relationId); } + + public Stream<Command> getPendingCommands(String vorgangId) { + return commandRemoteService.getPendingCommands(vorgangId); + } } \ No newline at end of file diff --git a/goofy-server/src/test/java/de/itvsh/goofy/common/command/CommandControllerTest.java b/goofy-server/src/test/java/de/itvsh/goofy/common/command/CommandControllerTest.java index 26884ee05293697b08bd11a95d3f5a214ea74991..11d9c2455c1ab86f39efcc6c8959a0b720e39174 100644 --- a/goofy-server/src/test/java/de/itvsh/goofy/common/command/CommandControllerTest.java +++ b/goofy-server/src/test/java/de/itvsh/goofy/common/command/CommandControllerTest.java @@ -7,6 +7,8 @@ import static org.mockito.Mockito.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import java.util.stream.Stream; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; @@ -176,4 +178,46 @@ class CommandControllerTest { verify(service).existsPendingCommands(VorgangHeaderTestFactory.ID); } } + + @Nested + class TestGetPendingCommands { + + @Nested + class TestGetCommand { + + private final Command command = CommandTestFactory.create(); + private final Stream<Command> pendingCommands = Stream.of(command); + + @BeforeEach + void initTest() { + when(service.getPendingCommands(anyString())).thenReturn(pendingCommands); + } + + @Test + void shouldReturnOk() throws Exception { + doRequest().andExpect(status().isOk()); + } + + @Test + void shouldCallService() throws Exception { + doRequest(); + + verify(service).getPendingCommands(VorgangHeaderTestFactory.ID); + } + + @Test + void shouldCallModelAssembler() throws Exception { + doRequest(); + + verify(modelAssembler).toCollectionModel(pendingCommands); + } + + private ResultActions doRequest() throws Exception { + return mockMvc.perform(get(CommandController.COMMANDS_PATH) + .param(CommandController.PARAM_PENDING, "true") + .param(CommandController.PARAM_VORGANG_ID, VorgangHeaderTestFactory.ID)) + .andExpect(status().is2xxSuccessful()); + } + } + } } \ No newline at end of file diff --git a/goofy-server/src/test/java/de/itvsh/goofy/common/command/CommandRemoteServiceTest.java b/goofy-server/src/test/java/de/itvsh/goofy/common/command/CommandRemoteServiceTest.java index 9c7317fd0b4440c71ebfcd7ab7af7ec9617ce4e3..0aa7c4f276da2dbbe6706096e000cec19020592d 100644 --- a/goofy-server/src/test/java/de/itvsh/goofy/common/command/CommandRemoteServiceTest.java +++ b/goofy-server/src/test/java/de/itvsh/goofy/common/command/CommandRemoteServiceTest.java @@ -4,6 +4,11 @@ import static org.assertj.core.api.Assertions.*; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; +import java.util.Collections; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; @@ -25,6 +30,8 @@ import de.itvsh.ozg.pluto.grpc.command.GrpcCreateCommandRequest; import de.itvsh.ozg.pluto.grpc.command.GrpcExistsPendingCommandsRequest; import de.itvsh.ozg.pluto.grpc.command.GrpcExistsPendingCommandsResponse; import de.itvsh.ozg.pluto.grpc.command.GrpcGetCommandRequest; +import de.itvsh.ozg.pluto.grpc.command.GrpcGetPendingCommandsRequest; +import de.itvsh.ozg.pluto.grpc.command.GrpcGetPendingCommandsResponse; import de.itvsh.ozg.pluto.grpc.command.GrpcRevokeCommandRequest; class CommandRemoteServiceTest { @@ -39,6 +46,8 @@ class CommandRemoteServiceTest { @Mock private ContextService contextService; + private GrpcCallContext grpcCallContext = GrpcCallContext.newBuilder().build(); + @Nested @DisplayName("Create CreateCommand") class TestCreateCommand { @@ -91,11 +100,9 @@ class CommandRemoteServiceTest { @DisplayName("Create Request") class TestCreateRequest { - private GrpcCallContext createdCallContext = GrpcCallContext.newBuilder().build(); - @BeforeEach void initTest() { - when(contextService.createCallContext()).thenReturn(createdCallContext); + when(contextService.createCallContext()).thenReturn(grpcCallContext); } @Test @@ -109,7 +116,7 @@ class CommandRemoteServiceTest { void shouldHaveCallContext() { var request = service.createCreateCommandRequest(CommandTestFactory.createCreateCommand(), CommandTestFactory.VERSION); - assertThat(request.getCallContext()).isSameAs(createdCallContext); + assertThat(request.getCallContext()).isSameAs(grpcCallContext); } @Test @@ -144,11 +151,9 @@ class CommandRemoteServiceTest { @Nested class TestCreateRequest { - private GrpcCallContext createdCallContext = GrpcCallContext.newBuilder().build(); - @BeforeEach void initTest() { - when(contextService.createCallContext()).thenReturn(createdCallContext); + when(contextService.createCallContext()).thenReturn(grpcCallContext); } @Test @@ -162,7 +167,7 @@ class CommandRemoteServiceTest { void shouldHaveCallContext() { var request = service.createRevokeRequest(CommandTestFactory.ID); - assertThat(request.getContext()).isNotNull().isSameAs(createdCallContext); + assertThat(request.getContext()).isNotNull().isSameAs(grpcCallContext); verify(contextService).createCallContext(); } @@ -216,11 +221,9 @@ class CommandRemoteServiceTest { @Nested class TestCreateRequest { - private GrpcCallContext createdCallContext = GrpcCallContext.newBuilder().build(); - @BeforeEach void initTest() { - when(contextService.createCallContext()).thenReturn(createdCallContext); + when(contextService.createCallContext()).thenReturn(grpcCallContext); } @Test @@ -234,7 +237,7 @@ class CommandRemoteServiceTest { void shouldHaveCallContext() { var request = service.createGetCommandRequest(CommandTestFactory.ID); - assertThat(request.getContext()).isNotNull().isSameAs(createdCallContext); + assertThat(request.getContext()).isNotNull().isSameAs(grpcCallContext); verify(contextService).createCallContext(); } @@ -285,12 +288,15 @@ class CommandRemoteServiceTest { class TestExistsPendingCommands { private final GrpcExistsPendingCommandsRequest request = GrpcExistsPendingCommandsRequest.newBuilder() + .setContext(grpcCallContext) .setVorgangId(VorgangHeaderTestFactory.ID).build(); @BeforeEach void mockServiceStub() { when(commandServiceStub.existsPendingCommands(any())) .thenReturn(GrpcExistsPendingCommandsResponse.newBuilder().setExistsPendingCommands(true).build()); + + when(contextService.createCallContext()).thenReturn(grpcCallContext); } @Test @@ -300,6 +306,13 @@ class CommandRemoteServiceTest { verify(commandServiceStub).existsPendingCommands(request); } + @Test + void shouldCallContextService() { + service.existsPendingCommands(VorgangHeaderTestFactory.ID); + + verify(contextService).createCallContext(); + } + @Test void shouldReturnValue() { var result = service.existsPendingCommands(VorgangHeaderTestFactory.ID); @@ -307,4 +320,57 @@ class CommandRemoteServiceTest { assertThat(result).isTrue(); } } + + @Nested + class TestGetPendingCommands { + + private final GrpcGetPendingCommandsRequest request = GrpcGetPendingCommandsRequest.newBuilder() + .setContext(grpcCallContext) + .setVorgangId(VorgangHeaderTestFactory.ID).build(); + private final GrpcCommand grpcCommand = GrpcCommand.newBuilder().build(); + private final Set<GrpcCommand> commandList = Collections.singleton(grpcCommand); + private final GrpcGetPendingCommandsResponse response = GrpcGetPendingCommandsResponse.newBuilder().addAllCommand(commandList).build(); + private final Command command = CommandTestFactory.create(); + + @BeforeEach + void mockServiceStub() { + when(commandServiceStub.getPendingCommands(any())).thenReturn(response); + when(contextService.createCallContext()).thenReturn(grpcCallContext); + } + + @Test + void shouldCallServiceStub() { + service.getPendingCommands(VorgangHeaderTestFactory.ID); + + verify(commandServiceStub).getPendingCommands(request); + } + + @Test + void shouldCallContextService() { + service.getPendingCommands(VorgangHeaderTestFactory.ID); + + verify(contextService).createCallContext(); + } + + @Test + void shouldCallMapper() { + var result = service.getPendingCommands(VorgangHeaderTestFactory.ID); + forceStreamOperationsDone(result); + + verify(mapper).toCommand(grpcCommand); + } + + @Test + void shouldReturnValue() { + when(mapper.toCommand(any())).thenReturn(command); + + var result = service.getPendingCommands(VorgangHeaderTestFactory.ID); + + assertThat(result.collect(Collectors.toList()).get(0)).isEqualTo(command); + } + + private void forceStreamOperationsDone(Stream<Command> stream) { + stream.collect(Collectors.toList()).stream(); + } + } } \ No newline at end of file diff --git a/goofy-server/src/test/java/de/itvsh/goofy/common/command/CommandServiceTest.java b/goofy-server/src/test/java/de/itvsh/goofy/common/command/CommandServiceTest.java index bec267f32f215868067c26a7226508039a160bab..8fca3ff9c9cc8e3e9080240a240fd1495abcd19c 100644 --- a/goofy-server/src/test/java/de/itvsh/goofy/common/command/CommandServiceTest.java +++ b/goofy-server/src/test/java/de/itvsh/goofy/common/command/CommandServiceTest.java @@ -84,4 +84,15 @@ class CommandServiceTest { verify(remoteService).existsPendingCommands(VorgangHeaderTestFactory.ID); } } + + @Nested + class TestGetPendingCommands { + + @Test + void shouldCallRemoteService() { + service.getPendingCommands(VorgangHeaderTestFactory.ID); + + verify(remoteService).getPendingCommands(VorgangHeaderTestFactory.ID); + } + } } \ No newline at end of file