Skip to content

Commit

Permalink
Update globalexceptionhandler add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriin2 committed Nov 20, 2024
1 parent c0f9b19 commit 44b3278
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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(),"");
}
}

Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 44b3278

Please sign in to comment.