From 4965422e4bf69e40a00e3bfed11be14da7674975 Mon Sep 17 00:00:00 2001 From: OZGCloud <ozgcloud@mgm-tp.com> Date: Thu, 27 Oct 2022 15:07:34 +0200 Subject: [PATCH] OZG-2626 OZG-3065 add NotNull Validation to organisationsEinheitId --- notification-manager/pom.xml | 4 ++ .../user/UserNotificationService.java | 10 +++- .../user/UserNotificationServiceTest.java | 56 ++++++++++--------- .../pluto/vorgang/UserNotificationITCase.java | 45 +++++++++++++++ 4 files changed, 87 insertions(+), 28 deletions(-) create mode 100644 pluto-server/src/test/java/de/itvsh/ozg/pluto/vorgang/UserNotificationITCase.java diff --git a/notification-manager/pom.xml b/notification-manager/pom.xml index 99cc54ecb..2a6c6965d 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 441131ea3..f9d8caa4c 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 d5f61c569..463469928 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 000000000..14b8bf9cb --- /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 -- GitLab