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 1832ded0ad1ece969a7525555cdd42a56ca3d2eb..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.notify(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 b6f9488e0a6ff7af20672719271cda04026e0bf6..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,11 +1,14 @@ package de.itvsh.kop.notification.user; -import de.itvsh.kop.notification.email.EmailRemoteService; -import de.itvsh.kop.notification.email.UserEmail; +import java.util.List; + import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; -import java.util.List; +import de.itvsh.kop.notification.email.EmailRemoteService; +import de.itvsh.kop.notification.email.UserEmail; +import lombok.NonNull; @Service public class UserNotificationService { @@ -19,12 +22,13 @@ public class UserNotificationService { @Autowired private UserRemoteService userRemoteService; - public void notify(String organisationeinheitId) { - var recipients = userRemoteService.getRecipients(organisationeinheitId); + @Async + public void sendNotification(@NonNull String organisationsEinheitId) { + var recipients = userRemoteService.getRecipients(organisationsEinheitId); emailRemoteService.sendEmail(buildUserEmail(recipients)); } 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 f02be746c026b88f5441efce4b049e69699419ec..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).notify(VorgangTestFactory.ORGANISATIONEINCHEIT_ID); + verifyNoInteractions(userNotificationService); + } } } } \ No newline at end of file diff --git a/notification-manager/src/test/java/de/itvsh/kop/notification/user/UserNotificationServiceTest.java b/notification-manager/src/test/java/de/itvsh/kop/notification/user/UserNotificationServiceTest.java index a44566565093d648d1ff2b3846efebe46813e721..d3af3bd5b085c8910d4630b8a033d92451db2ef0 100644 --- a/notification-manager/src/test/java/de/itvsh/kop/notification/user/UserNotificationServiceTest.java +++ b/notification-manager/src/test/java/de/itvsh/kop/notification/user/UserNotificationServiceTest.java @@ -1,9 +1,11 @@ package de.itvsh.kop.notification.user; -import de.itvsh.kop.notification.email.EmailRemoteService; -import de.itvsh.kop.notification.email.UserEmail; -import de.itvsh.kop.notification.email.UserEmailTestFactory; -import de.itvsh.kop.notification.vorgang.VorgangTestFactory; +import static org.assertj.core.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; + +import java.util.List; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; @@ -11,45 +13,74 @@ import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mock; -import java.util.List; - -import static org.mockito.Mockito.*; +import de.itvsh.kop.notification.email.EmailRemoteService; +import de.itvsh.kop.notification.email.UserEmail; +import de.itvsh.kop.notification.email.UserEmailTestFactory; +import de.itvsh.kop.notification.vorgang.VorgangTestFactory; class UserNotificationServiceTest { @InjectMocks - private UserNotificationService userNotificationService; + private UserNotificationService service; @Mock private EmailRemoteService emailRemoteService; @Mock private UserRemoteService userRemoteService; - @DisplayName("Notify") + @DisplayName("Send notification") @Nested - class TestOnNotify { + class TestSendNotification { - private static final List<Recipient> RECIPIENTS = List.of(RecipientTestFactory.create()); - private static final String ORGANISATIONEINCHEIT_ID = VorgangTestFactory.ORGANISATIONEINCHEIT_ID; - private static final UserEmail USER_EMAIL = UserEmailTestFactory.create(); + @DisplayName("with valid orgaId") + @Nested + class TestWithValidOrgaId { - @BeforeEach - void init() { - when(userRemoteService.getRecipients(ORGANISATIONEINCHEIT_ID)).thenReturn(RECIPIENTS); - } + @BeforeEach + void init() { + when(userRemoteService.getRecipients(VorgangTestFactory.ORGANISATIONS_EINHEIT_ID)).thenReturn(List.of(RecipientTestFactory.create())); + doNothing().when(emailRemoteService).sendEmail(any(UserEmail.class)); + } + + @Test + void shouldGetRecipients() { + service.sendNotification(VorgangTestFactory.ORGANISATIONS_EINHEIT_ID); - @Test - void shouldGetRecipients() { - userNotificationService.notify(ORGANISATIONEINCHEIT_ID); + verify(userRemoteService).getRecipients(VorgangTestFactory.ORGANISATIONS_EINHEIT_ID); + } - verify(userRemoteService).getRecipients(ORGANISATIONEINCHEIT_ID); + @Test + void shouldSendEmail() { + service.sendNotification(VorgangTestFactory.ORGANISATIONS_EINHEIT_ID); + + verify(emailRemoteService).sendEmail(UserEmailTestFactory.create()); + } } - @Test - void shouldSendEmail() { - userNotificationService.notify(ORGANISATIONEINCHEIT_ID); + @DisplayName("with null as orgaId") + @Nested + class TestWithInvalidOrgaId { + + @Test + void shouldThrowIllegalArgumentException() { + assertThatThrownBy(() -> service.sendNotification(null)) + .isInstanceOf(IllegalArgumentException.class) + .withFailMessage("organisationsEinheitId cannot be null."); + } + + @Test + void shouldNotCallUserRemoteService() { + assertThatThrownBy(() -> service.sendNotification(null)); + + verify(userRemoteService, never()).getRecipients(anyString()); + } + + @Test + void shouldNotCallEmailRemoteSErvie() { + assertThatThrownBy(() -> service.sendNotification(null)); - verify(emailRemoteService).sendEmail(USER_EMAIL); + verify(emailRemoteService, never()).sendEmail(any(UserEmail.class)); + } } } } \ No newline at end of file diff --git a/notification-manager/src/test/java/de/itvsh/kop/notification/vorgang/GrpcVorgangTestFactory.java b/notification-manager/src/test/java/de/itvsh/kop/notification/vorgang/GrpcVorgangTestFactory.java index 6bfd963d9daeb824cd70dadac49890852c78ea07..7b9d4f89f26eeb95e6f12f47ef9328365f871144 100644 --- a/notification-manager/src/test/java/de/itvsh/kop/notification/vorgang/GrpcVorgangTestFactory.java +++ b/notification-manager/src/test/java/de/itvsh/kop/notification/vorgang/GrpcVorgangTestFactory.java @@ -23,7 +23,7 @@ public class GrpcVorgangTestFactory { .setPostfachId(VorgangTestFactory.POSTFACH_ID) .build()) .setZustaendigeStelle( - GrpcZustaendigeStelle.newBuilder().setOrganisationseinheitenId(VorgangTestFactory.ORGANISATIONEINCHEIT_ID).build()) + GrpcZustaendigeStelle.newBuilder().setOrganisationseinheitenId(VorgangTestFactory.ORGANISATIONS_EINHEIT_ID).build()) .build()); } diff --git a/notification-manager/src/test/java/de/itvsh/kop/notification/vorgang/VorgangTestFactory.java b/notification-manager/src/test/java/de/itvsh/kop/notification/vorgang/VorgangTestFactory.java index 4d6a27b0aa842e281c7a744b62da1db1d1d50ec8..ad7d79deee60b6593feb7f7a89be857525c31a4a 100644 --- a/notification-manager/src/test/java/de/itvsh/kop/notification/vorgang/VorgangTestFactory.java +++ b/notification-manager/src/test/java/de/itvsh/kop/notification/vorgang/VorgangTestFactory.java @@ -14,7 +14,7 @@ public class VorgangTestFactory { public static final String POSTFACH_ID = UUID.randomUUID().toString(); public static final String FORM_ENGINE_NAME = "deluxeEngine"; - public static final String ORGANISATIONEINCHEIT_ID = "1234567889"; + public static final String ORGANISATIONS_EINHEIT_ID = "1234567889"; public static Vorgang create() { return createBuilder().build(); @@ -28,7 +28,7 @@ public class VorgangTestFactory { .createdAt(CREATED_AT) .postfachId(POSTFACH_ID) .formEngineName(FORM_ENGINE_NAME) - .organisationseinheitenId(ORGANISATIONEINCHEIT_ID); + .organisationseinheitenId(ORGANISATIONS_EINHEIT_ID); } }