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
No related tags found
No related merge requests found
package de.ozgcloud.nachrichten.postfach.osiv2.exception;
import java.io.IOException;
import jakarta.annotation.Nullable;
import org.springframework.http.HttpStatusCode;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestClientResponseException;
......@@ -24,11 +23,15 @@ public class Osi2ExceptionHandler {
}
PostfachMessageCode deriveMessageCodeFromRestClientResponseException(RestClientResponseException restClientResponseException) {
var cause = restClientResponseException.getCause();
if (cause instanceof IOException) {
return PostfachMessageCode.SERVER_CONNECTION_FAILED_MESSAGE_CODE;
if (hasStatusNotFound(restClientResponseException)) {
return PostfachMessageCode.SEND_FAILED_UNKNOWN_POSTFACH_ID_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 {
.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")
@Test
void shouldDeleteAttachmentsOnUploadException() {
......
......@@ -4,14 +4,13 @@ import static de.ozgcloud.nachrichten.postfach.PostfachMessageCode.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.io.IOException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.springframework.http.HttpStatusCode;
import org.springframework.web.client.RestClientResponseException;
class Osi2ExceptionHandlerTest {
......@@ -49,26 +48,24 @@ class Osi2ExceptionHandlerTest {
@DisplayName("derive message code from rest client response exception")
@Nested
class TestDeriveOsi2MessageCodeFromRestClientResponseException {
@Mock
IOException ioException;
@Mock
private RestClientResponseException restClientResponseException;
@DisplayName("should return server connection failed message code")
@DisplayName("should return unknown postfach id error code for 404")
@Test
void shouldReturnServerConnectionFailedMessageCode() {
when(restClientResponseException.getCause()).thenReturn(ioException);
void shouldReturnUnknownPostfachIdErrorCodeFor404() {
when(restClientResponseException.getStatusCode()).thenReturn(HttpStatusCode.valueOf(404));
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")
@Test
void shouldReturnProcessFailedMessageCodeByDefault() {
when(restClientResponseException.getCause()).thenReturn(null);
when(restClientResponseException.getStatusCode()).thenReturn(HttpStatusCode.valueOf(500));
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