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 4797dea5afcb9cb51a2be8af29c617ffdc5738dc..fe669bce3035a858be718db757e4742538dc982b 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 @@ -23,8 +23,6 @@ */ package de.itvsh.kop.notification.user; -import java.util.Objects; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; @@ -44,10 +42,6 @@ public class UserNotificationEventListener { @EventListener public void onVorgangCreated(VorgangCreatedEvent event) { - var organisationsEinheitId = vorgangService.getVorgang(VorgangId.from(event.getSource())).getOrganisationseinheitenId(); - - if (Objects.nonNull(organisationsEinheitId)) { - userNotificationService.sendNotification(organisationsEinheitId); - } + userNotificationService.sendNotification(vorgangService.getVorgang(VorgangId.from(event.getSource()))); } } 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 5fa3983ea961980b0a10271c7c6f2bb1f0ff63b7..c67083e6f466c08dc6767551345163eef60bd8c6 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 @@ -23,6 +23,8 @@ */ package de.itvsh.kop.notification.user; +import static java.util.Objects.*; + import java.util.List; import org.springframework.beans.factory.annotation.Autowired; @@ -31,12 +33,13 @@ import org.springframework.stereotype.Service; import de.itvsh.kop.notification.email.EmailRemoteService; import de.itvsh.kop.notification.email.UserEmail; +import de.itvsh.kop.notification.vorgang.Vorgang; import lombok.NonNull; @Service public class UserNotificationService { - public static final String MAIL_SUBJECT = "Neuer Vorgang"; + public static final String MAIL_SUBJECT_TEMPLATE = "Neuer Vorgang: %s"; public static final String MAIL_BODY = "Es ist ein neuer Vorgang im Allgemeinen Fachverfahren eingegangen."; @Autowired @@ -46,12 +49,14 @@ public class UserNotificationService { private UserRemoteService userRemoteService; @Async - public void sendNotification(@NonNull String organisationsEinheitId) { - var recipients = userRemoteService.getRecipients(organisationsEinheitId); - emailRemoteService.sendEmail(buildUserEmail(recipients)); + public void sendNotification(@NonNull Vorgang vorgang) { + if (nonNull(vorgang.getOrganisationseinheitenId())) { + var recipients = userRemoteService.getRecipients(vorgang.getOrganisationseinheitenId()); + emailRemoteService.sendEmail(buildUserEmail(recipients, MAIL_SUBJECT_TEMPLATE.formatted(vorgang.getVorgangName()))); + } } - private UserEmail buildUserEmail(List<Recipient> recipients) { - return UserEmail.builder().recipients(recipients).subject(MAIL_SUBJECT).body(MAIL_BODY).build(); + UserEmail buildUserEmail(List<Recipient> recipients, String subject) { + return UserEmail.builder().recipients(recipients).subject(subject).body(MAIL_BODY).build(); } } \ No newline at end of file diff --git a/notification-manager/src/test/java/de/itvsh/kop/notification/email/UserEmailTestFactory.java b/notification-manager/src/test/java/de/itvsh/kop/notification/email/UserEmailTestFactory.java index b1a8cc733224736a32999646fbe9bad4d130bb14..bb94b8788bb3674fd1eff56d97e27b7430b3c511 100644 --- a/notification-manager/src/test/java/de/itvsh/kop/notification/email/UserEmailTestFactory.java +++ b/notification-manager/src/test/java/de/itvsh/kop/notification/email/UserEmailTestFactory.java @@ -23,12 +23,13 @@ */ package de.itvsh.kop.notification.email; +import java.util.List; + import de.itvsh.kop.notification.email.UserEmail.UserEmailBuilder; import de.itvsh.kop.notification.user.Recipient; -import de.itvsh.kop.notification.user.UserNotificationService; import de.itvsh.kop.notification.user.RecipientTestFactory; - -import java.util.List; +import de.itvsh.kop.notification.user.UserNotificationService; +import de.itvsh.kop.notification.vorgang.VorgangTestFactory; public class UserEmailTestFactory { @@ -41,7 +42,7 @@ public class UserEmailTestFactory { public static UserEmailBuilder createBuilder() { return UserEmail.builder() .recipients(RECIPIENTS) - .subject(UserNotificationService.MAIL_SUBJECT) + .subject(UserNotificationService.MAIL_SUBJECT_TEMPLATE.formatted(VorgangTestFactory.VORGANG_NAME)) .body(UserNotificationService.MAIL_BODY); } } 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 65992561673a4c3c992215ab7c298f6a52907d89..806ab1f26092b17fa4dad57c0228b45a419892fa 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 @@ -72,27 +72,14 @@ class UserNotificationEventListenerTest { @Test void shouldNotifyWithOrganisationeinheitIds() { - userNotificationEventListener.onVorgangCreated(EVENT); - - verify(userNotificationService).sendNotification(VorgangTestFactory.ORGANISATIONS_EINHEIT_ID); - } - } - - @DisplayName("with missing orgaId") - @Nested - class TestWithoutOrgaId { + var vorgang = VorgangTestFactory.create(); + when(vorgangService.getVorgang(any())).thenReturn(vorgang); - @BeforeEach - void initMock() { - when(vorgangService.getVorgang(any())).thenReturn(VorgangTestFactory.createBuilder().organisationseinheitenId(null).build()); - } - - @Test - void shouldNotCallNotificationService() { userNotificationEventListener.onVorgangCreated(EVENT); - verifyNoInteractions(userNotificationService); + verify(userNotificationService).sendNotification(vorgang); } } + } } \ 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 0c2deb9aaa41387fd427e08d66e63ce00a81cf72..12c7f3cf9212e3969f1202a1ff88ddc9eb895c72 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 @@ -35,14 +35,17 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.Spy; 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.Vorgang; import de.itvsh.kop.notification.vorgang.VorgangTestFactory; class UserNotificationServiceTest { + @Spy @InjectMocks private UserNotificationService service; @@ -67,16 +70,27 @@ class UserNotificationServiceTest { @Test void shouldGetRecipients() { - service.sendNotification(VorgangTestFactory.ORGANISATIONS_EINHEIT_ID); + service.sendNotification(VorgangTestFactory.create()); verify(userRemoteService).getRecipients(VorgangTestFactory.ORGANISATIONS_EINHEIT_ID); } @Test void shouldSendEmail() { - service.sendNotification(VorgangTestFactory.ORGANISATIONS_EINHEIT_ID); + var email = UserEmailTestFactory.create(); + doReturn(email).when(service).buildUserEmail(anyList(), anyString()); + + service.sendNotification(VorgangTestFactory.create()); + + verify(emailRemoteService).sendEmail(email); + } + + @Test + void shouldSetSubject() { + service.sendNotification(VorgangTestFactory.create()); + + verify(service).buildUserEmail(anyList(), eq("Neuer Vorgang: " + VorgangTestFactory.VORGANG_NAME)); - verify(emailRemoteService).sendEmail(UserEmailTestFactory.create()); } } @@ -84,26 +98,28 @@ class UserNotificationServiceTest { @Nested class TestWithInvalidOrgaId { - @Test - void shouldThrowIllegalArgumentException() { - assertThatThrownBy(() -> service.sendNotification(null)) - .isInstanceOf(IllegalArgumentException.class) - .withFailMessage("organisationsEinheitId cannot be null."); - } + private Vorgang vorgang = VorgangTestFactory.createBuilder().organisationseinheitenId(null).build(); @Test void shouldNotCallUserRemoteService() { - assertThatThrownBy(() -> service.sendNotification(null)); + service.sendNotification(vorgang); verify(userRemoteService, never()).getRecipients(anyString()); } @Test - void shouldNotCallEmailRemoteSErvie() { - assertThatThrownBy(() -> service.sendNotification(null)); + void shouldNotCallEmailRemoteServie() { + service.sendNotification(vorgang); verify(emailRemoteService, never()).sendEmail(any(UserEmail.class)); } } + + @Test + void shouldThrowExceptionWhenVorgangNull() { + assertThatThrownBy(() -> service.sendNotification(null)) + .withFailMessage("organisationsEinheitId cannot be null.") + .isInstanceOf(IllegalArgumentException.class); + } } } \ No newline at end of file