diff --git a/nachrichten-manager-interface/src/main/protobuf/postfach.model.proto b/nachrichten-manager-interface/src/main/protobuf/postfach.model.proto index d3cfc107e10928b5897a651ae549a2dbf746072f..b7f2a680d8e3ed5f3a3925efbfe8cfba2255c883 100644 --- a/nachrichten-manager-interface/src/main/protobuf/postfach.model.proto +++ b/nachrichten-manager-interface/src/main/protobuf/postfach.model.proto @@ -87,7 +87,6 @@ message GrpcPostfachNachricht { message GrpcPostfachMail { string id = 1; string vorgangId = 2; - string postfachId = 3 [deprecated = true]; GrpcPostfachAddress postfachAddress = 14; string createdAt = 4; string createdBy = 5; diff --git a/nachrichten-manager-interface/src/test/java/de/ozgcloud/nachrichten/postfach/GrpcPostfachMailTestFactory.java b/nachrichten-manager-interface/src/test/java/de/ozgcloud/nachrichten/postfach/GrpcPostfachMailTestFactory.java index cb6eccfb3ba9f3e01d5b6917de337b63aaed8ffe..c1cd02759d0ad7ec66bd264b9e1e748c5214fe36 100644 --- a/nachrichten-manager-interface/src/test/java/de/ozgcloud/nachrichten/postfach/GrpcPostfachMailTestFactory.java +++ b/nachrichten-manager-interface/src/test/java/de/ozgcloud/nachrichten/postfach/GrpcPostfachMailTestFactory.java @@ -52,7 +52,6 @@ public class GrpcPostfachMailTestFactory { .setId(ID) .setReferencedNachricht(REFERENCED_NACHRICHT_ID) .setVorgangId(VORGANG_ID) - .setPostfachId(POSTFACH_ID) .setPostfachAddress(GrpcPostfachAddressTestFactory.create()) .setCreatedAt(CREATED_AT) .setCreatedBy(CREATED_BY) diff --git a/nachrichten-manager-postfach-interface/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachAddress.java b/nachrichten-manager-postfach-interface/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachAddress.java index 265719c1c85fa6a383d49b690e8e3c505101a597..af6b5712bc595ca5ef6e9ca19bf6a93d807229e5 100644 --- a/nachrichten-manager-postfach-interface/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachAddress.java +++ b/nachrichten-manager-postfach-interface/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachAddress.java @@ -13,6 +13,7 @@ public class PostfachAddress { public static final String TYPE_FIELD = "type"; public static final String IDENTIFIER_FIELD = "identifier"; public static final String SERVICEKONTO_TYPE_FIELD = "serviceKontoType"; + public static final String FIELD_POSTFACH_ID = "postfachId"; private String version; private int type; diff --git a/nachrichten-manager-postfach-interface/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachNachricht.java b/nachrichten-manager-postfach-interface/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachNachricht.java index d76a8251fc2c6df30d2f16e9b2e1e77328d2cafa..a7ee3f5cac3a2225716b49576cf8bd8fc535f59f 100644 --- a/nachrichten-manager-postfach-interface/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachNachricht.java +++ b/nachrichten-manager-postfach-interface/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachNachricht.java @@ -45,7 +45,6 @@ public class PostfachNachricht { public static final String FIELD_ID = "id"; public static final String FIELD_VORGANG_ID = "vorgangId"; - public static final String FIELD_POSTFACH_ID = "postfachId"; public static final String POSTFACH_ADDRESS_FIELD = "postfachAddress"; public static final String REFERENCED_NACHRICHT_FIELD = "referencedNachricht"; @@ -68,8 +67,6 @@ public class PostfachNachricht { private String id; private String vorgangId; - @Deprecated - private String postfachId; private PostfachAddress postfachAddress; private String messageId; private String referencedNachricht; diff --git a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/antragraum/AntragraumNachrichtMapper.java b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/antragraum/AntragraumNachrichtMapper.java index 3c14695971b75f4d1b8543f8f7826a47286a0506..b41a700747966ec81f65ea0dd4de8b8300e647f3 100644 --- a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/antragraum/AntragraumNachrichtMapper.java +++ b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/antragraum/AntragraumNachrichtMapper.java @@ -66,7 +66,6 @@ interface AntragraumNachrichtMapper { @Mapping(target = "messageCode", ignore = true) @Mapping(target = "messageId", source = "rueckfrageId") @Mapping(target = "postfachAddress", ignore = true) - @Mapping(target = "postfachId", ignore = true) @Mapping(target = "replyOption", constant = "FORBIDDEN") @Mapping(target = "sentSuccessful", constant = "true") @Mapping(target = "subject", constant = "Antwort") // TODO klären diff --git a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/GrpcPostfachNachrichtMapper.java b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/GrpcPostfachNachrichtMapper.java index 53a1512d66e5c20b10821c3953e9343ccb7ac690..7f4cc6381dc45324294af91163a40e2d5f0d8652 100644 --- a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/GrpcPostfachNachrichtMapper.java +++ b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/GrpcPostfachNachrichtMapper.java @@ -15,7 +15,6 @@ public interface GrpcPostfachNachrichtMapper { @Mapping(target = "direction", constant = "OUT") @Mapping(target = "messageCode", ignore = true) @Mapping(target = "messageId", ignore = true) - @Mapping(target = "postfachId", ignore = true) @Mapping(target = "sentAt", ignore = true) @Mapping(target = "sentSuccessful", ignore = true) @Mapping(target = "vorgangId", ignore = true) diff --git a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/NotConfiguredException.java b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/NotConfiguredException.java index a677491d25e04978252be8dd2cc48a70cf89bdd4..17faf85389cd8645a36bbf881e9500f6ea743494 100644 --- a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/NotConfiguredException.java +++ b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/NotConfiguredException.java @@ -27,9 +27,8 @@ public class NotConfiguredException extends PostfachException { // NOSONAR private static final long serialVersionUID = 1L; - public NotConfiguredException() { - super("Postfach is not completely configured. Sending and receiving of postfach mails is not possible.", - PostfachMessageCode.SERVER_CONNECTION_FAILED_MESSAGE_CODE); + public NotConfiguredException(String message) { + super(message, PostfachMessageCode.SERVICE_NOT_CONFIGURED); } } diff --git a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachEventListener.java b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachEventListener.java index 9937bbbfbc0ad58912046e60b791c4e8e8af8323..1f65a7f5a3b714fbba698a3c3c3a985192210451 100644 --- a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachEventListener.java +++ b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachEventListener.java @@ -77,7 +77,6 @@ public class PostfachEventListener { var postfachMailBuilder = PostfachNachricht.builder() .id(isNewNachricht(command) ? null : command.getRelationId()) .vorgangId(command.getVorgangId()) - .postfachId(MapUtils.getString(commandBody, PostfachNachricht.FIELD_POSTFACH_ID)) .replyOption(PostfachNachricht.ReplyOption.valueOf(MapUtils.getString(commandBody, PostfachNachricht.FIELD_REPLY_OPTION))) .createdBy(command.getCreatedBy()) .direction(Direction.OUT) @@ -110,7 +109,7 @@ public class PostfachEventListener { private PostfachAddressIdentifier buildIdentifier(Map<String, Object> postfachAddress) { var identifier = getIdentifier(postfachAddress); - return StringBasedIdentifier.builder().postfachId(MapUtils.getString(identifier, PostfachNachricht.FIELD_POSTFACH_ID)).build(); + return StringBasedIdentifier.builder().postfachId(MapUtils.getString(identifier, PostfachAddress.FIELD_POSTFACH_ID)).build(); } @SuppressWarnings("unchecked") diff --git a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachMessageCode.java b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachMessageCode.java index f2e5092837376b33c90d73b78b0fbf2ae963c85f..b40b750ad00759c5d0b124a3d47910715ea047e0 100644 --- a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachMessageCode.java +++ b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachMessageCode.java @@ -33,7 +33,8 @@ public enum PostfachMessageCode { PROCESS_FAILED_MESSAGE_CODE("postfachnachricht.server.processing_failed"), SERVER_CONNECTION_FAILED_MESSAGE_CODE("postfachnachricht.server.connection_failed"), SEND_SUCCESSFUL_MESSAGE_CODE("postfachnachricht.successful"), - SEND_FAILED_UNKNOWN_POSTFACH_ID_MESSAGE_CODE("postfachnachricht.server.unknown_postfach_id"); + SEND_FAILED_UNKNOWN_POSTFACH_ID_MESSAGE_CODE("postfachnachricht.server.unknown_postfach_id"), + SERVICE_NOT_CONFIGURED("postfachnachricht.server.service_not_configured"); @Getter private String messageCode; diff --git a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachNachrichtMapper.java b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachNachrichtMapper.java index f0b40d4b0da955f55c627a4edaa19ad7eb6e132e..f50c0fe9229b7d048c4da70b6f960a3ef0cd0847 100644 --- a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachNachrichtMapper.java +++ b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachNachrichtMapper.java @@ -76,7 +76,6 @@ public abstract class PostfachNachrichtMapper { .setId(MapUtils.getString(mailMap, PostfachNachricht.FIELD_ID)) .setVorgangId(MapUtils.getString(mailMap, PostfachNachricht.FIELD_VORGANG_ID)) .setReferencedNachricht(MapUtils.getString(mailMap, PostfachNachricht.REFERENCED_NACHRICHT_FIELD, StringUtils.EMPTY)) - .setPostfachId(MapUtils.getString(mailMap, PostfachNachricht.FIELD_POSTFACH_ID, StringUtils.EMPTY)) .setCreatedAt(MapUtils.getString(mailMap, PostfachNachricht.FIELD_CREATED_AT)) .setCreatedBy(MapUtils.getString(mailMap, PostfachNachricht.FIELD_CREATED_BY, StringUtils.EMPTY)) .setSentAt(MapUtils.getString(mailMap, PostfachNachricht.FIELD_SENT_AT, StringUtils.EMPTY)) @@ -131,7 +130,6 @@ public abstract class PostfachNachrichtMapper { .id(MapUtils.getString(mailMap, PostfachNachricht.FIELD_ID)) .vorgangId(MapUtils.getString(mailMap, (PostfachNachricht.FIELD_VORGANG_ID))) .referencedNachricht(MapUtils.getString(mailMap, PostfachNachricht.REFERENCED_NACHRICHT_FIELD, StringUtils.EMPTY)) - .postfachId(MapUtils.getString(mailMap, PostfachNachricht.FIELD_POSTFACH_ID, StringUtils.EMPTY)) .messageId(MapUtils.getString(mailMap, PostfachNachricht.FIELD_MESSAGE_ID)) .createdAt(ZonedDateTime.parse(MapUtils.getString(mailMap, PostfachNachricht.FIELD_CREATED_AT))) .createdBy(MapUtils.getString(mailMap, PostfachNachricht.FIELD_CREATED_BY)) @@ -183,7 +181,7 @@ public abstract class PostfachNachrichtMapper { private String getPostfachId(Map<String, Object> postfachAddressMap) { var identifier = getAsMap(postfachAddressMap, PostfachAddress.IDENTIFIER_FIELD); - return MapUtils.getString(identifier, PostfachNachricht.FIELD_POSTFACH_ID); + return MapUtils.getString(identifier, PostfachAddress.FIELD_POSTFACH_ID); } @@ -202,7 +200,6 @@ public abstract class PostfachNachrichtMapper { PostfachNachricht.FIELD_MAIL_BODY, nachricht.getMailBody(), PostfachNachricht.FIELD_ATTACHMENTS, nachricht.getAttachments())); - putIfNonNull(result, PostfachNachricht.FIELD_POSTFACH_ID, nachricht.getPostfachId()); putIfNonNull(result, PostfachNachricht.FIELD_MESSAGE_ID, nachricht.getMessageId()); putIfNonNull(result, PostfachNachricht.FIELD_CREATED_BY, nachricht.getCreatedBy()); putIfNonNull(result, PostfachNachricht.FIELD_SENT_SUCCESSFUL, nachricht.getSentSuccessful()); @@ -243,6 +240,6 @@ public abstract class PostfachNachrichtMapper { private Map<String, Object> buildPostfachAddressIdentifierMap(PostfachAddress postfachAddress) { var identifier = (StringBasedIdentifier) postfachAddress.getIdentifier(); - return Map.of(PostfachNachricht.FIELD_POSTFACH_ID, identifier.getPostfachId()); + return Map.of(PostfachAddress.FIELD_POSTFACH_ID, identifier.getPostfachId()); } } \ No newline at end of file diff --git a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachService.java b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachService.java index babbc9de0317a3d4fd3fb96abf14253fd3f0138e..0a72b6c68c8c0ce9c26de78c322623683cc6e1ed 100644 --- a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachService.java +++ b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/PostfachService.java @@ -23,11 +23,12 @@ package de.ozgcloud.nachrichten.postfach; import java.time.ZonedDateTime; +import java.util.Collection; import java.util.EnumSet; +import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; -import java.util.function.Predicate; import java.util.stream.Stream; import jakarta.annotation.PostConstruct; @@ -40,7 +41,6 @@ import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; import de.ozgcloud.nachrichten.antragraum.AntragraumService; -import de.ozgcloud.nachrichten.attributes.ClientAttributeService; import de.ozgcloud.nachrichten.info.InfoManagerService; import de.ozgcloud.nachrichten.postfach.PostfachNachricht.Direction; import de.ozgcloud.nachrichten.postfach.PostfachNachricht.ReplyOption; @@ -54,32 +54,25 @@ import lombok.extern.log4j.Log4j2; @Validated class PostfachService { - static final Predicate<PostfachNachricht> IS_MUK_ANSWER = nachricht -> StringUtils.isEmpty(nachricht.getVorgangId()) && StringUtils.isNotEmpty( - nachricht.getReferencedNachricht()); static final Set<ReplyOption> REPLY_POSSIBLE_OPTION = EnumSet.of(ReplyOption.POSSIBLE, ReplyOption.MANDATORY); - private static final Predicate<PostfachNachricht> IS_FROM_HUMAN_USER = nachricht -> !StringUtils.startsWith(nachricht.getCreatedBy(), "system"); private static final Set<String> POSTFACH_TYPES_WITH_ANTRAGSRAUM = Set.of("BAYERN_ID"); - @Autowired(required = false) - private PostfachRemoteService postfachRemoteService; @Autowired - private PostfachNachrichtMapper mapper; - + private Optional<Collection<PostfachRemoteService>> postfachRemoteServices; @Autowired - private PersistPostfachNachrichtService persistingService; + private Optional<InfoManagerService> infomanagerService; @Autowired - private ClientAttributeService clientAttributeService; - + private Optional<AntragraumService> antragraumService; @Autowired - private ApplicationEventPublisher publisher; - + private PersistPostfachNachrichtService persistingService; @Autowired private CurrentUserService userService; @Autowired - private Optional<InfoManagerService> infomanagerService; + private PostfachNachrichtMapper mapper; + @Autowired - private Optional<AntragraumService> antragraumService; + private ApplicationEventPublisher publisher; @PostConstruct void logInit() { @@ -88,29 +81,33 @@ class PostfachService { } public void saveDraft(String vorgangId, PostfachNachricht nachricht) { - persistMail(userService.getUser().getUserId(), + persistPostfachNachricht(userService.getUser().getUserId(), nachricht.toBuilder().vorgangId(vorgangId) .createdBy(userService.getUser().getUserId().orElse(null)) .build()); } public void sendMail(String commandId, String userId, @Valid PostfachNachricht mail) { - var sendResponse = handleSendMail(commandId, mail); - persistSentMail(userId, addMailSentInformation(mail, sendResponse)); + var sendResponse = handleSendPostfachNachricht(commandId, mail); + persistSentPostfachNachricht(userId, addPostfachNachrichtSentInformation(mail, sendResponse)); } - PostfachNachricht addMailSentInformation(PostfachNachricht mail, SendPostfachNachrichtResponse sendResponse) { + PostfachNachricht addPostfachNachrichtSentInformation(PostfachNachricht mail, SendPostfachNachrichtResponse sendResponse) { return mail.toBuilder().sentAt(ZonedDateTime.now()).sentSuccessful(sendResponse.isSentSuccessful()) .messageCode(sendResponse.getMessageCode().getMessageCode()) .build(); } - void persistSentMail(@NonNull String userId, PostfachNachricht mail) { - persistMail(Optional.of(userId), mail.toBuilder() - .direction(Direction.OUT) - .createdAt(ZonedDateTime.now().withNano(0)).createdBy(userId) - .build()); + void persistSentPostfachNachricht(@NonNull String userId, PostfachNachricht postfachNachricht) { + persistPostfachNachricht(Optional.of(userId), buildOutgoingPostfachNachricht(userId, postfachNachricht)); + } + PostfachNachricht buildOutgoingPostfachNachricht(String userId, PostfachNachricht postfachNachricht) { + return postfachNachricht.toBuilder() + .direction(Direction.OUT) + .createdAt(ZonedDateTime.now().withNano(0)) + .createdBy(userId) + .build(); } public Optional<Map<String, Object>> findById(String nachrichtId) { @@ -122,17 +119,29 @@ class PostfachService { } public void fetchAndPersistReplies() { - ifPostfachConfigured(() -> postfachRemoteService.getAllMessages().forEach(this::persistReceivedMail)); + getPostfachRemoteServices().forEach(this::fetchAndPersistReplies); + } + + void fetchAndPersistReplies(PostfachRemoteService postfachRemoteService) { + try { + postfachRemoteService.getAllMessages().map(this::persistReceivedPostfachNachricht) + .forEach(postfachNachricht -> postfachRemoteService.deleteMessage(postfachNachricht.getMessageId())); + } catch (UnsupportedOperationException e) { + LOG.debug("Fetching of messages not supported for postfach type: {}", postfachRemoteService.getPostfachType()); + } } - private void persistReceivedMail(PostfachNachricht nachricht) { - if (IS_MUK_ANSWER.test(nachricht)) { - nachricht = linkToVorgang(nachricht); + PostfachNachricht persistReceivedPostfachNachricht(PostfachNachricht postfachNachricht) { + if (isMukAnswer(postfachNachricht)) { + postfachNachricht = linkToVorgang(postfachNachricht); } - persistMail(Optional.empty(), nachricht); + persistPostfachNachricht(Optional.empty(), postfachNachricht); + return postfachNachricht; + } - postfachRemoteService.deleteMessage(nachricht.getMessageId()); + boolean isMukAnswer(PostfachNachricht postfachNachricht) { + return StringUtils.isEmpty(postfachNachricht.getVorgangId()) && StringUtils.isNotEmpty(postfachNachricht.getReferencedNachricht()); } PostfachNachricht linkToVorgang(PostfachNachricht nachricht) { @@ -140,52 +149,50 @@ class PostfachService { return nachricht.toBuilder().vorgangId(persistedNachricht.getVorgangId()).build(); } - void persistMail(Optional<String> userId, PostfachNachricht mail) { + void persistPostfachNachricht(Optional<String> userId, PostfachNachricht mail) { persistingService.persistNachricht(userId, mail); - - if (IS_FROM_HUMAN_USER.test(mail)) { - clientAttributeService.setHasPostfachNachricht(mail.getVorgangId()); - } } public void resendMail(String commandId, String postfachMailId) { PostfachNachricht nachricht = mapper.fromMapToPostfachMail(persistingService.getById(postfachMailId)); - var sendResponse = handleSendMail(commandId, nachricht); - patchMail(nachricht.getId(), createResendPatchMap(sendResponse)); + var sendResponse = handleSendPostfachNachricht(commandId, nachricht); + patchPostfachNachricht(nachricht.getId(), createResendPatchMap(sendResponse)); } - SendPostfachNachrichtResponse handleSendMail(String commandId, PostfachNachricht mail) { + SendPostfachNachrichtResponse handleSendPostfachNachricht(String commandId, PostfachNachricht mail) { try { var processedPostfachNachricht = processForAntragsraum(mail).orElse(mail); - doSendMail(processedPostfachNachricht); + doSendPostfachNachricht(processedPostfachNachricht); publishMailSentEvent(commandId); return buildSendNachrichtResponse(true, PostfachMessageCode.SEND_SUCCESSFUL_MESSAGE_CODE); } catch (PostfachException e) { - logSendMailFailed(commandId, e); + logSendPostfachNachrichtFailed(commandId, e); return proceedWithException(commandId, e); } } Optional<PostfachNachricht> processForAntragsraum(PostfachNachricht mail) { - if (isNotifyAntragsraum(mail.getReplyOption())) { + if (shouldNotifyAntragsraum(mail)) { infomanagerService.ifPresent(infoManager -> infoManager.notifyInfoManager(mail)); - return adjustMail(mail); + return adjustPostfachNachricht(mail); } return Optional.empty(); } - boolean isNotifyAntragsraum(ReplyOption replyOption) { - return REPLY_POSSIBLE_OPTION.contains(replyOption) && isPostfachWithAntragsraum(postfachRemoteService); + boolean shouldNotifyAntragsraum(PostfachNachricht postfachNachricht) { + return REPLY_POSSIBLE_OPTION.contains(postfachNachricht.getReplyOption()) + && findPostfachRemoteService(postfachNachricht.getPostfachAddress().getServiceKontoType()).filter(this::isPostfachWithAntragsraum) + .isPresent(); } - Optional<PostfachNachricht> adjustMail(PostfachNachricht nachricht) { + Optional<PostfachNachricht> adjustPostfachNachricht(PostfachNachricht nachricht) { return antragraumService.map(antragraum -> nachricht.toBuilder().mailBody(antragraum.getUserNotificationText()).build()); } - void logSendMailFailed(String commandId, PostfachException exception) { + void logSendPostfachNachrichtFailed(String commandId, PostfachException exception) { if (exception instanceof OsiPostfachServerProcessException || exception.getMessageCode() == PostfachMessageCode.SEND_FAILED_UNKNOWN_POSTFACH_ID_MESSAGE_CODE) { LOG.warn("Failed to send mail with commandId: {}", commandId, exception); @@ -199,8 +206,12 @@ class PostfachService { return buildSendNachrichtResponse(false, e.getMessageCode()); } - void doSendMail(PostfachNachricht nachricht) { - ifPostfachConfigured(() -> postfachRemoteService.sendMessage(nachricht)); + void doSendPostfachNachricht(PostfachNachricht postfachNachricht) { + var postfachType = postfachNachricht.getPostfachAddress().getServiceKontoType(); + findPostfachRemoteService(postfachType).ifPresentOrElse(remoteService -> remoteService.sendMessage(postfachNachricht), + () -> { + throw new NotConfiguredException("Postfach of type %s not configured".formatted(postfachType)); + }); } private void publishMailSentEvent(String commandId) { @@ -221,29 +232,24 @@ class PostfachService { PostfachNachricht.FIELD_MESSAGE_CODE, sendResponse.getMessageCode().getMessageCode()); } - private void patchMail(String postfachMailId, Map<String, Object> propertyMap) { + void patchPostfachNachricht(String postfachMailId, Map<String, Object> propertyMap) { persistingService.patch(postfachMailId, propertyMap); } public boolean isPostfachConfigured() { - return postfachRemoteService != null; + return postfachRemoteServices.isPresent(); } public Stream<Postfach> getPostfachs() { - return isPostfachConfigured() ? getPostfaecher() : Stream.empty(); + return postfachRemoteServices.stream().flatMap(Collection::stream).map(this::getPostfaecher).flatMap(Collection::stream); } - private Stream<Postfach> getPostfaecher() { - var postfach = buildPostfach(postfachRemoteService); - // TODO Nach Umstellung auf BAYERN_ID entfernen - if (postfachRemoteService.getPostfachType().equals("BAYERN_ID")) { - return Stream.of(postfach, Postfach.builder() - .type("BayernId") - .isReplyAllowed(postfach.isReplyAllowed()) - .build()); - } - // - return Stream.of(postfach); + // TODO Nach Umstellung auf BAYERN_ID entfernen + private List<Postfach> getPostfaecher(PostfachRemoteService remoteService) { + var postfach = buildPostfach(remoteService); + return "BAYERN_ID".equals(remoteService.getPostfachType()) + ? List.of(postfach, Postfach.builder().type("BayernId").isReplyAllowed(postfach.isReplyAllowed()).build()) + : List.of(postfach); } Postfach buildPostfach(PostfachRemoteService postfachRemoteService) { @@ -258,10 +264,16 @@ class PostfachService { return POSTFACH_TYPES_WITH_ANTRAGSRAUM.contains(postfachRemoteService.getPostfachType()) && infomanagerService.isPresent(); } - private void ifPostfachConfigured(Runnable block) { - if (!isPostfachConfigured()) { - throw new NotConfiguredException(); - } - block.run(); + Optional<PostfachRemoteService> findPostfachRemoteService(String postfachType) { + return getPostfachRemoteServices().filter(remoteService -> hasPostfachType(remoteService, postfachType)).findFirst(); + } + + boolean hasPostfachType(PostfachRemoteService postfachRemoteService, String postfachType) { + return StringUtils.equals(postfachRemoteService.getPostfachType(), postfachType); + } + + Stream<PostfachRemoteService> getPostfachRemoteServices() { + return postfachRemoteServices.map(Collection::stream).orElseThrow(() -> new NotConfiguredException("No postfach configured")); } + } \ No newline at end of file diff --git a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/osi/OsiPostfachMessageMapper.java b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/osi/OsiPostfachMessageMapper.java index ad900a06ffc7f7a077444f70276d627d2be8a657..38c7c4a73587a9ab0c4ac3371f62203f59d77fab 100644 --- a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/osi/OsiPostfachMessageMapper.java +++ b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/osi/OsiPostfachMessageMapper.java @@ -3,6 +3,7 @@ package de.ozgcloud.nachrichten.postfach.osi; import java.util.List; import java.util.Optional; +import org.apache.commons.lang3.StringUtils; import org.mapstruct.Mapper; import org.mapstruct.Mapping; import org.mapstruct.Named; @@ -51,15 +52,13 @@ public abstract class OsiPostfachMessageMapper { return attachments.stream().map(attachment -> messageAttachmentService.persistAttachment(vorgangId, attachment)).toList(); } - @Mapping(target = "attachment", ignore = true) - @Mapping(target = "messageId", ignore = true) String toPostfachId(PostfachNachricht nachricht) { return Optional.ofNullable(nachricht.getPostfachAddress()) .map(PostfachAddress::getIdentifier) .filter(StringBasedIdentifier.class::isInstance) .map(StringBasedIdentifier.class::cast) .map(StringBasedIdentifier::getPostfachId) - .orElse(nachricht.getPostfachId()); + .orElse(StringUtils.EMPTY); } PostfachAddress buildPostfachAddressByPostfachId(String postfachId) { diff --git a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/osi/OsiPostfachRemoteService.java b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/osi/OsiPostfachRemoteService.java index a355f1a63849411bf6ae24c8f9dd73e1b36767c8..bde0737b210d53d4d739ab85d565ccf0ecca5289 100644 --- a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/osi/OsiPostfachRemoteService.java +++ b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/osi/OsiPostfachRemoteService.java @@ -116,7 +116,8 @@ class OsiPostfachRemoteService implements PostfachRemoteService { private void checkWhetherIsConfigured() { if (isNotConfigured()) { - throw new NotConfiguredException(); + throw new NotConfiguredException( + "Postfach %s is not completely configured. Sending and receiving of postfach mails is not possible.".formatted(POSTFACH_TYPE)); } } diff --git a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachEventListenerTest.java b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachEventListenerTest.java index 5ad3dbcac003e9bfa6120fad4a40c41382bae03c..ceceb14dfd71b40b39c898826c0ef22cbda3115e 100644 --- a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachEventListenerTest.java +++ b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachEventListenerTest.java @@ -110,25 +110,6 @@ class PostfachEventListenerTest { assertThat(nachrichtCaptor.getValue().getId()).isEqualTo(PostfachNachrichtTestFactory.ID); } - @DisplayName("with postfachAddress") - @Nested - class TestWithPostfachAddress { - - @BeforeEach - void mockCommandBodyObject() { - var body = buildCommandBody(); - body.remove(PostfachNachricht.FIELD_POSTFACH_ID); - when(command.getBodyObject()).thenReturn(body); - } - - @Test - void shouldHandleEmptyPostfachId() { - var postfachMailToSend = sendPostfachNachricht(); - - assertThat(postfachMailToSend.getPostfachId()).isNull(); - } - } - @DisplayName("with postfachId") @Nested class TestWithPostfachId { @@ -160,13 +141,12 @@ class PostfachEventListenerTest { var map = new HashMap<String, Object>(); map.putAll(Map.of( PostfachNachricht.FIELD_VORGANG_ID, "fake", - PostfachNachricht.FIELD_POSTFACH_ID, MessageTestFactory.POSTFACH_ID, PostfachNachricht.POSTFACH_ADDRESS_FIELD, // Map.of(PostfachAddress.VERSION_FIELD, PostfachAddressTestFactory.VERSION, PostfachAddress.TYPE_FIELD, PostfachAddressTestFactory.TYPE, PostfachAddress.SERVICEKONTO_TYPE_FIELD, PostfachTestFactory.POSTFACH_TYPE, PostfachAddress.IDENTIFIER_FIELD, // - Map.of(PostfachNachricht.FIELD_POSTFACH_ID, PostfachAddressTestFactory.STRING_BASED_IDENTIFIER_POSTFACH_ID_VALUE)), + Map.of(PostfachAddress.FIELD_POSTFACH_ID, PostfachAddressTestFactory.STRING_BASED_IDENTIFIER_POSTFACH_ID_VALUE)), PostfachNachricht.FIELD_SUBJECT, MessageTestFactory.SUBJECT, PostfachNachricht.FIELD_MAIL_BODY, PostfachNachrichtTestFactory.MAIL_BODY, PostfachNachricht.FIELD_REPLY_OPTION, PostfachNachrichtTestFactory.REPLY_OPTION.name(), diff --git a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachGrpcServiceTest.java b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachGrpcServiceTest.java index 163cf3ac79d6d890cb8796b94d10bdf270d30400..ae6d5b59be186f5d2fd0c0a649881e0354a136f3 100644 --- a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachGrpcServiceTest.java +++ b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachGrpcServiceTest.java @@ -40,7 +40,6 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Spy; -import de.ozgcloud.nachrichten.postfach.osi.MessageTestFactory; import io.grpc.stub.StreamObserver; class PostfachGrpcServiceTest { @@ -129,7 +128,6 @@ class PostfachGrpcServiceTest { private PostfachNachricht createPostfachNachricht() { return PostfachNachrichtTestFactory.createBuilder() .id(GrpcPostfachMailTestFactory.ID) - .postfachId(GrpcPostfachMailTestFactory.POSTFACH_ID) .vorgangId(GrpcPostfachMailTestFactory.VORGANG_ID) .postfachAddress(PostfachAddressTestFactory.createBuilder() .identifier(StringBasedIdentifier.builder().postfachId(GrpcPostfachMailTestFactory.POSTFACH_ID).build()).build()) @@ -282,7 +280,7 @@ class PostfachGrpcServiceTest { @Nested class TestGetPostfachConfig { - private GrpcGetPostfachConfigRequest request = GrpcGetPostfachConfigRequest.newBuilder().build(); + private final GrpcGetPostfachConfigRequest request = GrpcGetPostfachConfigRequest.newBuilder().build(); @Mock private StreamObserver<GrpcGetPostfachConfigResponse> responseObserver; diff --git a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachNachrichtMapperTest.java b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachNachrichtMapperTest.java index 40f9862129b74ab2813d04e82cdcac2d398411b1..1aa4ab376a231060253d04240da6f0598160b4b4 100644 --- a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachNachrichtMapperTest.java +++ b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachNachrichtMapperTest.java @@ -37,8 +37,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; import org.mapstruct.factory.Mappers; import org.mockito.InjectMocks; import org.mockito.Mock; @@ -83,7 +81,7 @@ class PostfachNachrichtMapperTest { map.put(PostfachNachricht.FIELD_CREATED_BY, GrpcPostfachMailTestFactory.CREATED_BY); map.put(PostfachNachricht.FIELD_ID, GrpcPostfachMailTestFactory.ID); map.put(PostfachNachricht.FIELD_MESSAGE_CODE, GrpcPostfachMailTestFactory.MESSAGE_CODE); - map.put(PostfachNachricht.FIELD_POSTFACH_ID, GrpcPostfachMailTestFactory.POSTFACH_ID); + map.put(PostfachAddress.FIELD_POSTFACH_ID, GrpcPostfachMailTestFactory.POSTFACH_ID); map.put(PostfachNachricht.FIELD_VORGANG_ID, GrpcPostfachMailTestFactory.VORGANG_ID); return map; } @@ -109,15 +107,6 @@ class PostfachNachrichtMapperTest { assertThat(grpcMail.getSentAt()).isEmpty(); } - @ParameterizedTest - @ValueSource(strings = PostfachNachricht.FIELD_POSTFACH_ID) - void shouldMapDefaultOnNonExistingValues(String fieldName) { - var mapped = fromMap(getMapWithoutProperty(fieldName)); - - var fieldValue = getAttributeFromGrpcObject(mapped, fieldName); - assertThat((String) fieldValue).isEmpty(); - } - @Test void shouldProceedWithSingleAttachment() { var postfachNachrichtMap = PostfachNachrichtTestFactory.asMap(); @@ -214,15 +203,6 @@ class PostfachNachrichtMapperTest { } } - @ParameterizedTest - @ValueSource(strings = PostfachNachricht.FIELD_POSTFACH_ID) - void shouldMapDefaultOnNonExistingValues(String fieldName) { - var mapped = fromMapToPostfachMail(getMapWithoutProperty(fieldName)); - - var fieldValue = getAttributeFromObject(mapped, fieldName); - assertThat((String) fieldValue).isEmpty(); - } - private PostfachNachricht fromMapToPostfachMail(Map<String, Object> map) { return mapper.fromMapToPostfachMail(map); } @@ -288,13 +268,12 @@ class PostfachNachrichtMapperTest { .setMessageCode(PostfachNachrichtTestFactory.MESSAGE_CODE) .setPostfachAddress(GrpcPostfachAddressTestFactory.createBuilder() .setIdentifier(GrpcPostfachAddressTestFactory.createIdentifier(MessageTestFactory.POSTFACH_ID))) - .setPostfachId(MessageTestFactory.POSTFACH_ID) .setVorgangId(MessageTestFactory.VORGANG_ID).build(); } @Test void shouldHandleEmptyPostfachId() { - var postfachMailWithoutPostfachId = GrpcPostfachMailTestFactory.createBuilder().clearPostfachId().build(); + var postfachMailWithoutPostfachId = GrpcPostfachMailTestFactory.createBuilder().build(); assertDoesNotThrow(() -> fromGrpcMail(postfachMailWithoutPostfachId)); } @@ -354,7 +333,7 @@ class PostfachNachrichtMapperTest { @Test void shouldHandleNullAsPostfachId() { - var postfachMail = PostfachNachrichtTestFactory.createBuilder().postfachId(null).build(); + var postfachMail = PostfachNachrichtTestFactory.create(); assertDoesNotThrow(() -> mapper.mapToMap(postfachMail)); } diff --git a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachNachrichtTestFactory.java b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachNachrichtTestFactory.java index d58ab79a63e7b6984c5c9d641bef9460086e2117..4e028a95578a5bb01ac8464acfe24826b1e926c5 100644 --- a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachNachrichtTestFactory.java +++ b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachNachrichtTestFactory.java @@ -61,7 +61,6 @@ public class PostfachNachrichtTestFactory { return PostfachNachricht.builder() .id(ID) .referencedNachricht(GrpcPostfachMailTestFactory.REFERENCED_NACHRICHT_ID) - .postfachId(POSTFACH_ID) .postfachAddress(PostfachAddressTestFactory.create()) .messageId(MESSAGE_ID) .vorgangId(VORGANG_ID) @@ -81,7 +80,6 @@ public class PostfachNachrichtTestFactory { var map = new HashMap<String, Object>(); map.put(PostfachNachricht.FIELD_ID, ID); map.put(PostfachNachricht.REFERENCED_NACHRICHT_FIELD, GrpcPostfachMailTestFactory.REFERENCED_NACHRICHT_ID); - map.put(PostfachNachricht.FIELD_POSTFACH_ID, POSTFACH_ID); map.put(PostfachNachricht.POSTFACH_ADDRESS_FIELD, getPostfachAddressAsMap()); map.put(PostfachNachricht.FIELD_MESSAGE_ID, MESSAGE_ID); map.put(PostfachNachricht.FIELD_VORGANG_ID, VORGANG_ID); @@ -106,6 +104,6 @@ public class PostfachNachrichtTestFactory { } private static Map<String, Object> getPostfachAddressIdentifierAsMap() { - return Map.of(PostfachNachricht.FIELD_POSTFACH_ID, PostfachAddressTestFactory.STRING_BASED_IDENTIFIER_POSTFACH_ID_VALUE); + return Map.of(PostfachAddress.FIELD_POSTFACH_ID, PostfachAddressTestFactory.STRING_BASED_IDENTIFIER_POSTFACH_ID_VALUE); } } diff --git a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachServiceTest.java b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachServiceTest.java index 7355c0c544c960eca1c258c24856a18039599e67..ca8bde69ac33680ed7dc28b5ab92d491934d5e8a 100644 --- a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachServiceTest.java +++ b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/PostfachServiceTest.java @@ -22,13 +22,16 @@ */ package de.ozgcloud.nachrichten.postfach; -import static de.ozgcloud.nachrichten.postfach.osi.MessageTestFactory.*; import static org.assertj.core.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; import java.time.ZonedDateTime; import java.time.temporal.ChronoUnit; +import java.util.Collection; +import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.UUID; import java.util.stream.Stream; @@ -40,7 +43,6 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.EnumSource; -import org.mapstruct.factory.Mappers; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.InjectMocks; @@ -51,11 +53,7 @@ import org.springframework.context.ApplicationEventPublisher; import org.springframework.test.util.ReflectionTestUtils; import de.ozgcloud.nachrichten.antragraum.AntragraumService; -import de.ozgcloud.nachrichten.attributes.ClientAttributeService; import de.ozgcloud.nachrichten.info.InfoManagerService; -import de.ozgcloud.nachrichten.postfach.PostfachNachricht.Direction; -import de.ozgcloud.nachrichten.postfach.PostfachNachricht.ReplyOption; -import de.ozgcloud.nachrichten.postfach.osi.MessageAttachmentService; import de.ozgcloud.nachrichten.postfach.osi.MessageTestFactory; import de.ozgcloud.nachrichten.postfach.osi.OsiPostfachServerProcessExceptionTestFactory; import de.ozgcloud.vorgang.callcontext.CallContextUserTestFactory; @@ -71,16 +69,14 @@ class PostfachServiceTest { @Spy @InjectMocks private PostfachService service; - @Mock - private PostfachRemoteService postfachRemoteService; - @Mock - private PersistPostfachNachrichtService persistingService; + + private final PostfachRemoteService postfachRemoteService = mock(PostfachRemoteService.class); @Spy - private PostfachNachrichtMapper mapper = Mappers.getMapper(PostfachNachrichtMapper.class); + private Optional<Collection<PostfachRemoteService>> postfachRemoteServices = Optional.of(List.of(postfachRemoteService)); @Mock - private ClientAttributeService clientAttributeService; + private PersistPostfachNachrichtService persistingService; @Mock - private MessageAttachmentService messageAttachmentService; + private PostfachNachrichtMapper mapper; @Mock private ApplicationEventPublisher publisher; @Mock @@ -100,7 +96,7 @@ class PostfachServiceTest { @BeforeEach void init() { - doNothing().when(service).persistMail(any(), any()); + doNothing().when(service).persistPostfachNachricht(any(), any()); when(userService.getUser()).thenReturn(CallContextUserTestFactory.create()); } @@ -109,7 +105,7 @@ class PostfachServiceTest { void shouldUseUserIdFromContext() { service.saveDraft(MessageTestFactory.VORGANG_ID, PostfachNachrichtTestFactory.create()); - verify(service).persistMail(userIdCaptor.capture(), any()); + verify(service).persistPostfachNachricht(userIdCaptor.capture(), any()); assertThat(userIdCaptor.getValue()).contains(CallContextUserTestFactory.ID); } @@ -117,228 +113,306 @@ class PostfachServiceTest { void shouldPersistNachricht() { service.saveDraft(MessageTestFactory.VORGANG_ID, PostfachNachrichtTestFactory.create()); - verify(service).persistMail(any(), any()); + verify(service).persistPostfachNachricht(any(), any()); } @Test void shouldAddVorgangId() { service.saveDraft(MessageTestFactory.VORGANG_ID, PostfachNachrichtTestFactory.createBuilder().vorgangId(null).build()); - verify(service).persistMail(any(), nachrichtCaptor.capture()); + verify(service).persistPostfachNachricht(any(), nachrichtCaptor.capture()); assertThat(nachrichtCaptor.getValue().getVorgangId()).isEqualTo(MessageTestFactory.VORGANG_ID); } } @Nested - class TestSendMail { + class TestSendPostfachNachricht { + + @Mock + private PostfachNachricht postfachNachricht; + @Mock + private PostfachNachricht postfachNachrichtWithSentInformation; + @Mock + private SendPostfachNachrichtResponse sendResponse; + + @BeforeEach + void init() { + doNothing().when(service).persistSentPostfachNachricht(any(), any()); + doReturn(postfachNachrichtWithSentInformation).when(service).addPostfachNachrichtSentInformation(any(), any()); + doReturn(sendResponse).when(service).handleSendPostfachNachricht(any(), any()); + } @Test - void shouldCallDoSendMail() { - when(postfachRemoteService.getPostfachType()).thenReturn(PostfachTestFactory.POSTFACH_TYPE); - var mail = PostfachNachrichtTestFactory.create(); + void shouldCallHandleSendPostfachNachricht() { + service.sendMail(COMMAND_ID, USER_ID, postfachNachricht); + + verify(service).handleSendPostfachNachricht(COMMAND_ID, postfachNachricht); + } - service.sendMail(COMMAND_ID, USER_ID, mail); + @Test + void shouldCallAddPostfachNachrichtSentInformation() { + service.sendMail(COMMAND_ID, USER_ID, postfachNachricht); - verify(service).doSendMail(mail); + verify(service).addPostfachNachrichtSentInformation(postfachNachricht, sendResponse); } @Test - void shouldCallPersistSentMail() { - when(postfachRemoteService.getPostfachType()).thenReturn(PostfachTestFactory.POSTFACH_TYPE); - var mail = PostfachNachrichtTestFactory.create(); - doReturn(mail).when(service).addMailSentInformation(any(PostfachNachricht.class), any(SendPostfachNachrichtResponse.class)); + void shouldCallPersistSentPostfachNachricht() { + service.sendMail(COMMAND_ID, USER_ID, postfachNachricht); + + verify(service).persistSentPostfachNachricht(USER_ID, postfachNachrichtWithSentInformation); + } + } + + @Nested + class TestAddPostfachNachrichtSentInformation { + private static final PostfachNachricht POSTFACH_NACHRICHT = PostfachNachrichtTestFactory.createBuilder().sentAt(null).sentSuccessful(null) + .messageCode(null).build(); + private static final SendPostfachNachrichtResponse SEND_POSTFACH_NACHRICHT_RESPONSE = SendPostfachNachrichtResponseTestFactory.create(); - service.sendMail(COMMAND_ID, USER_ID, PostfachNachrichtTestFactory.create()); + @Test + void shouldSetSentAt() { + var result = addPostfachNachrichtSentInformation(); - verify(service).persistSentMail(USER_ID, mail); + assertThat(result.getSentAt()).isCloseTo(ZonedDateTime.now(), within(2, ChronoUnit.SECONDS)); } @Test - void shouldAddMailSentInformation() { - var postfachMail = service.addMailSentInformation(PostfachNachrichtTestFactory.createBuilder().sentAt(null).sentSuccessful(null).build(), - SendPostfachNachrichtResponseTestFactory.create()); + void shouldSetSentSuccessful() { + var result = addPostfachNachrichtSentInformation(); - assertThat(postfachMail.getSentAt()).isNotNull(); - assertThat(postfachMail.getSentSuccessful()).isEqualTo(SendPostfachNachrichtResponseTestFactory.SENT_SUCCESSFUL); - assertThat(postfachMail.getMessageCode()).isEqualTo(SendPostfachNachrichtResponseTestFactory.MESSAGE_CODE.getMessageCode()); + assertThat(result.getSentSuccessful()).isTrue(); } @Test - void shouldHandleSendMail() { - when(postfachRemoteService.getPostfachType()).thenReturn(PostfachTestFactory.POSTFACH_TYPE); - var mail = PostfachNachrichtTestFactory.create(); - service.sendMail(COMMAND_ID, USER_ID, mail); + void shouldSetMessageCode() { + var result = addPostfachNachrichtSentInformation(); - verify(service).handleSendMail(COMMAND_ID, mail); + assertThat(result.getMessageCode()).isEqualTo(SendPostfachNachrichtResponseTestFactory.MESSAGE_CODE.getMessageCode()); } - @Nested - class TestPersistSentMail { + private PostfachNachricht addPostfachNachrichtSentInformation() { + return service.addPostfachNachrichtSentInformation(POSTFACH_NACHRICHT, SEND_POSTFACH_NACHRICHT_RESPONSE); + } + } - @Captor - private ArgumentCaptor<PostfachNachricht> mailCaptor; + @Nested + class TestPersistSentPostfachNachricht { - @Test - void shouldCallPersistingService() { - var mail = PostfachNachrichtTestFactory.create(); + private final PostfachNachricht outgoingPostfachNachricht = PostfachNachrichtTestFactory.create(); - service.persistSentMail(USER_ID, mail); + @BeforeEach + void init() { + doReturn(outgoingPostfachNachricht).when(service).buildOutgoingPostfachNachricht(any(), any()); + } - verify(persistingService).persistNachricht(any(), notNull()); - } + @Test + void shouldCallBuildOutgoingPostfachNachricht() { + var postfachNachricht = PostfachNachrichtTestFactory.create(); - @Test - void shouldSetDirectionToIN() { - service.persistSentMail(USER_ID, PostfachNachrichtTestFactory.createBuilder().direction(null).build()); + service.buildOutgoingPostfachNachricht(PostfachNachrichtTestFactory.CREATED_BY, postfachNachricht); - verify(persistingService).persistNachricht(any(), mailCaptor.capture()); - assertThat(mailCaptor.getValue().getDirection()).isEqualTo(Direction.OUT); - } + verify(service).buildOutgoingPostfachNachricht(PostfachNachrichtTestFactory.CREATED_BY, postfachNachricht); + } - @Test - void shouldSetCreatedAt() { - service.persistSentMail(USER_ID, PostfachNachrichtTestFactory.createBuilder().createdAt(null).build()); + @Test + void shouldCallPersistPostfachNachricht() { + service.persistSentPostfachNachricht(PostfachNachrichtTestFactory.CREATED_BY, PostfachNachrichtTestFactory.create()); - verify(persistingService).persistNachricht(any(), mailCaptor.capture()); - assertThat(mailCaptor.getValue().getCreatedAt()).isCloseTo(ZonedDateTime.now(), within(2, ChronoUnit.SECONDS)); - } + verify(service).persistPostfachNachricht(Optional.of(PostfachNachrichtTestFactory.CREATED_BY), outgoingPostfachNachricht); + } + } - @Test - void shouldSetCreatedBy() { - service.persistSentMail(USER_ID, PostfachNachrichtTestFactory.createBuilder().createdBy(null).build()); + @Nested + class TestBuildOutgoingPostfachNachricht { - verify(persistingService).persistNachricht(any(), mailCaptor.capture()); - assertThat(mailCaptor.getValue().getCreatedBy()).isEqualTo(USER_ID); - } + private final PostfachNachricht postfachNachricht = PostfachNachrichtTestFactory.createBuilder().direction(null).createdAt(null) + .createdBy(null).build(); - @Test - void shouldSetUserId() { - var mail = PostfachNachrichtTestFactory.create(); + @Test + void shouldSetDirectionToOut() { + var result = buildOutgoingPostfachNachricht(); - service.persistSentMail(USER_ID, mail); + assertThat(result.getDirection()).isEqualTo(PostfachNachricht.Direction.OUT); + } - verify(persistingService).persistNachricht(eq(Optional.of(USER_ID)), any()); - } + @Test + void shouldSetCreatedAt() { + var result = buildOutgoingPostfachNachricht(); - @Test - void shouldSetClientAttribute() { - service.persistSentMail(USER_ID, PostfachNachrichtTestFactory.create()); + assertThat(result.getCreatedAt()).isCloseTo(ZonedDateTime.now(), within(2, ChronoUnit.SECONDS)); + } - verify(clientAttributeService).setHasPostfachNachricht(MessageTestFactory.VORGANG_ID); - } + @Test + void shouldSetCreatedBy() { + var result = buildOutgoingPostfachNachricht(); - @Test - void shouldNOTSetNewNachricht() { - service.persistSentMail(USER_ID, PostfachNachrichtTestFactory.create()); + assertThat(result.getCreatedBy()).isEqualTo(PostfachNachrichtTestFactory.CREATED_BY); + } - verify(clientAttributeService, never()).setHasNewPostfachNachricht(any()); - } + private PostfachNachricht buildOutgoingPostfachNachricht() { + return service.buildOutgoingPostfachNachricht(PostfachNachrichtTestFactory.CREATED_BY, postfachNachricht); } + } + + @Nested + class TestProcessForAntragsraum { @Nested - class TestProcessForAntragsraum { + class TestWithNotification { - @Mock - private PostfachNachricht modifiedMail; + @BeforeEach + void init() { + doReturn(false).when(service).shouldNotifyAntragsraum(any()); + } @Test void shouldReturnEmpty() { - doReturn(false).when(service).isPostfachWithAntragsraum(any()); - var result = service.processForAntragsraum(PostfachNachrichtTestFactory.create()); assertThat(result).isEmpty(); } + } + + @Nested + class TestWithoutNotification { + + @BeforeEach + void init() { + doReturn(true).when(service).shouldNotifyAntragsraum(any()); + } @Test void shouldCallNotifyAntragsraum() { - doReturn(true).when(service).isPostfachWithAntragsraum(any()); - var mail = PostfachNachrichtTestFactory.create(); + var postfachNachricht = PostfachNachrichtTestFactory.create(); + + service.processForAntragsraum(postfachNachricht); + + verify(infomanagerService.get()).notifyInfoManager(postfachNachricht); + } + + @Test + void shouldCallAdjustPostfachNachricht() { + var postfachNachricht = PostfachNachrichtTestFactory.create(); - service.processForAntragsraum(mail); + service.processForAntragsraum(postfachNachricht); - verify(infomanagerService.get()).notifyInfoManager(mail); + verify(service).adjustPostfachNachricht(postfachNachricht); } @Test - void shouldReturnAdjustedMail() { - doReturn(true).when(service).isPostfachWithAntragsraum(any()); - doReturn(Optional.of(modifiedMail)).when(service).adjustMail(any()); + void shouldReturnAdjustedPostfachNachricht() { + var modifiedPostfachNachricht = PostfachNachrichtTestFactory.create(); + doReturn(Optional.of(modifiedPostfachNachricht)).when(service).adjustPostfachNachricht(any()); var result = service.processForAntragsraum(PostfachNachrichtTestFactory.create()); - assertThat(result).contains(modifiedMail); + assertThat(result).contains(modifiedPostfachNachricht); } + } - @DisplayName("Is notify antragsraum") - @Nested - class TestIsNotifyAntragsraum { + } - @DisplayName("should return true if") - @ParameterizedTest(name = "replyOption is {0}") - @EnumSource(value = ReplyOption.class, names = { "FORBIDDEN" }, mode = EnumSource.Mode.EXCLUDE) - void shouldConfirm(ReplyOption replyOption) { - doReturn(true).when(service).isPostfachWithAntragsraum(any()); + @DisplayName("Is notify antragsraum") + @Nested + class TestIsNotifyAntragsraum { - var result = service.isNotifyAntragsraum(replyOption); + @Test + void shouldCallFindPostfachRemoteService() { + service.shouldNotifyAntragsraum(PostfachNachrichtTestFactory.create()); - assertThat(result).isTrue(); - } + verify(service).findPostfachRemoteService(PostfachTestFactory.POSTFACH_TYPE); + } - @DisplayName("should return false if reply is forbidden") - @Test - void shouldDeclineIfForbidden() { - var result = service.isNotifyAntragsraum(ReplyOption.FORBIDDEN); + @Test + void shouldCallIsPostfachWithAntragsraum() { + doReturn(true).when(service).isPostfachWithAntragsraum(any()); + doReturn(Optional.of(postfachRemoteService)).when(service).findPostfachRemoteService(any()); - assertThat(result).isFalse(); - } + service.shouldNotifyAntragsraum(PostfachNachrichtTestFactory.create()); - @DisplayName("should return false if antragsraum is not configured") - @Test - void shouldDeclineIfNotConfigured() { - doReturn(false).when(service).isPostfachWithAntragsraum(any()); + verify(service).isPostfachWithAntragsraum(postfachRemoteService); + } - var result = service.isNotifyAntragsraum(ReplyOption.POSSIBLE); + @DisplayName("should return true if postfach with antragraum configured and") + @ParameterizedTest(name = "replyOption is {0}") + @EnumSource(value = PostfachNachricht.ReplyOption.class, names = { "FORBIDDEN" }, mode = EnumSource.Mode.EXCLUDE) + void shouldConfirm(PostfachNachricht.ReplyOption replyOption) { + doReturn(Optional.of(postfachRemoteService)).when(service).findPostfachRemoteService(any()); + doReturn(true).when(service).isPostfachWithAntragsraum(any()); + var postfachNachricht = PostfachNachrichtTestFactory.createBuilder().replyOption(replyOption).build(); - assertThat(result).isFalse(); - } - } + var result = service.shouldNotifyAntragsraum(postfachNachricht); - @Nested - class TestAdjustMail { + assertThat(result).isTrue(); + } - @Mock - private PostfachNachricht mail; + @DisplayName("should return false if postfach with antragraum configured and reply is forbidden") + @Test + void shouldDeclineIfForbidden() { + var postfachNachricht = PostfachNachrichtTestFactory.createBuilder().replyOption(PostfachNachricht.ReplyOption.FORBIDDEN).build(); - private Optional<PostfachNachricht> adjustMail() { - return service.adjustMail(mail); - } + var result = service.shouldNotifyAntragsraum(postfachNachricht); - @Nested - class TestReplaceBody { + assertThat(result).isFalse(); + } - @Test - void shouldReturnEmptyIfNoAntragsraum() { - ReflectionTestUtils.setField(service, "antragraumService", Optional.empty()); + @DisplayName("should return false if no postfach is configured") + @Test + void shouldDeclineIfNoPostfachRemoteService() { + doReturn(Optional.empty()).when(service).findPostfachRemoteService(any()); + var postfachNachricht = PostfachNachrichtTestFactory.createBuilder().replyOption(PostfachNachricht.ReplyOption.POSSIBLE).build(); - var result = adjustMail(); + var result = service.shouldNotifyAntragsraum(postfachNachricht); - assertThat(result).isEmpty(); - } + assertThat(result).isFalse(); + } - @Test - void shouldAdjustMailBody() { - when(mail.toBuilder()).thenReturn(PostfachNachrichtTestFactory.createBuilder()); - var expectedMailBody = "Antragsraum Text"; - when(antragsraumService.get().getUserNotificationText()).thenReturn(expectedMailBody); + @DisplayName("should return false if configured postfach without antragsraum") + @Test + void shouldDeclineIfNotConfigured() { + doReturn(Optional.of(postfachRemoteService)).when(service).findPostfachRemoteService(any()); + doReturn(false).when(service).isPostfachWithAntragsraum(any()); + var postfachNachricht = PostfachNachrichtTestFactory.createBuilder().replyOption(PostfachNachricht.ReplyOption.POSSIBLE).build(); - var result = adjustMail(); + var result = service.shouldNotifyAntragsraum(postfachNachricht); - assertThat(result).isPresent().get().extracting(PostfachNachricht::getMailBody).isEqualTo(expectedMailBody); - } - } + assertThat(result).isFalse(); + } + } + + @Nested + class TestAdjustPostfachNachricht { + + @Mock + private PostfachNachricht postfachNachricht; + + @Nested + class TestReplaceBody { + + @Test + void shouldReturnEmptyIfNoAntragsraum() { + ReflectionTestUtils.setField(service, "antragraumService", Optional.empty()); + + var result = adjustPostfachNachricht(); + + assertThat(result).isEmpty(); } + @Test + void shouldAdjustMailBody() { + when(postfachNachricht.toBuilder()).thenReturn(PostfachNachrichtTestFactory.createBuilder()); + var expectedMailBody = "Antragsraum Text"; + when(antragsraumService.get().getUserNotificationText()).thenReturn(expectedMailBody); + + var result = adjustPostfachNachricht(); + + assertThat(result).isPresent().get().extracting(PostfachNachricht::getMailBody).isEqualTo(expectedMailBody); + } + + private Optional<PostfachNachricht> adjustPostfachNachricht() { + return service.adjustPostfachNachricht(postfachNachricht); + } } } @@ -366,87 +440,117 @@ class PostfachServiceTest { } @Nested - class TestFetchAndPersistReplies { - - private static final PostfachNachricht nachricht = PostfachNachrichtTestFactory.create(); + class TestFetchAndPersistRepliesAllPostfachs { @BeforeEach void initTest() { - lenient().when(postfachRemoteService.getAllMessages()).thenReturn(Stream.of(nachricht)); + doReturn(Stream.of(postfachRemoteService)).when(service).getPostfachRemoteServices(); } @Test - void shouldCallGetAllMessages() { + void shouldCallGetPostfachRemoteServices() { service.fetchAndPersistReplies(); - verify(postfachRemoteService).getAllMessages(); + verify(service).getPostfachRemoteServices(); } @Test - void shouldCallPersistingService() { + void shouldCallFetchAndPersistReplies() { service.fetchAndPersistReplies(); - verify(persistingService).persistNachricht(eq(Optional.empty()), any()); + verify(service).fetchAndPersistReplies(postfachRemoteService); } + } - @Test - void shouldCallDelete() { - service.fetchAndPersistReplies(); + @Nested + class TestFetchAndPersistReplies { - verify(postfachRemoteService).deleteMessage(MessageTestFactory.MESSAGE_ID); - } + @Nested + class TestHappyPath { - @Test - void shouldThrowNotConfiguredException() { - doReturn(false).when(service).isPostfachConfigured(); + private final PostfachNachricht mappedPostfachNachricht = PostfachNachrichtTestFactory.create(); - assertThatExceptionOfType(NotConfiguredException.class).isThrownBy(() -> service.fetchAndPersistReplies()); - verify(postfachRemoteService, never()).getAllMessages(); - } - } + @Mock + private PostfachNachricht postfachNachricht; - @Nested - class TestPeristMail { - @Test - void shouldPersistMail() { - PostfachNachricht mail = PostfachNachrichtTestFactory.create(); + @BeforeEach + void init() { + when(postfachRemoteService.getAllMessages()).thenReturn(Stream.of(postfachNachricht)); + doReturn(mappedPostfachNachricht).when(service).persistReceivedPostfachNachricht(any()); + } + + @Test + void shouldCallGetAllMessages() { + service.fetchAndPersistReplies(postfachRemoteService); + + verify(postfachRemoteService).getAllMessages(); + } + + @Test + void shouldCallPersistReceivedPostfachNachricht() { + service.fetchAndPersistReplies(postfachRemoteService); + + verify(service).persistReceivedPostfachNachricht(postfachNachricht); + } - service.persistMail(Optional.of(USER_ID), mail); + @Test + void shouldCallDeleteMessage() { + service.fetchAndPersistReplies(postfachRemoteService); - verify(persistingService).persistNachricht(eq(Optional.of(USER_ID)), same(mail)); + verify(postfachRemoteService).deleteMessage(MessageTestFactory.MESSAGE_ID); + } } - @Test - void shouldSetHasPostfachNachricht() { - var mail = PostfachNachrichtTestFactory.create(); + @Nested + class TestWithUnsupportedOperationException { + + @Test + void shouldHandleWhenCallGetAllMessage() { + doThrow(new UnsupportedOperationException()).when(postfachRemoteService).getAllMessages(); + + assertDoesNotThrow(() -> service.fetchAndPersistReplies(postfachRemoteService)); + } - service.persistMail(Optional.of(USER_ID), mail); + @Test + void shouldHandleWhenCallDeleteMessage() { + doThrow(new UnsupportedOperationException()).when(postfachRemoteService).deleteMessage(anyString()); - verify(clientAttributeService).setHasPostfachNachricht(MessageTestFactory.VORGANG_ID); + assertDoesNotThrow(() -> service.fetchAndPersistReplies(postfachRemoteService)); + } } + } + + @Nested + class TestPeristPostfachNachricht { @Test - @DisplayName("should NOT set hasPostfachNachricht if Nachricht is sent from system") - void shouldNOTSetHasNachrichtForSystemNachricht() { - var mail = PostfachNachrichtTestFactory.createBuilder().createdBy("system-123").build(); + void shouldPersistPostfachNachricht() { + var PostfachNachricht = PostfachNachrichtTestFactory.create(); - service.persistMail(Optional.of(USER_ID), mail); + service.persistPostfachNachricht(Optional.of(USER_ID), PostfachNachricht); - verify(clientAttributeService, never()).setHasPostfachNachricht(MessageTestFactory.VORGANG_ID); + verify(persistingService).persistNachricht(eq(Optional.of(USER_ID)), same(PostfachNachricht)); } + } @Nested class TestResendPostfachMail { - static final String COMMAND_ID = UUID.randomUUID().toString(); - static final PostfachNachricht mail = PostfachNachrichtTestFactory.create(); + private static final String COMMAND_ID = UUID.randomUUID().toString(); + private static final PostfachNachricht POSTFACH_NACHRICHT = PostfachNachrichtTestFactory.create(); + private static final Map<String, Object> RESEND_PATCH_MAP = Map.of("key", "value"); + + @Mock + private SendPostfachNachrichtResponse sendResponse; @BeforeEach void mockService() { when(persistingService.getById(anyString())).thenReturn(PostfachNachrichtTestFactory.asMap()); - doReturn(mail).when(mapper).fromMapToPostfachMail(anyMap()); - when(postfachRemoteService.getPostfachType()).thenReturn(PostfachTestFactory.POSTFACH_TYPE); + doReturn(POSTFACH_NACHRICHT).when(mapper).fromMapToPostfachMail(anyMap()); + doReturn(sendResponse).when(service).handleSendPostfachNachricht(any(), any()); + doReturn(RESEND_PATCH_MAP).when(service).createResendPatchMap(any()); + doNothing().when(service).patchPostfachNachricht(any(), any()); } @Test @@ -464,25 +568,26 @@ class PostfachServiceTest { } @Test - void shouldSendMail() { + void shouldCallHandleSentPostfachNachricht() { service.resendMail(COMMAND_ID, PostfachNachrichtTestFactory.ID); - verify(postfachRemoteService).sendMessage(any(PostfachNachricht.class)); + verify(service).handleSendPostfachNachricht(COMMAND_ID, POSTFACH_NACHRICHT); } @Test - void shouldPatchMail() { + void shouldCallCreateResendPatchMap() { service.resendMail(COMMAND_ID, PostfachNachrichtTestFactory.ID); - verify(persistingService).patch(anyString(), anyMap()); + verify(service).createResendPatchMap(sendResponse); } @Test - void shouldHandleSentMail() { + void shouldCallPatchMail() { service.resendMail(COMMAND_ID, PostfachNachrichtTestFactory.ID); - verify(service).handleSendMail(COMMAND_ID, mail); + verify(service).patchPostfachNachricht(PostfachNachrichtTestFactory.ID, RESEND_PATCH_MAP); } + } @Nested @@ -513,20 +618,20 @@ class PostfachServiceTest { } } - @DisplayName("Handle send mail") + @DisplayName("Handle send PostfachNachricht") @Nested - class TestHandleSendMail { + class TestHandleSendPostfachNachricht { final String COMMAND_ID = UUID.randomUUID().toString(); - final PostfachNachricht mail = PostfachNachrichtTestFactory.create(); + final PostfachNachricht postfachNachricht = PostfachNachrichtTestFactory.create(); @Test - void shouldDoSendMail() { + void shouldDoSendPostfachNachricht() { when(postfachRemoteService.getPostfachType()).thenReturn(PostfachTestFactory.POSTFACH_TYPE); - service.handleSendMail(COMMAND_ID, mail); + service.handleSendPostfachNachricht(COMMAND_ID, postfachNachricht); - verify(service).doSendMail(mail); + verify(service).doSendPostfachNachricht(postfachNachricht); } @Nested @@ -536,22 +641,27 @@ class PostfachServiceTest { private ArgumentCaptor<PostfachMailSentEvent> eventCaptor; @Mock - private PostfachNachricht modifiedMail; + private PostfachNachricht modifiedPostfachNachricht; + + @BeforeEach + void init() { + doNothing().when(service).doSendPostfachNachricht(any()); + } @Test - void shouldDoSendMail() { + void shouldDoSendPostfachNachricht() { when(postfachRemoteService.getPostfachType()).thenReturn(PostfachTestFactory.POSTFACH_TYPE); - service.handleSendMail(COMMAND_ID, mail); + service.handleSendPostfachNachricht(COMMAND_ID, postfachNachricht); - verify(service).doSendMail(mail); + verify(service).doSendPostfachNachricht(postfachNachricht); } @Test void shouldPublishEvent() { when(postfachRemoteService.getPostfachType()).thenReturn(PostfachTestFactory.POSTFACH_TYPE); - service.handleSendMail(COMMAND_ID, mail); + service.handleSendPostfachNachricht(COMMAND_ID, postfachNachricht); verify(publisher).publishEvent(eventCaptor.capture()); assertThat(eventCaptor.getValue().getSource()).isEqualTo(COMMAND_ID); @@ -561,7 +671,7 @@ class PostfachServiceTest { void shouldReturnResponse() { when(postfachRemoteService.getPostfachType()).thenReturn(PostfachTestFactory.POSTFACH_TYPE); - var response = service.handleSendMail(COMMAND_ID, mail); + var response = service.handleSendPostfachNachricht(COMMAND_ID, postfachNachricht); assertThat(response.isSentSuccessful()).isTrue(); assertThat(response.getMessageCode()).isEqualTo(PostfachMessageCode.SEND_SUCCESSFUL_MESSAGE_CODE); @@ -569,11 +679,11 @@ class PostfachServiceTest { @Test void shouldSendProcessedPostfachNachricht() { - doReturn(Optional.of(modifiedMail)).when(service).processForAntragsraum(any()); + doReturn(Optional.of(modifiedPostfachNachricht)).when(service).processForAntragsraum(any()); - service.handleSendMail(COMMAND_ID, mail); + service.handleSendPostfachNachricht(COMMAND_ID, postfachNachricht); - verify(service).doSendMail(modifiedMail); + verify(service).doSendPostfachNachricht(modifiedPostfachNachricht); } } @@ -601,7 +711,7 @@ class PostfachServiceTest { @Test void shouldPublishEvent() { - service.handleSendMail(COMMAND_ID, mail); + service.handleSendPostfachNachricht(COMMAND_ID, postfachNachricht); verify(publisher).publishEvent(eventFailedCaptor.capture()); assertThat(eventFailedCaptor.getValue().getSource()).isEqualTo(COMMAND_ID); @@ -609,15 +719,15 @@ class PostfachServiceTest { } @Test - void shouldCallLogSendMailFailed() { - service.handleSendMail(COMMAND_ID, mail); + void shouldCallLogSendPostfachNachrichtFailed() { + service.handleSendPostfachNachricht(COMMAND_ID, postfachNachricht); - verify(service).logSendMailFailed(COMMAND_ID, POSTFACH_EXCEPTION); + verify(service).logSendPostfachNachrichtFailed(COMMAND_ID, POSTFACH_EXCEPTION); } @Test void shouldReturnResponse() { - var response = service.handleSendMail(COMMAND_ID, mail); + var response = service.handleSendPostfachNachricht(COMMAND_ID, postfachNachricht); assertThat(response.isSentSuccessful()).isFalse(); assertThat(response.getMessageCode()).isEqualTo(PostfachMessageCode.PROCESS_FAILED_MESSAGE_CODE); @@ -638,7 +748,7 @@ class PostfachServiceTest { @Test void shouldPublishEvent() { - service.handleSendMail(COMMAND_ID, mail); + service.handleSendPostfachNachricht(COMMAND_ID, postfachNachricht); verify(publisher).publishEvent(eventFailedCaptor.capture()); assertThat(eventFailedCaptor.getValue().getSource()).isEqualTo(COMMAND_ID); @@ -647,7 +757,7 @@ class PostfachServiceTest { @Test void shouldReturnResponse() { - var response = service.handleSendMail(COMMAND_ID, mail); + var response = service.handleSendPostfachNachricht(COMMAND_ID, postfachNachricht); assertThat(response.isSentSuccessful()).isFalse(); assertThat(response.getMessageCode()).isEqualTo(PostfachMessageCode.PROCESS_FAILED_MESSAGE_CODE); @@ -657,7 +767,7 @@ class PostfachServiceTest { } @Nested - class TestLogSendMailFailed { + class TestLogSendPostfachNachrichtFailed { @DisplayName("should log warning on OsiPostfachServerProcessException") @Test @@ -665,7 +775,7 @@ class PostfachServiceTest { try (var logCaptor = LogCaptor.forClass(PostfachService.class)) { var exception = OsiPostfachServerProcessExceptionTestFactory.create(); - service.logSendMailFailed(COMMAND_ID, exception); + service.logSendPostfachNachrichtFailed(COMMAND_ID, exception); assertThat(logCaptor.getLogEvents()).hasSize(1).first().extracting(LogEvent::getLevel).isEqualTo(LogLevel.WARN.name()); } @@ -677,7 +787,7 @@ class PostfachServiceTest { try (var logCaptor = LogCaptor.forClass(PostfachService.class)) { var exception = new PostfachException("Test", PostfachMessageCode.SEND_FAILED_UNKNOWN_POSTFACH_ID_MESSAGE_CODE); - service.logSendMailFailed(COMMAND_ID, exception); + service.logSendPostfachNachrichtFailed(COMMAND_ID, exception); assertThat(logCaptor.getLogEvents()).hasSize(1).first().extracting(LogEvent::getLevel).isEqualTo(LogLevel.WARN.name()); } @@ -690,7 +800,7 @@ class PostfachServiceTest { try (var logCaptor = LogCaptor.forClass(PostfachService.class)) { var exception = new PostfachException("Test", messageCode); - service.logSendMailFailed(COMMAND_ID, exception); + service.logSendPostfachNachrichtFailed(COMMAND_ID, exception); assertThat(logCaptor.getLogEvents().get(0).getLevel()).isEqualTo(LogLevel.ERROR.name()); } @@ -699,27 +809,36 @@ class PostfachServiceTest { } @Nested - class TestDoSendMail { + class TestDoSendPostfachNachricht { + + @Mock + private PostfachRemoteService postfachRemoteService; + @Test - void shouldCallPostfachRemoteService() { - service.doSendMail(PostfachNachrichtTestFactory.create()); + void shouldCallFindPostfachRemoteService() { + doReturn(Optional.of(postfachRemoteService)).when(service).findPostfachRemoteService(any()); - verify(postfachRemoteService).sendMessage(any()); + service.doSendPostfachNachricht(PostfachNachrichtTestFactory.create()); + + verify(service).findPostfachRemoteService(PostfachTestFactory.POSTFACH_TYPE); } - @Nested - class WithoutRemoteService { - @InjectMocks - private PostfachService service; + @Test + void shouldCallSendMessage() { + doReturn(Optional.of(postfachRemoteService)).when(service).findPostfachRemoteService(any()); + var postfachNachricht = PostfachNachrichtTestFactory.create(); - @Test - void shouldThrowNotConfiguredException() { - var nachricht = PostfachNachrichtTestFactory.create(); + service.doSendPostfachNachricht(postfachNachricht); - assertThatExceptionOfType(NotConfiguredException.class).isThrownBy(() -> service.doSendMail(nachricht)); + verify(postfachRemoteService).sendMessage(postfachNachricht); + } - verify(postfachRemoteService, never()).sendMessage(any()); - } + @Test + void shouldThrowNotConfiguredException() { + doReturn(Optional.empty()).when(service).findPostfachRemoteService(any()); + var postfachNachricht = PostfachNachrichtTestFactory.create(); + + assertThrows(NotConfiguredException.class, () -> service.doSendPostfachNachricht(postfachNachricht)); } } @@ -728,8 +847,10 @@ class PostfachServiceTest { class TestIsPostfachConfigured { @Test - void shouldReturnTrue() { - assertThat(service.isPostfachConfigured()).isTrue(); + void shouldCallIsPresent() { + service.isPostfachConfigured(); + + verify(postfachRemoteServices).isPresent(); } } @@ -743,7 +864,7 @@ class PostfachServiceTest { void shouldCallBuildPostfach() { when(postfachRemoteService.getPostfachType()).thenReturn(PostfachTestFactory.POSTFACH_TYPE); - service.getPostfachs(); + getPostfachs(); verify(service).buildPostfach(postfachRemoteService); } @@ -758,14 +879,10 @@ class PostfachServiceTest { assertThat(result).containsOnly(postfach); } - @Test - void shouldReturnEmptyStream() { - doReturn(false).when(service).isPostfachConfigured(); - - var result = service.getPostfachs(); - - assertThat(result).isEmpty(); + private List<Postfach> getPostfachs() { + return service.getPostfachs().toList(); } + } @DisplayName("on bayern id postfach") @Nested @@ -776,7 +893,6 @@ class PostfachServiceTest { @BeforeEach void mock() { - doReturn(true).when(service).isPostfachConfigured(); when(postfachRemoteService.getPostfachType()).thenReturn(BAYERN_ID_POSTFACH); } @@ -809,99 +925,192 @@ class PostfachServiceTest { @Nested class TestBuildPostfach { + @Test + void shouldSetPostfachType() { + when(postfachRemoteService.getPostfachType()).thenReturn(PostfachTestFactory.POSTFACH_TYPE); + + var result = service.buildPostfach(postfachRemoteService); + + assertThat(result.getType()).isEqualTo(PostfachTestFactory.POSTFACH_TYPE); + } + + @Test + void shouldSetIsReplyAllowed() { + doReturn(true).when(service).isReplyAllowed(any()); + + var result = service.buildPostfach(postfachRemoteService); + + assertThat(result.isReplyAllowed()).isTrue(); + } + } + + @Nested + class TestIsReplyAllowed { + + @Test + void shouldAllowWithAntragsraum() { + doReturn(true).when(service).isPostfachWithAntragsraum(postfachRemoteService); + + var result = service.isReplyAllowed(postfachRemoteService); + + assertThat(result).isTrue(); + } + + @Test + void shouldAllowWithPostfach() { + doReturn(false).when(service).isPostfachWithAntragsraum(postfachRemoteService); + when(postfachRemoteService.isReplyAllowed()).thenReturn(true); + + var result = service.isReplyAllowed(postfachRemoteService); + + assertThat(result).isTrue(); + } + + @Test + void shouldNotAllow() { + doReturn(false).when(service).isPostfachWithAntragsraum(postfachRemoteService); + + var result = service.isReplyAllowed(postfachRemoteService); + + assertThat(result).isFalse(); + } + } + + @Nested + class TestPersistReceivedPostfachNachricht { + + @Test + void shouldCallIsMukAnswer() { + var postfachNachricht = PostfachNachrichtTestFactory.create(); + + service.persistReceivedPostfachNachricht(postfachNachricht); + + verify(service).isMukAnswer(postfachNachricht); + } + + @Nested + class TestPostfachNachricht { + + @BeforeEach + void init() { + doReturn(false).when(service).isMukAnswer(any()); + } + @Test - void shouldSetPostfachType() { - when(postfachRemoteService.getPostfachType()).thenReturn(PostfachTestFactory.POSTFACH_TYPE); + void shouldCallPersistPostfachNachricht() { + var postfachNachricht = PostfachNachrichtTestFactory.create(); - var result = service.buildPostfach(postfachRemoteService); + service.persistReceivedPostfachNachricht(postfachNachricht); - assertThat(result.getType()).isEqualTo(PostfachTestFactory.POSTFACH_TYPE); + verify(service).persistPostfachNachricht(Optional.empty(), postfachNachricht); } @Test - void shouldSetIsReplyAllowed() { - doReturn(true).when(service).isReplyAllowed(any()); + void shouldReturnPostfachNachricht() { + var postfachNachricht = PostfachNachrichtTestFactory.create(); - var result = service.buildPostfach(postfachRemoteService); + var result = service.persistReceivedPostfachNachricht(postfachNachricht); - assertThat(result.isReplyAllowed()).isTrue(); + assertThat(result).isSameAs(postfachNachricht); } } @Nested - class TestIsReplyAllowed { - - @Test - void shouldAllowWithAntragsraum() { - doReturn(true).when(service).isPostfachWithAntragsraum(postfachRemoteService); + class TestMukAnswer { - var result = service.isReplyAllowed(postfachRemoteService); + private final PostfachNachricht postfachNachrichtWithVorgang = PostfachNachrichtTestFactory.create(); - assertThat(result).isTrue(); + @BeforeEach + void init() { + doReturn(true).when(service).isMukAnswer(any()); + doReturn(postfachNachrichtWithVorgang).when(service).linkToVorgang(any()); } @Test - void shouldAllowWithPostfach() { - doReturn(false).when(service).isPostfachWithAntragsraum(postfachRemoteService); - when(postfachRemoteService.isReplyAllowed()).thenReturn(true); + void shouldCallLinkToVorgang() { + var postfachNachricht = PostfachNachrichtTestFactory.create(); - var result = service.isReplyAllowed(postfachRemoteService); + service.persistReceivedPostfachNachricht(postfachNachricht); - assertThat(result).isTrue(); + verify(service).linkToVorgang(postfachNachricht); } @Test - void shouldNotAllow() { - doReturn(false).when(service).isPostfachWithAntragsraum(postfachRemoteService); + void shouldCallPersistPostfachNachricht() { + service.persistReceivedPostfachNachricht(PostfachNachrichtTestFactory.create()); + + verify(service).persistPostfachNachricht(Optional.empty(), postfachNachrichtWithVorgang); + } - var result = service.isReplyAllowed(postfachRemoteService); + @Test + void shouldReturnUpdatedPostfachNachricht() { + var result = service.persistReceivedPostfachNachricht(PostfachNachrichtTestFactory.create()); - assertThat(result).isFalse(); + assertThat(result).isSameAs(postfachNachrichtWithVorgang); } } + } @Nested class TestSavingMukAnswer { + private final String refId = ObjectId.get().toHexString(); private final PostfachNachricht answer = PostfachNachrichtTestFactory.createBuilder().vorgangId(null).referencedNachricht(refId).build(); @Test void shouldIdentifyAsAnswer() { - var isMukAnswer = PostfachService.IS_MUK_ANSWER.test(answer); + var isMukAnswer = service.isMukAnswer(answer); assertThat(isMukAnswer).isTrue(); } @Test void shouldNotIdentifyAsAnswer() { - var isMukAnswer = PostfachService.IS_MUK_ANSWER.test(PostfachNachrichtTestFactory.create()); + var isMukAnswer = service.isMukAnswer(PostfachNachrichtTestFactory.create()); assertThat(isMukAnswer).isFalse(); } - @Nested - class TestLinkingMukAnswerToVorgang { + } - @Test - void shouldLoadPersistedNachricht() { - when(service.isPostfachConfigured()).thenReturn(Boolean.TRUE); - when(postfachRemoteService.getAllMessages()).thenReturn(Stream.of(answer)); - when(persistingService.getById(anyString())).thenReturn(PostfachNachrichtTestFactory.asMap()); + @Nested + class TestLinkToVorgang { - service.fetchAndPersistReplies(); + private static final String REFERENCE_ID = "reference-id"; + private static final PostfachNachricht POSTFACH_NACHRICHT = PostfachNachrichtTestFactory.createBuilder().vorgangId(null) + .referencedNachricht(REFERENCE_ID).build(); + private static final PostfachNachricht PERSISTED_NACHRICHT = PostfachNachrichtTestFactory.create(); + private static final Map<String, Object> POSTFACH_NACHRICHT_MAP = PostfachNachrichtTestFactory.asMap(); - verify(persistingService).getById(any()); - } + @BeforeEach + void init() { + when(mapper.fromMapToPostfachMail(anyMap())).thenReturn(PERSISTED_NACHRICHT); + when(persistingService.getById(anyString())).thenReturn(POSTFACH_NACHRICHT_MAP); + } - @Test - void shouldSetVorgangId() { - when(persistingService.getById(anyString())).thenReturn(PostfachNachrichtTestFactory.asMap()); + @Test + void shouldLoadPersistedNachricht() { + service.linkToVorgang(POSTFACH_NACHRICHT); - var linkedNachricht = service.linkToVorgang(answer); + verify(persistingService).getById(REFERENCE_ID); + } - assertThat(linkedNachricht.getVorgangId()).isEqualTo(VORGANG_ID); - } + @Test + void shouldCallFromMapToPostfachMail() { + service.linkToVorgang(POSTFACH_NACHRICHT); + + verify(mapper).fromMapToPostfachMail(POSTFACH_NACHRICHT_MAP); + } + + @Test + void shouldSetVorgangId() { + when(persistingService.getById(anyString())).thenReturn(PostfachNachrichtTestFactory.asMap()); + + var result = service.linkToVorgang(POSTFACH_NACHRICHT); + + assertThat(result.getVorgangId()).isEqualTo(MessageTestFactory.VORGANG_ID); } } + } \ No newline at end of file diff --git a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/osi/MessageJsonReplyTestFactory.java b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/osi/MessageJsonReplyTestFactory.java index 43dd50e2c6728e4931e4a5a2c651f689ccad7742..ce467cfc04af79b62a1c95430efe16c819bd914b 100644 --- a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/osi/MessageJsonReplyTestFactory.java +++ b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/osi/MessageJsonReplyTestFactory.java @@ -1,37 +1,64 @@ package de.ozgcloud.nachrichten.postfach.osi; +import java.util.List; +import java.util.stream.Collectors; + +import de.ozgcloud.common.test.TestUtils; +import lombok.Builder; +import lombok.Singular; + public class MessageJsonReplyTestFactory { - private static final String REPLY_JSON_TMPL = """ - [{ - "messageId": "%s", - "nameIdentifier": "%s", - "body": "%s", - "isHtml": false, - "replyAction": %d, - "subject": "%s", - "sequenceNumber": "%s", - "eidasLevel": 1, - "isObligatory": true, - "attachments": [{ - "fileName": "%s", - "content": "%s" - }] - }]"""; - - public static String buildReplyJson() { - return buildReplyJson(MessageTestFactory.create(), MessageAttachmentTestFactory.create()); + public static final MessageAttachment MESSAGE_ATTACHMENT = MessageAttachmentTestFactory.create(); + + public static String create() { + return createBuilder().build(); } - public static String buildReplyJson(Message msg, MessageAttachment attachement) { - return String.format(REPLY_JSON_TMPL, - msg.getMessageId(), - msg.getPostfachId(), - msg.getMailBody(), - msg.getReplyOption().toValue(), - msg.getSubject(), - msg.getVorgangId(), - attachement.getFileName(), - attachement.getContent()); + public static MessageJsonReply.MessageJsonReplyBuilder createBuilder() { + return MessageJsonReply.builder() + .messageId(MessageTestFactory.MESSAGE_ID) + .postfachId(MessageTestFactory.POSTFACH_ID) + .mailBody(MessageTestFactory.MAIL_BODY) + .replyOption(MessageTestFactory.REPLY_OPTION) + .subject(MessageTestFactory.SUBJECT) + .vorgangId(MessageTestFactory.VORGANG_ID) + .attachment(MESSAGE_ATTACHMENT); + } + + @Builder + private static class MessageJsonReply { + + private static final String REPLY_MESSAGE_JSON_TMPL = "reply-message-json.tmpl"; + + private static final String ATTACHMENT_TMPL = """ + { + "fileName": "%s", + "content": "%s" + }"""; + + private String messageId; + private String postfachId; + private String mailBody; + private ReplyOption replyOption; + private String subject; + private String vorgangId; + @Singular + private List<MessageAttachment> attachments; + + public static class MessageJsonReplyBuilder { + + public String build() { + return TestUtils.loadTextFile(REPLY_MESSAGE_JSON_TMPL) + .formatted(messageId, postfachId, mailBody, replyOption.toValue(), subject, vorgangId, + buildAttachments()); + } + + public String buildAttachments() { + return attachments.stream().map(attachment -> ATTACHMENT_TMPL.formatted(attachment.getFileName(), attachment.getContent())) + .collect(Collectors.joining(",")); + } + } } + } diff --git a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/osi/OsiPostfachMessageMapperTest.java b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/osi/OsiPostfachMessageMapperTest.java index 03296b4b114b2e57e1b4e6158f9cd09ca50cbbd3..acbf9bdae93369065186749cd2c2361a1e828d96 100644 --- a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/osi/OsiPostfachMessageMapperTest.java +++ b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/osi/OsiPostfachMessageMapperTest.java @@ -172,10 +172,10 @@ class OsiPostfachMessageMapperTest { class TestWithoutPostfachAddress { @Test - void shouldMapPostfachAddressToPostfachId() { + void shouldMapPostfachAddressToEmptyString() { var message = toOsiMessage(PostfachNachrichtTestFactory.createBuilder().postfachAddress(null).build()); - assertThat(message.getPostfachId()).isEqualTo(MessageTestFactory.POSTFACH_ID); + assertThat(message.getPostfachId()).isEmpty(); } } } diff --git a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/osi/OsiPostfachRemoteServiceTest.java b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/osi/OsiPostfachRemoteServiceTest.java index 0681df0f45b9605bd5d025636913836084c8e21f..db3b32338093fcb94d5c639dfff0f44c1a125865 100644 --- a/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/osi/OsiPostfachRemoteServiceTest.java +++ b/nachrichten-manager-server/src/test/java/de/ozgcloud/nachrichten/postfach/osi/OsiPostfachRemoteServiceTest.java @@ -144,7 +144,7 @@ class OsiPostfachRemoteServiceTest { private void prepareMockServer() { mockServer.expect(ExpectedCount.once(), requestTo(TEST_URL)) .andExpect(method(HttpMethod.GET)) - .andRespond(withSuccess(MessageJsonReplyTestFactory.buildReplyJson(), MediaType.APPLICATION_JSON)); + .andRespond(withSuccess(MessageJsonReplyTestFactory.create(), MediaType.APPLICATION_JSON)); } } @@ -152,7 +152,7 @@ class OsiPostfachRemoteServiceTest { @Nested class TestSendMessage { - private PostfachNachricht message = PostfachNachrichtTestFactory.create(); + private final PostfachNachricht message = PostfachNachrichtTestFactory.create(); @Test void shouldCallRestTemplateExchange() { diff --git a/nachrichten-manager-server/src/test/resources/reply-message-json.tmpl b/nachrichten-manager-server/src/test/resources/reply-message-json.tmpl new file mode 100644 index 0000000000000000000000000000000000000000..d3aa6a59e2252adf43facbb29595146409e31aff --- /dev/null +++ b/nachrichten-manager-server/src/test/resources/reply-message-json.tmpl @@ -0,0 +1,12 @@ +[{ + "messageId": "%s", + "nameIdentifier": "%s", + "body": "%s", + "isHtml": false, + "replyAction": %d, + "subject": "%s", + "sequenceNumber": "%s", + "eidasLevel": 1, + "isObligatory": true, + "attachments": [%s] +}] \ No newline at end of file