diff --git a/goofy-server/src/main/java/de/itvsh/goofy/wiedervorlage/WiedervorlageCommandController.java b/goofy-server/src/main/java/de/itvsh/goofy/wiedervorlage/WiedervorlageCommandController.java index d5ed213e3aba8d0898044548ef843c56a6ef8613..1ac411298f08e6b98880b0e196b004bb4ced2389 100644 --- a/goofy-server/src/main/java/de/itvsh/goofy/wiedervorlage/WiedervorlageCommandController.java +++ b/goofy-server/src/main/java/de/itvsh/goofy/wiedervorlage/WiedervorlageCommandController.java @@ -34,30 +34,46 @@ public class WiedervorlageCommandController { @PostMapping public ResponseEntity<Void> updateWiedervorlage(@RequestBody WiedervorlageCommand command, @PathVariable String wiedervorlageId, @PathVariable long wiedervorlageVersion) { - var preparedWiedervorlage = prepareWiedervorlageForOrder(command, wiedervorlageId); + var commandWithUpdatedWiedervorlage = buildCommand(service.getById(wiedervorlageId), command, wiedervorlageVersion); - var createdCommand = createCommandByOrder(command.getOrder(), preparedWiedervorlage, wiedervorlageId, wiedervorlageVersion); + var createdCommand = createByOrder(commandWithUpdatedWiedervorlage, command.getOrder(), wiedervorlageVersion); - service.updateNextFrist(preparedWiedervorlage, preparedWiedervorlage.getVorgangId()); + service.updateNextFrist((Wiedervorlage) commandWithUpdatedWiedervorlage.getBody(), commandWithUpdatedWiedervorlage.getVorgangId()); return ResponseEntity.created(linkTo(CommandController.class).slash(createdCommand.getId()).toUri()).build(); } - Wiedervorlage prepareWiedervorlageForOrder(WiedervorlageCommand command, String wiedervorlageId) { - var loadedWiedervorlage = service.getById(wiedervorlageId); + CreateCommand buildCommand(Wiedervorlage wiedervorlage, WiedervorlageCommand command, long wiedervorlageVersion) { + var commandBuilder = CreateCommand.builder() + .order(CommandOrder.UPDATE_ATTACHED_ITEM) + .relationId(wiedervorlage.getId()) + .relationVersion(wiedervorlageVersion) + .vorgangId(wiedervorlage.getVorgangId()); switch (command.getOrder()) { case WIEDERVORLAGE_ERLEDIGEN: { - return loadedWiedervorlage.toBuilder().done(true).build(); + return commandBuilder.body(wiedervorlage.toBuilder().done(true).build()).build(); } case WIEDERVORLAGE_WIEDEREROEFFNEN: { - return loadedWiedervorlage.toBuilder().done(false).build(); + return commandBuilder.body(wiedervorlage.toBuilder().done(false).build()).build(); } default: - return updateWiedervorlageByCommand(loadedWiedervorlage, command); + return commandBuilder.body(updateWiedervorlageByCommand(wiedervorlage, command)).build(); } } + Command createByOrder(CreateCommand command, CommandOrder commandOrder, long wiedervorlageVersion) { + if (shouldBeValidated(commandOrder)) { + return service.editWiedervorlage((Wiedervorlage) command.getBody(), command.getRelationId(), wiedervorlageVersion); + } else { + return commandByRelationController.createCommandWithoutValidation(command, WiedervorlageRemoteService.ITEM_NAME, wiedervorlageVersion); + } + } + + private boolean shouldBeValidated(CommandOrder commandOrder) { + return commandOrder == CommandOrder.EDIT_WIEDERVORLAGE; + } + Wiedervorlage updateWiedervorlageByCommand(Wiedervorlage wiedervorlage, WiedervorlageCommand command) { return wiedervorlage.toBuilder() .clearAttachments().attachments(stripAttachmentFileUriToId(command.getWiedervorlage()).getAttachments()) @@ -66,26 +82,6 @@ public class WiedervorlageCommandController { .frist(command.getWiedervorlage().getFrist()).build(); } - Command createCommandByOrder(CommandOrder commandOrder, Wiedervorlage wiedervorlage, String wiedervorlageId, long wiedervorlageVersion) { - if (commandOrder == CommandOrder.EDIT_WIEDERVORLAGE) { - return service.editWiedervorlage(wiedervorlage, wiedervorlageId, wiedervorlageVersion); - } else { - var createCommand = buildCommand(wiedervorlage, wiedervorlageVersion); - return commandByRelationController.createCommandWithoutValidation(createCommand, WiedervorlageRemoteService.ITEM_NAME, - wiedervorlageVersion); - } - } - - CreateCommand buildCommand(Wiedervorlage wiedervorlage, long wiedervorlageVersion) { - return CreateCommand.builder() - .order(CommandOrder.UPDATE_ATTACHED_ITEM) - .relationId(wiedervorlage.getId()) - .relationVersion(wiedervorlageVersion) - .vorgangId(wiedervorlage.getVorgangId()) - .body(wiedervorlage) - .build(); - } - @RestController @RequestMapping(WiedervorlageCommandByVorgangController.WIEDERVORLAGE_COMMANDS_BY_VORGANG) public static class WiedervorlageCommandByVorgangController { diff --git a/goofy-server/src/test/java/de/itvsh/goofy/wiedervorlage/WiedervorlageCommandControllerTest.java b/goofy-server/src/test/java/de/itvsh/goofy/wiedervorlage/WiedervorlageCommandControllerTest.java index 9a59b341c85508cd723d09a95ecded1c2be6cdbd..75b74286112a09ab33960d5f07ff50f0e8a543f5 100644 --- a/goofy-server/src/test/java/de/itvsh/goofy/wiedervorlage/WiedervorlageCommandControllerTest.java +++ b/goofy-server/src/test/java/de/itvsh/goofy/wiedervorlage/WiedervorlageCommandControllerTest.java @@ -23,6 +23,7 @@ import static org.assertj.core.api.Assertions.*; import de.itvsh.goofy.common.binaryfile.BinaryFileTestFactory; import de.itvsh.goofy.common.binaryfile.FileId; +import de.itvsh.goofy.common.command.Command; import de.itvsh.goofy.common.command.CommandController.CommandByRelationController; import de.itvsh.goofy.common.command.CommandOrder; import de.itvsh.goofy.common.command.CommandTestFactory; @@ -50,35 +51,33 @@ class WiedervorlageCommandControllerTest { mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); } - @DisplayName("Update wiedervoralge") + @DisplayName("Create command") @Nested - class TestUpdateWiedervorlage { + class TestCreateCommand { private static final String RESPONSE_HEADER = "http://localhost/api/commands/" + WiedervorlageCommandTestFactory.ID; - @DisplayName("controller methods") @Nested - class TestControllerMethods { + class ControllerMethods { @BeforeEach void init() { - doReturn(WiedervorlageTestFactory.create()).when(controller).prepareWiedervorlageForOrder(any(), any()); - doReturn(CommandTestFactory.create()).when(controller).createCommandByOrder(any(), any(), any(), anyLong()); + when(service.getById(any())).thenReturn(WiedervorlageTestFactory.create()); + doReturn(CommandTestFactory.create()).when(controller).createByOrder(any(), any(), anyLong()); } @Test - void shouldCallPrepareWiedervorlageForOrder() throws Exception { + void shouldCallServiceGetById() throws Exception { doRequest(); - verify(controller).prepareWiedervorlageForOrder(any(WiedervorlageCommand.class), eq(WiedervorlageTestFactory.ID)); + verify(service).getById(WiedervorlageTestFactory.ID); } @Test - void shouldCallCreateCommandByOrder() throws Exception { + void shouldCallCreateByOrderController() throws Exception { doRequest(); - verify(controller).createCommandByOrder(eq(WiedervorlageCommandTestFactory.ORDER), any(Wiedervorlage.class), - eq(WiedervorlageTestFactory.ID), eq(WiedervorlageTestFactory.VERSION)); + verify(controller).createByOrder(any(), any(), anyLong()); } @Test @@ -98,6 +97,13 @@ class WiedervorlageCommandControllerTest { doRequest().andExpect(header().string("Location", RESPONSE_HEADER)); } + @Test + void shouldBuildCommand() throws Exception { + doRequest(); + + verify(controller).buildCommand(any(Wiedervorlage.class), any(WiedervorlageCommand.class), eq(WiedervorlageTestFactory.VERSION)); + } + private ResultActions doRequest() throws Exception { return mockMvc.perform( post(WiedervorlageCommandController.WIEDERVORLAGE_COMMANDS, WiedervorlageTestFactory.ID, WiedervorlageTestFactory.VERSION) @@ -107,140 +113,172 @@ class WiedervorlageCommandControllerTest { } @Nested - class TestPrepareWiedervorlageForOrder { + class BuildCommand { - @BeforeEach - void init() { - when(service.getById(anyString())).thenReturn(WiedervorlageTestFactory.create()); + @Test + void shouldHaveOrder() { + var command = callBuildCommand(); + + assertThat(command.getOrder()).isEqualTo(CommandOrder.UPDATE_ATTACHED_ITEM); } @Test - void shouldCallGetById() { - callPrepareWiedervorlageForOrder(WiedervorlageCommandTestFactory.create()); + void shouldHaveRelationId() { + var command = callBuildCommand(); - verify(service).getById(WiedervorlageTestFactory.ID); + assertThat(command.getRelationId()).isEqualTo(WiedervorlageTestFactory.ID); } @Test - void shouldHaveSetDoneTrueOnErledigenOrder() { - var command = WiedervorlageCommandTestFactory.createBuilder() - .wiedervorlage(WiedervorlageTestFactory.createBuilder().done(false).build()) - .order(CommandOrder.WIEDERVORLAGE_ERLEDIGEN) - .build(); - var preparedWiedervorlage = callPrepareWiedervorlageForOrder(command); - - assertThat(preparedWiedervorlage.isDone()).isTrue(); + void shouldHaveRelationVersion() { + var command = callBuildCommand(); + + assertThat(command.getRelationVersion()).isEqualTo(WiedervorlageTestFactory.VERSION); } @Test - void shouldHaveSetDoneFalseOnWiedereroeffnenOrder() { - var command = WiedervorlageCommandTestFactory.createBuilder() - .wiedervorlage(WiedervorlageTestFactory.createBuilder().done(true).build()) - .order(CommandOrder.WIEDERVORLAGE_WIEDEREROEFFNEN) - .build(); - var preparedWiedervorlage = callPrepareWiedervorlageForOrder(command); - - assertThat(preparedWiedervorlage.isDone()).isFalse(); + void shouldHaveVorgangId() { + var command = callBuildCommand(); + + assertThat(command.getVorgangId()).isEqualTo(VorgangHeaderTestFactory.ID); } - @Test - void shouldCallUpdateWiedervorlageByCommandOnEditOrder() { - doReturn(WiedervorlageTestFactory.create()).when(controller).updateWiedervorlageByCommand(any(), any()); + @Nested + class OnErledigenOrder { - var command = WiedervorlageCommandTestFactory.createBuilder() - .order(CommandOrder.EDIT_WIEDERVORLAGE) - .build(); - callPrepareWiedervorlageForOrder(command); + @Test + void shouldHaveDoneTrue() { + var command = callBuildCommand(CommandOrder.WIEDERVORLAGE_ERLEDIGEN); - verify(controller).updateWiedervorlageByCommand(any(Wiedervorlage.class), eq(command)); + assertThat(((Wiedervorlage) command.getBody()).isDone()).isTrue(); + } - } + @Test + void shouldContainsExistingWiedervorlageAsBody() { + var command = callBuildCommand(CommandOrder.WIEDERVORLAGE_ERLEDIGEN); - private Wiedervorlage callPrepareWiedervorlageForOrder(WiedervorlageCommand command) { - return controller.prepareWiedervorlageForOrder(command, WiedervorlageTestFactory.ID); + assertThat(((Wiedervorlage) command.getBody())).usingRecursiveComparison().ignoringFields("done") + .isEqualTo(WiedervorlageTestFactory.create()); + } } - } - @DisplayName("Create command by order") - @Nested - class TestCreateCommandByOrder { + @Nested + class OnWiedereroeffnenOrder { - private final Wiedervorlage wiedervorlage = WiedervorlageTestFactory.create(); + @Test + void shouldHaveDoneFalse() { + var command = callBuildCommand(CommandOrder.WIEDERVORLAGE_WIEDEREROEFFNEN); - @Nested - class TestOnEditOrder { + assertThat(((Wiedervorlage) command.getBody()).isDone()).isFalse(); + } @Test - void shouldCallServiceOnEdit() { - controller.createCommandByOrder(CommandOrder.EDIT_WIEDERVORLAGE, wiedervorlage, WiedervorlageTestFactory.ID, - WiedervorlageTestFactory.VERSION); + void shouldContainsExistingWiedervorlageAsBody() { + var command = callBuildCommand(CommandOrder.WIEDERVORLAGE_WIEDEREROEFFNEN); - verify(service).editWiedervorlage(wiedervorlage, WiedervorlageTestFactory.ID, WiedervorlageTestFactory.VERSION); + assertThat(((Wiedervorlage) command.getBody())).usingRecursiveComparison().ignoringFields("done") + .isEqualTo(WiedervorlageTestFactory.create()); } } @Nested - class TestOnOtherOrder { + class OnEditOrder { + + @Test + void shouldHaveDoneByCommand() { + var command = callBuildCommandByEditOrder(); + + assertThat(((Wiedervorlage) command.getBody()).isDone()).isEqualTo(WiedervorlageTestFactory.DONE); + } - private final CreateCommand createCommand = CommandTestFactory.createCreateCommand(); + @Test + void shouldHaveAttachmentsByCommand() { + var command = callBuildCommandByEditOrder(); + + assertThat(((Wiedervorlage) command.getBody()).getAttachments().get(0)).isEqualTo(BinaryFileTestFactory.FILE_ID); + } - @BeforeEach - void init() { - doReturn(createCommand).when(controller).buildCommand(any(), anyLong()); + @Test + void shouldHaveBetreffByCommand() { + var command = callBuildCommandByEditOrder(); + + assertThat(((Wiedervorlage) command.getBody()).getBetreff()).isEqualTo(WiedervorlageTestFactory.BETREFF); } @Test - void shouldCallBuildCommand() { - controller.createCommandByOrder(CommandOrder.WIEDERVORLAGE_ERLEDIGEN, wiedervorlage, - WiedervorlageTestFactory.ID, WiedervorlageTestFactory.VERSION); + void shouldHaveBeschreibungByCommand() { + var command = callBuildCommandByEditOrder(); - verify(controller).buildCommand(wiedervorlage, WiedervorlageTestFactory.VERSION); + assertThat(((Wiedervorlage) command.getBody()).getBeschreibung()).isEqualTo(WiedervorlageTestFactory.BESCHREIBUNG); } @Test - void shouldCallCommandByRelationController() { - controller.createCommandByOrder(CommandOrder.WIEDERVORLAGE_ERLEDIGEN, wiedervorlage, - WiedervorlageTestFactory.ID, WiedervorlageTestFactory.VERSION); + void shouldHaveFristByCommand() { + var command = callBuildCommandByEditOrder(); - verify(commandByRelationController).createCommandWithoutValidation(createCommand, WiedervorlageRemoteService.ITEM_NAME, - WiedervorlageTestFactory.VERSION); + assertThat(((Wiedervorlage) command.getBody()).getFrist()).isEqualTo(WiedervorlageTestFactory.FRIST); + } + + CreateCommand callBuildCommandByEditOrder() { + return callBuildCommand(WiedervorlageCommandTestFactory.createBuilder().order(CommandOrder.EDIT_WIEDERVORLAGE).build()); } } - } - @Nested - class BuildCommand { + CreateCommand callBuildCommand(CommandOrder order) { + return callBuildCommand(WiedervorlageCommandTestFactory.createBuilder().order(order).build()); + } - @Test - void shouldHaveOrder() { - var command = callBuildCommand(); + CreateCommand callBuildCommand() { + return callBuildCommand(WiedervorlageCommandTestFactory.create()); + } - assertThat(command.getOrder()).isEqualTo(CommandOrder.UPDATE_ATTACHED_ITEM); + CreateCommand callBuildCommand(WiedervorlageCommand command) { + return controller.buildCommand(WiedervorlageTestFactory.create(), command, WiedervorlageTestFactory.VERSION); } + } - @Test - void shouldHaveRelationId() { - var command = callBuildCommand(); + @Nested + class CreateByOrder { - assertThat(command.getRelationId()).isEqualTo(WiedervorlageTestFactory.ID); + @Nested + class ForEdit { + + @Test + void shouldCallCreateCommand() { + createCommandByOrder(CommandOrder.EDIT_WIEDERVORLAGE); + + verify(service).editWiedervorlage(any(Wiedervorlage.class), eq(CommandTestFactory.RELATION_ID), + eq(WiedervorlageTestFactory.VERSION)); + } } - @Test - void shouldHaveRelationVersion() { - var command = callBuildCommand(); + @Nested + class ForErledigen { - assertThat(command.getRelationVersion()).isEqualTo(WiedervorlageTestFactory.VERSION); + @Test + void shouldCallCreateCommandWithoutValidation() { + createCommandByOrder(CommandOrder.WIEDERVORLAGE_ERLEDIGEN); + + verify(commandByRelationController).createCommandWithoutValidation(any(), eq(WiedervorlageRemoteService.ITEM_NAME), + eq(WiedervorlageTestFactory.VERSION)); + } } - @Test - void shouldHaveVorgangId() { - var command = callBuildCommand(); + @Nested + class ForWiedereroeffnen { - assertThat(command.getVorgangId()).isEqualTo(VorgangHeaderTestFactory.ID); + @Test + void shouldCallCreateCommandWithoutValidation() { + createCommandByOrder(CommandOrder.WIEDERVORLAGE_WIEDEREROEFFNEN); + + verify(commandByRelationController).createCommandWithoutValidation(any(), eq(WiedervorlageRemoteService.ITEM_NAME), + eq(WiedervorlageTestFactory.VERSION)); + } } - private CreateCommand callBuildCommand() { - return controller.buildCommand(WiedervorlageTestFactory.create(), WiedervorlageTestFactory.VERSION); + private Command createCommandByOrder(CommandOrder order) { + return controller.createByOrder(CommandTestFactory.createCreateCommandBuilder().body(WiedervorlageTestFactory.create()).build(), + order, WiedervorlageTestFactory.VERSION); } } }