From 789f671e627bd82618a2b085d0bccb9eaeb049f5 Mon Sep 17 00:00:00 2001
From: OZGCloud <ozgcloud@mgm-tp.com>
Date: Fri, 27 Sep 2024 08:19:31 +0200
Subject: [PATCH] OZG-6666 extend PostfachEventListener to publish
 PostfachNachrichtReceived

---
 nachrichten-manager-server/pom.xml            |  9 ++++-
 .../postfach/PostfachEventListener.java       | 25 ++++++++++----
 .../postfach/PostfachEventListenerTest.java   | 33 +++++++++++++++++++
 3 files changed, 60 insertions(+), 7 deletions(-)

diff --git a/nachrichten-manager-server/pom.xml b/nachrichten-manager-server/pom.xml
index 574e2f6..c1b115a 100644
--- a/nachrichten-manager-server/pom.xml
+++ b/nachrichten-manager-server/pom.xml
@@ -45,7 +45,7 @@
 		<jaxb-maven-plugin.version>3.0.1</jaxb-maven-plugin.version>
 		<ozg-info-manager-interface.version>0.1.0-SNAPSHOT</ozg-info-manager-interface.version>
 		<bayernid-proxy-interface.version>0.1.0</bayernid-proxy-interface.version>
-		<vorgang-manager.version>2.8.0</vorgang-manager.version>
+		<vorgang-manager.version>2.15.0-SNAPSHOT</vorgang-manager.version>
 		<muk-postfach.version>0.1.0-SNAPSHOT</muk-postfach.version>
 		<ozgcloud-starter.version>0.10.0</ozgcloud-starter.version>
 	</properties>
@@ -214,6 +214,13 @@
 			<type>test-jar</type>
 			<scope>test</scope>
 		</dependency>
+		<dependency>
+			<groupId>de.ozgcloud.command</groupId>
+			<artifactId>command-manager</artifactId>
+			<version>${vorgang-manager.version}</version>
+			<type>test-jar</type>
+			<scope>test</scope>
+		</dependency>
 	</dependencies>
 
 	<build>
diff --git a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachEventListener.java b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachEventListener.java
index 98dc2be..88fdba8 100644
--- a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachEventListener.java
+++ b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachEventListener.java
@@ -33,25 +33,33 @@ import java.util.function.Predicate;
 
 import org.apache.commons.collections.MapUtils;
 import org.apache.commons.lang3.StringUtils;
-import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationEventPublisher;
 import org.springframework.context.event.EventListener;
 import org.springframework.stereotype.Component;
 
 import de.ozgcloud.command.Command;
 import de.ozgcloud.command.CommandCreatedEvent;
+import de.ozgcloud.command.PostfachNachrichtReceived;
+import de.ozgcloud.command.VorgangAttachedItemCreatedEvent;
 import de.ozgcloud.nachrichten.postfach.PostfachNachricht.Direction;
+import lombok.RequiredArgsConstructor;
 import lombok.extern.log4j.Log4j2;
 
 @Component
 @Log4j2
