From e31713ba6d9ec8c7d22e05fff188d45ba80ea495 Mon Sep 17 00:00:00 2001 From: OZGCloud <ozgcloud@mgm-tp.com> Date: Wed, 30 Oct 2024 08:12:49 +0100 Subject: [PATCH] OZG-7009 fix zustaendigeStelleId; validate collaborationLevel --- .../alfa/collaboration/Collaboration.java | 11 +++- .../OrganisationsEinheitController.java | 4 +- .../CollaborationTestFactory.java | 31 ++++++++--- .../alfa/common/command/CommandITCase.java | 52 ++++++++++++++++--- .../createCollaborationRequestBody.json.tmpl | 9 ++++ 5 files changed, 91 insertions(+), 16 deletions(-) create mode 100644 alfa-service/src/test/resources/jsonTemplates/command/createCollaborationRequestBody.json.tmpl 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 b062636d2f..8b13977760 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 @@ -1,14 +1,22 @@ package de.ozgcloud.alfa.collaboration; +import jakarta.validation.constraints.NotNull; + import com.fasterxml.jackson.annotation.JsonIgnore; import de.ozgcloud.alfa.common.LinkedResource; import de.ozgcloud.alfa.common.command.CommandBody; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; +import lombok.NoArgsConstructor; +//TODO Rename to CollaborationRequest @Getter @Builder +@NoArgsConstructor(access = AccessLevel.PRIVATE) +@AllArgsConstructor(access = AccessLevel.PRIVATE) public class Collaboration implements CommandBody { @JsonIgnore @@ -18,7 +26,8 @@ public class Collaboration implements CommandBody { private String titel; private String beschreibung; - private int collaborationLevel; + @NotNull + private Integer collaborationLevel; @LinkedResource(controllerClass = OrganisationsEinheitController.class) private String zustaendigeStelle; diff --git a/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/OrganisationsEinheitController.java b/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/OrganisationsEinheitController.java index e60539a723..29d5c233e6 100644 --- a/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/OrganisationsEinheitController.java +++ b/alfa-service/src/main/java/de/ozgcloud/alfa/collaboration/OrganisationsEinheitController.java @@ -13,10 +13,10 @@ import org.springframework.web.bind.annotation.RestController; import lombok.RequiredArgsConstructor; +@RequiredArgsConstructor @RestController @RequestMapping(OrganisationsEinheitController.PATH) -@RequiredArgsConstructor -class OrganisationsEinheitController { +public class OrganisationsEinheitController { static final String PATH = "/api/organisationseinheits"; // NOSONAR static final String SEARCH_BY_PARAM = "searchBy"; diff --git a/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/CollaborationTestFactory.java b/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/CollaborationTestFactory.java index 9959f1a2b1..505ddbaf56 100644 --- a/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/CollaborationTestFactory.java +++ b/alfa-service/src/test/java/de/ozgcloud/alfa/collaboration/CollaborationTestFactory.java @@ -1,30 +1,49 @@ package de.ozgcloud.alfa.collaboration; +import java.util.Objects; import java.util.UUID; import com.thedeanda.lorem.LoremIpsum; import de.ozgcloud.alfa.collaboration.Collaboration.CollaborationBuilder; +import de.ozgcloud.alfa.common.command.CommandOrder; +import de.ozgcloud.common.test.TestUtils; public class CollaborationTestFactory { public static final String VORGANG_ID = UUID.randomUUID().toString(); public static final String COLLABORATION_VORGANG_ID = UUID.randomUUID().toString(); public static final String TITEL = LoremIpsum.getInstance().getWords(7); - public static final String BESCHREIBUNG = LoremIpsum.getInstance().getParagraphs(2, 5); + public static final String BESCHREIBUNG = LoremIpsum.getInstance().getWords(10); public static final String ZUSTAENDIGE_STELLE = OrganisationsEinheitTestFactory.ID; + public static final Integer COLLABORATION_LEVEL = 1; public static Collaboration create() { - return createBuilder() - .build(); + return createBuilder().build(); } - private static CollaborationBuilder createBuilder() { + public static CollaborationBuilder createBuilder() { return Collaboration.builder() .vorgangId(VORGANG_ID) .collaborationVorgangId(COLLABORATION_VORGANG_ID) .titel(TITEL) .beschreibung(BESCHREIBUNG) - .zustaendigeStelle(ZUSTAENDIGE_STELLE); + .zustaendigeStelle(ZUSTAENDIGE_STELLE) + .collaborationLevel(COLLABORATION_LEVEL); } -} + + public static String buildCreateCollaborationRequestContent(Collaboration collaboration) { + return TestUtils.loadTextFile("jsonTemplates/command/createCollaborationRequestBody.json.tmpl", + CommandOrder.CREATE_COLLABORATION_REQUEST.name(), + collaboration.getTitel(), + collaboration.getBeschreibung(), + buildZustaendigeStelleUri(collaboration.getZustaendigeStelle()), + Objects.isNull(collaboration.getCollaborationLevel()) ? null : TestUtils.addQuote(collaboration.getCollaborationLevel().toString())); + } + + private static String buildZustaendigeStelleUri(String zustaendigeStelleId) { + return Objects.nonNull(zustaendigeStelleId) + ? "\"/api/organisationseinheits/" + zustaendigeStelleId + "\"" + : zustaendigeStelleId; + } +} \ No newline at end of file diff --git a/alfa-service/src/test/java/de/ozgcloud/alfa/common/command/CommandITCase.java b/alfa-service/src/test/java/de/ozgcloud/alfa/common/command/CommandITCase.java index 3f396641e5..422532823a 100644 --- a/alfa-service/src/test/java/de/ozgcloud/alfa/common/command/CommandITCase.java +++ b/alfa-service/src/test/java/de/ozgcloud/alfa/common/command/CommandITCase.java @@ -28,6 +28,7 @@ import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import org.apache.commons.lang3.ArrayUtils; @@ -47,6 +48,8 @@ import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; +import de.ozgcloud.alfa.collaboration.CollaborationTestFactory; +import de.ozgcloud.alfa.collaboration.OrganisationsEinheitHeaderTestFactory; import de.ozgcloud.alfa.common.ValidationMessageCodes; import de.ozgcloud.alfa.common.user.UserProfileTestFactory; import de.ozgcloud.alfa.postfach.PostfachMailTestFactory; @@ -56,6 +59,7 @@ import de.ozgcloud.alfa.vorgang.VorgangHeaderTestFactory; import de.ozgcloud.alfa.vorgang.VorgangWithEingangTestFactory; import de.ozgcloud.alfa.vorgang.forwarding.RedirectRequest; import de.ozgcloud.common.test.TestUtils; +import lombok.SneakyThrows; @AutoConfigureMockMvc @SpringBootTest @@ -68,6 +72,40 @@ public class CommandITCase { @MockBean private VorgangController vorgangController; + @DisplayName("Collaboration request") + @WithMockUser + @Nested + class TestCollaborationRequest { + + @Captor + private ArgumentCaptor<CreateCommand> commandCaptor; + + @SneakyThrows + @Test + void shouldValidateCollaborationLevel() { + var requestContent = CollaborationTestFactory.buildCreateCollaborationRequestContent(CollaborationTestFactory.createBuilder() + .collaborationLevel(null) + .build()); + + doRequest(requestContent).andDo(print()).andExpect(status().isUnprocessableEntity()) + .andExpect(jsonPath("$.invalidParams.length()").value(1)) + .andExpect(jsonPath("$.invalidParams[0].name").value("createCommand.command.body.collaborationLevel")) + .andExpect(jsonPath("$.invalidParams[0].reason").value("must not be null")); + } + + @SneakyThrows + @Test + void shouldExtractZustaendigeStelleId() { + when(commandRemoteService.createCommand(any())).thenReturn(CommandTestFactory.create()); + var requestContent = CollaborationTestFactory.buildCreateCollaborationRequestContent(CollaborationTestFactory.create()); + + doRequest(requestContent); + + verify(commandRemoteService).createCommand(commandCaptor.capture()); + assertThat(commandCaptor.getValue().getBody()).hasFieldOrPropertyWithValue("zustaendigeStelle", OrganisationsEinheitHeaderTestFactory.ID); + } + } + @Nested @WithMockUser class TestAssignUser { @@ -295,13 +333,13 @@ public class CommandITCase { } } } + } - ResultActions doRequest(String content) throws Exception { - return mockMvc.perform(post("/api/vorgangs/" + CommandTestFactory.VORGANG_ID + "/relations/" + CommandTestFactory.RELATION_ID + "/" - + CommandTestFactory.RELATION_VERSION + "/commands") - .with(csrf()) - .contentType(MediaType.APPLICATION_JSON) - .content(content)); - } + ResultActions doRequest(String content) throws Exception { + return mockMvc.perform(post("/api/vorgangs/" + CommandTestFactory.VORGANG_ID + "/relations/" + CommandTestFactory.RELATION_ID + "/" + + CommandTestFactory.RELATION_VERSION + "/commands") + .with(csrf()) + .contentType(MediaType.APPLICATION_JSON) + .content(content)); } } \ No newline at end of file diff --git a/alfa-service/src/test/resources/jsonTemplates/command/createCollaborationRequestBody.json.tmpl b/alfa-service/src/test/resources/jsonTemplates/command/createCollaborationRequestBody.json.tmpl new file mode 100644 index 0000000000..6649d26825 --- /dev/null +++ b/alfa-service/src/test/resources/jsonTemplates/command/createCollaborationRequestBody.json.tmpl @@ -0,0 +1,9 @@ +{ + "order": "%s", + "body": { + "titel": "%s", + "beschreibung": "%s", + "zustaendigeStelle": %s, + "collaborationLevel": %s + } +} \ No newline at end of file -- GitLab