BaseRestApiControllerAdvice { public RestApiErrorResponse handleException( final Exception e, final String message) { logger.error(e.getMessage(), e); RestApiErrorResponse restApiErrorResponse = new RestApiErrorResponse(); if (!message.isEmpty()) { restApiErrorResponse.getErrorDetails().setErrorMessage(message); } return restApiErrorResponse; } BaseRestApiControllerAdvice(final Logger log); RestApiErrorResponse handleException(
            final Exception e, final String message);  }
@Test public void testBaseRestApiControllerAdviceException() { RestApiExceptionHandler restApiExceptionHandler = new RestApiExceptionHandler(); RestApiException e = new RestApiException("Internal server error"); ResponseEntity<RestApiErrorResponse> actualErrorResponse = restApiExceptionHandler.handleException(e); Assert.assertNotNull(actualErrorResponse); Assert.assertTrue(actualErrorResponse.getStatusCode().toString().equals("500")); Assert.assertTrue( actualErrorResponse.getBody().getErrorDetails().getErrorMessage().equals("Internal server error")); }
@Test public void testBaseRestApiControllerAdviceHttpMediaTypeNotAcceptableException() { HttpMediaTypeNotAcceptableExceptionHandler httpMediaTypeNotAcceptableExceptionHandler = new HttpMediaTypeNotAcceptableExceptionHandler(); HttpMediaTypeNotAcceptableException e = new HttpMediaTypeNotAcceptableException("Media Type Not Supported"); RestApiErrorResponse actualErrorResponse = httpMediaTypeNotAcceptableExceptionHandler.handleException(e); Assert.assertNotNull(actualErrorResponse); Assert.assertTrue(actualErrorResponse.getErrorDetails().getErrorMessage().equals("Not Acceptable")); }
@Test public void testBaseRestApiControllerAdviceHttpRequestMethodNotSupportedException() { HttpRequestMethodNotSupportedExceptionHandler httpRequestMethodNotSupportedExceptionHandler = new HttpRequestMethodNotSupportedExceptionHandler(); HttpRequestMethodNotSupportedException e = new HttpRequestMethodNotSupportedException("GET"); RestApiErrorResponse actualErrorResponse = httpRequestMethodNotSupportedExceptionHandler.handleException(e); Assert.assertNotNull(actualErrorResponse); Assert.assertTrue(actualErrorResponse.getErrorDetails().getErrorMessage().equals("Method Not Allowed")); }
@Test public void testBaseRestApiControllerAdviceIllegalArgumentException() { IllegalArgumentExceptionHandler illegalArgumentExceptionHandler = new IllegalArgumentExceptionHandler(); IllegalArgumentException e = new IllegalArgumentException("Arguments passed are invalid"); RestApiErrorResponse actualErrorResponse = illegalArgumentExceptionHandler.handleException(e); Assert.assertNotNull(actualErrorResponse); Assert.assertTrue( actualErrorResponse.getErrorDetails().getErrorMessage().equals("Arguments passed are invalid")); }
@Test public void testBaseRestApiControllerAdviceUntrustedIssuerException() { UntrustedIssuerExceptionHandler untrustedIssuerExceptionHandler = new UntrustedIssuerExceptionHandler(); UntrustedIssuerException e = new UntrustedIssuerException("Not a trusted Issuer"); RestApiErrorResponse actualErrorResponse = untrustedIssuerExceptionHandler.handleException(e); Assert.assertNotNull(actualErrorResponse); Assert.assertTrue(actualErrorResponse.getErrorDetails().getErrorMessage().equals("Not a trusted Issuer")); }
@Test public void testBaseRestApiControllerAdviceSecurityException() { SecurityExceptionHandler securityExceptionHandler = new SecurityExceptionHandler(); SecurityException e = new SecurityException("Not a trusted Issuer"); RestApiErrorResponse actualErrorResponse = securityExceptionHandler.handleException(e); Assert.assertNotNull(actualErrorResponse); Assert.assertTrue(actualErrorResponse.getErrorDetails().getErrorMessage().equals("Not a trusted Issuer")); }
@Test public void testBaseRestApiControllerAdviceHttpMessageNotReadableException() { HttpMessageNotReadableExceptionHandler httpMessageNotReadableExceptionHandler = new HttpMessageNotReadableExceptionHandler(); HttpMessageNotReadableException e = new HttpMessageNotReadableException("{JSON}"); RestApiErrorResponse actualErrorResponse = httpMessageNotReadableExceptionHandler.handleException(e); Assert.assertNotNull(actualErrorResponse); Assert.assertTrue(actualErrorResponse.getErrorDetails().getErrorCode().equals("FAILED")); Assert.assertTrue( actualErrorResponse.getErrorDetails().getErrorMessage().equals("Malformed JSON syntax. {JSON}")); }
@Test public void testBaseRestApiControllerAdviceHttpMediaTypeNotSupportedException() { HttpMediaTypeNotSupportedExceptionHandler httpMediaTypeNotSupportedExceptionHandler = new HttpMediaTypeNotSupportedExceptionHandler(); HttpMediaTypeNotSupportedException e = new HttpMediaTypeNotSupportedException("JSON"); RestApiErrorResponse actualErrorResponse = httpMediaTypeNotSupportedExceptionHandler.handleException(e); Assert.assertNotNull(actualErrorResponse); Assert.assertTrue(actualErrorResponse.getErrorDetails().getErrorMessage().equals("Unsupported Media Type")); }