diff --git a/alfa-service/src/main/java/de/ozgcloud/alfa/historie/HistorieCommandHandler.java b/alfa-service/src/main/java/de/ozgcloud/alfa/historie/HistorieCommandHandler.java index 094d44f852516d7b667376c26a3aa7c21c8768ef..e7f6307b6518c49161460cc229d352c5cfff3b34 100644 --- a/alfa-service/src/main/java/de/ozgcloud/alfa/historie/HistorieCommandHandler.java +++ b/alfa-service/src/main/java/de/ozgcloud/alfa/historie/HistorieCommandHandler.java @@ -56,22 +56,13 @@ class HistorieCommandHandler { private static final Predicate<Command> IS_CREATE_ATTACHED_ITEM = command -> command.getCommandOrder() == CommandOrder.CREATE_ATTACHED_ITEM; public boolean isHistorieCommand(Command command) { - return !isOutgoingPostfachNachrichtByOzgCloudNachrichtenManager(command) - && !isDeleteVorgangAttachedItem(command); - } - - boolean isOutgoingPostfachNachrichtByOzgCloudNachrichtenManager(Command command) { - return command.getCommandOrder().equals(CommandOrder.CREATE_ATTACHED_ITEM) && isOzgCloudNachrichtenManager(command) && isOutgoing(command); + return !isDeleteVorgangAttachedItem(command); } private boolean isOzgCloudNachrichtenManager(Command command) { return StringUtils.equals(MapUtils.getString(command.getBody(), CLIENT), OZGCLOUD_NACHRICHTEN_MANAGER); } - private boolean isOutgoing(Command command) { - return isDirection(command, DIRECTION_OUTGOING); - } - boolean isDeleteVorgangAttachedItem(Command command) { return command.getCommandOrder() == CommandOrder.DELETE_ATTACHED_ITEM; } diff --git a/alfa-service/src/main/java/de/ozgcloud/alfa/postfach/PostfachNachrichtHistorieProcessor.java b/alfa-service/src/main/java/de/ozgcloud/alfa/postfach/PostfachNachrichtHistorieProcessor.java new file mode 100644 index 0000000000000000000000000000000000000000..9d7a05e6e2b92f274d1699e7600d74ecc9b3bf8b --- /dev/null +++ b/alfa-service/src/main/java/de/ozgcloud/alfa/postfach/PostfachNachrichtHistorieProcessor.java @@ -0,0 +1,43 @@ +package de.ozgcloud.alfa.postfach; + +import java.util.Map; +import java.util.stream.Stream; + +import org.apache.commons.collections.MapUtils; +import org.apache.commons.lang3.StringUtils; + +import de.ozgcloud.alfa.common.command.Command; +import de.ozgcloud.alfa.common.command.CommandBodyMapper; +import de.ozgcloud.alfa.common.command.CommandOrder; +import de.ozgcloud.alfa.historie.HistorieProcessor; + +class PostfachNachrichtHistorieProcessor implements HistorieProcessor { + + static final String DIRECTION_INCOMMING = "IN"; + static final String DIRECTION_OUTGOING = "OUT"; + static final String DIRECTION = "direction"; + static final String OZGCLOUD_NACHRICHTEN_MANAGER = "OzgCloud_NachrichtenManager"; + static final String CLIENT = "client"; + + @Override + public Stream<Command> process(Stream<Command> commands) { + return commands.filter(this::isNotOutgoingPostfachNachrichtByOzgCloudNachrichtenManager); + } + + boolean isNotOutgoingPostfachNachrichtByOzgCloudNachrichtenManager(Command command) { + return !(command.getCommandOrder().equals(CommandOrder.CREATE_ATTACHED_ITEM) && isOzgCloudNachrichtenManager(command) && isOutgoing(command)); + } + + private boolean isOzgCloudNachrichtenManager(Command command) { + return StringUtils.equals(MapUtils.getString(command.getBody(), CLIENT), OZGCLOUD_NACHRICHTEN_MANAGER); + } + + private boolean isOutgoing(Command command) { + return StringUtils.equals(MapUtils.getString(getItemMap(command), DIRECTION), DIRECTION_OUTGOING); + } + + @SuppressWarnings("unchecked") + private Map<String, Object> getItemMap(Command command) { + return MapUtils.getMap(command.getBody(), CommandBodyMapper.ITEM_PROPERTY); + } +} diff --git a/alfa-service/src/test/java/de/ozgcloud/alfa/historie/HistorieCommandHandlerTest.java b/alfa-service/src/test/java/de/ozgcloud/alfa/historie/HistorieCommandHandlerTest.java index 993b8bf42752ec8d92e2393400e4bb8b10427aa7..787ad1a283dcd8c9e78c1a568e25f1403ac0928c 100644 --- a/alfa-service/src/test/java/de/ozgcloud/alfa/historie/HistorieCommandHandlerTest.java +++ b/alfa-service/src/test/java/de/ozgcloud/alfa/historie/HistorieCommandHandlerTest.java @@ -72,13 +72,6 @@ class HistorieCommandHandlerTest { assertThat(result).isTrue(); } - @Test - void shouldReturnFalseOnLoeschAnforderungZuruecknehmenRevoked() { - var result = handler.isHistorieCommand(CommandTestFactory.create()); - - assertThat(result).isFalse(); - } - @Test void shouldReturnFalseOnDeleteVorgangAttachedItem() { var result = handler.isHistorieCommand(CommandTestFactory.createBuilder().order(CommandOrder.DELETE_ATTACHED_ITEM.name()).build()); @@ -96,49 +89,6 @@ class HistorieCommandHandlerTest { } } - @DisplayName("postfach nachricht verification") - @Nested - class TestIsOutgoingPostfachNachrichtenByOzgCloudNachrichtenManager { - - private final Command matchingCommand = CommandTestFactory.createBuilder() - .order(CommandOrder.CREATE_ATTACHED_ITEM.name()) - .body(Map.of("item", Map.of(HistorieCommandHandler.DIRECTION, HistorieCommandHandler.DIRECTION_OUTGOING), - HistorieCommandHandler.CLIENT, HistorieCommandHandler.OZGCLOUD_NACHRICHTEN_MANAGER)) - .build(); - - @DisplayName("should return true if the command is CREATED_ATTACHED_ITEM order outgoing by mailserice") - @Test - void shouldReturnTrue() { - var result = handler.isOutgoingPostfachNachrichtByOzgCloudNachrichtenManager(matchingCommand); - - assertThat(result).isTrue(); - } - - @Test - void shouldReturnFalseOnIncoming() { - var nonMatchingCommand = matchingCommand.toBuilder() - .body(Map.of("item", Map.of(HistorieCommandHandler.DIRECTION, HistorieCommandHandler.DIRECTION_INCOMMING), - HistorieCommandHandler.CLIENT, HistorieCommandHandler.OZGCLOUD_NACHRICHTEN_MANAGER)) - .build(); - - var result = handler.isOutgoingPostfachNachrichtByOzgCloudNachrichtenManager(nonMatchingCommand); - - assertThat(result).isFalse(); - } - - @Test - void shouldReturnFalseOnOtherClient() { - var nonMatchingCommand = matchingCommand.toBuilder() - .body(Map.of("item", Map.of(HistorieCommandHandler.DIRECTION, HistorieCommandHandler.DIRECTION_OUTGOING), - HistorieCommandHandler.CLIENT, "Alfa")) - .build(); - - var result = handler.isOutgoingPostfachNachrichtByOzgCloudNachrichtenManager(nonMatchingCommand); - - assertThat(result).isFalse(); - } - } - @DisplayName("translate order") @Nested class TestTranslateOrders { diff --git a/alfa-service/src/test/java/de/ozgcloud/alfa/postfach/PostfachNachrichtHistorieProcessorTest.java b/alfa-service/src/test/java/de/ozgcloud/alfa/postfach/PostfachNachrichtHistorieProcessorTest.java new file mode 100644 index 0000000000000000000000000000000000000000..af6982d49c6cf3a20ab3991bf0dd7e30ba66636b --- /dev/null +++ b/alfa-service/src/test/java/de/ozgcloud/alfa/postfach/PostfachNachrichtHistorieProcessorTest.java @@ -0,0 +1,112 @@ +package de.ozgcloud.alfa.postfach; + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; + +import java.util.List; +import java.util.Map; +import java.util.stream.Stream; + +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.mockito.Spy; + +import com.thedeanda.lorem.LoremIpsum; + +import de.ozgcloud.alfa.common.command.Command; +import de.ozgcloud.alfa.common.command.CommandOrder; +import de.ozgcloud.alfa.common.command.CommandTestFactory; + +class PostfachNachrichtHistorieProcessorTest { + + @Spy + private PostfachNachrichtHistorieProcessor processor; + + @Nested + class TestProcess { + + private final Command command = CommandTestFactory.create(); + + @Test + void shouldCallIsNotOutgoingPostfachNachrichtByOzgCloudNachrichtenManager() { + process(); + + verify(processor).isNotOutgoingPostfachNachrichtByOzgCloudNachrichtenManager(command); + } + + @Test + void shouldContainCommandIfIsNotOutgoingPostfachNachrichtByOzgCloudNachrichtenManager() { + doReturn(true).when(processor).isNotOutgoingPostfachNachrichtByOzgCloudNachrichtenManager(any()); + + var commands = process(); + + assertThat(commands).containsExactly(command); + } + + @Test + void shouldNotContainCommandIfisNotOutgoingPostfachNachrichtByOzgCloudNachrichtenManager() { + doReturn(false).when(processor).isNotOutgoingPostfachNachrichtByOzgCloudNachrichtenManager(any()); + + var commands = process(); + + assertThat(commands).isEmpty(); + } + + private List<Command> process() { + return processor.process(Stream.of(command)).toList(); + } + } + + @Nested + class TestIsOutgoingPostfachNachrichtenByOzgCloudNachrichtenManager { + + private final Command matchingCommand = CommandTestFactory.createBuilder() + .order(CommandOrder.CREATE_ATTACHED_ITEM.name()) + .body(Map.of("item", Map.of(PostfachNachrichtHistorieProcessor.DIRECTION, PostfachNachrichtHistorieProcessor.DIRECTION_OUTGOING), + PostfachNachrichtHistorieProcessor.CLIENT, PostfachNachrichtHistorieProcessor.OZGCLOUD_NACHRICHTEN_MANAGER)) + .build(); + + @Test + void shouldReturnFalse() { + var result = processor.isNotOutgoingPostfachNachrichtByOzgCloudNachrichtenManager(matchingCommand); + + assertThat(result).isFalse(); + } + + @Test + void shouldReturnTrueOnOtherOrder() { + var result = processor.isNotOutgoingPostfachNachrichtByOzgCloudNachrichtenManager(CommandTestFactory.create()); + + assertThat(result).isTrue(); + } + + @Test + void shouldReturnTrueOnIncoming() { + var commandBody = Map.of("item", + Map.of(PostfachNachrichtHistorieProcessor.DIRECTION, PostfachNachrichtHistorieProcessor.DIRECTION_INCOMMING), + PostfachNachrichtHistorieProcessor.CLIENT, PostfachNachrichtHistorieProcessor.OZGCLOUD_NACHRICHTEN_MANAGER); + var nonMatchingCommand = matchingCommand.toBuilder() + .body(commandBody) + .build(); + + var result = processor.isNotOutgoingPostfachNachrichtByOzgCloudNachrichtenManager(nonMatchingCommand); + + assertThat(result).isTrue(); + } + + @Test + void shouldReturnTrueOnOtherClient() { + var commandBody = Map.of("item", + Map.of(PostfachNachrichtHistorieProcessor.DIRECTION, PostfachNachrichtHistorieProcessor.DIRECTION_OUTGOING), + PostfachNachrichtHistorieProcessor.CLIENT, LoremIpsum.getInstance().getWords(1)); + var nonMatchingCommand = matchingCommand.toBuilder() + .body(commandBody) + .build(); + + var result = processor.isNotOutgoingPostfachNachrichtByOzgCloudNachrichtenManager(nonMatchingCommand); + + assertThat(result).isTrue(); + } + } +}