-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ConstraintViolationException HTTP status should be configurable
- Loading branch information
1 parent
547e0dd
commit 021d5de
Showing
7 changed files
with
141 additions
and
7 deletions.
There are no files selected for viewing
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
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
47 changes: 47 additions & 0 deletions
47
...c/test/java/com/tietoevry/quarkus/resteasy/problem/ConstraintViolationMapperConfigIT.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,47 @@ | ||
package com.tietoevry.quarkus.resteasy.problem; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.quarkus.test.junit.QuarkusTestProfile; | ||
import io.quarkus.test.junit.TestProfile; | ||
import io.restassured.RestAssured; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Map; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.Matchers.hasSize; | ||
|
||
@QuarkusTest | ||
@TestProfile(ConstraintViolationMapperConfigIT.CustomHttpStatus.class) | ||
class ConstraintViolationMapperConfigIT { | ||
|
||
static { | ||
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails(); | ||
} | ||
|
||
@Test | ||
void constraintViolationShouldProvideErrorDetails() { | ||
given() | ||
.body("{\"phraseName\": 10 }") | ||
.contentType(APPLICATION_JSON) | ||
.post("/throw/validation/constraint-violation-exception") | ||
.then() | ||
.statusCode(422) | ||
.body("title", equalTo("Constraint violation")) | ||
.body("status", equalTo(422)) | ||
.body("violations", hasSize(1)); | ||
} | ||
|
||
public static class CustomHttpStatus implements QuarkusTestProfile { | ||
|
||
@Override | ||
public Map<String, String> getConfigOverrides() { | ||
return Map.of( | ||
"quarkus.resteasy.problem.constraint-violation.status", "422", | ||
"quarkus.resteasy.problem.constraint-violation.title", "Constraint violation" | ||
); | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
.../java/com/tietoevry/quarkus/resteasy/problem/ConstraintViolationMapperConfigNativeIT.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,7 @@ | ||
package com.tietoevry.quarkus.resteasy.problem; | ||
|
||
import io.quarkus.test.junit.QuarkusIntegrationTest; | ||
|
||
@QuarkusIntegrationTest | ||
class ConstraintViolationMapperConfigNativeIT extends ConstraintViolationMapperConfigIT { | ||
} |
46 changes: 46 additions & 0 deletions
46
runtime/src/main/java/com/tietoevry/quarkus/resteasy/problem/ProblemRuntimeConfig.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,46 @@ | ||
package com.tietoevry.quarkus.resteasy.problem; | ||
|
||
import io.quarkus.runtime.annotations.ConfigPhase; | ||
import io.quarkus.runtime.annotations.ConfigRoot; | ||
import io.smallrye.config.ConfigMapping; | ||
import io.smallrye.config.WithDefault; | ||
import io.smallrye.config.WithName; | ||
|
||
@ConfigMapping(prefix = "quarkus.resteasy.problem") | ||
@ConfigRoot(phase = ConfigPhase.RUN_TIME) | ||
public interface ProblemRuntimeConfig { | ||
|
||
/** | ||
* Config for ConstraintViolationException mapper | ||
*/ | ||
@WithName("constraint-violation") | ||
ConstraintViolationMapperConfig constraintViolation(); | ||
|
||
interface ConstraintViolationMapperConfig { | ||
static ConstraintViolationMapperConfig defaults() { | ||
return new ConstraintViolationMapperConfig() { | ||
@Override | ||
public int status() { | ||
return 400; | ||
} | ||
|
||
@Override | ||
public String title() { | ||
return "Bad Request"; | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Response status code when ConstraintViolationException is thrown. | ||
*/ | ||
@WithDefault("400") | ||
int status(); | ||
|
||
/** | ||
* Response title when ConstraintViolationException is thrown. | ||
*/ | ||
@WithDefault("Bad Request") | ||
String title(); | ||
} | ||
} |
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
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