+@RequiredArgsConstructor
 public class PostfachEventListener {
 
 	private static final String IS_SEND_POSTFACH_NACHRICHT_COMMAND = "{T(de.ozgcloud.nachrichten.postfach.PostfachEventListener).IS_SEND_POSTFACH_NACHRICHT.test(event.getSource())}";
-
-	public static final Predicate<Command> IS_SEND_POSTFACH_NACHRICHT = command -> command.getOrder().equals("SEND_POSTFACH_NACHRICHT");
-
-	@Autowired
-	private PostfachService service;
+	public static final Predicate<Command> IS_SEND_POSTFACH_NACHRICHT = command -> StringUtils.equals(command.getOrder(), "SEND_POSTFACH_NACHRICHT");
+	public static final String KEY_ITEM_NAME = "itemName";
+	public static final Predicate<Command> IS_INCOMING_NACHRICHT_CREATED = command -> StringUtils.equals(command.getOrder(), "CREATE_ATTACHED_ITEM")
+			&& StringUtils.equals(MapUtils.getString(command.getBodyObject(), KEY_ITEM_NAME), AttachedItemRemoteService.ITEM_NAME)
+			&& StringUtils.equals(MapUtils.getString(command.getBodyObject(), PostfachNachricht.FIELD_DIRECTION), "IN");
+	private static final String IS_INCOMING_NACHRICHT_ITEM_CREATED_CONDITION =
+			"{T(de.ozgcloud.nachrichten.postfach.PostfachEventListener).IS_INCOMING_NACHRICHT_CREATED.test(event.getCommand())}";
+	private final PostfachService service;
+	private final ApplicationEventPublisher publisher;
 
 	@EventListener(condition = IS_SEND_POSTFACH_NACHRICHT_COMMAND)
 	public void sendPostfachNachricht(CommandCreatedEvent event) {
@@ -122,4 +130,9 @@ public class PostfachEventListener {
 			return List.of(object.toString());
 		}
 	}
+
+	@EventListener(condition = IS_INCOMING_NACHRICHT_ITEM_CREATED_CONDITION)
+	public void onIncomingNachrichtItemCreated(VorgangAttachedItemCreatedEvent event) {
+		publisher.publishEvent(new PostfachNachrichtReceived(event.getCommand(), event.getCreatedResource()));
+	}
 }
\ No newline at end of file
diff --git a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachEventListenerTest.java b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachEventListenerTest.java
index adfc03a..00e0d57 100644
--- a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachEventListenerTest.java
+++ b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachEventListenerTest.java
@@ -41,9 +41,12 @@ import org.mockito.ArgumentCaptor;
 import org.mockito.Captor;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
+import org.springframework.context.ApplicationEventPublisher;
 
 import de.ozgcloud.command.Command;
 import de.ozgcloud.command.CommandCreatedEvent;
+import de.ozgcloud.command.PostfachNachrichtReceived;
+import de.ozgcloud.command.VorgangAttachedItemCreatedEvent;
 import de.ozgcloud.nachrichten.postfach.PostfachNachricht.Direction;
 import de.ozgcloud.nachrichten.postfach.osi.MessageTestFactory;
 
@@ -56,6 +59,8 @@ class PostfachEventListenerTest {
 	private PostfachService service;
 	@Mock
 	private Command command;
+	@Mock
+	private ApplicationEventPublisher publisher;
 
 	@Captor
 	private ArgumentCaptor<PostfachNachricht> nachrichtCaptor;
@@ -213,4 +218,32 @@ class PostfachEventListenerTest {
 			assertThat(list).isEmpty();
 		}
 	}
+
+	@DisplayName("On create incoming nachricht item")
+	@Nested
+	class TestOnCreateIncomingNachrichtItem {
+
+		private static final String CREATED_RESOURCE = "created-resource";
+		@Captor
+		public ArgumentCaptor<PostfachNachrichtReceived> eventCaptor;
+		@Mock
+		private VorgangAttachedItemCreatedEvent vorgangAttachedItemCreatedEvent;
+
+		@BeforeEach
+		void init() {
+			when(vorgangAttachedItemCreatedEvent.getCommand()).thenReturn(command);
+			when(vorgangAttachedItemCreatedEvent.getCreatedResource()).thenReturn(CREATED_RESOURCE);
+		}
+
+		@Test
+		void shouldPublishPostfachNachrichtReceivedEvent() {
+			listener.onIncomingNachrichtItemCreated(vorgangAttachedItemCreatedEvent);
+
+			verify(publisher).publishEvent(eventCaptor.capture());
+			assertThat(eventCaptor.getValue()).satisfies(event -> {
+				assertThat(event.getSource()).isSameAs(command);
+				assertThat(event.getNachrichtId()).isEqualTo(CREATED_RESOURCE);
+			});
+		}
+	}
 }
\ No newline at end of file
-- 
GitLab