From 44b327856ab3501ab87f79dd3a9331634a45ffb6 Mon Sep 17 00:00:00 2001 From: Dmitri Sidorov Manko <159002112+dmitriin2@users.noreply.github.com> Date: Wed, 20 Nov 2024 13:30:08 +0100 Subject: [PATCH] Update globalexceptionhandler add tests --- .../handler/GlobalExceptionHandler.java | 2 +- .../handler/GlobalExceptionHandlerTest.java | 99 +++++++++++++++++++ 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 src/test/java/es/in2/vcverifier/exception/handler/GlobalExceptionHandlerTest.java diff --git a/src/main/java/es/in2/vcverifier/exception/handler/GlobalExceptionHandler.java b/src/main/java/es/in2/vcverifier/exception/handler/GlobalExceptionHandler.java index 09cd8f7..9a1c056 100644 --- a/src/main/java/es/in2/vcverifier/exception/handler/GlobalExceptionHandler.java +++ b/src/main/java/es/in2/vcverifier/exception/handler/GlobalExceptionHandler.java @@ -61,7 +61,7 @@ public GlobalErrorMessage handleException(Exception ex) { @ResponseStatus(HttpStatus.UNAUTHORIZED) public GlobalErrorMessage handleException(InvalidVPtokenException ex) { log.error("VP token is not valid: ", ex); - return new GlobalErrorMessage("","",""); + return new GlobalErrorMessage("VP token is not valid", ex.getMessage(),""); } } diff --git a/src/test/java/es/in2/vcverifier/exception/handler/GlobalExceptionHandlerTest.java b/src/test/java/es/in2/vcverifier/exception/handler/GlobalExceptionHandlerTest.java new file mode 100644 index 0000000..5b3f3bc --- /dev/null +++ b/src/test/java/es/in2/vcverifier/exception/handler/GlobalExceptionHandlerTest.java @@ -0,0 +1,99 @@ +package es.in2.vcverifier.exception.handler; + +import es.in2.vcverifier.exception.*; +import es.in2.vcverifier.model.GlobalErrorMessage; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.NoSuchElementException; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@SpringBootTest +class GlobalExceptionHandlerTest { + + private GlobalExceptionHandler globalExceptionHandler; + + @BeforeEach + void setUp() { + globalExceptionHandler = new GlobalExceptionHandler(); + } + + @Test + void testHandleResourceNotFoundException() { + ResourceNotFoundException exception = new ResourceNotFoundException("Resource not found"); + + GlobalErrorMessage response = globalExceptionHandler.handleResourceNotFoundException(exception); + + assertEquals("", response.title()); + assertEquals("", response.message()); + assertEquals("", response.path()); + } + + @Test + void testHandleNoSuchElementException() { + NoSuchElementException exception = new NoSuchElementException("Element not found"); + + GlobalErrorMessage response = globalExceptionHandler.handleNoSuchElementException(exception); + + assertEquals("", response.title()); + assertEquals("", response.message()); + assertEquals("", response.path()); + } + + @Test + void testHandleQRCodeGenerationException() { + QRCodeGenerationException exception = new QRCodeGenerationException("QR Code Generation Failed"); + + GlobalErrorMessage response = globalExceptionHandler.handleQRCodeGenerationException(exception); + + assertEquals("QR Code Generation Failed", response.title()); + assertEquals("", response.message()); + assertEquals("", response.path()); + } + + @Test + void testHandleCredentialRevokedException() { + CredentialRevokedException exception = new CredentialRevokedException("Credential revoked"); + + GlobalErrorMessage response = globalExceptionHandler.handleException(exception); + + assertEquals("Verifiable presentation failed", response.title()); + assertEquals("", response.message()); + assertEquals("", response.path()); + } + + @Test + void testHandleMismatchOrganizationIdentifierException() { + MismatchOrganizationIdentifierException exception = new MismatchOrganizationIdentifierException("Mismatch org identifier"); + + GlobalErrorMessage response = globalExceptionHandler.handleException(exception); + + assertEquals("", response.title()); + assertEquals("", response.message()); + assertEquals("", response.path()); + } + + @Test + void testHandleGenericException() { + Exception exception = new Exception("Generic error"); + + GlobalErrorMessage response = globalExceptionHandler.handleException(exception); + + assertEquals("", response.title()); + assertEquals("", response.message()); + assertEquals("", response.path()); + } + + @Test + void testHandleInvalidVPtokenException() { + InvalidVPtokenException exception = new InvalidVPtokenException("Invalid VP token"); + + GlobalErrorMessage response = globalExceptionHandler.handleException(exception); + + assertEquals("VP token is not valid", response.title()); + assertEquals("Invalid VP token", response.message()); + assertEquals("", response.path()); + } +} \ No newline at end of file