Skip to content
Snippets Groups Projects
Commit 3b3badd0 authored by Jan Zickermann's avatar Jan Zickermann
Browse files

OZG-4097 error-handling: Provide messageCode for unknown postfachId

parent e454a6b1
No related branches found
Tags
No related merge requests found
package de.ozgcloud.nachrichten.postfach.osiv2.exception; package de.ozgcloud.nachrichten.postfach.osiv2.exception;
import java.io.IOException;
import jakarta.annotation.Nullable; import jakarta.annotation.Nullable;
import org.springframework.http.HttpStatusCode;
import org.springframework.web.client.ResourceAccessException; import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestClientResponseException; import org.springframework.web.client.RestClientResponseException;
...@@ -24,11 +23,15 @@ public class Osi2ExceptionHandler { ...@@ -24,11 +23,15 @@ public class Osi2ExceptionHandler {
} }
PostfachMessageCode deriveMessageCodeFromRestClientResponseException(RestClientResponseException restClientResponseException) { PostfachMessageCode deriveMessageCodeFromRestClientResponseException(RestClientResponseException restClientResponseException) {
var cause = restClientResponseException.getCause(); if (hasStatusNotFound(restClientResponseException)) {
if (cause instanceof IOException) { return PostfachMessageCode.SEND_FAILED_UNKNOWN_POSTFACH_ID_MESSAGE_CODE;
return PostfachMessageCode.SERVER_CONNECTION_FAILED_MESSAGE_CODE;
} }
return PostfachMessageCode.PROCESS_FAILED_MESSAGE_CODE; return PostfachMessageCode.PROCESS_FAILED_MESSAGE_CODE;
} }
private boolean hasStatusNotFound(RestClientResponseException restClientResponseException) {
return restClientResponseException.getStatusCode().isSameCodeAs(HttpStatusCode.valueOf(404));
}
} }
...@@ -144,6 +144,19 @@ class OsiPostfachRemoteServiceITCase { ...@@ -144,6 +144,19 @@ class OsiPostfachRemoteServiceITCase {
.hasFieldOrPropertyWithValue("messageCode", PostfachMessageCode.SERVER_CONNECTION_FAILED_MESSAGE_CODE); .hasFieldOrPropertyWithValue("messageCode", PostfachMessageCode.SERVER_CONNECTION_FAILED_MESSAGE_CODE);
} }
@DisplayName("should throw postfach exception with unknown postfach id error code")
@Test
void shouldThrowPostfachExceptionWithUnknownPostfachIdErrorCode() {
postfachFacadeMockServer.stubFor(post(urlPathTemplate("/MessageExchange/v1/Send/{mailboxId}"))
.willReturn(notFound())
);
var postfachNachricht = PostfachNachrichtTestFactory.create();
assertThatThrownBy(() -> osiPostfachRemoteService.sendMessage(postfachNachricht))
.isInstanceOf(Osi2PostfachException.class)
.hasFieldOrPropertyWithValue("messageCode", PostfachMessageCode.SEND_FAILED_UNKNOWN_POSTFACH_ID_MESSAGE_CODE);
}
@DisplayName("should delete attachments on upload exception") @DisplayName("should delete attachments on upload exception")
@Test @Test
void shouldDeleteAttachmentsOnUploadException() { void shouldDeleteAttachmentsOnUploadException() {
......
...@@ -4,14 +4,13 @@ import static de.ozgcloud.nachrichten.postfach.PostfachMessageCode.*; ...@@ -4,14 +4,13 @@ import static de.ozgcloud.nachrichten.postfach.PostfachMessageCode.*;
import static org.assertj.core.api.Assertions.*; import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import java.io.IOException;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Spy; import org.mockito.Spy;
import org.springframework.http.HttpStatusCode;
import org.springframework.web.client.RestClientResponseException; import org.springframework.web.client.RestClientResponseException;
class Osi2ExceptionHandlerTest { class Osi2ExceptionHandlerTest {
...@@ -49,26 +48,24 @@ class Osi2ExceptionHandlerTest { ...@@ -49,26 +48,24 @@ class Osi2ExceptionHandlerTest {
@DisplayName("derive message code from rest client response exception") @DisplayName("derive message code from rest client response exception")
@Nested @Nested
class TestDeriveOsi2MessageCodeFromRestClientResponseException { class TestDeriveOsi2MessageCodeFromRestClientResponseException {
@Mock
IOException ioException;
@Mock @Mock
private RestClientResponseException restClientResponseException; private RestClientResponseException restClientResponseException;
@DisplayName("should return server connection failed message code") @DisplayName("should return unknown postfach id error code for 404")
@Test @Test
void shouldReturnServerConnectionFailedMessageCode() { void shouldReturnUnknownPostfachIdErrorCodeFor404() {
when(restClientResponseException.getCause()).thenReturn(ioException); when(restClientResponseException.getStatusCode()).thenReturn(HttpStatusCode.valueOf(404));
var result = handler.deriveMessageCodeFromRestClientResponseException(restClientResponseException); var result = handler.deriveMessageCodeFromRestClientResponseException(restClientResponseException);
assertThat(result).isEqualTo(SERVER_CONNECTION_FAILED_MESSAGE_CODE); assertThat(result).isEqualTo(SEND_FAILED_UNKNOWN_POSTFACH_ID_MESSAGE_CODE);
} }
@DisplayName("should return process failed message code by default") @DisplayName("should return process failed message code by default")
@Test @Test
void shouldReturnProcessFailedMessageCodeByDefault() { void shouldReturnProcessFailedMessageCodeByDefault() {
when(restClientResponseException.getCause()).thenReturn(null); when(restClientResponseException.getStatusCode()).thenReturn(HttpStatusCode.valueOf(500));
var result = handler.deriveMessageCodeFromRestClientResponseException(restClientResponseException); var result = handler.deriveMessageCodeFromRestClientResponseException(restClientResponseException);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment