Skip to content
Snippets Groups Projects
Commit 9b26f300 authored by OZGCloud's avatar OZGCloud
Browse files

Merge pull request 'OZG-3841: Vorgangsname in E-Mail' (#191) from...

Merge pull request 'OZG-3841: Vorgangsname in E-Mail' (#191) from OZG-3841-Vorgangsname-in-EMail into master

Reviewed-on: https://git.ozg-sh.de/mgm/pluto/pulls/191


Reviewed-by: default avatarOZGCloud <ozgcloud@mgm-tp.com>
parents 109c0875 a16b5fac
No related branches found
No related tags found
No related merge requests found
......@@ -23,8 +23,6 @@
*/
package de.itvsh.kop.notification.user;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
......@@ -44,10 +42,6 @@ public class UserNotificationEventListener {
@EventListener
public void onVorgangCreated(VorgangCreatedEvent event) {
var organisationsEinheitId = vorgangService.getVorgang(VorgangId.from(event.getSource())).getOrganisationseinheitenId();
if (Objects.nonNull(organisationsEinheitId)) {
userNotificationService.sendNotification(organisationsEinheitId);
}
userNotificationService.sendNotification(vorgangService.getVorgang(VorgangId.from(event.getSource())));
}
}
......@@ -23,6 +23,8 @@
*/
package de.itvsh.kop.notification.user;
import static java.util.Objects.*;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -31,12 +33,13 @@ import org.springframework.stereotype.Service;
import de.itvsh.kop.notification.email.EmailRemoteService;
import de.itvsh.kop.notification.email.UserEmail;
import de.itvsh.kop.notification.vorgang.Vorgang;
import lombok.NonNull;
@Service
public class UserNotificationService {
public static final String MAIL_SUBJECT = "Neuer Vorgang";
public static final String MAIL_SUBJECT_TEMPLATE = "Neuer Vorgang: %s";
public static final String MAIL_BODY = "Es ist ein neuer Vorgang im Allgemeinen Fachverfahren eingegangen.";
@Autowired
......@@ -46,12 +49,14 @@ public class UserNotificationService {
private UserRemoteService userRemoteService;
@Async
public void sendNotification(@NonNull String organisationsEinheitId) {
var recipients = userRemoteService.getRecipients(organisationsEinheitId);
emailRemoteService.sendEmail(buildUserEmail(recipients));
public void sendNotification(@NonNull Vorgang vorgang) {
if (nonNull(vorgang.getOrganisationseinheitenId())) {
var recipients = userRemoteService.getRecipients(vorgang.getOrganisationseinheitenId());
emailRemoteService.sendEmail(buildUserEmail(recipients, MAIL_SUBJECT_TEMPLATE.formatted(vorgang.getVorgangName())));
}
}
private UserEmail buildUserEmail(List<Recipient> recipients) {
return UserEmail.builder().recipients(recipients).subject(MAIL_SUBJECT).body(MAIL_BODY).build();
UserEmail buildUserEmail(List<Recipient> recipients, String subject) {
return UserEmail.builder().recipients(recipients).subject(subject).body(MAIL_BODY).build();
}
}
\ No newline at end of file
......@@ -23,12 +23,13 @@
*/
package de.itvsh.kop.notification.email;
import java.util.List;
import de.itvsh.kop.notification.email.UserEmail.UserEmailBuilder;
import de.itvsh.kop.notification.user.Recipient;
import de.itvsh.kop.notification.user.UserNotificationService;
import de.itvsh.kop.notification.user.RecipientTestFactory;
import java.util.List;
import de.itvsh.kop.notification.user.UserNotificationService;
import de.itvsh.kop.notification.vorgang.VorgangTestFactory;
public class UserEmailTestFactory {
......@@ -41,7 +42,7 @@ public class UserEmailTestFactory {
public static UserEmailBuilder createBuilder() {
return UserEmail.builder()
.recipients(RECIPIENTS)
.subject(UserNotificationService.MAIL_SUBJECT)
.subject(UserNotificationService.MAIL_SUBJECT_TEMPLATE.formatted(VorgangTestFactory.VORGANG_NAME))
.body(UserNotificationService.MAIL_BODY);
}
}
......@@ -72,27 +72,14 @@ class UserNotificationEventListenerTest {
@Test
void shouldNotifyWithOrganisationeinheitIds() {
userNotificationEventListener.onVorgangCreated(EVENT);
verify(userNotificationService).sendNotification(VorgangTestFactory.ORGANISATIONS_EINHEIT_ID);
}
}
@DisplayName("with missing orgaId")
@Nested
class TestWithoutOrgaId {
var vorgang = VorgangTestFactory.create();
when(vorgangService.getVorgang(any())).thenReturn(vorgang);
@BeforeEach
void initMock() {
when(vorgangService.getVorgang(any())).thenReturn(VorgangTestFactory.createBuilder().organisationseinheitenId(null).build());
}
@Test
void shouldNotCallNotificationService() {
userNotificationEventListener.onVorgangCreated(EVENT);
verifyNoInteractions(userNotificationService);
verify(userNotificationService).sendNotification(vorgang);
}
}
}
}
\ No newline at end of file
......@@ -35,14 +35,17 @@ import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
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.Vorgang;
import de.itvsh.kop.notification.vorgang.VorgangTestFactory;
class UserNotificationServiceTest {
@Spy
@InjectMocks
private UserNotificationService service;
......@@ -67,16 +70,27 @@ class UserNotificationServiceTest {
@Test
void shouldGetRecipients() {
service.sendNotification(VorgangTestFactory.ORGANISATIONS_EINHEIT_ID);
service.sendNotification(VorgangTestFactory.create());
verify(userRemoteService).getRecipients(VorgangTestFactory.ORGANISATIONS_EINHEIT_ID);
}
@Test
void shouldSendEmail() {
service.sendNotification(VorgangTestFactory.ORGANISATIONS_EINHEIT_ID);
var email = UserEmailTestFactory.create();
doReturn(email).when(service).buildUserEmail(anyList(), anyString());
service.sendNotification(VorgangTestFactory.create());
verify(emailRemoteService).sendEmail(email);
}
@Test
void shouldSetSubject() {
service.sendNotification(VorgangTestFactory.create());
verify(service).buildUserEmail(anyList(), eq("Neuer Vorgang: " + VorgangTestFactory.VORGANG_NAME));
verify(emailRemoteService).sendEmail(UserEmailTestFactory.create());
}
}
......@@ -84,26 +98,28 @@ class UserNotificationServiceTest {
@Nested
class TestWithInvalidOrgaId {
@Test
void shouldThrowIllegalArgumentException() {
assertThatThrownBy(() -> service.sendNotification(null))
.isInstanceOf(IllegalArgumentException.class)
.withFailMessage("organisationsEinheitId cannot be null.");
}
private Vorgang vorgang = VorgangTestFactory.createBuilder().organisationseinheitenId(null).build();
@Test
void shouldNotCallUserRemoteService() {
assertThatThrownBy(() -> service.sendNotification(null));
service.sendNotification(vorgang);
verify(userRemoteService, never()).getRecipients(anyString());
}
@Test
void shouldNotCallEmailRemoteSErvie() {
assertThatThrownBy(() -> service.sendNotification(null));
void shouldNotCallEmailRemoteServie() {
service.sendNotification(vorgang);
verify(emailRemoteService, never()).sendEmail(any(UserEmail.class));
}
}
@Test
void shouldThrowExceptionWhenVorgangNull() {
assertThatThrownBy(() -> service.sendNotification(null))
.withFailMessage("organisationsEinheitId cannot be null.")
.isInstanceOf(IllegalArgumentException.class);
}
}
}
\ 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