Skip to content

Commit 67e03ab

Browse files
committed
Add ProblemExceptionTests
1 parent d7dcfec commit 67e03ab

File tree

1 file changed

+183
-0
lines changed

1 file changed

+183
-0
lines changed
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
package io.github.malczuuu.problem4j.core;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class ProblemExceptionTests {
9+
10+
@Test
11+
void
12+
givenProblemWithAllFields_whenCreatingException_thenMessageWithAllFieldsWithProperFormatting() {
13+
Problem problem =
14+
Problem.builder()
15+
.type("https://example.com/error")
16+
.title("Validation Error")
17+
.detail("The request body is invalid")
18+
.status(400)
19+
.build();
20+
21+
ProblemException exception = new ProblemException(problem);
22+
23+
assertEquals(
24+
"Validation Error: The request body is invalid (code: 400)", exception.getMessage());
25+
}
26+
27+
@Test
28+
void
29+
givenProblemWithTitleAndDetail_whenCreatingException_thenMessageWithTitleAndDetailWithColon() {
30+
Problem problem =
31+
Problem.builder()
32+
.title("Not Found")
33+
.detail("The requested resource does not exist")
34+
.build();
35+
36+
ProblemException exception = new ProblemException(problem);
37+
38+
assertEquals("Not Found: The requested resource does not exist", exception.getMessage());
39+
}
40+
41+
@Test
42+
void givenProblemWithTitleAndStatus_whenCreatingException_thenMessageWithTitleAndStatus() {
43+
Problem problem = Problem.builder().title("Unauthorized").status(401).build();
44+
45+
ProblemException exception = new ProblemException(problem);
46+
47+
assertEquals("Unauthorized (code: 401)", exception.getMessage());
48+
}
49+
50+
@Test
51+
void givenProblemWithDetailAndStatus_whenCreatingException_thenMessageWithDetailAndStatus() {
52+
Problem problem = Problem.builder().detail("Token has expired").status(401).build();
53+
54+
ProblemException exception = new ProblemException(problem);
55+
56+
assertEquals("Unauthorized: Token has expired (code: 401)", exception.getMessage());
57+
}
58+
59+
@Test
60+
void givenProblemWithTitle_whenCreatingException_thenMessageWithTitle() {
61+
Problem problem = Problem.builder().title("Internal Server Error").build();
62+
63+
ProblemException exception = new ProblemException(problem);
64+
65+
assertEquals("Internal Server Error", exception.getMessage());
66+
}
67+
68+
@Test
69+
void givenProblemWithDetail_whenCreatingException_thenMessageWithDetail() {
70+
Problem problem = Problem.builder().detail("Something went wrong").build();
71+
72+
ProblemException exception = new ProblemException(problem);
73+
74+
assertEquals("Something went wrong", exception.getMessage());
75+
}
76+
77+
@Test
78+
void givenProblemWithStatus_whenCreatingException_thenMessageWithStatus() {
79+
Problem problem = Problem.builder().status(500).build();
80+
81+
ProblemException exception = new ProblemException(problem);
82+
83+
assertEquals("Internal Server Error (code: 500)", exception.getMessage());
84+
}
85+
86+
@Test
87+
void givenProblemWithNullTitleAndDetail_whenCreatingException_thenMessageIsNull() {
88+
Problem problem = new Problem(null, null, 0, null, null, new Object[0]);
89+
90+
ProblemException exception = new ProblemException(problem);
91+
92+
assertNull(exception.getMessage());
93+
}
94+
95+
@Test
96+
void givenProblemWithEmptyStringFields_whenCreatingException_thenMessageIsNull() {
97+
Problem problem = Problem.builder().title("").detail("").build();
98+
99+
ProblemException exception = new ProblemException(problem);
100+
101+
assertNull(exception.getMessage());
102+
}
103+
104+
@Test
105+
void givenProblemWithZeroStatus_whenCreatingException_thenStatusNotIncludedInMessage() {
106+
Problem problem =
107+
Problem.builder().title("Error").detail("Something happened").status(0).build();
108+
109+
ProblemException exception = new ProblemException(problem);
110+
111+
assertEquals("Error: Something happened", exception.getMessage());
112+
}
113+
114+
@Test
115+
void givenProblemWithNegativeStatus_whenCreatingException_thenStatusIncludedInMessage() {
116+
Problem problem = Problem.builder().title("Custom Error").status(-1).build();
117+
118+
ProblemException exception = new ProblemException(problem);
119+
120+
assertEquals("Custom Error (code: -1)", exception.getMessage());
121+
}
122+
123+
@Test
124+
void givenProblemWithLargeStatus_whenCreatingException_thenStatusIncludedInMessage() {
125+
Problem problem = Problem.builder().detail("Custom protocol error").status(999).build();
126+
127+
ProblemException exception = new ProblemException(problem);
128+
129+
assertEquals("Custom protocol error (code: 999)", exception.getMessage());
130+
}
131+
132+
@Test
133+
void givenProblemWithWhitespaceTitle_whenCreatingException_thenTitleIncludedInMessage() {
134+
Problem problem = Problem.builder().title(" ").detail("Error occurred").build();
135+
136+
ProblemException exception = new ProblemException(problem);
137+
138+
assertEquals(" : Error occurred", exception.getMessage());
139+
}
140+
141+
@Test
142+
void givenProblemWithSpecialCharacters_whenCreatingException_thenSpecialCharactersPreserved() {
143+
Problem problem =
144+
Problem.builder()
145+
.title("Validation Error: Required Fields Missing")
146+
.detail("Fields 'name' & 'email' are required (check docs)")
147+
.status(422)
148+
.build();
149+
150+
ProblemException exception = new ProblemException(problem);
151+
152+
assertEquals(
153+
"Validation Error: Required Fields Missing: Fields 'name' & 'email' are required (check docs) (code: 422)",
154+
exception.getMessage());
155+
}
156+
157+
@Test
158+
void givenProblemWithUnknownStatusCode_whenCreatingException_thenNoTitleResolutionOccurs() {
159+
Problem problem = new Problem(null, null, 999, "Custom error", null, new Object[0]);
160+
161+
ProblemException exception = new ProblemException(problem);
162+
163+
assertEquals("Custom error (code: 999)", exception.getMessage());
164+
}
165+
166+
@Test
167+
void givenProblemWithUnknownStatusCode_whenCreatingException_thenStatusShown() {
168+
Problem problem = new Problem(null, null, 999, null, null, new Object[0]);
169+
170+
ProblemException exception = new ProblemException(problem);
171+
172+
assertEquals("(code: 999)", exception.getMessage());
173+
}
174+
175+
@Test
176+
void givenProblemWithExplicitNullTitle_whenCreatingException_thenNoTitleInMessage() {
177+
Problem problem = new Problem(null, null, 404, "Resource not found", null, new Object[0]);
178+
179+
ProblemException exception = new ProblemException(problem);
180+
181+
assertEquals("Resource not found (code: 404)", exception.getMessage());
182+
}
183+
}

0 commit comments

Comments
 (0)