-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FAIRSPC-81: tuned global exception handling
- Loading branch information
1 parent
17e7514
commit 921ade7
Showing
7 changed files
with
184 additions
and
63 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
projects/saturn/src/main/java/io/fairspace/saturn/controller/dto/ErrorDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.fairspace.saturn.controller.dto; | ||
|
||
import ioinformarics.oss.jackson.module.jsonld.annotation.JsonldProperty; | ||
import ioinformarics.oss.jackson.module.jsonld.annotation.JsonldType; | ||
|
||
import io.fairspace.saturn.vocabulary.FS; | ||
|
||
@JsonldType(FS.ERROR_URI) | ||
public record ErrorDto( | ||
@JsonldProperty(FS.ERROR_STATUS_URI) int status, | ||
@JsonldProperty(FS.ERROR_MESSAGE_URI) String message, | ||
@JsonldProperty(FS.ERROR_DETAILS_URI) Object details) {} |
30 changes: 13 additions & 17 deletions
30
...saturn/src/main/java/io/fairspace/saturn/controller/exception/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 0 additions & 25 deletions
25
projects/saturn/src/main/java/io/fairspace/saturn/services/PayloadParsingException.java
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
projects/saturn/src/main/java/io/fairspace/saturn/services/errors/ErrorDto.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
...rn/src/test/java/io/fairspace/saturn/controller/exception/GlobalExceptionHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package io.fairspace.saturn.controller.exception; | ||
|
||
import java.util.Set; | ||
|
||
import jakarta.validation.ConstraintViolation; | ||
import jakarta.validation.ConstraintViolationException; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import io.fairspace.saturn.controller.BaseControllerTest; | ||
import io.fairspace.saturn.services.metadata.validation.ValidationException; | ||
import io.fairspace.saturn.services.metadata.validation.Violation; | ||
|
||
import static org.mockito.Mockito.doThrow; | ||
import static org.mockito.Mockito.when; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@WebMvcTest({GlobalExceptionHandler.class, TestController.class}) | ||
public class GlobalExceptionHandlerTest extends BaseControllerTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@MockBean | ||
private TestController.TestInnerClass testInnerClass; | ||
|
||
@Test | ||
public void testHandleConstraintViolationException() throws Exception { | ||
// Mocking a ConstraintViolationException with a couple of violations | ||
ConstraintViolation<?> violation1 = Mockito.mock(ConstraintViolation.class); | ||
ConstraintViolation<?> violation2 = Mockito.mock(ConstraintViolation.class); | ||
when(violation1.getMessage()).thenReturn("Violation 1"); | ||
when(violation2.getMessage()).thenReturn("Violation 2"); | ||
Set<ConstraintViolation<?>> violations = Set.of(violation1, violation2); | ||
ConstraintViolationException exception = new ConstraintViolationException(violations); | ||
|
||
doThrow(exception).when(testInnerClass).method(); // Simulating the exception | ||
|
||
mockMvc.perform(get("/test")) | ||
.andExpect(status().isBadRequest()) | ||
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect( | ||
content() | ||
.json( | ||
""" | ||
{ | ||
"status": 400, | ||
"message": "Validation Error", | ||
"details": "Violations: Violation 1; Violation 2" | ||
} | ||
""")); | ||
} | ||
|
||
@Test | ||
public void testHandleValidationException() throws Exception { | ||
// Mocking a ValidationException with a violation | ||
Set<Violation> violations = Set.of(new Violation("Invalid value", "subject", "predicate", "value")); | ||
ValidationException exception = new ValidationException(violations); | ||
|
||
doThrow(exception).when(testInnerClass).method(); // Simulating the exception | ||
|
||
mockMvc.perform(get("/test")) | ||
.andExpect(status().isBadRequest()) | ||
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect( | ||
content() | ||
.json( | ||
""" | ||
{ | ||
"status": 400, | ||
"message": "Validation Error", | ||
"details": [ | ||
{ | ||
"message": "Invalid value", | ||
"subject": "subject", | ||
"predicate": "predicate", | ||
"value": "value" | ||
} | ||
] | ||
} | ||
""")); | ||
} | ||
|
||
@Test | ||
public void testHandleIllegalArgumentException() throws Exception { | ||
// Mocking an IllegalArgumentException | ||
IllegalArgumentException exception = new IllegalArgumentException("Invalid argument"); | ||
|
||
doThrow(exception).when(testInnerClass).method(); // Simulating the exception | ||
|
||
mockMvc.perform(get("/test")) | ||
.andExpect(status().isBadRequest()) | ||
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect( | ||
content() | ||
.json( | ||
""" | ||
{ | ||
"status": 400, | ||
"message": "Validation Error", | ||
"details": "Invalid argument" | ||
} | ||
""")); | ||
} | ||
|
||
@Test | ||
public void testHandleAccessDeniedException() throws Exception { | ||
// Mocking an AccessDeniedException | ||
AccessDeniedException exception = new AccessDeniedException("Access denied"); | ||
|
||
doThrow(exception).when(testInnerClass).method(); // Simulating the exception | ||
|
||
mockMvc.perform(get("/test")) | ||
.andExpect(status().isForbidden()) | ||
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect( | ||
content() | ||
.json( | ||
""" | ||
{ | ||
"status": 403, | ||
"message": "Access Denied", | ||
"details": null | ||
} | ||
""")); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
projects/saturn/src/test/java/io/fairspace/saturn/controller/exception/TestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.fairspace.saturn.controller.exception; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class TestController { | ||
|
||
private final TestInnerClass testInnerClass; | ||
|
||
@GetMapping("/test") | ||
public void testMethod() { | ||
testInnerClass.method(); | ||
} | ||
|
||
@Component | ||
public static class TestInnerClass { | ||
public void method() {} | ||
} | ||
} |