Skip to content
Snippets Groups Projects
Commit b84b32e0 authored by Martin's avatar Martin
Browse files

OZG-7872 use enum instead of string; adjust naming; remove redundant order

parent 9b6c9a56
Branches
Tags
1 merge request!26OZG-7872 OZG-8039 add related resource link on postfach
...@@ -58,15 +58,13 @@ class CommandModelAssembler implements RepresentationModelAssembler<Command, Ent ...@@ -58,15 +58,13 @@ class CommandModelAssembler implements RepresentationModelAssembler<Command, Ent
static final Predicate<Command> HAS_KNOWN_COMMAND_ORDER = command -> command.getCommandOrder() != CommandOrder.UNBEKANNT; static final Predicate<Command> HAS_KNOWN_COMMAND_ORDER = command -> command.getCommandOrder() != CommandOrder.UNBEKANNT;
private static final List<String> NOT_PROCESSABLE_ORDER = List.of( private static final List<CommandOrder> ORDER_PROCESSED_SEPERATELY = List.of(
CommandOrder.RECEIVE_POSTFACH_NACHRICHT.name(), CommandOrder.RECEIVE_POSTFACH_NACHRICHT,
CommandOrder.RESEND_POSTFACH_MAIL.name(), CommandOrder.RESEND_POSTFACH_MAIL,
CommandOrder.SEND_POSTFACH_MAIL.name(), CommandOrder.SEND_POSTFACH_MAIL,
CommandOrder.SEND_POSTFACH_NACHRICHT.name(), CommandOrder.SEND_POSTFACH_NACHRICHT);
CommandOrder.RESEND_POSTFACH_MAIL.name(),
CommandOrder.RECEIVE_POSTFACH_NACHRICHT.name()); static final Predicate<Command> IS_PROCESSABLE_ORDER = command -> !ORDER_PROCESSED_SEPERATELY.contains(command.getCommandOrder());
static final Predicate<Command> IS_PROCESSABLE_ORDER = command -> !NOT_PROCESSABLE_ORDER.contains(command.getOrder());
private final List<CommandProcessor> processors; private final List<CommandProcessor> processors;
......
package de.ozgcloud.alfa.common.command; package de.ozgcloud.alfa.common.command;
import java.util.Objects; import java.util.Objects;
import java.util.function.Predicate;
import org.springframework.hateoas.EntityModel; import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.LinkRelation; import org.springframework.hateoas.LinkRelation;
...@@ -12,8 +11,6 @@ public abstract class CommandProcessor { ...@@ -12,8 +11,6 @@ public abstract class CommandProcessor {
static final LinkRelation REL_EFFECTED_RESOURCE = LinkRelation.of("effected_resource"); static final LinkRelation REL_EFFECTED_RESOURCE = LinkRelation.of("effected_resource");
static final LinkRelation REL_RELATED_RESOURCE = LinkRelation.of("related_resource"); static final LinkRelation REL_RELATED_RESOURCE = LinkRelation.of("related_resource");
static final Predicate<Command> HAS_KNOWN_COMMAND_ORDER = command -> command.getCommandOrder() != CommandOrder.UNBEKANNT;
public void process(EntityModel<Command> model) { public void process(EntityModel<Command> model) {
var command = model.getContent(); var command = model.getContent();
...@@ -21,17 +18,17 @@ public abstract class CommandProcessor { ...@@ -21,17 +18,17 @@ public abstract class CommandProcessor {
return; return;
} }
model.addIf(CommandHelper.IS_DONE.test(command) && isResponsibleForEffectedResource(command.getOrder()), model.addIf(CommandHelper.IS_DONE.test(command) && isResponsibleForEffectedResource(command.getCommandOrder()),
() -> createEffectedResourceLinkBuilder(command).withRel(REL_EFFECTED_RESOURCE)); () -> createEffectedResourceLinkBuilder(command).withRel(REL_EFFECTED_RESOURCE));
model.addIf(isResponsible(command.getOrder()), model.addIf(isResponsibleForRelatedResource(command.getCommandOrder()),
() -> createRelatedResourceLinkBuilder(command).withRel(REL_RELATED_RESOURCE)); () -> createRelatedResourceLinkBuilder(command).withRel(REL_RELATED_RESOURCE));
} }
public abstract boolean isResponsibleForEffectedResource(String order); public abstract boolean isResponsibleForEffectedResource(CommandOrder order);
public abstract WebMvcLinkBuilder createEffectedResourceLinkBuilder(Command command); public abstract WebMvcLinkBuilder createEffectedResourceLinkBuilder(Command command);
public abstract boolean isResponsible(String order); public abstract boolean isResponsibleForRelatedResource(CommandOrder order);
public abstract WebMvcLinkBuilder createRelatedResourceLinkBuilder(Command command); public abstract WebMvcLinkBuilder createRelatedResourceLinkBuilder(Command command);
} }
\ No newline at end of file
...@@ -3,7 +3,6 @@ package de.ozgcloud.alfa.postfach; ...@@ -3,7 +3,6 @@ package de.ozgcloud.alfa.postfach;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*; import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
import java.util.List; import java.util.List;
import java.util.Optional;
import org.springframework.hateoas.server.mvc.WebMvcLinkBuilder; import org.springframework.hateoas.server.mvc.WebMvcLinkBuilder;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -15,19 +14,19 @@ import de.ozgcloud.alfa.common.command.CommandProcessor; ...@@ -15,19 +14,19 @@ import de.ozgcloud.alfa.common.command.CommandProcessor;
@Component @Component
public class PostfachNachrichtCommandProcessor extends CommandProcessor { public class PostfachNachrichtCommandProcessor extends CommandProcessor {
private static final List<String> RESPONSIBLE_ORDER = List.of( private static final List<CommandOrder> RESPONSIBLE_ORDER = List.of(
CommandOrder.RECEIVE_POSTFACH_NACHRICHT.name(), CommandOrder.RECEIVE_POSTFACH_NACHRICHT,
CommandOrder.RESEND_POSTFACH_MAIL.name()); CommandOrder.RESEND_POSTFACH_MAIL);
private static final List<String> RESPONSIBLE_EFFECTED_RESOURCE_ORDER = List.of( private static final List<CommandOrder> RESPONSIBLE_EFFECTED_RESOURCE_ORDER = List.of(
CommandOrder.SEND_POSTFACH_MAIL.name(), CommandOrder.SEND_POSTFACH_MAIL,
CommandOrder.SEND_POSTFACH_NACHRICHT.name(), CommandOrder.SEND_POSTFACH_NACHRICHT,
CommandOrder.RESEND_POSTFACH_MAIL.name(), CommandOrder.RESEND_POSTFACH_MAIL,
CommandOrder.RECEIVE_POSTFACH_NACHRICHT.name()); CommandOrder.RECEIVE_POSTFACH_NACHRICHT);
@Override @Override
public boolean isResponsible(String order) { public boolean isResponsibleForRelatedResource(CommandOrder order) {
return Optional.ofNullable(order).map(RESPONSIBLE_ORDER::contains).orElse(false); return RESPONSIBLE_ORDER.contains(order);
} }
@Override @Override
...@@ -36,8 +35,8 @@ public class PostfachNachrichtCommandProcessor extends CommandProcessor { ...@@ -36,8 +35,8 @@ public class PostfachNachrichtCommandProcessor extends CommandProcessor {
} }
@Override @Override
public boolean isResponsibleForEffectedResource(String order) { public boolean isResponsibleForEffectedResource(CommandOrder order) {
return Optional.ofNullable(order).map(RESPONSIBLE_EFFECTED_RESOURCE_ORDER::contains).orElse(false); return RESPONSIBLE_EFFECTED_RESOURCE_ORDER.contains(order);
} }
@Override @Override
......
...@@ -21,6 +21,11 @@ class CommandProcessorTest { ...@@ -21,6 +21,11 @@ class CommandProcessorTest {
@Nested @Nested
class TestProcess { class TestProcess {
private final Command command = CommandTestFactory.create();
private final EntityModel<Command> model = EntityModel.of(command);
private final WebMvcLinkBuilder linkBuilder = WebMvcLinkBuilder.linkTo(CommandController.class).slash("id");
@Test @Test
void shouldNotThrowExceptionsOnNullContent() { void shouldNotThrowExceptionsOnNullContent() {
EntityModel<Command> entityModel = when(mock(EntityModel.class).getContent()).thenReturn(null).getMock(); EntityModel<Command> entityModel = when(mock(EntityModel.class).getContent()).thenReturn(null).getMock();
...@@ -28,49 +33,93 @@ class CommandProcessorTest { ...@@ -28,49 +33,93 @@ class CommandProcessorTest {
assertDoesNotThrow(() -> processor.process(entityModel)); assertDoesNotThrow(() -> processor.process(entityModel));
} }
@DisplayName("effected resource link") @DisplayName("effected resource")
@Nested @Nested
class TestEffectedResource { class TestEffectedResource {
@DisplayName("should verify if responsibility is related to the order")
@Test
void shouldCallIsResponsibleForEffectedResource() {
processor.process(model);
verify(processor).isResponsibleForEffectedResource(CommandOrder.fromOrder(CommandTestFactory.ORDER));
}
@DisplayName("should create link builder for effected resource if the responsibility matches")
@Test @Test
void shouldBeAddedIfResponsible() { void shouldCallCreateEffectedResourceLinkBuilder() {
var linkBuilder = WebMvcLinkBuilder.linkTo(CommandController.class).slash("id"); when(processor.isResponsibleForEffectedResource(any())).thenReturn(true);
doReturn(true).when(processor).isResponsibleForEffectedResource(any());
doReturn(linkBuilder).when(processor).createEffectedResourceLinkBuilder(any()); doReturn(linkBuilder).when(processor).createEffectedResourceLinkBuilder(any());
var command = CommandTestFactory.createBuilder().status(CommandStatus.FINISHED).build();
var entityModel = EntityModel.of(command);
processor.process(entityModel); processor.process(model);
assertThat(entityModel.getLink(CommandProcessor.REL_EFFECTED_RESOURCE).get().getHref()).isEqualTo("/api/commands/id"); verify(processor).createEffectedResourceLinkBuilder(command);
} }
@Test @DisplayName("link")
void shouldNotBeAddedIfCommandNotDone() { @Nested
var command = CommandTestFactory.createBuilder().status(CommandStatus.PENDING).build(); class TestEffectedResourceLink {
var entityModel = EntityModel.of(command);
@Test
void shouldBeAddedIfResponsible() {
doReturn(true).when(processor).isResponsibleForEffectedResource(any());
doReturn(linkBuilder).when(processor).createEffectedResourceLinkBuilder(any());
var finishedCommand = CommandTestFactory.createBuilder().status(CommandStatus.FINISHED).build();
var entityModel = EntityModel.of(finishedCommand);
processor.process(entityModel);
assertThat(entityModel.getLink(CommandProcessor.REL_EFFECTED_RESOURCE).get().getHref()).isEqualTo("/api/commands/id");
}
processor.process(entityModel); @Test
void shouldNotBeAddedIfCommandNotDone() {
var pendingCommand = CommandTestFactory.createBuilder().status(CommandStatus.PENDING).build();
var entityModel = EntityModel.of(pendingCommand);
assertThat(entityModel.getLink(CommandProcessor.REL_EFFECTED_RESOURCE)).isNotPresent(); processor.process(entityModel);
assertThat(entityModel.getLink(CommandProcessor.REL_EFFECTED_RESOURCE)).isNotPresent();
}
} }
} }
@DisplayName("related resource link") @DisplayName("related resource")
@Nested @Nested
class TestRelatedResource { class TestRelatedResource {
@DisplayName("should verify if responsibility is related to the order")
@Test
void shouldCallIsResponsibleForRelatedResource() {
processor.process(model);
verify(processor).isResponsibleForRelatedResource(CommandOrder.fromOrder(CommandTestFactory.ORDER));
}
@DisplayName("should create link builder for related resource if the responsibility matches")
@Test @Test
void shouldBeAddedIfResponsible() { void shouldCallCreateRelatedResourceLinkBuilder() {
var linkBuilder = WebMvcLinkBuilder.linkTo(CommandController.class).slash("id"); when(processor.isResponsibleForRelatedResource(any())).thenReturn(true);
doReturn(true).when(processor).isResponsible(any());
doReturn(linkBuilder).when(processor).createRelatedResourceLinkBuilder(any()); doReturn(linkBuilder).when(processor).createRelatedResourceLinkBuilder(any());
var command = CommandTestFactory.create();
var entityModel = EntityModel.of(command);
processor.process(entityModel); processor.process(model);
verify(processor).createRelatedResourceLinkBuilder(command);
}
@DisplayName("link")
@Nested
class TestRelatedResourceLink {
@Test
void shouldBeAddedIfResponsible() {
doReturn(true).when(processor).isResponsibleForRelatedResource(any());
doReturn(linkBuilder).when(processor).createRelatedResourceLinkBuilder(any());
processor.process(model);
assertThat(entityModel.getLink(CommandProcessor.REL_RELATED_RESOURCE).get().getHref()).isEqualTo("/api/commands/id"); assertThat(model.getLink(CommandProcessor.REL_RELATED_RESOURCE).get().getHref()).isEqualTo("/api/commands/id");
}
} }
} }
} }
......
...@@ -29,7 +29,7 @@ class PostfachNachrichtCommandProcessorTest { ...@@ -29,7 +29,7 @@ class PostfachNachrichtCommandProcessorTest {
@EnumSource(mode = Mode.INCLUDE, names = { "RECEIVE_POSTFACH_NACHRICHT", "RESEND_POSTFACH_MAIL" }) @EnumSource(mode = Mode.INCLUDE, names = { "RECEIVE_POSTFACH_NACHRICHT", "RESEND_POSTFACH_MAIL" })
@ParameterizedTest(name = "{0}") @ParameterizedTest(name = "{0}")
void shouldReturnTrueOnOrder(CommandOrder order) { void shouldReturnTrueOnOrder(CommandOrder order) {
var isResponsible = processor.isResponsible(order.name()); var isResponsible = processor.isResponsibleForRelatedResource(order);
assertThat(isResponsible).isTrue(); assertThat(isResponsible).isTrue();
} }
...@@ -37,7 +37,7 @@ class PostfachNachrichtCommandProcessorTest { ...@@ -37,7 +37,7 @@ class PostfachNachrichtCommandProcessorTest {
@EnumSource(mode = Mode.EXCLUDE, names = { "RECEIVE_POSTFACH_NACHRICHT", "RESEND_POSTFACH_MAIL" }) @EnumSource(mode = Mode.EXCLUDE, names = { "RECEIVE_POSTFACH_NACHRICHT", "RESEND_POSTFACH_MAIL" })
@ParameterizedTest(name = "{0}") @ParameterizedTest(name = "{0}")
void shouldReturnFalseOnOrder(CommandOrder order) { void shouldReturnFalseOnOrder(CommandOrder order) {
var isResponsible = processor.isResponsible(order.name()); var isResponsible = processor.isResponsibleForRelatedResource(order);
assertThat(isResponsible).isFalse(); assertThat(isResponsible).isFalse();
} }
...@@ -63,7 +63,7 @@ class PostfachNachrichtCommandProcessorTest { ...@@ -63,7 +63,7 @@ class PostfachNachrichtCommandProcessorTest {
"RECEIVE_POSTFACH_NACHRICHT" }) "RECEIVE_POSTFACH_NACHRICHT" })
@ParameterizedTest(name = "{0}") @ParameterizedTest(name = "{0}")
void shouldReturnTrueOnOrder(CommandOrder order) { void shouldReturnTrueOnOrder(CommandOrder order) {
var isResponsible = processor.isResponsibleForEffectedResource(order.name()); var isResponsible = processor.isResponsibleForEffectedResource(order);
assertThat(isResponsible).isTrue(); assertThat(isResponsible).isTrue();
} }
...@@ -72,7 +72,7 @@ class PostfachNachrichtCommandProcessorTest { ...@@ -72,7 +72,7 @@ class PostfachNachrichtCommandProcessorTest {
"RECEIVE_POSTFACH_NACHRICHT" }) "RECEIVE_POSTFACH_NACHRICHT" })
@ParameterizedTest(name = "{0}") @ParameterizedTest(name = "{0}")
void shouldReturnFalseOnOrder(CommandOrder order) { void shouldReturnFalseOnOrder(CommandOrder order) {
var isResponsible = processor.isResponsibleForEffectedResource(order.name()); var isResponsible = processor.isResponsibleForEffectedResource(order);
assertThat(isResponsible).isFalse(); assertThat(isResponsible).isFalse();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment