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 f02f000c3e3f93d9b4a151c3fc50c9e0e2e958dc..f73fd1f86b856a832f4d7bc1f60db2e72a447fbd 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 @@ -16,6 +16,10 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import de.itvsh.goofy.common.binaryfile.FileId; +import de.itvsh.goofy.postfach.PostfachMail; +import de.itvsh.goofy.vorgang.VorgangController; +import de.itvsh.goofy.vorgang.VorgangWithEingang; import lombok.Getter; @RestController @@ -68,16 +72,49 @@ public class CommandController { @Autowired private CommandService service; + @Autowired + private VorgangController vorgangController; @PostMapping public ResponseEntity<Void> createCommand(@PathVariable String vorgangId, @PathVariable String relationId, @PathVariable long relationVersion, @RequestBody CreateCommand command) { command = command.toBuilder().vorgangId(vorgangId).relationId(relationId).build(); + + if (isSendPostfachMailOrder(command)) { + command = prepareCommandForPostfachNachricht(command, vorgangId); + } + var created = createCommand(command, relationVersion); return ResponseEntity.created(linkTo(CommandController.class).slash(created.getId()).toUri()).build(); } + private boolean isSendPostfachMailOrder(CreateCommand command) { + return command.getOrder() == CommandOrder.SEND_POSTFACH_MAIL || command.getOrder() == CommandOrder.SEND_POSTFACH_NACHRICHT; + } + + CreateCommand prepareCommandForPostfachNachricht(CreateCommand command, String vorgangId) { + var postfachBody = (PostfachMail) command.getBody(); + var attachments = postfachBody.getAttachments().stream().map(this::stripToFileId).toList(); + postfachBody = postfachBody.toBuilder().postfachId(getPostfachId(vorgangId)) + .clearAttachments().attachments(attachments) + .build(); + return command.toBuilder().body(postfachBody).build(); + } + + private FileId stripToFileId(FileId fileUri) { + var fileUriStr = fileUri.toString(); + return FileId.from(fileUriStr.substring(fileUriStr.lastIndexOf("/") + 1, fileUriStr.length())); + } + + private String getPostfachId(String vorgangId) { + return getVorgang(vorgangId).getEingang().getAntragsteller().getPostfachId(); + } + + private VorgangWithEingang getVorgang(String vorgangId) { + return vorgangController.getVorgang(vorgangId); + } + public Command createCommand(CreateCommand command, long version) { return service.createCommand(command, version); } 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 e98357d2cfd302867c266da614d38687a35db42b..1d701b2269b468c4d63d4a3f13309c47d7e930af 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 @@ -16,6 +16,7 @@ import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.Spy; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; @@ -23,10 +24,15 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders; import static org.assertj.core.api.Assertions.*; +import de.itvsh.goofy.common.binaryfile.FileId; import de.itvsh.goofy.common.command.CommandController.CommandByRelationController; import de.itvsh.goofy.common.errorhandling.ExceptionController; +import de.itvsh.goofy.postfach.PostfachMail; +import de.itvsh.goofy.postfach.PostfachMailTestFactory; +import de.itvsh.goofy.vorgang.AntragstellerTestFactory; import de.itvsh.goofy.vorgang.VorgangController; import de.itvsh.goofy.vorgang.VorgangHeaderTestFactory; +import de.itvsh.goofy.vorgang.VorgangWithEingangTestFactory; import de.itvsh.kop.common.test.TestUtils; class CommandControllerTest { @@ -37,8 +43,6 @@ class CommandControllerTest { private CommandService service; @Mock private CommandModelAssembler modelAssembler; - @Mock - private VorgangController vorgangController; private MockMvc mockMvc; @@ -116,12 +120,15 @@ class CommandControllerTest { @Nested class TestCommandByRelationController { + @Spy @InjectMocks // NOSONAR private CommandByRelationController controller; @Mock private CommandService service; @Mock private CommandModelAssembler modelAssembler; + @Mock + private VorgangController vorgangController; private MockMvc mockMvc; @@ -164,6 +171,20 @@ class CommandControllerTest { .andExpect(header().stringValues("location", "http://localhost" + COMMANDS_PATH + "/" + CommandTestFactory.ID)); } + @Nested + @DisplayName("CreateCommand with postfach mail") + class WithPostfachMail { + + @Test + void shouldPrepareForPostfachNachricht() throws Exception { + doReturn(CommandTestFactory.createCreateCommand()).when(controller).prepareCommandForPostfachNachricht(any(), any()); + + doRequest(PostfachMailTestFactory.buildSendPostfachMailContent()); + + verify(controller).prepareCommandForPostfachNachricht(any(), eq(VorgangHeaderTestFactory.ID)); + } + } + private ResultActions doRequest() throws Exception { return doRequest( TestUtils.loadTextFile("jsonTemplates/command/createVorgangCommand.json.tmpl", CommandOrder.VORGANG_ANNEHMEN.name())); @@ -180,6 +201,53 @@ class CommandControllerTest { } } + @Nested + class TestPrepareCommandForPostfachNachricht { + + @BeforeEach + void mockVorgangController() { + when(vorgangController.getVorgang(anyString())).thenReturn(VorgangWithEingangTestFactory.create()); + } + + @Test + void shouldCallVorgangController() { + controller.prepareCommandForPostfachNachricht(createPostfachCreateCommand(), + VorgangHeaderTestFactory.ID); + + verify(vorgangController).getVorgang(VorgangHeaderTestFactory.ID); + } + + @Test + void shouldMapAttachments() { + var preparedCommand = controller.prepareCommandForPostfachNachricht(createPostfachCreateCommand(), VorgangHeaderTestFactory.ID); + + assertThat(((PostfachMail) preparedCommand.getBody()).getAttachments()).contains(FileId.from(PostfachMailTestFactory.ID)); + } + + @Test + void shouldAddPostfachId() { + var preparedCommand = controller.prepareCommandForPostfachNachricht(createPostfachCreateCommand(), VorgangHeaderTestFactory.ID); + + assertThat(((PostfachMail) preparedCommand.getBody()).getPostfachId()).isEqualTo(AntragstellerTestFactory.POSTFACH_ID); + } + + @Test + void shouldProceedWithEmptyAttachments() { + var createCommand = CommandTestFactory.createCreateCommandBuilder() + .body(PostfachMailTestFactory.createBuilder().clearAttachments().build()).build(); + + var preparedCommand = controller.prepareCommandForPostfachNachricht(createCommand, VorgangHeaderTestFactory.ID); + + assertThat(((PostfachMail) preparedCommand.getBody()).getAttachments()).isEmpty(); + } + + private CreateCommand createPostfachCreateCommand() { + var postfachNachricht = PostfachMailTestFactory.createBuilder().clearAttachments() + .attachment(FileId.from("api/resource/" + PostfachMailTestFactory.ID)).build(); + return CommandTestFactory.createCreateCommandBuilder().body(postfachNachricht).build(); + } + } + @Nested class CreateByVorgangAndItemName { diff --git a/goofy-server/src/test/java/de/itvsh/goofy/postfach/PostfachMailTestFactory.java b/goofy-server/src/test/java/de/itvsh/goofy/postfach/PostfachMailTestFactory.java index bf3c56e612c8df5b84f05cd6b437ee16503defe8..4d2661b45aa7355d3eb52348f1e4292baa54cb41 100644 --- a/goofy-server/src/test/java/de/itvsh/goofy/postfach/PostfachMailTestFactory.java +++ b/goofy-server/src/test/java/de/itvsh/goofy/postfach/PostfachMailTestFactory.java @@ -79,7 +79,8 @@ public class PostfachMailTestFactory { return TestUtils.loadTextFile("jsonTemplates/command/createCommandWithPostfachMail.json.tmpl", order.name(), postfachMail.getReplyOption().name(), TestUtils.addQuote(postfachMail.getSubject()), - TestUtils.addQuote(postfachMail.getMailBody())); + TestUtils.addQuote(postfachMail.getMailBody()), + postfachMail.getAttachments().get(0).toString()); } public static String buildResendPostfachMailContent() { diff --git a/goofy-server/src/test/resources/jsonTemplates/command/createCommandWithPostfachMail.json.tmpl b/goofy-server/src/test/resources/jsonTemplates/command/createCommandWithPostfachMail.json.tmpl index 48616ee6e0851973aca270eeaf5cd3abc864081c..1481e0b8af4c3810e80c70855a01bab4351b59b8 100644 --- a/goofy-server/src/test/resources/jsonTemplates/command/createCommandWithPostfachMail.json.tmpl +++ b/goofy-server/src/test/resources/jsonTemplates/command/createCommandWithPostfachMail.json.tmpl @@ -3,6 +3,7 @@ "body": { "replyOption": "%s", "subject": %s, - "mailBody": %s + "mailBody": %s, + "attachments": ["%s"] } } \ No newline at end of file