From a8440561787b04276a05cdfa61ea393b84e4f38c Mon Sep 17 00:00:00 2001
From: OZGCloud <ozgcloud@mgm-tp.com>
Date: Mon, 16 Sep 2024 10:25:53 +0200
Subject: [PATCH] OZG-6640 log ungueltiger postkorb handle error as warning

---
 .../nachrichten/postfach/PostfachService.java    | 16 +++++++++++-----
 .../bayernid/BayernIdServerException.java        | 13 +++++++++++--
 .../postfach/PostfachServiceTest.java            |  6 +-----
 3 files changed, 23 insertions(+), 12 deletions(-)

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 6df32e7..6bd73d7 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
@@ -30,6 +30,9 @@ import java.util.Set;
 import java.util.function.Predicate;
 import java.util.stream.Stream;
 
+import jakarta.annotation.PostConstruct;
+import jakarta.validation.Valid;
+
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.ApplicationEventPublisher;
@@ -41,10 +44,9 @@ 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.bayernid.BayernIdServerException;
 import de.ozgcloud.nachrichten.postfach.osi.OsiPostfachServerProcessException;
 import de.ozgcloud.vorgang.callcontext.CurrentUserService;
-import jakarta.annotation.PostConstruct;
-import jakarta.validation.Valid;
 import lombok.NonNull;
 import lombok.extern.log4j.Log4j2;
 
@@ -189,9 +191,13 @@ class PostfachService {
 		return proceedWithException(commandId, e);
 	}
 
-	SendPostfachNachrichtResponse proceedWithErrorException(String commandId, PostfachException e) {
-		LOG.error(e.getMessage(), e);
-		return proceedWithException(commandId, e);
+	SendPostfachNachrichtResponse proceedWithErrorException(String commandId, PostfachException exception) {
+		if (exception instanceof BayernIdServerException exp && exp.isUngueltigerPostkorbHandleKey()) {
+			LOG.warn(exception.getMessage(), exception);
+		} else {
+			LOG.error(exception.getMessage(), exception);
+		}
+		return proceedWithException(commandId, exception);
 	}
 
 	private SendPostfachNachrichtResponse proceedWithException(String commandId, PostfachException e) {
diff --git a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/bayernid/BayernIdServerException.java b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/bayernid/BayernIdServerException.java
index 76b1862..0cbd6fe 100644
--- a/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/bayernid/BayernIdServerException.java
+++ b/nachrichten-manager-server/src/main/java/de/ozgcloud/nachrichten/postfach/bayernid/BayernIdServerException.java
@@ -1,12 +1,15 @@
 package de.ozgcloud.nachrichten.postfach.bayernid;
 
+import java.util.Optional;
+
 import de.ozgcloud.nachrichten.postfach.PostfachException;
 import de.ozgcloud.nachrichten.postfach.PostfachMessageCode;
 
-class BayernIdServerException extends PostfachException {
+public class BayernIdServerException extends PostfachException { // NOSONAR "This class has 6 parents which is greater than 5 authorized."
 
 	static final String TABELLE_NUMMER_EMPFANG_ERGEBNISSTATUS = "9006";
 	private static final String ERROR_MESSAGE_TEMPLATE = TABELLE_NUMMER_EMPFANG_ERGEBNISSTATUS + " / %s / %s / %s";
+	private static final String KEY_UNGUELTIGER_POSTKORB_HANDLE = "30";
 
 	private final MailSendingResponseStatus mailSendingResponseStatus;
 
@@ -20,11 +23,17 @@ class BayernIdServerException extends PostfachException {
 		this.mailSendingResponseStatus = mailSendingResponseStatus;
 	}
 
+	public boolean isUngueltigerPostkorbHandleKey() {
+		return Optional.ofNullable(mailSendingResponseStatus).map(MailSendingResponseStatus::getSchluessel)
+				.filter(KEY_UNGUELTIGER_POSTKORB_HANDLE::equals).isPresent();
+	}
+
 	@Override
 	public String getMessage() {
 		if (mailSendingResponseStatus == null) {
 			return super.getMessage();
 		}
-		return ERROR_MESSAGE_TEMPLATE.formatted(mailSendingResponseStatus.getSchluessel(), mailSendingResponseStatus.getMessage(), super.getMessage());
+		return ERROR_MESSAGE_TEMPLATE.formatted(mailSendingResponseStatus.getSchluessel(), mailSendingResponseStatus.getMessage(),
+				super.getMessage());
 	}
 }
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 5b5f2a9..d4230c1 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
@@ -33,7 +33,6 @@ import java.util.Optional;
 import java.util.UUID;
 import java.util.stream.Stream;
 
-import org.apache.logging.log4j.Logger;
 import org.bson.types.ObjectId;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.DisplayName;
@@ -633,9 +632,6 @@ class PostfachServiceTest {
 
 				private final static String MESSAGE = "Postfach server returned false";
 
-				@Mock
-				private Logger logger;
-
 				@BeforeEach
 				void mockService() {
 					doThrow(OsiPostfachServerProcessExceptionTestFactory.create()).when(postfachRemoteService).sendMessage(any());
@@ -799,7 +795,7 @@ class PostfachServiceTest {
 	@Nested
 	class TestSavingMukAnswer {
 		private final String refId = ObjectId.get().toHexString();
-		private PostfachNachricht answer = PostfachNachrichtTestFactory.createBuilder().vorgangId(null).referencedNachricht(refId).build();
+		private final PostfachNachricht answer = PostfachNachrichtTestFactory.createBuilder().vorgangId(null).referencedNachricht(refId).build();
 
 		@Test
 		void shouldIdentifyAsAnswer() {
-- 
GitLab