|
| 1 | +package org.cardanofoundation.rosetta.common.exception; |
| 2 | + |
| 3 | +import java.util.Collections; |
| 4 | +import jakarta.servlet.http.HttpServletRequest; |
| 5 | + |
| 6 | +import org.springframework.http.HttpStatus; |
| 7 | +import org.springframework.http.ResponseEntity; |
| 8 | +import org.springframework.validation.BindingResult; |
| 9 | +import org.springframework.validation.FieldError; |
| 10 | +import org.springframework.web.bind.MethodArgumentNotValidException; |
| 11 | +import org.mockito.InjectMocks; |
| 12 | +import org.mockito.Mock; |
| 13 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 14 | + |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 17 | + |
| 18 | +import org.cardanofoundation.rosetta.common.util.RosettaConstants.RosettaErrorType; |
| 19 | + |
| 20 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 21 | +import static org.mockito.Mockito.when; |
| 22 | + |
| 23 | +@ExtendWith(MockitoExtension.class) |
| 24 | +class GlobalExceptionHandlerTest { |
| 25 | + |
| 26 | + private static final String TEST_ID = "1"; |
| 27 | + private static final Integer CARDANO_ERROR_CODE = 5000; |
| 28 | + private static final String FIELD_NAME = "fieldName"; |
| 29 | + private static final String MISSING_FIELD_MESSAGE = "must be not null"; |
| 30 | + private static final String ARGUMENT_NOT_VALID_MESSAGE = FIELD_NAME + " " + MISSING_FIELD_MESSAGE; |
| 31 | + private static final String GLOBAL_MESSAGE = "Global Exception"; |
| 32 | + private static final String API_MESSAGE = "API Exception"; |
| 33 | + private static final String GLOBAL_EXCEPTION_MESSAGE = |
| 34 | + "An error occurred for request " + TEST_ID + ": " + GLOBAL_MESSAGE; |
| 35 | + private static final String API_EXCEPTION_MESSAGE = |
| 36 | + "An error occurred for request " + TEST_ID + ": " + API_MESSAGE; |
| 37 | + private static final String ARGUMENT_NOT_VALID_EXCEPTION_MESSAGE = |
| 38 | + "An error occurred for request " + TEST_ID + ": [" + ARGUMENT_NOT_VALID_MESSAGE + "]"; |
| 39 | + |
| 40 | + @Mock |
| 41 | + private HttpServletRequest request; |
| 42 | + |
| 43 | + @Mock |
| 44 | + private MethodArgumentNotValidException methodArgumentNotValidException; |
| 45 | + |
| 46 | + @Mock |
| 47 | + private FieldError fieldError; |
| 48 | + |
| 49 | + @Mock |
| 50 | + private BindingResult bindingResult; |
| 51 | + |
| 52 | + @InjectMocks |
| 53 | + private GlobalExceptionHandler underTest; |
| 54 | + |
| 55 | + @Test |
| 56 | + void handleGlobalException_shouldReturnInternalServerError() { |
| 57 | + Exception globalException = new Exception(GLOBAL_MESSAGE); |
| 58 | + |
| 59 | + when(request.getRequestId()).thenReturn(TEST_ID); |
| 60 | + ResponseEntity<Error> result = underTest.handleGlobalException(globalException, request); |
| 61 | + |
| 62 | + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, result.getStatusCode()); |
| 63 | + assertEquals(GLOBAL_EXCEPTION_MESSAGE, result.getBody().getDetails().getMessage()); |
| 64 | + assertEquals(CARDANO_ERROR_CODE, result.getBody().getCode()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void handleAPIException_shouldReturnInternalServerError() { |
| 69 | + ApiException apiException = createApiException(); |
| 70 | + |
| 71 | + ResponseEntity<Error> result = underTest.handleApiException(apiException); |
| 72 | + |
| 73 | + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, result.getStatusCode()); |
| 74 | + assertEquals(apiException.getError().getDetails().getMessage(), |
| 75 | + result.getBody().getDetails().getMessage()); |
| 76 | + assertEquals(apiException.getError().getCode(), result.getBody().getCode()); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void handleMethodArgumentNotValidException_shouldReturnInternalServerError() { |
| 81 | + when(methodArgumentNotValidException.getBindingResult()).thenReturn(bindingResult); |
| 82 | + when(bindingResult.getFieldErrors()).thenReturn(Collections.singletonList(fieldError)); |
| 83 | + when(fieldError.getField()).thenReturn(FIELD_NAME); |
| 84 | + when(fieldError.getDefaultMessage()).thenReturn(MISSING_FIELD_MESSAGE); |
| 85 | + when(request.getRequestId()).thenReturn(TEST_ID); |
| 86 | + |
| 87 | + ResponseEntity<Error> result = underTest.handleMethodArgumentNotValidException( |
| 88 | + methodArgumentNotValidException, request); |
| 89 | + |
| 90 | + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, result.getStatusCode()); |
| 91 | + assertEquals(ARGUMENT_NOT_VALID_EXCEPTION_MESSAGE, result.getBody().getDetails().getMessage()); |
| 92 | + assertEquals(CARDANO_ERROR_CODE, result.getBody().getCode()); |
| 93 | + } |
| 94 | + |
| 95 | + private ApiException createApiException() { |
| 96 | + return new ApiException(RosettaErrorType.UNSPECIFIED_ERROR.toRosettaError(true, |
| 97 | + new Details(API_EXCEPTION_MESSAGE))); |
| 98 | + } |
| 99 | +} |
0 commit comments