diff --git a/src/main/java/git/tracehub/pmo/controller/AdviceController.java b/src/main/java/git/tracehub/pmo/controller/AdviceController.java index b348869..3b0a1a5 100644 --- a/src/main/java/git/tracehub/pmo/controller/AdviceController.java +++ b/src/main/java/git/tracehub/pmo/controller/AdviceController.java @@ -25,8 +25,6 @@ package git.tracehub.pmo.controller; import git.tracehub.pmo.exception.Logged; -import git.tracehub.pmo.exception.ResourceAlreadyExistsException; -import git.tracehub.pmo.exception.ResourceNotFoundException; import git.tracehub.pmo.exception.RestError; import java.util.stream.Collectors; import org.springframework.context.support.DefaultMessageSourceResolvable; @@ -35,8 +33,8 @@ import org.springframework.security.access.AccessDeniedException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.server.ResponseStatusException; /** * Exception handler. @@ -54,7 +52,6 @@ public class AdviceController { * @return ResponseEntity */ @ExceptionHandler(MethodArgumentNotValidException.class) - @ResponseStatus(HttpStatus.BAD_REQUEST) public ResponseEntity handle( final MethodArgumentNotValidException exception ) { @@ -71,21 +68,20 @@ public ResponseEntity handle( } /** - * Handle ResourceAlreadyExistsException. + * Handle ResponseStatusException. * * @param exception Exception * @return ResponseEntity */ - @ExceptionHandler(ResourceAlreadyExistsException.class) - @ResponseStatus(HttpStatus.BAD_REQUEST) + @ExceptionHandler(ResponseStatusException.class) public ResponseEntity handle( - final ResourceAlreadyExistsException exception + final ResponseStatusException exception ) { return new Logged( exception, new RestError( exception.getMessage(), - HttpStatus.BAD_REQUEST + HttpStatus.valueOf(exception.getStatusCode().value()) ) ).value(); } @@ -97,7 +93,6 @@ public ResponseEntity handle( * @return ResponseEntity */ @ExceptionHandler(IllegalArgumentException.class) - @ResponseStatus(HttpStatus.BAD_REQUEST) public ResponseEntity handle( final IllegalArgumentException exception ) { @@ -110,26 +105,6 @@ public ResponseEntity handle( ).value(); } - /** - * Handle ResourceNotFoundException. - * - * @param exception Exception - * @return ResponseEntity - */ - @ExceptionHandler(ResourceNotFoundException.class) - @ResponseStatus(HttpStatus.NOT_FOUND) - public ResponseEntity handle( - final ResourceNotFoundException exception - ) { - return new Logged( - exception, - new RestError( - exception.getMessage(), - HttpStatus.NOT_FOUND - ) - ).value(); - } - /** * Handle AccessDeniedException. * @@ -137,7 +112,6 @@ public ResponseEntity handle( * @return ResponseEntity */ @ExceptionHandler(AccessDeniedException.class) - @ResponseStatus(HttpStatus.FORBIDDEN) public ResponseEntity handle( final AccessDeniedException exception ) { @@ -157,7 +131,6 @@ public ResponseEntity handle( * @return ResponseEntity */ @ExceptionHandler(Exception.class) - @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public ResponseEntity handle(final Exception exception) { return new Logged( exception, diff --git a/src/main/java/git/tracehub/pmo/exception/ResourceAlreadyExistsException.java b/src/main/java/git/tracehub/pmo/exception/ResourceAlreadyExistsException.java index d94646f..aefdeb0 100644 --- a/src/main/java/git/tracehub/pmo/exception/ResourceAlreadyExistsException.java +++ b/src/main/java/git/tracehub/pmo/exception/ResourceAlreadyExistsException.java @@ -24,12 +24,15 @@ package git.tracehub.pmo.exception; +import org.springframework.http.HttpStatus; +import org.springframework.web.server.ResponseStatusException; + /** * ResourceAlreadyExistsException. * * @since 0.0.0 */ -public class ResourceAlreadyExistsException extends RuntimeException { +public class ResourceAlreadyExistsException extends ResponseStatusException { /** * Constructor. @@ -37,6 +40,6 @@ public class ResourceAlreadyExistsException extends RuntimeException { * @param message Message */ public ResourceAlreadyExistsException(final String message) { - super(message); + super(HttpStatus.CONFLICT, message); } } diff --git a/src/main/java/git/tracehub/pmo/exception/ResourceNotFoundException.java b/src/main/java/git/tracehub/pmo/exception/ResourceNotFoundException.java index 397d7f2..b475dc0 100644 --- a/src/main/java/git/tracehub/pmo/exception/ResourceNotFoundException.java +++ b/src/main/java/git/tracehub/pmo/exception/ResourceNotFoundException.java @@ -24,12 +24,15 @@ package git.tracehub.pmo.exception; +import org.springframework.http.HttpStatus; +import org.springframework.web.server.ResponseStatusException; + /** * ResourceNotFoundException. * * @since 0.0.0 */ -public class ResourceNotFoundException extends RuntimeException { +public class ResourceNotFoundException extends ResponseStatusException { /** * Constructor. @@ -37,6 +40,6 @@ public class ResourceNotFoundException extends RuntimeException { * @param message Message */ public ResourceNotFoundException(final String message) { - super(message); + super(HttpStatus.NOT_FOUND, message); } } diff --git a/src/test/java/git/tracehub/pmo/controller/AdviceControllerTest.java b/src/test/java/git/tracehub/pmo/controller/AdviceControllerTest.java index 812d26d..58b1da9 100644 --- a/src/test/java/git/tracehub/pmo/controller/AdviceControllerTest.java +++ b/src/test/java/git/tracehub/pmo/controller/AdviceControllerTest.java @@ -64,9 +64,10 @@ void handlesResourceAlreadyExistsException() { "ResourceAlreadyExistsException isn't handled", new AdviceController().handle( new ResourceAlreadyExistsException(message) - ), + ).getStatusCode(), new IsEqual<>( - new RestError(message, HttpStatus.BAD_REQUEST).value() + new RestError(message, HttpStatus.CONFLICT).value() + .getStatusCode() ) ); } @@ -92,9 +93,10 @@ void handlesResourceNotFoundException() { "ResourceNotFoundException isn't handled", new AdviceController().handle( new ResourceNotFoundException(message) - ), + ).getStatusCode(), new IsEqual<>( new RestError(message, HttpStatus.NOT_FOUND).value() + .getStatusCode() ) ); } diff --git a/src/test/java/git/tracehub/pmo/project/DefaultProjectsTest.java b/src/test/java/git/tracehub/pmo/project/DefaultProjectsTest.java index 14f619a..6ee3912 100644 --- a/src/test/java/git/tracehub/pmo/project/DefaultProjectsTest.java +++ b/src/test/java/git/tracehub/pmo/project/DefaultProjectsTest.java @@ -184,7 +184,7 @@ void throwsOnInvalidProjectId() throws SQLException { "Exception is not thrown or valid", () -> this.projects.byId(id), new Throws<>( - "Project %s not found".formatted(id), + "404 NOT_FOUND \"Project %s not found\"".formatted(id), ResourceNotFoundException.class ) ).affirm(); diff --git a/src/test/java/git/tracehub/pmo/secret/DefaultSecretsTest.java b/src/test/java/git/tracehub/pmo/secret/DefaultSecretsTest.java index c43c663..d18e5d2 100644 --- a/src/test/java/git/tracehub/pmo/secret/DefaultSecretsTest.java +++ b/src/test/java/git/tracehub/pmo/secret/DefaultSecretsTest.java @@ -181,7 +181,7 @@ void throwsOnInvalidKey() throws SQLException { "Exception is not thrown", () -> this.secrets.value(key), new Throws<>( - "Secret with project = %s and key = %s not found" + "404 NOT_FOUND \"Secret with project = %s and key = %s not found\"" .formatted(key.getProject(), key.getName()), ResourceNotFoundException.class ) diff --git a/src/test/java/git/tracehub/pmo/secret/ValidatedSecretsTest.java b/src/test/java/git/tracehub/pmo/secret/ValidatedSecretsTest.java index c669d2a..201e4c1 100644 --- a/src/test/java/git/tracehub/pmo/secret/ValidatedSecretsTest.java +++ b/src/test/java/git/tracehub/pmo/secret/ValidatedSecretsTest.java @@ -144,7 +144,7 @@ void throwsOnDuplicate() { "Exception is not thrown or valid", () -> this.secrets.create(() -> expected), new Throws<>( - "Secret with project = %s and key = %s already exists" + "409 CONFLICT \"Secret with project = %s and key = %s already exists\"" .formatted(expected.getProject(), expected.getKey()), ResourceAlreadyExistsException.class ) @@ -164,7 +164,7 @@ void throwsOnNonExistentSecret() { "Exception is not thrown or valid", () -> this.secrets.update(() -> expected), new Throws<>( - "Secret with project = %s and key = %s not found" + "404 NOT_FOUND \"Secret with project = %s and key = %s not found\"" .formatted(expected.getProject(), expected.getKey()), ResourceNotFoundException.class ) diff --git a/src/test/java/git/tracehub/pmo/ticket/DefaultTicketsTest.java b/src/test/java/git/tracehub/pmo/ticket/DefaultTicketsTest.java index 6efd87b..7d2c6f8 100644 --- a/src/test/java/git/tracehub/pmo/ticket/DefaultTicketsTest.java +++ b/src/test/java/git/tracehub/pmo/ticket/DefaultTicketsTest.java @@ -192,7 +192,8 @@ void throwsOnInvalidJob() throws SQLException { "Exception is not thrown or valid", () -> this.tickets.byJob(job, repo), new Throws<>( - "Ticket with job = %s and repo = %s not found".formatted(job, repo), + "404 NOT_FOUND \"Ticket with job = %s and repo = %s not found\"" + .formatted(job, repo), ResourceNotFoundException.class ) ).affirm(); @@ -208,7 +209,8 @@ void throwsOnInvalidIssueNumber() throws SQLException { "Exception is not thrown or valid", () -> this.tickets.byNumber(number, repo), new Throws<>( - "Ticket with issue = %s and repo = %s not found".formatted(number, repo), + "404 NOT_FOUND \"Ticket with issue = %s and repo = %s not found\"" + .formatted(number, repo), ResourceNotFoundException.class ) ).affirm(); diff --git a/src/test/java/it/web/CreateSecretITCase.java b/src/test/java/it/web/CreateSecretITCase.java index ae2b702..27a2e44 100644 --- a/src/test/java/it/web/CreateSecretITCase.java +++ b/src/test/java/it/web/CreateSecretITCase.java @@ -174,7 +174,7 @@ void throwsOnDuplicate() throws Exception { response.status() ), response.status(), - new IsEqual<>(400) + new IsEqual<>(409) ); MatcherAssert.assertThat( "Message %s isn't correct".formatted(response.body()), @@ -183,7 +183,7 @@ void throwsOnDuplicate() throws Exception { new MutableJson() .with( "message", - "Secret with project = %s and key = %s already exists" + "409 CONFLICT \"Secret with project = %s and key = %s already exists\"" .formatted(project, key) ) .toString() diff --git a/src/test/java/it/web/RetrieveSecretByKeyITCase.java b/src/test/java/it/web/RetrieveSecretByKeyITCase.java index 6c32f69..81c1531 100644 --- a/src/test/java/it/web/RetrieveSecretByKeyITCase.java +++ b/src/test/java/it/web/RetrieveSecretByKeyITCase.java @@ -128,7 +128,7 @@ void throwsOnInvalidKey() throws Exception { new MutableJson() .with( "message", - "Secret with project = %s and key = %s not found" + "404 NOT_FOUND \"Secret with project = %s and key = %s not found\"" .formatted(project, key) ).toString() ) diff --git a/src/test/java/it/web/RetrieveTicketByJobITCase.java b/src/test/java/it/web/RetrieveTicketByJobITCase.java index 73af2c5..a551a4a 100644 --- a/src/test/java/it/web/RetrieveTicketByJobITCase.java +++ b/src/test/java/it/web/RetrieveTicketByJobITCase.java @@ -127,7 +127,7 @@ void throwsOnInvalidJob() throws Exception { new MutableJson() .with( "message", - "Ticket with job = %s and repo = %s not found" + "404 NOT_FOUND \"Ticket with job = %s and repo = %s not found\"" .formatted(job, repo) ).toString() ) diff --git a/src/test/java/it/web/RetrieveTicketByNumberITCase.java b/src/test/java/it/web/RetrieveTicketByNumberITCase.java index 890a6ec..709656b 100644 --- a/src/test/java/it/web/RetrieveTicketByNumberITCase.java +++ b/src/test/java/it/web/RetrieveTicketByNumberITCase.java @@ -127,7 +127,7 @@ void throwsOnInvalidNumber() throws Exception { new MutableJson() .with( "message", - "Ticket with issue = %s and repo = %s not found" + "404 NOT_FOUND \"Ticket with issue = %s and repo = %s not found\"" .formatted(number, repo) ).toString() ) diff --git a/src/test/java/it/web/UpdateSecretITCase.java b/src/test/java/it/web/UpdateSecretITCase.java index c12a617..52f241c 100644 --- a/src/test/java/it/web/UpdateSecretITCase.java +++ b/src/test/java/it/web/UpdateSecretITCase.java @@ -183,7 +183,7 @@ void throwsOnNonExistentSecret() throws Exception { new MutableJson() .with( "message", - "Secret with project = %s and key = %s not found" + "404 NOT_FOUND \"Secret with project = %s and key = %s not found\"" .formatted(project, key) ) .toString()