From 3e2201cc0100e3d5d2338160dbf27c69546cc826 Mon Sep 17 00:00:00 2001
From: OZGCloud <ozgcloud@mgm-tp.com>
Date: Tue, 1 Nov 2022 12:38:14 +0100
Subject: [PATCH] OZG-2626 OZG-3065 move condition to listener; add @NonNull

---
 .../user/UserNotificationEventListener.java   | 16 +++--
 .../user/UserNotificationService.java         |  9 +--
 .../UserNotificationEventListenerTest.java    | 61 +++++++++++++------
 3 files changed, 54 insertions(+), 32 deletions(-)

diff --git a/notification-manager/src/main/java/de/itvsh/kop/notification/user/UserNotificationEventListener.java b/notification-manager/src/main/java/de/itvsh/kop/notification/user/UserNotificationEventListener.java
index c7a556064..5ede2345f 100644
--- a/notification-manager/src/main/java/de/itvsh/kop/notification/user/UserNotificationEventListener.java
+++ b/notification-manager/src/main/java/de/itvsh/kop/notification/user/UserNotificationEventListener.java
@@ -1,13 +1,14 @@
 package de.itvsh.kop.notification.user;
 
-import de.itvsh.kop.notification.vorgang.VorgangId;
-import de.itvsh.kop.notification.vorgang.VorgangService;
-import de.itvsh.ozg.pluto.command.VorgangCreatedEvent;
+import java.util.Objects;
+
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.event.EventListener;
 import org.springframework.stereotype.Component;
 
