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
Branches
Tags
No related merge requests found
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);
}
}
}
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,8 +22,9 @@ 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));
}
......
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,11 +30,14 @@ class UserNotificationEventListenerTest {
class TestOnVorgangCreated {
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
void initMock() {
when(vorgangService.getVorgang(any())).thenReturn(VORGANG);
when(vorgangService.getVorgang(any())).thenReturn(VorgangTestFactory.create());
}
@Test
......@@ -48,7 +51,25 @@ class UserNotificationEventListenerTest {
void shouldNotifyWithOrganisationeinheitIds() {
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;
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);
when(userRemoteService.getRecipients(VorgangTestFactory.ORGANISATIONS_EINHEIT_ID)).thenReturn(List.of(RecipientTestFactory.create()));
doNothing().when(emailRemoteService).sendEmail(any(UserEmail.class));
}
@Test
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
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 {
.setPostfachId(VorgangTestFactory.POSTFACH_ID)
.build())
.setZustaendigeStelle(
GrpcZustaendigeStelle.newBuilder().setOrganisationseinheitenId(VorgangTestFactory.ORGANISATIONEINCHEIT_ID).build())
GrpcZustaendigeStelle.newBuilder().setOrganisationseinheitenId(VorgangTestFactory.ORGANISATIONS_EINHEIT_ID).build())
.build());
}
......
......@@ -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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment