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 {
@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 {
......
......@@ -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 {
@BeforeEach
void init() {
when(service.getById(anyString())).thenReturn(WiedervorlageTestFactory.create());
}
class BuildCommand {
@Test
void shouldCallGetById() {
callPrepareWiedervorlageForOrder(WiedervorlageCommandTestFactory.create());
void shouldHaveOrder() {
var command = callBuildCommand();
verify(service).getById(WiedervorlageTestFactory.ID);
assertThat(command.getOrder()).isEqualTo(CommandOrder.UPDATE_ATTACHED_ITEM);
}
@Test
void shouldHaveSetDoneTrueOnErledigenOrder() {
var command = WiedervorlageCommandTestFactory.createBuilder()
.wiedervorlage(WiedervorlageTestFactory.createBuilder().done(false).build())
.order(CommandOrder.WIEDERVORLAGE_ERLEDIGEN)
.build();
var preparedWiedervorlage = callPrepareWiedervorlageForOrder(command);
void shouldHaveRelationId() {
var command = callBuildCommand();
assertThat(preparedWiedervorlage.isDone()).isTrue();
assertThat(command.getRelationId()).isEqualTo(WiedervorlageTestFactory.ID);
}
@Test
void shouldHaveSetDoneFalseOnWiedereroeffnenOrder() {
var command = WiedervorlageCommandTestFactory.createBuilder()
.wiedervorlage(WiedervorlageTestFactory.createBuilder().done(true).build())
.order(CommandOrder.WIEDERVORLAGE_WIEDEREROEFFNEN)
.build();
var preparedWiedervorlage = callPrepareWiedervorlageForOrder(command);
void shouldHaveRelationVersion() {
var command = callBuildCommand();
assertThat(preparedWiedervorlage.isDone()).isFalse();
assertThat(command.getRelationVersion()).isEqualTo(WiedervorlageTestFactory.VERSION);
}
@Test
void shouldCallUpdateWiedervorlageByCommandOnEditOrder() {
doReturn(WiedervorlageTestFactory.create()).when(controller).updateWiedervorlageByCommand(any(), any());
void shouldHaveVorgangId() {
var command = callBuildCommand();
var command = WiedervorlageCommandTestFactory.createBuilder()
.order(CommandOrder.EDIT_WIEDERVORLAGE)
.build();
callPrepareWiedervorlageForOrder(command);
assertThat(command.getVorgangId()).isEqualTo(VorgangHeaderTestFactory.ID);
}
@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) {
return controller.prepareWiedervorlageForOrder(command, WiedervorlageTestFactory.ID);
@Test
void shouldContainsExistingWiedervorlageAsBody() {
var command = callBuildCommand(CommandOrder.WIEDERVORLAGE_ERLEDIGEN);
assertThat(((Wiedervorlage) command.getBody())).usingRecursiveComparison().ignoringFields("done")
.isEqualTo(WiedervorlageTestFactory.create());
}
}
@DisplayName("Create command by order")
@Nested
class TestCreateCommandByOrder {
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 {
private final CreateCommand createCommand = CommandTestFactory.createCreateCommand();
@Test
void shouldHaveDoneByCommand() {
var command = callBuildCommandByEditOrder();
@BeforeEach
void init() {
doReturn(createCommand).when(controller).buildCommand(any(), anyLong());
assertThat(((Wiedervorlage) command.getBody()).isDone()).isEqualTo(WiedervorlageTestFactory.DONE);
}
@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
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);
}
}
@Nested
class CreateByOrder {
@Nested
class ForEdit {
@Test
void shouldHaveRelationId() {
var command = callBuildCommand();
void shouldCallCreateCommand() {
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
void shouldHaveRelationVersion() {
var command = callBuildCommand();
void shouldCallCreateCommandWithoutValidation() {
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
void shouldHaveVorgangId() {
var command = callBuildCommand();
void shouldCallCreateCommandWithoutValidation() {
createCommandByOrder(CommandOrder.WIEDERVORLAGE_WIEDEREROEFFNEN);
assertThat(command.getVorgangId()).isEqualTo(VorgangHeaderTestFactory.ID);
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);
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment