diff --git a/notification-manager/pom.xml b/notification-manager/pom.xml index 99cc54ecb668c79b90c2d14dbcb17c8fdc50a864..2a6c6965dab0391967d437badcf1ca091ea7d323 100644 --- a/notification-manager/pom.xml +++ b/notification-manager/pom.xml @@ -61,6 +61,10 @@ </dependency> <!-- tools --> + <dependency> + <groupId>jakarta.validation</groupId> + <artifactId>jakarta.validation-api</artifactId> + </dependency> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct</artifactId> 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 441131ea3508bb17ef46f2f154d0d2df04aa8637..f9d8caa4c6041d534236605121518538e853d7cb 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 @@ -2,12 +2,17 @@ package de.itvsh.kop.notification.user; import java.util.List; +import javax.validation.constraints.NotNull; + import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; +import org.springframework.validation.annotation.Validated; import de.itvsh.kop.notification.email.EmailRemoteService; import de.itvsh.kop.notification.email.UserEmail; +@Validated @Service public class UserNotificationService { @@ -20,8 +25,9 @@ public class UserNotificationService { @Autowired private UserRemoteService userRemoteService; - public void sendNotification(String organisationeinheitId) { - var recipients = userRemoteService.getRecipients(organisationeinheitId); + @Async + public void sendNotification(@NotNull String organisationEinheitId) { + var recipients = userRemoteService.getRecipients(organisationEinheitId); emailRemoteService.sendEmail(buildUserEmail(recipients)); } 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 d5f61c5695eb0c05337981f866b9f902be0d1139..463469928c2c03bd1a5863679a861dbde33eda8f 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,10 @@ 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.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 +12,48 @@ 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 orgaIds") + @Nested + class TestWithValidOrgaIds { - @BeforeEach - void init() { - when(userRemoteService.getRecipients(ORGANISATIONEINCHEIT_ID)).thenReturn(RECIPIENTS); - } + @BeforeEach + void init() { + when(userRemoteService.getRecipients(VorgangTestFactory.ORGANISATIONEINCHEIT_ID)).thenReturn(List.of(RecipientTestFactory.create())); + doNothing().when(emailRemoteService).sendEmail(any(UserEmail.class)); + } - @Test - void shouldGetRecipients() { - userNotificationService.sendNotification(ORGANISATIONEINCHEIT_ID); + @Test + void shouldGetRecipients() { + service.sendNotification(VorgangTestFactory.ORGANISATIONEINCHEIT_ID); - verify(userRemoteService).getRecipients(ORGANISATIONEINCHEIT_ID); - } + verify(userRemoteService).getRecipients(VorgangTestFactory.ORGANISATIONEINCHEIT_ID); + } - @Test - void shouldSendEmail() { - userNotificationService.sendNotification(ORGANISATIONEINCHEIT_ID); + @Test + void shouldSendEmail() { + service.sendNotification(VorgangTestFactory.ORGANISATIONEINCHEIT_ID); - verify(emailRemoteService).sendEmail(USER_EMAIL); + verify(emailRemoteService).sendEmail(UserEmailTestFactory.create()); + } } } } \ No newline at end of file diff --git a/pluto-server/src/test/java/de/itvsh/ozg/pluto/vorgang/UserNotificationITCase.java b/pluto-server/src/test/java/de/itvsh/ozg/pluto/vorgang/UserNotificationITCase.java new file mode 100644 index 0000000000000000000000000000000000000000..14b8bf9cbdd80ca4efc67ac8c862635685ebf1c6 --- /dev/null +++ b/pluto-server/src/test/java/de/itvsh/ozg/pluto/vorgang/UserNotificationITCase.java @@ -0,0 +1,45 @@ +package de.itvsh.ozg.pluto.vorgang; + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; + +import javax.validation.ConstraintViolationException; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.security.test.context.support.WithMockUser; + +import de.itvsh.kop.common.test.DataITCase; +import de.itvsh.kop.notification.user.UserNotificationEventListener; +import de.itvsh.kop.notification.vorgang.Vorgang; +import de.itvsh.kop.notification.vorgang.VorgangId; +import de.itvsh.kop.notification.vorgang.VorgangService; +import de.itvsh.ozg.pluto.command.VorgangCreatedEvent; + +@DataITCase +@WithMockUser +public class UserNotificationITCase { + + @Autowired + private UserNotificationEventListener notificationListener; + + @MockBean + private VorgangService vorgangService; + + @DisplayName("Send notification") + @Nested + class TestSendNotification { + + @Test + void shouldThrowConstraintViolationExceptionOnMissingOrgaIds() { + when(vorgangService.getVorgang(any(VorgangId.class))).thenReturn(Vorgang.builder().organisationseinheitenId(null).build()); + + var vorgangCreatedEvent = new VorgangCreatedEvent(VorgangTestFactory.ID.toString()); + assertThatThrownBy(() -> notificationListener.onVorgangCreated(vorgangCreatedEvent)).isInstanceOf(ConstraintViolationException.class); + } + } +} \ No newline at end of file