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 edcdb065cdd9d24f228f0da6ab6ac17146981e8b..d9ada859d343ed2759c0598f17642f4598c1e403 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 @@ -32,7 +32,9 @@ 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_SUCCESSFUL_MESSAGE_CODE("postfachnachricht.successful"), + SEND_FAILED_UNKNOWN_POSTFACH_ID_MESSAGE_CODE("postfachnachricht.server.unknown_postfach_id"), + ; @Getter private String messageCode; 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 6bd73d7ff7a20a286eabe71e62ab46fe2f3bb8d3..3466e0491f951e78de2774b0630b239188844d01 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 @@ -44,7 +44,6 @@ 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 lombok.NonNull; @@ -162,11 +161,10 @@ class PostfachService { publishMailSentEvent(commandId); return buildSendNachrichtResponse(true, PostfachMessageCode.SEND_SUCCESSFUL_MESSAGE_CODE); - } catch (OsiPostfachServerProcessException e) { - return proceedwithWarnException(commandId, e); } catch (PostfachException e) { - return proceedWithErrorException(commandId, e); + logSendMailFailed(commandId, e); + return proceedWithException(commandId, e); } } @@ -186,24 +184,18 @@ class PostfachService { return antragraumService.map(antragraum -> nachricht.toBuilder().mailBody(antragraum.getUserNotificationText()).build()); } - SendPostfachNachrichtResponse proceedwithWarnException(String commandId, OsiPostfachServerProcessException e) { - LOG.warn(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); + void logSendMailFailed(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); } else { - LOG.error(exception.getMessage(), exception); + LOG.error("Failed to send mail with commandId: {}", commandId, exception); } - return proceedWithException(commandId, exception); } private SendPostfachNachrichtResponse proceedWithException(String commandId, PostfachException e) { publishMailSentFailedEvent(commandId, e.getMessage()); return buildSendNachrichtResponse(false, e.getMessageCode()); - } void doSendMail(PostfachNachricht nachricht) { 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 0cbd6fe97272199bb3f7a39ddedd42aa8506d635..2936cc72946593a4f64d24d4b2df4c0f56b9f3ff 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 @@ -5,11 +5,10 @@ import java.util.Optional; import de.ozgcloud.nachrichten.postfach.PostfachException; import de.ozgcloud.nachrichten.postfach.PostfachMessageCode; -public class BayernIdServerException extends PostfachException { // NOSONAR "This class has 6 parents which is greater than 5 authorized." +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; @@ -19,13 +18,14 @@ public class BayernIdServerException extends PostfachException { // NOSONAR "Thi } public BayernIdServerException(String message, MailSendingResponseStatus mailSendingResponseStatus) { - super(message, PostfachMessageCode.PROCESS_FAILED_MESSAGE_CODE); + super(message, getPostfachMessageCode(mailSendingResponseStatus)); this.mailSendingResponseStatus = mailSendingResponseStatus; } - public boolean isUngueltigerPostkorbHandleKey() { - return Optional.ofNullable(mailSendingResponseStatus).map(MailSendingResponseStatus::getSchluessel) - .filter(KEY_UNGUELTIGER_POSTKORB_HANDLE::equals).isPresent(); + static PostfachMessageCode getPostfachMessageCode(MailSendingResponseStatus mailSendingResponseStatus) { + return Optional.ofNullable(mailSendingResponseStatus).filter(status -> status == MailSendingResponseStatus.INVALID_POSTKORB_ID) + .map(status -> PostfachMessageCode.SEND_FAILED_UNKNOWN_POSTFACH_ID_MESSAGE_CODE) + .orElse(PostfachMessageCode.PROCESS_FAILED_MESSAGE_CODE); } @Override 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 d4230c104e66dbbfe5f43853055db289db4ad863..814643a4b90850b46274407b26712f77f0615365 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 @@ -57,16 +57,17 @@ 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.OsiPostfachServerProcessException; import de.ozgcloud.nachrichten.postfach.osi.OsiPostfachServerProcessExceptionTestFactory; import de.ozgcloud.vorgang.callcontext.CallContextUserTestFactory; import de.ozgcloud.vorgang.callcontext.CurrentUserService; import nl.altindag.log.LogCaptor; +import nl.altindag.log.model.LogEvent; class PostfachServiceTest { static final String COMMAND_ID = UUID.randomUUID().toString(); static final String USER_ID = UUID.randomUUID().toString(); + @Spy @InjectMocks private PostfachService service; @@ -589,12 +590,13 @@ class PostfachServiceTest { class TestOnOsiPostfachException { private final static String MESSAGE = "Osi Postfach throws an exception."; + private static final PostfachException POSTFACH_EXCEPTION = new PostfachException(MESSAGE, + PostfachMessageCode.PROCESS_FAILED_MESSAGE_CODE); @BeforeEach void mockService() { when(postfachRemoteService.getPostfachType()).thenReturn(PostfachTestFactory.POSTFACH_TYPE); - doThrow(new PostfachException(MESSAGE, PostfachMessageCode.SEND_SUCCESSFUL_MESSAGE_CODE)).when(postfachRemoteService) - .sendMessage(any()); + doThrow(POSTFACH_EXCEPTION).when(postfachRemoteService).sendMessage(any()); } @Test @@ -607,14 +609,14 @@ class PostfachServiceTest { } @Test - void shouldLogError() { - var logCaptor = LogCaptor.forClass(PostfachService.class); + void shouldCallLogSendMailFailed() { + // var logCaptor = LogCaptor.forClass(PostfachService.class); service.handleSendMail(COMMAND_ID, mail); - verify(service).proceedWithErrorException(eq(COMMAND_ID), any(PostfachException.class)); - assertThat(logCaptor.getLogEvents().get(0).getLevel()).isEqualTo(LogLevel.ERROR.name()); - assertThat(logCaptor.getLogEvents().get(0).getMessage()).startsWith(MESSAGE); + verify(service).logSendMailFailed(COMMAND_ID, POSTFACH_EXCEPTION); + // assertThat(logCaptor.getLogEvents().get(0).getLevel()).isEqualTo(LogLevel.ERROR.name()); + // assertThat(logCaptor.getLogEvents().get(0).getMessage()).startsWith(MESSAGE); } @Test @@ -653,7 +655,7 @@ class PostfachServiceTest { service.handleSendMail(COMMAND_ID, mail); - verify(service).proceedwithWarnException(eq(COMMAND_ID), any(OsiPostfachServerProcessException.class)); + // verify(service).proceedwithWarnException(eq(COMMAND_ID), any(OsiPostfachServerProcessException.class)); assertThat(logCaptor.getLogEvents().get(0).getLevel()).isEqualTo(LogLevel.WARN.name()); assertThat(logCaptor.getLogEvents().get(0).getMessage()).startsWith(MESSAGE); } @@ -667,6 +669,45 @@ class PostfachServiceTest { } } } + } + + @Nested + class TestLogSendMailFailed { + + @Test + void shouldLogWarningWhenOsiException() { + try (var logCaptor = LogCaptor.forClass(PostfachService.class)) { + var exception = OsiPostfachServerProcessExceptionTestFactory.create(); + + service.logSendMailFailed(COMMAND_ID, exception); + + assertThat(logCaptor.getLogEvents()).hasSize(1).first().extracting(LogEvent::getLevel).isEqualTo(LogLevel.WARN.name()); + } + } + + @Test + void shouldLogWarningWhenWrongPostfachId() { + 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); + + assertThat(logCaptor.getLogEvents()).hasSize(1).first().extracting(LogEvent::getLevel).isEqualTo(LogLevel.WARN.name()); + } + } + + @DisplayName("should log error") + @ParameterizedTest(name = "when error code is {0}") + @EnumSource(value = PostfachMessageCode.class, names = { "SEND_FAILED_UNKNOWN_POSTFACH_ID_MESSAGE_CODE" }, mode = EnumSource.Mode.EXCLUDE) + void shouldLogError(PostfachMessageCode messageCode) { + try (var logCaptor = LogCaptor.forClass(PostfachService.class)) { + var exception = new PostfachException("Test", messageCode); + + service.logSendMailFailed(COMMAND_ID, exception); + + assertThat(logCaptor.getLogEvents().get(0).getLevel()).isEqualTo(LogLevel.ERROR.name()); + } + } }