-import java.util.List;
+import de.itvsh.kop.notification.vorgang.VorgangId;
+import de.itvsh.kop.notification.vorgang.VorgangService;
+import de.itvsh.ozg.pluto.command.VorgangCreatedEvent;
 
 @Component
 public class UserNotificationEventListener {
@@ -20,7 +21,10 @@ public class UserNotificationEventListener {
 
 	@EventListener
 	public void onVorgangCreated(VorgangCreatedEvent event) {
-		var organisationeinheitId = vorgangService.getVorgang(VorgangId.from(event.getSource())).getOrganisationseinheitenId();
-		userNotificationService.sendNotification(organisationeinheitId);
+		var organisationsEinheitId = vorgangService.getVorgang(VorgangId.from(event.getSource())).getOrganisationseinheitenId();
+
+		if (Objects.nonNull(organisationsEinheitId)) {
+			userNotificationService.sendNotification(organisationsEinheitId);
+		}
 	}
 }
diff --git a/notification-manager/src/main/java/de/itvsh/kop/notification/user/UserNotificationService.java b/notification-manager/src/main/java/de/itvsh/kop/notification/user/UserNotificationService.java
index 242282a1d..e55534df0 100644
--- a/notification-manager/src/main/java/de/itvsh/kop/notification/user/UserNotificationService.java
+++ b/notification-manager/src/main/java/de/itvsh/kop/notification/user/UserNotificationService.java
@@ -1,7 +1,6 @@
 package de.itvsh.kop.notification.user;
 
 import java.util.List;
-import java.util.Objects;
 
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.Async;
@@ -9,6 +8,7 @@ import org.springframework.stereotype.Service;
 
 import de.itvsh.kop.notification.email.EmailRemoteService;
 import de.itvsh.kop.notification.email.UserEmail;
+import lombok.NonNull;
 
 @Service
 public class UserNotificationService {
@@ -23,10 +23,7 @@ public class UserNotificationService {
 	private UserRemoteService userRemoteService;
 
 	@Async
-	public void sendNotification(String organisationsEinheitId) {
-		if (Objects.isNull(organisationsEinheitId)) {
-			throw new IllegalArgumentException("organisationsEinheitId cannot be null.");
-		}
+	public void sendNotification(@NonNull String organisationsEinheitId) {
 		var recipients = userRemoteService.getRecipients(organisationsEinheitId);
 		emailRemoteService.sendEmail(buildUserEmail(recipients));
 	}
@@ -34,4 +31,4 @@ public class UserNotificationService {
 	private UserEmail buildUserEmail(List<Recipient> recipients) {
 		return UserEmail.builder().recipients(recipients).subject(MAIL_SUBJECT).body(MAIL_BODY).build();
 	}
-}
+}
\ No newline at end of file
diff --git a/notification-manager/src/test/java/de/itvsh/kop/notification/user/UserNotificationEventListenerTest.java b/notification-manager/src/test/java/de/itvsh/kop/notification/user/UserNotificationEventListenerTest.java
index 904edd060..00e379d12 100644
--- a/notification-manager/src/test/java/de/itvsh/kop/notification/user/UserNotificationEventListenerTest.java
+++ b/notification-manager/src/test/java/de/itvsh/kop/notification/user/UserNotificationEventListenerTest.java
@@ -1,10 +1,8 @@
 package de.itvsh.kop.notification.user;
 
-import de.itvsh.kop.notification.vorgang.Vorgang;
-import de.itvsh.kop.notification.vorgang.VorgangId;
-import de.itvsh.kop.notification.vorgang.VorgangService;
-import de.itvsh.kop.notification.vorgang.VorgangTestFactory;
-import de.itvsh.ozg.pluto.command.VorgangCreatedEvent;
+import static org.mockito.ArgumentMatchers.*;
+import static org.mockito.Mockito.*;
+
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.DisplayName;
 import org.junit.jupiter.api.Nested;
@@ -12,8 +10,10 @@ import org.junit.jupiter.api.Test;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 
-import static org.mockito.ArgumentMatchers.*;
-import static org.mockito.Mockito.*;
+import de.itvsh.kop.notification.vorgang.VorgangId;
+import de.itvsh.kop.notification.vorgang.VorgangService;
+import de.itvsh.kop.notification.vorgang.VorgangTestFactory;
+import de.itvsh.ozg.pluto.command.VorgangCreatedEvent;
 
 class UserNotificationEventListenerTest {
 
@@ -30,25 +30,46 @@ class UserNotificationEventListenerTest {
 	class TestOnVorgangCreated {
 
 		private static final VorgangCreatedEvent EVENT = new VorgangCreatedEvent(VorgangTestFactory.ID.toString());
-		private static final Vorgang VORGANG = VorgangTestFactory.create();
 
-		@BeforeEach
-		void initMock() {
-			when(vorgangService.getVorgang(any())).thenReturn(VORGANG);
-		}
+		@DisplayName("with existing orgaId")
+		@Nested
+		class TestWithOrgaId {
+
+			@BeforeEach
+			void initMock() {
+				when(vorgangService.getVorgang(any())).thenReturn(VorgangTestFactory.create());
+			}
 
-		@Test
-		void shouldCallVorgangService() {
-			userNotificationEventListener.onVorgangCreated(EVENT);
+			@Test
+			void shouldCallVorgangService() {
+				userNotificationEventListener.onVorgangCreated(EVENT);
 
-			verify(vorgangService).getVorgang(VorgangId.from(EVENT.getSource()));
+				verify(vorgangService).getVorgang(VorgangId.from(EVENT.getSource()));
+			}
+
+			@Test
+			void shouldNotifyWithOrganisationeinheitIds() {
+				userNotificationEventListener.onVorgangCreated(EVENT);
+
+				verify(userNotificationService).sendNotification(VorgangTestFactory.ORGANISATIONS_EINHEIT_ID);
+			}
 		}
 
-		@Test
-		void shouldNotifyWithOrganisationeinheitIds() {
-			userNotificationEventListener.onVorgangCreated(EVENT);
+		@DisplayName("with missing orgaId")
+		@Nested
+		class TestWithoutOrgaId {
+
+			@BeforeEach
+			void initMock() {
+				when(vorgangService.getVorgang(any())).thenReturn(VorgangTestFactory.createBuilder().organisationseinheitenId(null).build());
+			}
+
+			@Test
+			void shouldNotCallNotificationService() {
+				userNotificationEventListener.onVorgangCreated(EVENT);
 
-			verify(userNotificationService).sendNotification(VorgangTestFactory.ORGANISATIONS_EINHEIT_ID);
+				verifyNoInteractions(userNotificationService);
+			}
 		}
 	}
 }
\ No newline at end of file
-- 
GitLab