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 c7a5560641597c16c35e915a97926510518dd490..5ede2345fdbc4840a68cbd967fc60a56e5515d1b 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 242282a1dfa3ea405322ffefd548d619a948e559..e55534df0d021654fb5838469645cf6ed844820a 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 904edd060c85a21aa929caa211ed29f6a078fc34..00e379d12ac96c1ba253e8ec38215732748c944e 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