Skip to content

Commit 556e425

Browse files
Merge branch 'release-v1.2.x' into release-v2.2.x
2 parents 0782c53 + 01d2373 commit 556e425

19 files changed

Lines changed: 1359 additions & 0 deletions
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (c) 2025-2026 The Problem4J Authors
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in all
11+
* copies or substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
* SOFTWARE.
20+
*/
21+
22+
package io.github.problem4j.spring.web.resolver;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
import static org.mockito.Mockito.mock;
26+
import static org.mockito.Mockito.when;
27+
28+
import io.github.problem4j.core.Problem;
29+
import io.github.problem4j.core.ProblemContext;
30+
import io.github.problem4j.spring.web.parameter.Violation;
31+
import jakarta.validation.ConstraintViolation;
32+
import jakarta.validation.ConstraintViolationException;
33+
import jakarta.validation.Path;
34+
import java.util.List;
35+
import java.util.Set;
36+
import org.junit.jupiter.api.BeforeEach;
37+
import org.junit.jupiter.api.Test;
38+
import org.springframework.http.HttpHeaders;
39+
import org.springframework.http.HttpStatus;
40+
41+
class ConstraintViolationProblemResolverTest {
42+
43+
private ConstraintViolationProblemResolver resolver;
44+
45+
@BeforeEach
46+
void beforeEach() {
47+
resolver = new ConstraintViolationProblemResolver();
48+
}
49+
50+
@Test
51+
void givenDefaultConstructor_whenGetExceptionClass_thenReturnsConstraintViolationException() {
52+
assertThat(resolver.getExceptionClass()).isEqualTo(ConstraintViolationException.class);
53+
}
54+
55+
@Test
56+
void givenExceptionWithNoViolations_whenResolve_thenReturnsBadRequestWithEmptyErrors() {
57+
ConstraintViolationException ex = new ConstraintViolationException(Set.of());
58+
59+
Problem problem =
60+
resolver
61+
.resolveBuilder(ProblemContext.create(), ex, new HttpHeaders(), HttpStatus.BAD_REQUEST)
62+
.build();
63+
64+
assertThat(problem.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value());
65+
assertThat(problem.getDetail()).isEqualTo("Validation failed");
66+
assertThat(problem.getExtensionMembers()).containsEntry("errors", List.of());
67+
}
68+
69+
@Test
70+
@SuppressWarnings("unchecked")
71+
void givenExceptionWithViolation_whenResolve_thenIncludesViolationInErrors() {
72+
Path.Node node = mock(Path.Node.class);
73+
when(node.getName()).thenReturn("email");
74+
Path path = mock(Path.class);
75+
when(path.iterator()).thenReturn(List.of(node).iterator());
76+
ConstraintViolation<?> violation = mock(ConstraintViolation.class);
77+
when(violation.getPropertyPath()).thenReturn(path);
78+
when(violation.getMessage()).thenReturn("must not be blank");
79+
80+
ConstraintViolationException ex = new ConstraintViolationException(Set.of(violation));
81+
82+
Problem problem =
83+
resolver
84+
.resolveBuilder(ProblemContext.create(), ex, new HttpHeaders(), HttpStatus.BAD_REQUEST)
85+
.build();
86+
87+
assertThat(problem.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value());
88+
assertThat(problem.getDetail()).isEqualTo("Validation failed");
89+
assertThat((List<Violation>) problem.getExtensionValue("errors"))
90+
.containsExactly(new Violation("email", "must not be blank"));
91+
}
92+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (c) 2025-2026 The Problem4J Authors
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in all
11+
* copies or substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
* SOFTWARE.
20+
*/
21+
22+
package io.github.problem4j.spring.web.resolver;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
import io.github.problem4j.core.Problem;
27+
import io.github.problem4j.core.ProblemContext;
28+
import org.junit.jupiter.api.BeforeEach;
29+
import org.junit.jupiter.api.Test;
30+
import org.springframework.http.HttpHeaders;
31+
import org.springframework.http.HttpStatus;
32+
import org.springframework.http.ProblemDetail;
33+
import org.springframework.web.ErrorResponseException;
34+
35+
class ErrorResponseProblemResolverTest {
36+
37+
private ErrorResponseProblemResolver resolver;
38+
39+
@BeforeEach
40+
void beforeEach() {
41+
resolver = new ErrorResponseProblemResolver();
42+
}
43+
44+
@Test
45+
void givenDefaultConstructor_whenGetExceptionClass_thenReturnsErrorResponseException() {
46+
assertThat(resolver.getExceptionClass()).isEqualTo(ErrorResponseException.class);
47+
}
48+
49+
@Test
50+
void givenErrorResponseException_whenResolve_thenCopiesStatusFromException() {
51+
ErrorResponseException ex = new ErrorResponseException(HttpStatus.CONFLICT);
52+
53+
Problem problem =
54+
resolver
55+
.resolveBuilder(ProblemContext.create(), ex, new HttpHeaders(), HttpStatus.OK)
56+
.build();
57+
58+
assertThat(problem.getStatus()).isEqualTo(HttpStatus.CONFLICT.value());
59+
}
60+
61+
@Test
62+
void givenErrorResponseExceptionWithDetail_whenResolve_thenCopiesDetailFromBody() {
63+
ProblemDetail body = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "invalid input");
64+
ErrorResponseException ex = new ErrorResponseException(HttpStatus.BAD_REQUEST, body, null);
65+
66+
Problem problem =
67+
resolver
68+
.resolveBuilder(
69+
ProblemContext.create(), ex, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR)
70+
.build();
71+
72+
assertThat(problem.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value());
73+
assertThat(problem.getDetail()).isEqualTo("invalid input");
74+
}
75+
76+
@Test
77+
void givenErrorResponseException_whenResolve_thenIgnoresPassedStatus() {
78+
ErrorResponseException ex = new ErrorResponseException(HttpStatus.FORBIDDEN);
79+
80+
Problem problem =
81+
resolver
82+
.resolveBuilder(
83+
ProblemContext.create(), ex, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR)
84+
.build();
85+
86+
assertThat(problem.getStatus()).isEqualTo(HttpStatus.FORBIDDEN.value());
87+
}
88+
}

