Skip to content
Snippets Groups Projects
Commit 3e2201cc authored by OZGCloud's avatar OZGCloud
Browse files

OZG-2626 OZG-3065 move condition to listener; add @NonNull

parent 427f62b6
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.sendNotification(organisationeinheitId);
var organisationsEinheitId = vorgangService.getVorgang(VorgangId.from(event.getSource())).getOrganisationseinheitenId();
if (Objects.nonNull(organisationsEinheitId)) {
userNotificationService.sendNotification(organisationsEinheitId);
}
}
}
package de.itvsh.kop.notification.user;
import java.util.List;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
......@@ -9,6 +8,7 @@ import org.springframework.stereotype.Service;
import de.itvsh.kop.notification.email.EmailRemoteService;
import de.itvsh.kop.notification.email.UserEmail;
import lombok.NonNull;
@Service
public class UserNotificationService {
......@@ -23,10 +23,7 @@ public class UserNotificationService {
private UserRemoteService userRemoteService;
@Async
public void sendNotification(String organisationsEinheitId) {
if (Objects.isNull(organisationsEinheitId)) {
throw new IllegalArgumentException("organisationsEinheitId cannot be null.");
}
public void sendNotification(@NonNull String organisationsEinheitId) {
var recipients = userRemoteService.getRecipients(organisationsEinheitId);
emailRemoteService.sendEmail(buildUserEmail(recipients));
}
......@@ -34,4 +31,4 @@ public class UserNotificationService {
private UserEmail buildUserEmail(List<Recipient> recipients) {
return UserEmail.builder().recipients(recipients).subject(MAIL_SUBJECT).body(MAIL_BODY).build();
}
}
}
\ No newline at end of file
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,25 +30,46 @@ class UserNotificationEventListenerTest {
class TestOnVorgangCreated {
private static final VorgangCreatedEvent EVENT = new VorgangCreatedEvent(VorgangTestFactory.ID.toString());
private static final Vorgang VORGANG = VorgangTestFactory.create();
@BeforeEach
void initMock() {
when(vorgangService.getVorgang(any())).thenReturn(VORGANG);
}
@DisplayName("with existing orgaId")
@Nested
class TestWithOrgaId {
@BeforeEach
void initMock() {
when(vorgangService.getVorgang(any())).thenReturn(VorgangTestFactory.create());
}
@Test
void shouldCallVorgangService() {
userNotificationEventListener.onVorgangCreated(EVENT);
@Test
void shouldCallVorgangService() {
userNotificationEventListener.onVorgangCreated(EVENT);
verify(vorgangService).getVorgang(VorgangId.from(EVENT.getSource()));
verify(vorgangService).getVorgang(VorgangId.from(EVENT.getSource()));
}
@Test
void shouldNotifyWithOrganisationeinheitIds() {
userNotificationEventListener.onVorgangCreated(EVENT);
verify(userNotificationService).sendNotification(VorgangTestFactory.ORGANISATIONS_EINHEIT_ID);
}
}
@Test
void shouldNotifyWithOrganisationeinheitIds() {
userNotificationEventListener.onVorgangCreated(EVENT);
@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);
verify(userNotificationService).sendNotification(VorgangTestFactory.ORGANISATIONS_EINHEIT_ID);
verifyNoInteractions(userNotificationService);
}
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment