Skip to content
Snippets Groups Projects
Commit 33ae623c authored by OZGCloud's avatar OZGCloud
Browse files

Merge pull request 'OZG-3065_HandleException' (#80) from OZG-3065_HandleException into master

parents d860014c 38d7c06d
No related branches found
No related tags found
No related merge requests found
package de.itvsh.kop.notification.user; package de.itvsh.kop.notification.user;
import de.itvsh.kop.notification.vorgang.VorgangId; import java.util.Objects;
import de.itvsh.kop.notification.vorgang.VorgangService;
import de.itvsh.ozg.pluto.command.VorgangCreatedEvent;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener; import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component; 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 @Component
public class UserNotificationEventListener { public class UserNotificationEventListener {
...@@ -20,7 +21,10 @@ public class UserNotificationEventListener { ...@@ -20,7 +21,10 @@ public class UserNotificationEventListener {
@EventListener @EventListener
public void onVorgangCreated(VorgangCreatedEvent event) { public void onVorgangCreated(VorgangCreatedEvent event) {
var organisationeinheitId = vorgangService.getVorgang(VorgangId.from(event.getSource())).getOrganisationseinheitenId(); var organisationsEinheitId = vorgangService.getVorgang(VorgangId.from(event.getSource())).getOrganisationseinheitenId();
userNotificationService.notify(organisationeinheitId);
if (Objects.nonNull(organisationsEinheitId)) {
userNotificationService.sendNotification(organisationsEinheitId);
}
} }
} }
package de.itvsh.kop.notification.user; package de.itvsh.kop.notification.user;
import de.itvsh.kop.notification.email.EmailRemoteService; import java.util.List;
import de.itvsh.kop.notification.email.UserEmail;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; 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 @Service
public class UserNotificationService { public class UserNotificationService {
...@@ -19,8 +22,9 @@ public class UserNotificationService { ...@@ -19,8 +22,9 @@ public class UserNotificationService {
@Autowired @Autowired
private UserRemoteService userRemoteService; private UserRemoteService userRemoteService;
public void notify(String organisationeinheitId) { @Async
var recipients = userRemoteService.getRecipients(organisationeinheitId); public void sendNotification(@NonNull String organisationsEinheitId) {
var recipients = userRemoteService.getRecipients(organisationsEinheitId);
emailRemoteService.sendEmail(buildUserEmail(recipients)); emailRemoteService.sendEmail(buildUserEmail(recipients));
} }
......
package de.itvsh.kop.notification.user; package de.itvsh.kop.notification.user;
import de.itvsh.kop.notification.vorgang.Vorgang; import static org.mockito.ArgumentMatchers.*;
import de.itvsh.kop.notification.vorgang.VorgangId; import static org.mockito.Mockito.*;
import de.itvsh.kop.notification.vorgang.VorgangService;
import de.itvsh.kop.notification.vorgang.VorgangTestFactory;
import de.itvsh.ozg.pluto.command.VorgangCreatedEvent;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
...@@ -12,8 +10,10 @@ import org.junit.jupiter.api.Test; ...@@ -12,8 +10,10 @@ import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import static org.mockito.ArgumentMatchers.*; import de.itvsh.kop.notification.vorgang.VorgangId;
import static org.mockito.Mockito.*; import de.itvsh.kop.notification.vorgang.VorgangService;
import de.itvsh.kop.notification.vorgang.VorgangTestFactory;
import de.itvsh.ozg.pluto.command.VorgangCreatedEvent;
class UserNotificationEventListenerTest { class UserNotificationEventListenerTest {
...@@ -30,11 +30,14 @@ class UserNotificationEventListenerTest { ...@@ -30,11 +30,14 @@ class UserNotificationEventListenerTest {
class TestOnVorgangCreated { class TestOnVorgangCreated {
private static final VorgangCreatedEvent EVENT = new VorgangCreatedEvent(VorgangTestFactory.ID.toString()); private static final VorgangCreatedEvent EVENT = new VorgangCreatedEvent(VorgangTestFactory.ID.toString());
private static final Vorgang VORGANG = VorgangTestFactory.create();
@DisplayName("with existing orgaId")
@Nested
class TestWithOrgaId {
@BeforeEach @BeforeEach
void initMock() { void initMock() {
when(vorgangService.getVorgang(any())).thenReturn(VORGANG); when(vorgangService.getVorgang(any())).thenReturn(VorgangTestFactory.create());
} }
@Test @Test
...@@ -48,7 +51,25 @@ class UserNotificationEventListenerTest { ...@@ -48,7 +51,25 @@ class UserNotificationEventListenerTest {
void shouldNotifyWithOrganisationeinheitIds() { void shouldNotifyWithOrganisationeinheitIds() {
userNotificationEventListener.onVorgangCreated(EVENT); userNotificationEventListener.onVorgangCreated(EVENT);
verify(userNotificationService).notify(VorgangTestFactory.ORGANISATIONEINCHEIT_ID); verify(userNotificationService).sendNotification(VorgangTestFactory.ORGANISATIONS_EINHEIT_ID);
}
}
@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);
verifyNoInteractions(userNotificationService);
}
} }
} }
} }
\ No newline at end of file
package de.itvsh.kop.notification.user; package de.itvsh.kop.notification.user;
import de.itvsh.kop.notification.email.EmailRemoteService; import static org.assertj.core.api.Assertions.*;
import de.itvsh.kop.notification.email.UserEmail; import static org.mockito.ArgumentMatchers.*;
import de.itvsh.kop.notification.email.UserEmailTestFactory; import static org.mockito.Mockito.*;
import de.itvsh.kop.notification.vorgang.VorgangTestFactory;
import java.util.List;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
...@@ -11,45 +13,74 @@ import org.junit.jupiter.api.Test; ...@@ -11,45 +13,74 @@ import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import java.util.List; import de.itvsh.kop.notification.email.EmailRemoteService;
import de.itvsh.kop.notification.email.UserEmail;
import static org.mockito.Mockito.*; import de.itvsh.kop.notification.email.UserEmailTestFactory;
import de.itvsh.kop.notification.vorgang.VorgangTestFactory;
class UserNotificationServiceTest { class UserNotificationServiceTest {
@InjectMocks @InjectMocks
private UserNotificationService userNotificationService; private UserNotificationService service;
@Mock @Mock
private EmailRemoteService emailRemoteService; private EmailRemoteService emailRemoteService;
@Mock @Mock
private UserRemoteService userRemoteService; private UserRemoteService userRemoteService;
@DisplayName("Notify") @DisplayName("Send notification")
@Nested @Nested
class TestOnNotify { class TestSendNotification {
private static final List<Recipient> RECIPIENTS = List.of(RecipientTestFactory.create()); @DisplayName("with valid orgaId")
private static final String ORGANISATIONEINCHEIT_ID = VorgangTestFactory.ORGANISATIONEINCHEIT_ID; @Nested
private static final UserEmail USER_EMAIL = UserEmailTestFactory.create(); class TestWithValidOrgaId {
@BeforeEach @BeforeEach
void init() { void init() {
when(userRemoteService.getRecipients(ORGANISATIONEINCHEIT_ID)).thenReturn(RECIPIENTS); when(userRemoteService.getRecipients(VorgangTestFactory.ORGANISATIONS_EINHEIT_ID)).thenReturn(List.of(RecipientTestFactory.create()));
doNothing().when(emailRemoteService).sendEmail(any(UserEmail.class));
} }
@Test @Test
void shouldGetRecipients() { void shouldGetRecipients() {
userNotificationService.notify(ORGANISATIONEINCHEIT_ID); service.sendNotification(VorgangTestFactory.ORGANISATIONS_EINHEIT_ID);
verify(userRemoteService).getRecipients(ORGANISATIONEINCHEIT_ID); verify(userRemoteService).getRecipients(VorgangTestFactory.ORGANISATIONS_EINHEIT_ID);
} }
@Test @Test
void shouldSendEmail() { void shouldSendEmail() {
userNotificationService.notify(ORGANISATIONEINCHEIT_ID); service.sendNotification(VorgangTestFactory.ORGANISATIONS_EINHEIT_ID);
verify(emailRemoteService).sendEmail(USER_EMAIL); verify(emailRemoteService).sendEmail(UserEmailTestFactory.create());
}
}
@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, never()).sendEmail(any(UserEmail.class));
}
} }
} }
} }
\ No newline at end of file
...@@ -23,7 +23,7 @@ public class GrpcVorgangTestFactory { ...@@ -23,7 +23,7 @@ public class GrpcVorgangTestFactory {
.setPostfachId(VorgangTestFactory.POSTFACH_ID) .setPostfachId(VorgangTestFactory.POSTFACH_ID)
.build()) .build())
.setZustaendigeStelle( .setZustaendigeStelle(
GrpcZustaendigeStelle.newBuilder().setOrganisationseinheitenId(VorgangTestFactory.ORGANISATIONEINCHEIT_ID).build()) GrpcZustaendigeStelle.newBuilder().setOrganisationseinheitenId(VorgangTestFactory.ORGANISATIONS_EINHEIT_ID).build())
.build()); .build());
} }
......
...@@ -14,7 +14,7 @@ public class VorgangTestFactory { ...@@ -14,7 +14,7 @@ public class VorgangTestFactory {
public static final String POSTFACH_ID = UUID.randomUUID().toString(); public static final String POSTFACH_ID = UUID.randomUUID().toString();
public static final String FORM_ENGINE_NAME = "deluxeEngine"; 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() { public static Vorgang create() {
return createBuilder().build(); return createBuilder().build();
...@@ -28,7 +28,7 @@ public class VorgangTestFactory { ...@@ -28,7 +28,7 @@ public class VorgangTestFactory {
.createdAt(CREATED_AT) .createdAt(CREATED_AT)
.postfachId(POSTFACH_ID) .postfachId(POSTFACH_ID)
.formEngineName(FORM_ENGINE_NAME) .formEngineName(FORM_ENGINE_NAME)
.organisationseinheitenId(ORGANISATIONEINCHEIT_ID); .organisationseinheitenId(ORGANISATIONS_EINHEIT_ID);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment