Skip to content
Snippets Groups Projects
Commit bf0c9baf authored by OZGCloud's avatar OZGCloud
Browse files

OZG-2566 OZG-2684 simplify editWiedervorlage changes

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