Skip to content

Commit

Permalink
more coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
DeclanClarkeCGI committed Sep 5, 2024
1 parent 4f9c28a commit 4ca66a3
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.gov.hmcts.opal.controllers.advice;

import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceException;
import jakarta.persistence.QueryTimeoutException;
import jakarta.servlet.http.HttpServletRequest;
import org.hibernate.PropertyValueException;
Expand All @@ -12,6 +13,7 @@
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.dao.InvalidDataAccessResourceUsageException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
Expand Down Expand Up @@ -127,6 +129,15 @@ void testHandleInvalidDataAccessApiUsageException() {
assertEquals("Invalid API usage", response.getBody().get("error"));
}

@Test
void handleInvalidDataAccessResourceUsageException_ShouldReturnInternalServerError() {
InvalidDataAccessResourceUsageException exception = new InvalidDataAccessResourceUsageException("Invalid resource usage");
ResponseEntity<Map<String, String>> response = globalExceptionHandler.handleInvalidDataAccessResourceUsageException(exception);

assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
assertEquals("Invalid resource usage", response.getBody().get("error"));
}

@Test
void testHandleEntityNotFoundException() {
EntityNotFoundException exception = new EntityNotFoundException("Entity not found");
Expand Down Expand Up @@ -156,6 +167,16 @@ void testHandleDatabaseExceptions_queryTimeout() {
response.getBody().get("message"));
}

@Test
void handleDatabaseExceptions_OtherDatabaseException_ShouldReturnInternalServerError() {
PersistenceException exception = new PersistenceException("Persistence exception");
ResponseEntity<Map<String, String>> response = globalExceptionHandler.handleDatabaseExceptions(exception);

assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
assertEquals("Internal Server Error", response.getBody().get("error"));
assertEquals("An unexpected error occurred", response.getBody().get("message"));
}

@Test
void testHandlePsqlException_serviceUnavailable() {
PSQLException exception = new PSQLException("PSQL Exception",
Expand All @@ -168,6 +189,16 @@ void testHandlePsqlException_serviceUnavailable() {
assertEquals("Opal Fines Database is currently unavailable", response.getBody().get("message"));
}

@Test
void handlePsqlException_WithOtherCause_ShouldReturnInternalServerError() {
PSQLException exception = new PSQLException("PSQL Exception", PSQLState.UNEXPECTED_ERROR, new Throwable("Unexpected error"));
ResponseEntity<Map<String, String>> response = globalExceptionHandler.handlePsqlException(exception);

assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
assertEquals("Internal Server Error", response.getBody().get("error"));
assertEquals("PSQL Exception", response.getBody().get("message"));
}

@Test
void testHandleDataAccessResourceFailureException() {
DataAccessResourceFailureException exception =
Expand Down

0 comments on commit 4ca66a3

Please sign in to comment.