|
| 1 | +package uk.gov.companieshouse.limitedpartnershipsapi.exception; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.BeforeEach; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 6 | +import org.mockito.ArgumentCaptor; |
| 7 | +import org.mockito.Captor; |
| 8 | +import org.mockito.Mock; |
| 9 | +import org.mockito.MockedStatic; |
| 10 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 11 | +import org.springframework.http.HttpStatus; |
| 12 | +import org.springframework.http.ResponseEntity; |
| 13 | +import org.springframework.test.util.ReflectionTestUtils; |
| 14 | +import org.springframework.web.context.request.WebRequest; |
| 15 | +import uk.gov.companieshouse.limitedpartnershipsapi.utils.ApiLogger; |
| 16 | + |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 22 | +import static org.mockito.ArgumentMatchers.eq; |
| 23 | +import static org.mockito.Mockito.mockStatic; |
| 24 | +import static org.mockito.Mockito.times; |
| 25 | +import static org.mockito.Mockito.when; |
| 26 | +import static uk.gov.companieshouse.limitedpartnershipsapi.utils.Constants.ERIC_REQUEST_ID_KEY; |
| 27 | + |
| 28 | +@ExtendWith(MockitoExtension.class) |
| 29 | +class GlobalExceptionHandlerTest { |
| 30 | + |
| 31 | + private static final String REQUEST_ID = "1234"; |
| 32 | + private GlobalExceptionHandler globalExceptionHandler; |
| 33 | + |
| 34 | + @Mock |
| 35 | + private WebRequest webRequest; |
| 36 | + |
| 37 | + @Captor |
| 38 | + private ArgumentCaptor<Map<String, Object>> logMapCaptor; |
| 39 | + |
| 40 | + @BeforeEach |
| 41 | + void setUp() { |
| 42 | + this.globalExceptionHandler = new GlobalExceptionHandler(); |
| 43 | + setTruncationLength(1000); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void testHandleExceptionReturnsCorrectResponse() { |
| 48 | + when(webRequest.getHeader(ERIC_REQUEST_ID_KEY)).thenReturn(REQUEST_ID); |
| 49 | + |
| 50 | + ResponseEntity<Object> entity = globalExceptionHandler.handleException(new Exception(), webRequest); |
| 51 | + |
| 52 | + assertNotNull(entity); |
| 53 | + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, entity.getStatusCode()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void testHandleExceptionEncodesException() { |
| 58 | + Throwable rootCause = new Throwable("root cause \n"); |
| 59 | + Exception exception = new Exception("exception message \n", rootCause); |
| 60 | + |
| 61 | + when(webRequest.getHeader(ERIC_REQUEST_ID_KEY)).thenReturn(REQUEST_ID); |
| 62 | + |
| 63 | + try (MockedStatic<ApiLogger> apiLogger = mockStatic(ApiLogger.class)) { |
| 64 | + |
| 65 | + globalExceptionHandler.handleException(exception, webRequest); |
| 66 | + |
| 67 | + apiLogger.verify(() -> ApiLogger.errorContext( |
| 68 | + eq(REQUEST_ID), |
| 69 | + eq("exception message \\n"), |
| 70 | + eq(null), |
| 71 | + logMapCaptor.capture()), times(1)); |
| 72 | + |
| 73 | + Map<String, Object> logMap = logMapCaptor.getValue(); |
| 74 | + String stackTraceString = (String) logMap.get("stackTrace"); |
| 75 | + assertTrue(stackTraceString.contains("exception message \\n")); |
| 76 | + |
| 77 | + String rootCauseString = (String) logMap.get("rootCause"); |
| 78 | + assertTrue(rootCauseString.contains("root cause \\n")); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void testHandleExceptionTruncatesException() { |
| 84 | + setTruncationLength(20); |
| 85 | + Throwable rootCause = new Throwable("root cause"); |
| 86 | + Exception exception = new Exception("12345678901234567890123", rootCause); |
| 87 | + |
| 88 | + when(webRequest.getHeader(ERIC_REQUEST_ID_KEY)).thenReturn(REQUEST_ID); |
| 89 | + |
| 90 | + try (MockedStatic<ApiLogger> apiLogger = mockStatic(ApiLogger.class)) { |
| 91 | + globalExceptionHandler.handleException(exception, webRequest); |
| 92 | + |
| 93 | + apiLogger.verify(() -> ApiLogger.errorContext( |
| 94 | + eq(REQUEST_ID), |
| 95 | + eq("12345678901234567890"), |
| 96 | + eq(null), |
| 97 | + logMapCaptor.capture()), times(1)); |
| 98 | + |
| 99 | + Map<String, Object> logMap = logMapCaptor.getValue(); |
| 100 | + String stackTraceString = (String) logMap.get("stackTrace"); |
| 101 | + assertEquals(20, stackTraceString.length()); |
| 102 | + |
| 103 | + String rootCauseString = (String) logMap.get("rootCause"); |
| 104 | + assertEquals(20, rootCauseString.length()); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private void setTruncationLength(int length) { |
| 109 | + ReflectionTestUtils.setField(globalExceptionHandler, "truncationLength", length); |
| 110 | + } |
| 111 | +} |
0 commit comments