diff --git a/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/Collaboration.java b/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/Collaboration.java index 09e1c53d132549eeb068c144d03835d801ee05e8..e08743eef041fb3c8eb87521da09994806ffabb2 100644 --- a/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/Collaboration.java +++ b/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/Collaboration.java @@ -2,6 +2,8 @@ package de.ozgcloud.alfa.collaboration; public interface Collaboration { + String getId(); + String getVorgangId(); String getCollaborationVorgangId(); diff --git a/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/CollaborationController.java b/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/CollaborationController.java index 63a1fa8edc08547408395b4b70739cd766ef27fe..394074c9e1e6b1c7b815d558f64570ae02a7d65a 100644 --- a/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/CollaborationController.java +++ b/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/CollaborationController.java @@ -1,7 +1,10 @@ package de.ozgcloud.alfa.collaboration; +import java.util.Optional; + import org.springframework.hateoas.CollectionModel; import org.springframework.hateoas.EntityModel; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @@ -17,10 +20,26 @@ public class CollaborationController { private final CollaborationModelAssembler assembler; private final CollaborationService service; - static final String PATH = "/api/vorgangs"; // NOSONAR + static final String PATH = "/api/collaborations"; // NOSONAR + + @GetMapping("/{collaborationId}") + public ResponseEntity<EntityModel<Collaboration>> getById(@PathVariable String collaborationId) { + return ResponseEntity.of(Optional.of(service.getById(collaborationId)).map(assembler::toModel)); + } + + @RestController + @RequestMapping(CollaborationByVorgangController.PATH) + @RequiredArgsConstructor + public static class CollaborationByVorgangController { + + static final String PATH = "/api/vorgangs"; + + private final CollaborationModelAssembler assembler; + private final CollaborationService service; - @GetMapping("/{vorgangId}/collaborations") - public CollectionModel<EntityModel<Collaboration>> getAllByVorgangId(@PathVariable String vorgangId) { - return assembler.toCollectionModel(service.getCollaborations(vorgangId), vorgangId); + @GetMapping("/{vorgangId}/collaborations") + public CollectionModel<EntityModel<Collaboration>> getAllByVorgangId(@PathVariable String vorgangId) { + return assembler.toCollectionModel(service.getCollaborations(vorgangId), vorgangId); + } } } diff --git a/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/CollaborationModelAssembler.java b/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/CollaborationModelAssembler.java index 0323c66fe4fa72971c00afc34815b8d92a064670..b85d9d2f8141ed86a177e74acda70821acd6e6c0 100644 --- a/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/CollaborationModelAssembler.java +++ b/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/CollaborationModelAssembler.java @@ -11,6 +11,8 @@ import org.springframework.hateoas.LinkRelation; import org.springframework.hateoas.server.RepresentationModelAssembler; import org.springframework.stereotype.Component; +import de.ozgcloud.alfa.collaboration.CollaborationController.CollaborationByVorgangController; +import de.ozgcloud.alfa.common.CollectionModelBuilder; import de.ozgcloud.alfa.common.ModelBuilder; import de.ozgcloud.alfa.common.command.CommandController; import de.ozgcloud.alfa.vorgang.VorgangController; @@ -26,17 +28,23 @@ class CollaborationModelAssembler implements RepresentationModelAssembler<Collab @Override public EntityModel<Collaboration> toModel(Collaboration collaboration) { + var selfLink = linkTo(methodOn(CollaborationController.class).getById(collaboration.getId())).withSelfRel(); + return ModelBuilder.fromEntity(collaboration) - // TODO: Wenn Schnittstelle zum laden der Collaboration existiert, muss self - // link ergänzt werden + .addLink(selfLink) .buildModel(); } public CollectionModel<EntityModel<Collaboration>> toCollectionModel(Stream<? extends Collaboration> entities, String vorgangId) { - var collectionModel = CollectionModel.of(entities.map(this::toModel).toList()) - .add(linkTo(methodOn(CollaborationController.class).getAllByVorgangId(vorgangId)).withSelfRel()); - return collectionModel - .addIf(collectionModel.getContent().isEmpty(), () -> buildCreateCollaborationRequestLink(vorgangId)); + var entityModels = entities.map(this::toModel).toList(); + + var selfLink = linkTo(methodOn(CollaborationByVorgangController.class).getAllByVorgangId(vorgangId)).withSelfRel(); + + return CollectionModelBuilder.fromEntities(entityModels) + .addLink(selfLink) + .ifMatch(entityModels::isEmpty) + .addLink(() -> buildCreateCollaborationRequestLink(vorgangId)) + .buildModel(); } Link buildCreateCollaborationRequestLink(String vorgangId) { diff --git a/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/CollaborationRemoteService.java b/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/CollaborationRemoteService.java index 325c0aa03274b059dbed2a9cf70bc27c60de623d..b0d34a700a5df2e7f9bb17e3632e15fcdbf81598 100644 --- a/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/CollaborationRemoteService.java +++ b/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/CollaborationRemoteService.java @@ -10,6 +10,7 @@ import de.ozgcloud.alfa.common.GrpcUtil; import de.ozgcloud.collaboration.request.CollaborationRequestServiceGrpc.CollaborationRequestServiceBlockingStub; import de.ozgcloud.collaboration.request.GrpcCollaborationRequest; import de.ozgcloud.collaboration.request.GrpcFindRequestsRequest; +import de.ozgcloud.collaboration.request.GrpcGetRequestRequest; import net.devh.boot.grpc.client.inject.GrpcClient; @Service @@ -21,12 +22,20 @@ public class CollaborationRemoteService { @Autowired private CollaborationMapper collaborationMapper; +// public Collaboration getById(String collaborationId) { +// serviceStub.getRequest() +// } + public Stream<Collaboration> getCollaborations(String vorgangId) { var toCollaboration = getCollaborationMapperForVorgang(vorgangId); return serviceStub.findRequests(buildSearchRequest(vorgangId)) .getRequestsList().stream().map(toCollaboration); } + GrpcGetRequestRequest buildGetRequest(String collaborationId) { + return GrpcGetRequestRequest.newBuilder().setId(collaborationId).build(); + } + GrpcFindRequestsRequest buildSearchRequest(String vorgangId) { return GrpcFindRequestsRequest.newBuilder().setVorgangId(vorgangId).build(); } diff --git a/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/CollaborationService.java b/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/CollaborationService.java index 8b4406b926453d570d635b13a556ef6e9050b65b..32a4bb5ad3c2e9bc9b4aed32de405a1a8feb67cb 100644 --- a/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/CollaborationService.java +++ b/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/CollaborationService.java @@ -12,6 +12,10 @@ class CollaborationService { private final CollaborationRemoteService remoteService; + public Collaboration getById(String collaborationId) { + return null; + } + public Stream<Collaboration> getCollaborations(String vorgangId) { return remoteService.getCollaborations(vorgangId); } diff --git a/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/CollaborationVorgangProcessor.java b/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/CollaborationVorgangProcessor.java index 2f8f332a5e4ebdf81d10932bdf138c53767de8cf..96805f72229e992c97f725a98525cef4844547fe 100644 --- a/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/CollaborationVorgangProcessor.java +++ b/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/CollaborationVorgangProcessor.java @@ -11,6 +11,7 @@ import org.springframework.hateoas.LinkRelation; import org.springframework.hateoas.server.RepresentationModelProcessor; import org.springframework.stereotype.Component; +import de.ozgcloud.alfa.collaboration.CollaborationController.CollaborationByVorgangController; import de.ozgcloud.alfa.common.ModelBuilder; import de.ozgcloud.alfa.common.user.CurrentUserService; import de.ozgcloud.alfa.common.user.UserRole; @@ -40,7 +41,7 @@ class CollaborationVorgangProcessor implements RepresentationModelProcessor<Enti return ModelBuilder.fromModel(model) .ifMatch(() -> !collaborationService.hasCollaboration(vorgang.getId())) .addLinks(buildSearchOrganisationsEinheitLink(), buildSearchFachstelleLink()) - .addLink(linkTo(methodOn(CollaborationController.class).getAllByVorgangId(vorgang.getId())).withRel(REL_COLLABORATIONS)) + .addLink(linkTo(methodOn(CollaborationByVorgangController.class).getAllByVorgangId(vorgang.getId())).withRel(REL_COLLABORATIONS)) .buildModel(); } diff --git a/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/FachstelleCollaboration.java b/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/FachstelleCollaboration.java index bfb7417ba47d9bfa7952ba406d04978dda900f4f..ec2175f2f8f6bcb1f0c40cbcc570aff03df2bacd 100644 --- a/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/FachstelleCollaboration.java +++ b/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/FachstelleCollaboration.java @@ -14,6 +14,8 @@ import lombok.NoArgsConstructor; @NoArgsConstructor public class FachstelleCollaboration implements Collaboration { + @JsonIgnore + private String id; @JsonIgnore private String vorgangId; @JsonIgnore diff --git a/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/OrganisationsEinheitCollaboration.java b/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/OrganisationsEinheitCollaboration.java index 793d2bf2160d5f713264b1f743cf8033b0b4a754..4930840df4c9fa8028cc78fec993871600f557e9 100644 --- a/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/OrganisationsEinheitCollaboration.java +++ b/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/OrganisationsEinheitCollaboration.java @@ -14,6 +14,8 @@ import lombok.NoArgsConstructor; @NoArgsConstructor public class OrganisationsEinheitCollaboration implements Collaboration { + @JsonIgnore + private String id; @JsonIgnore private String vorgangId; @JsonIgnore diff --git a/alfa-service/src/main/java/de/ozgcloud/alfa/common/CollectionModelBuilder.java b/alfa-service/src/main/java/de/ozgcloud/alfa/common/CollectionModelBuilder.java index 0a61151ea2eb7ad0c600621cccdc23382d2512ba..01ceb2d404037d97e024e9ff33baa69b6cc88607 100644 --- a/alfa-service/src/main/java/de/ozgcloud/alfa/common/CollectionModelBuilder.java +++ b/alfa-service/src/main/java/de/ozgcloud/alfa/common/CollectionModelBuilder.java @@ -27,6 +27,7 @@ import java.util.LinkedList; import java.util.List; import java.util.function.BooleanSupplier; import java.util.function.Predicate; +import java.util.function.Supplier; import java.util.stream.Stream; import org.springframework.hateoas.CollectionModel; @@ -82,5 +83,12 @@ public class CollectionModelBuilder<T> { } return CollectionModelBuilder.this; } + + public CollectionModelBuilder<T> addLink(Supplier<Link> linkSupplier) { + if (conditionFulfilled) { + links.add(linkSupplier.get()); + } + return CollectionModelBuilder.this; + } } } \ No newline at end of file diff --git a/alfa-service/src/main/java/de/ozgcloud/alfa/common/command/CommandModelAssembler.java b/alfa-service/src/main/java/de/ozgcloud/alfa/common/command/CommandModelAssembler.java index d8825572907482b5b7acaae723eea79c2cde445b..bf60adfa089d3533a786e81ed04b39b3c77f5277 100644 --- a/alfa-service/src/main/java/de/ozgcloud/alfa/common/command/CommandModelAssembler.java +++ b/alfa-service/src/main/java/de/ozgcloud/alfa/common/command/CommandModelAssembler.java @@ -38,7 +38,7 @@ import org.springframework.stereotype.Component; import de.ozgcloud.alfa.bescheid.BescheidController; import de.ozgcloud.alfa.bescheid.DocumentController; -import de.ozgcloud.alfa.collaboration.CollaborationController; +import de.ozgcloud.alfa.collaboration.CollaborationController.CollaborationByVorgangController; import de.ozgcloud.alfa.common.ModelBuilder; import de.ozgcloud.alfa.kommentar.KommentarController; import de.ozgcloud.alfa.postfach.PostfachMailController; @@ -83,7 +83,7 @@ class CommandModelAssembler implements RepresentationModelAssembler<Command, Ent case WIEDERVORLAGE -> linkTo(WiedervorlageController.class).slash(entity.getRelationId()); case BESCHEID -> linkTo(methodOn(BescheidController.class).getDraft(entity.getVorgangId())); case DOCUMENT -> linkTo(DocumentController.class).slash(entity.getCreatedResource()); - case COLLABORATION -> linkTo(methodOn(CollaborationController.class).getAllByVorgangId(entity.getVorgangId())); + case COLLABORATION -> linkTo(methodOn(CollaborationByVorgangController.class).getAllByVorgangId(entity.getVorgangId())); case NONE -> throw new IllegalArgumentException("Unknown CommandOrder: " + entity.getOrder()); }; diff --git a/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/CollaborationAssertions.java b/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/CollaborationAssertions.java new file mode 100644 index 0000000000000000000000000000000000000000..1afbee05856e5cb5b28e4d0738b1b75fec51984f --- /dev/null +++ b/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/CollaborationAssertions.java @@ -0,0 +1,45 @@ +package de.ozgcloud.alfa.collaboration; + +import org.springframework.test.web.servlet.ResultActions; +import org.springframework.test.web.servlet.result.JsonPathResultMatchers; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.web.util.UriComponentsBuilder; + +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +class CollaborationAssertions { + + private final boolean collectionModel; + + void assertHasFields(ResultActions response) throws Exception { + response.andExpect(jsonPath("id").doesNotExist()) + .andExpect(jsonPath("vorgangId").doesNotExist()) + .andExpect(jsonPath("collaborationVorgangId").doesNotExist()) + .andExpect(jsonPath("titel").value(OrganisationsEinheitCollaborationTestFactory.TITEL)) + .andExpect(jsonPath("beschreibung").value(OrganisationsEinheitCollaborationTestFactory.BESCHREIBUNG)) + .andExpect(jsonPath("collaborationLevel").value(OrganisationsEinheitCollaborationTestFactory.COLLABORATION_LEVEL)); + } + + void assertHasZustaendigeStelleLink(ResultActions response, String controllerPath, String zustaendigeStelle) throws Exception { + var expectedLink = UriComponentsBuilder + .fromUriString("http://localhost") + .path(controllerPath) + .pathSegment(zustaendigeStelle) + .build(); + + response.andExpect(jsonPath("zustaendigeStelle").value(expectedLink.toString())); + } + + private JsonPathResultMatchers jsonPath(String expression, Object... args) { + return MockMvcResultMatchers.jsonPath(jsonPathPrefix() + expression, args); + } + + private String jsonPathPrefix() { + var prefix = "$."; + if (collectionModel) { + prefix += "content[0]."; + } + return prefix; + } +} diff --git a/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/CollaborationByVorgangControllerTest.java b/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/CollaborationByVorgangControllerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..81b8fddaec0406939860bfe8c4b46ec88a183d12 --- /dev/null +++ b/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/CollaborationByVorgangControllerTest.java @@ -0,0 +1,108 @@ +package de.ozgcloud.alfa.collaboration; + +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.Collections; +import java.util.stream.Stream; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.springframework.hateoas.CollectionModel; +import org.springframework.hateoas.EntityModel; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.ResultActions; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +import de.ozgcloud.alfa.collaboration.CollaborationController.CollaborationByVorgangController; +import lombok.SneakyThrows; + +class CollaborationByVorgangControllerTest { + + @InjectMocks + private CollaborationByVorgangController controller; + + @Mock + private CollaborationService service; + + @Mock + private CollaborationModelAssembler assembler; + + private MockMvc mockMvc; + + private static final String VORGANG_ID = OrganisationsEinheitCollaborationTestFactory.VORGANG_ID; + + @BeforeEach + void initTest() { + mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); + } + + @Nested + class TestGetAllByVorgangId { + + private final Collaboration collaboration = OrganisationsEinheitCollaborationTestFactory.create(); + + @Test + void shouldCallCollaborationService() { + performRequest(); + + verify(service).getCollaborations(VORGANG_ID); + } + + @Nested + class TestOnExistingCollaboration { + + private final CollaborationAssertions collaborationAssertions = new CollaborationAssertions(true); + private final Stream<Collaboration> collaborations = Stream.of(collaboration); + + @BeforeEach + void mock() { + when(service.getCollaborations(VORGANG_ID)).thenReturn(collaborations); + when(assembler.toCollectionModel(collaborations, VORGANG_ID)) + .thenReturn(CollectionModel.of(Collections.singletonList(EntityModel.of(collaboration)))); + } + + @Test + void shouldCallAssembler() { + performRequest(); + + verify(assembler).toCollectionModel(collaborations, VORGANG_ID); + } + + @SneakyThrows + @Test + void shouldReturnStatusOk() { + var response = performRequest(); + + response.andExpect(status().isOk()); + } + + @SneakyThrows + @Test + void shouldHaveContentWithFields() { + var response = performRequest(); + + collaborationAssertions.assertHasFields(response); + } + + @SneakyThrows + @Test + void shouldHaveContentWithLink() { + var response = performRequest(); + + collaborationAssertions.assertHasZustaendigeStelleLink(response, OrganisationsEinheitController.PATH, + OrganisationsEinheitCollaborationTestFactory.ZUSTAENDIGE_STELLE); + } + } + + @SneakyThrows + private ResultActions performRequest() { + return mockMvc.perform( + get(CollaborationByVorgangController.PATH + "/" + VORGANG_ID + "/collaborations")); + } + } +} diff --git a/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/CollaborationControllerTest.java b/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/CollaborationControllerTest.java index 019c4b4491ba4bc242adc17d9acd2e5df3a2d741..1395351560c42652bc739451a8b77fee221e414f 100644 --- a/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/CollaborationControllerTest.java +++ b/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/CollaborationControllerTest.java @@ -4,20 +4,15 @@ 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.Collections; -import java.util.stream.Stream; - import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.springframework.hateoas.CollectionModel; import org.springframework.hateoas.EntityModel; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import org.springframework.web.util.UriComponentsBuilder; import lombok.SneakyThrows; @@ -40,109 +35,92 @@ class CollaborationControllerTest { } @Nested - class TestGetAllByVorgangId { - - private final Collaboration collaboration = OrganisationsEinheitCollaborationTestFactory.create(); + class TestGetById { - @SneakyThrows - @Test - void shouldCallCollaborationService() { - performRequest(); - - verify(service).getCollaborations(OrganisationsEinheitCollaborationTestFactory.VORGANG_ID); - } + private final CollaborationAssertions collaborationAssertions = new CollaborationAssertions(false); @Nested - class TestOnExistingCollaboration { + class OnOrganisationsEinheitCollaboration { - private final Stream<Collaboration> collaborations = Stream.of(collaboration); + public static final String COLLABORATION_ID = OrganisationsEinheitCollaborationTestFactory.ID; + private final Collaboration collaboration = OrganisationsEinheitCollaborationTestFactory.create(); @BeforeEach - void mock() { - when(service.getCollaborations(OrganisationsEinheitCollaborationTestFactory.VORGANG_ID)).thenReturn(collaborations); + void init() { + when(service.getById(OrganisationsEinheitCollaborationTestFactory.ID)).thenReturn(collaboration); + when(assembler.toModel(collaboration)).thenReturn(EntityModel.of(collaboration)); + } + + @Test + void shouldCallCollaborationService() { + performRequest(); + + verify(service).getById(COLLABORATION_ID); } @Test - void shouldCallAssembler() { + void shouldCreateModel() { performRequest(); - verify(assembler).toCollectionModel(collaborations, OrganisationsEinheitCollaborationTestFactory.VORGANG_ID); + verify(assembler).toModel(collaboration); } @SneakyThrows @Test void shouldReturnStatusOk() { - when(assembler.toCollectionModel(collaborations, OrganisationsEinheitCollaborationTestFactory.VORGANG_ID)) - .thenReturn(CollectionModel.of(Collections.singletonList(EntityModel.of(collaboration)))); - var response = performRequest(); response.andExpect(status().isOk()); } - @Nested - class TestResponseBody { - - @BeforeEach - void mockAssembler() { - when(assembler.toCollectionModel(collaborations, OrganisationsEinheitCollaborationTestFactory.VORGANG_ID)) - .thenReturn(CollectionModel.of(Collections.singletonList(EntityModel.of(collaboration)))); - } - - @SneakyThrows - @Test - void shouldNotHaveVorgangId() { - var response = performRequest(); - - response.andExpect(jsonPath("$.vorgangId").doesNotExist()); - } - - @SneakyThrows - @Test - void shouldNotHaveCollaborationVorgangId() { - var response = performRequest(); + @SneakyThrows + @Test + void shouldHaveContentWithFields() { + var response = performRequest(); - response.andExpect(jsonPath("$.collaborationVorgangId").doesNotExist()); - } + collaborationAssertions.assertHasFields(response); + } - @SneakyThrows - @Test - void shouldHaveTitel() { - var response = performRequest(); + @SneakyThrows + @Test + void shouldHaveContentWithLink() { + var response = performRequest(); - response.andExpect(jsonPath("$.content[0].titel").value(OrganisationsEinheitCollaborationTestFactory.TITEL)); - } + collaborationAssertions.assertHasZustaendigeStelleLink(response, OrganisationsEinheitController.PATH, + OrganisationsEinheitCollaborationTestFactory.ZUSTAENDIGE_STELLE); + } - @SneakyThrows - @Test - void shouldHaveBeschreibung() { - var response = performRequest(); + @SneakyThrows + private ResultActions performRequest() { + return mockMvc.perform(get(CollaborationController.PATH + "/" + COLLABORATION_ID)); + } + } - System.out.println(response.andReturn().getResponse().getContentAsString()); + @Nested + class OnFachstelleCollaboration { - response.andExpect(jsonPath("$.content[0].beschreibung").value(OrganisationsEinheitCollaborationTestFactory.BESCHREIBUNG)); - } + public static final String COLLABORATION_ID = FachstelleCollaborationTestFactory.ID; + private final Collaboration collaboration = FachstelleCollaborationTestFactory.create(); - @SneakyThrows - @Test - void shouldHaveZustaendigeStelle() { - var expectedLink = UriComponentsBuilder - .fromUriString("http://localhost") - .path(OrganisationsEinheitController.PATH) - .pathSegment(OrganisationsEinheitCollaborationTestFactory.ZUSTAENDIGE_STELLE) - .build(); + @BeforeEach + void init() { + when(service.getById(FachstelleCollaborationTestFactory.ID)).thenReturn(collaboration); + when(assembler.toModel(collaboration)).thenReturn(EntityModel.of(collaboration)); + } - var response = performRequest(); + @SneakyThrows + @Test + void shouldHaveContentWithLink() { + var response = performRequest(); - response.andExpect(jsonPath("$.content[0].zustaendigeStelle") - .value(expectedLink.toString())); - } + collaborationAssertions.assertHasZustaendigeStelleLink(response, FachstelleController.PATH, + FachstelleCollaborationTestFactory.ZUSTAENDIGE_STELLE); } - } - @SneakyThrows - private ResultActions performRequest() { - return mockMvc.perform(get(CollaborationController.PATH + "/" + OrganisationsEinheitCollaborationTestFactory.VORGANG_ID + "/collaborations")); + @SneakyThrows + private ResultActions performRequest() { + return mockMvc.perform(get(CollaborationController.PATH + "/" + COLLABORATION_ID)); + } } } } diff --git a/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/CollaborationModelAssemblerTest.java b/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/CollaborationModelAssemblerTest.java index 9d8d09e3a20f3b30e8f84e2af8a0e016b7579252..c2e4a7d26085f49987d665331226f0c8a6ad3da6 100644 --- a/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/CollaborationModelAssemblerTest.java +++ b/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/CollaborationModelAssemblerTest.java @@ -17,6 +17,7 @@ import org.springframework.hateoas.IanaLinkRelations; import org.springframework.hateoas.Link; import org.springframework.hateoas.UriTemplate; +import de.ozgcloud.alfa.collaboration.CollaborationController.CollaborationByVorgangController; import de.ozgcloud.alfa.common.ModelBuilder; import de.ozgcloud.alfa.common.command.CommandController.CommandByRelationController; import de.ozgcloud.alfa.vorgang.VorgangController; @@ -118,7 +119,7 @@ class CollaborationModelAssemblerTest { var collectionModel = callAssembler(); assertThat(collectionModel.getLink(IanaLinkRelations.SELF_VALUE)).get().extracting(Link::getHref) - .isEqualTo(CollaborationController.PATH + "/" + VorgangHeaderTestFactory.ID + "/collaborations"); + .isEqualTo(CollaborationByVorgangController.PATH + "/" + VorgangHeaderTestFactory.ID + "/collaborations"); } @Test @@ -156,7 +157,7 @@ class CollaborationModelAssemblerTest { var collectionModel = callAssembler(); assertThat(collectionModel.getLink(IanaLinkRelations.SELF_VALUE)).get().extracting(Link::getHref) - .isEqualTo(CollaborationController.PATH + "/" + VorgangHeaderTestFactory.ID + "/collaborations"); + .isEqualTo(CollaborationByVorgangController.PATH + "/" + VorgangHeaderTestFactory.ID + "/collaborations"); } @Test diff --git a/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/FachstelleCollaborationTestFactory.java b/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/FachstelleCollaborationTestFactory.java index cb55dbe7db7012f0e49b39f33265fdf2f67cd5fb..4090387d6aa3a6fe593d9a0f3d0e78230e796896 100644 --- a/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/FachstelleCollaborationTestFactory.java +++ b/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/FachstelleCollaborationTestFactory.java @@ -8,6 +8,7 @@ import de.ozgcloud.alfa.collaboration.FachstelleCollaboration.FachstelleCollabor public class FachstelleCollaborationTestFactory { + public static final String ID = UUID.randomUUID().toString(); public static final String VORGANG_ID = UUID.randomUUID().toString(); public static final String COLLABORATION_VORGANG_ID = UUID.randomUUID().toString(); public static final long COLLABORATION_LEVEL = 4L; @@ -22,6 +23,7 @@ public class FachstelleCollaborationTestFactory { private static FachstelleCollaborationBuilder createBuilder() { return FachstelleCollaboration.builder() + .id(ID) .vorgangId(VORGANG_ID) .collaborationVorgangId(COLLABORATION_VORGANG_ID) .collaborationLevel(COLLABORATION_LEVEL) diff --git a/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/GrpcCollaborationRequestForFachstelleTestFactory.java b/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/GrpcCollaborationRequestForFachstelleTestFactory.java index 6df288e13363ec4240ece9029c4a44d2083c9960..acab0e537c3fae1c888940931970114c1cd7a55f 100644 --- a/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/GrpcCollaborationRequestForFachstelleTestFactory.java +++ b/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/GrpcCollaborationRequestForFachstelleTestFactory.java @@ -1,12 +1,10 @@ package de.ozgcloud.alfa.collaboration; -import java.util.UUID; - import de.ozgcloud.collaboration.request.GrpcCollaborationRequest; class GrpcCollaborationRequestForFachstelleTestFactory { - public static final String ID = UUID.randomUUID().toString(); + public static final String ID = FachstelleCollaborationTestFactory.ID; public static final String COLLABORATION_VORGANG_ID = FachstelleCollaborationTestFactory.COLLABORATION_VORGANG_ID; public static final String TITEL = FachstelleCollaborationTestFactory.TITEL; public static final String BESCHREIBUNG = FachstelleCollaborationTestFactory.BESCHREIBUNG; diff --git a/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/GrpcCollaborationRequestForOrganisationsEinheitTestFactory.java b/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/GrpcCollaborationRequestForOrganisationsEinheitTestFactory.java index 0b4c49eb4190eda26bf99afcc5b13e75a60364f3..e60a7e5a2411ec02ae2abf69db1108d677a91e27 100644 --- a/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/GrpcCollaborationRequestForOrganisationsEinheitTestFactory.java +++ b/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/GrpcCollaborationRequestForOrganisationsEinheitTestFactory.java @@ -1,12 +1,10 @@ package de.ozgcloud.alfa.collaboration; -import java.util.UUID; - import de.ozgcloud.collaboration.request.GrpcCollaborationRequest; class GrpcCollaborationRequestForOrganisationsEinheitTestFactory { - public static final String ID = UUID.randomUUID().toString(); + public static final String ID = OrganisationsEinheitCollaborationTestFactory.ID; public static final String COLLABORATION_VORGANG_ID = OrganisationsEinheitCollaborationTestFactory.COLLABORATION_VORGANG_ID; public static final String TITEL = OrganisationsEinheitCollaborationTestFactory.TITEL; public static final String BESCHREIBUNG = OrganisationsEinheitCollaborationTestFactory.BESCHREIBUNG; diff --git a/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/OrganisationsEinheitCollaborationTestFactory.java b/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/OrganisationsEinheitCollaborationTestFactory.java index 5a21747edd2000bcb4dab54ea871d757b2603d71..29ba6f8f613b0197aaaeac0163fb54572f78aec8 100644 --- a/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/OrganisationsEinheitCollaborationTestFactory.java +++ b/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/OrganisationsEinheitCollaborationTestFactory.java @@ -8,6 +8,7 @@ import de.ozgcloud.alfa.collaboration.OrganisationsEinheitCollaboration.Organisa public class OrganisationsEinheitCollaborationTestFactory { + public static final String ID = UUID.randomUUID().toString(); public static final String VORGANG_ID = UUID.randomUUID().toString(); public static final String COLLABORATION_VORGANG_ID = UUID.randomUUID().toString(); public static final long COLLABORATION_LEVEL = 1L; @@ -22,6 +23,7 @@ public class OrganisationsEinheitCollaborationTestFactory { private static OrganisationsEinheitCollaborationBuilder createBuilder() { return OrganisationsEinheitCollaboration.builder() + .id(ID) .vorgangId(VORGANG_ID) .collaborationVorgangId(COLLABORATION_VORGANG_ID) .collaborationLevel(COLLABORATION_LEVEL) diff --git a/alfa-service/src/test/java/de/ozgcloud/alfa/common/CollectionModelBuilderTest.java b/alfa-service/src/test/java/de/ozgcloud/alfa/common/CollectionModelBuilderTest.java index 9a17c28370aeb3e29aa4e4f5066d05996d529470..76abf923218502732baf4b7657921f928bef713d 100644 --- a/alfa-service/src/test/java/de/ozgcloud/alfa/common/CollectionModelBuilderTest.java +++ b/alfa-service/src/test/java/de/ozgcloud/alfa/common/CollectionModelBuilderTest.java @@ -1,12 +1,15 @@ package de.ozgcloud.alfa.common; import static org.assertj.core.api.Assertions.*; +import static org.mockito.Mockito.*; import java.util.List; +import java.util.function.Supplier; import java.util.stream.Stream; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import org.mockito.Mock; import org.springframework.hateoas.Link; import de.ozgcloud.alfa.vorgang.VorgangHeaderTestFactory; @@ -88,6 +91,36 @@ class CollectionModelBuilderTest { assertThat(model.getLinks()).isEmpty(); } } + + @Nested + class TestWithLinkSupplier { + + @Mock + private Supplier<Link> linkSupplier; + + @Test + void shouldAddLink() { + when(linkSupplier.get()).thenReturn(Link.of(HREF, REL)); + + var model = CollectionModelBuilder.fromEntities(List.of()).ifMatch(() -> true).addLink(linkSupplier).buildModel(); + + assertThat(model.getLinks()).hasSize(1).first().extracting(Link::getHref, link -> link.getRel().value()).containsExactly(HREF, REL); + } + + @Test + void shouldNotCallLinkSupplier() { + CollectionModelBuilder.fromEntities(List.of()).ifMatch(() -> false).addLink(linkSupplier).buildModel(); + + verify(linkSupplier, never()).get(); + } + + @Test + void shouldNotAddLink() { + var model = CollectionModelBuilder.fromEntities(List.of()).ifMatch(() -> false).addLink(linkSupplier).buildModel(); + + assertThat(model.getLinks()).isEmpty(); + } + } } } \ No newline at end of file