problem4j-spring-web/src/test/java/io/github/problem4j/spring/web/resolver/HandlerMethodValidationProblemResolverTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
package io.github.problem4j.spring.web.resolver;
2323

24+
import static org.assertj.core.api.Assertions.assertThat;
2425
import static org.junit.jupiter.api.Assertions.assertEquals;
2526
import static org.mockito.Mockito.mock;
2627

@@ -42,6 +43,12 @@ void beforeEach() {
4243
handlerMethodValidationProblemResolver = new HandlerMethodValidationProblemResolver();
4344
}
4445

46+
@Test
47+
void givenDefaultConstructor_whenGetExceptionClass_thenReturnsHandlerMethodValidationException() {
48+
assertThat(handlerMethodValidationProblemResolver.getExceptionClass())
49+
.isEqualTo(HandlerMethodValidationException.class);
50+
}
51+
4552
@Test
4653
void givenHandlerMethodValidationException_shouldGenerateProblem() {
4754
MethodValidationResult mockMethodValidationResult = mock(MethodValidationResult.class);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2025-2026 The Problem4J Authors
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in all
11+
* copies or substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
* SOFTWARE.
20+
*/
21+
22+
package io.github.problem4j.spring.web.resolver;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
import io.github.problem4j.core.Problem;
27+
import io.github.problem4j.core.ProblemContext;
28+
import java.util.List;
29+
import org.junit.jupiter.api.BeforeEach;
30+
import org.junit.jupiter.api.Test;
31+
import org.springframework.http.HttpHeaders;
32+
import org.springframework.http.HttpStatus;
33+
import org.springframework.web.HttpMediaTypeNotAcceptableException;
34+
35+
class HttpMediaTypeNotAcceptableProblemResolverTest {
36+
37+
private HttpMediaTypeNotAcceptableProblemResolver resolver;
38+
39+
@BeforeEach
40+
void beforeEach() {
41+
resolver = new HttpMediaTypeNotAcceptableProblemResolver();
42+
}
43+
44+
@Test
45+
void
46+
givenDefaultConstructor_whenGetExceptionClass_thenReturnsHttpMediaTypeNotAcceptableException() {
47+
assertThat(resolver.getExceptionClass()).isEqualTo(HttpMediaTypeNotAcceptableException.class);
48+
}
49+
50+
@Test
51+
void givenHttpMediaTypeNotAcceptableException_whenResolve_thenReturnsNotAcceptableProblem() {
52+
HttpMediaTypeNotAcceptableException ex = new HttpMediaTypeNotAcceptableException(List.of());
53+
54+
Problem problem =
55+
resolver
56+
.resolveBuilder(
57+
ProblemContext.create(), ex, new HttpHeaders(), HttpStatus.NOT_ACCEPTABLE)
58+
.build();
59+
60+
assertThat(problem.getStatus()).isEqualTo(HttpStatus.NOT_ACCEPTABLE.value());
61+
}
62+
63+
@Test
64+
void givenHttpMediaTypeNotAcceptableException_whenResolve_thenIgnoresPassedStatus() {
65+
HttpMediaTypeNotAcceptableException ex = new HttpMediaTypeNotAcceptableException(List.of());
66+
67+
Problem problem =
68+
resolver
69+
.resolveBuilder(
70+
ProblemContext.create(), ex, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR)
71+
.build();
72+
73+
assertThat(problem.getStatus()).isEqualTo(HttpStatus.NOT_ACCEPTABLE.value());
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2025-2026 The Problem4J Authors
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in all
11+
* copies or substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
* SOFTWARE.
20+
*/
21+
22+
package io.github.problem4j.spring.web.resolver;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
import io.github.problem4j.core.Problem;
27+
import io.github.problem4j.core.ProblemContext;
28+
import org.junit.jupiter.api.BeforeEach;
29+
import org.junit.jupiter.api.Test;
30+
import org.springframework.http.HttpHeaders;
31+
import org.springframework.http.HttpStatus;
32+
import org.springframework.web.HttpMediaTypeNotSupportedException;
33+
34+
class HttpMediaTypeNotSupportedProblemResolverTest {
35+
36+
private HttpMediaTypeNotSupportedProblemResolver resolver;
37+
38+
@BeforeEach
39+
void beforeEach() {
40+
resolver = new HttpMediaTypeNotSupportedProblemResolver();
41+
}
42+
43+
@Test
44+
void
45+
givenDefaultConstructor_whenGetExceptionClass_thenReturnsHttpMediaTypeNotSupportedException() {
46+
assertThat(resolver.getExceptionClass()).isEqualTo(HttpMediaTypeNotSupportedException.class);
47+
}
48+
49+
@Test
50+
void
51+
givenHttpMediaTypeNotSupportedException_whenResolve_thenReturnsUnsupportedMediaTypeProblem() {
52+
HttpMediaTypeNotSupportedException ex = new HttpMediaTypeNotSupportedException("msg");
53+
54+
Problem problem =
55+
resolver
56+
.resolveBuilder(
57+
ProblemContext.create(), ex, new HttpHeaders(), HttpStatus.UNSUPPORTED_MEDIA_TYPE)
58+
.build();
59+
60+
assertThat(problem.getStatus()).isEqualTo(HttpStatus.UNSUPPORTED_MEDIA_TYPE.value());
61+
}
62+
63+
@Test
64+
void givenHttpMediaTypeNotSupportedException_whenResolve_thenIgnoresPassedStatus() {
65+
HttpMediaTypeNotSupportedException ex = new HttpMediaTypeNotSupportedException("msg");
66+
67+
Problem problem =
68+
resolver
69+
.resolveBuilder(
70+
ProblemContext.create(), ex, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR)
71+
.build();
72+
73+
assertThat(problem.getStatus()).isEqualTo(HttpStatus.UNSUPPORTED_MEDIA_TYPE.value());
74+
}
75+
}

0 commit comments

Comments
 (0)