Skip to content

Commit 69e6f36

Browse files
committed
Add ProblemTests
1 parent 67e03ab commit 69e6f36

File tree

1 file changed

+297
-0
lines changed

1 file changed

+297
-0
lines changed
Lines changed: 297 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
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.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.net.URI;
8+
import java.util.Map;
9+
import org.junit.jupiter.api.Test;
10+
11+
class ProblemTests {
12+
13+
@Test
14+
void givenEvenNumberOfArguments_whenCreatingProblem_thenExtensionsBuiltCorrectly() {
15+
Problem problem =
16+
new Problem(
17+
URI.create("https://example.com/error"),
18+
"Test Error",
19+
400,
20+
"Test detail",
21+
URI.create("https://example.com/instance"),
22+
"key1",
23+
"value1",
24+
"key2",
25+
"value2");
26+
27+
assertEquals(2, problem.getExtensions().size());
28+
assertEquals("value1", problem.getExtensionValue("key1"));
29+
assertEquals("value2", problem.getExtensionValue("key2"));
30+
}
31+
32+
@Test
33+
void givenOddNumberOfArguments_whenCreatingProblem_thenFailWithException() {
34+
IllegalArgumentException e =
35+
assertThrows(
36+
IllegalArgumentException.class,
37+
() -> {
38+
new Problem(
39+
URI.create("https://example.com/error"),
40+
"Test Error",
41+
400,
42+
"Test detail",
43+
URI.create("https://example.com/instance"),
44+
"key1",
45+
"value1",
46+
"key2");
47+
});
48+
49+
assertEquals("extensions arguments must be key-value pairs", e.getMessage());
50+
}
51+
52+
@Test
53+
void givenEmptyArgumentsArray_whenCreatingProblem_thenNoExtensions() {
54+
Problem problem =
55+
new Problem(
56+
URI.create("https://example.com/error"),
57+
"Test Error",
58+
400,
59+
"Test detail",
60+
URI.create("https://example.com/instance"));
61+
62+
assertTrue(problem.getExtensions().isEmpty());
63+
}
64+
65+
@Test
66+
void givenNullKeyInArguments_whenCreatingProblem_thenNullPointerExceptionThrown() {
67+
assertThrows(
68+
NullPointerException.class,
69+
() -> {
70+
new Problem(
71+
URI.create("https://example.com/error"),
72+
"Test Error",
73+
400,
74+
"Test detail",
75+
URI.create("https://example.com/instance"),
76+
null,
77+
"value1",
78+
"key2",
79+
"value2");
80+
});
81+
}
82+
83+
@Test
84+
void givenNullValueInArguments_whenCreatingProblem_thenNullPointerExceptionThrown() {
85+
assertThrows(
86+
NullPointerException.class,
87+
() -> {
88+
new Problem(
89+
URI.create("https://example.com/error"),
90+
"Test Error",
91+
400,
92+
"Test detail",
93+
URI.create("https://example.com/instance"),
94+
"key1",
95+
null,
96+
"key2",
97+
"value2");
98+
});
99+
}
100+
101+
@Test
102+
void givenNumericKeys_whenCreatingProblem_thenKeysConvertedToStrings() {
103+
Problem problem =
104+
new Problem(
105+
URI.create("https://example.com/error"),
106+
"Test Error",
107+
400,
108+
"Test detail",
109+
URI.create("https://example.com/instance"),
110+
123,
111+
"numericKey",
112+
456.78,
113+
"floatKey");
114+
115+
assertEquals(2, problem.getExtensions().size());
116+
assertEquals("numericKey", problem.getExtensionValue("123"));
117+
assertEquals("floatKey", problem.getExtensionValue("456.78"));
118+
}
119+
120+
@Test
121+
void givenComplexObjectValues_whenCreatingProblem_thenObjectsStoredAsValues() {
122+
Object complexObject =
123+
new Object() {
124+
@Override
125+
public String toString() {
126+
return "complex";
127+
}
128+
};
129+
130+
Problem problem =
131+
new Problem(
132+
URI.create("https://example.com/error"),
133+
"Test Error",
134+
400,
135+
"Test detail",
136+
URI.create("https://example.com/instance"),
137+
"objectKey",
138+
complexObject,
139+
"booleanKey",
140+
true);
141+
142+
assertEquals(2, problem.getExtensions().size());
143+
assertEquals(complexObject, problem.getExtensionValue("objectKey"));
144+
assertEquals(true, problem.getExtensionValue("booleanKey"));
145+
}
146+
147+
@Test
148+
void givenLargeNumberOfArguments_whenCreatingProblem_thenAllPairsProcessed() {
149+
Problem problem =
150+
new Problem(
151+
URI.create("https://example.com/error"),
152+
"Test Error",
153+
400,
154+
"Test detail",
155+
URI.create("https://example.com/instance"),
156+
"k1",
157+
"v1",
158+
"k2",
159+
"v2",
160+
"k3",
161+
"v3",
162+
"k4",
163+
"v4",
164+
"k5",
165+
"v5");
166+
167+
assertEquals(5, problem.getExtensions().size());
168+
assertEquals("v1", problem.getExtensionValue("k1"));
169+
assertEquals("v2", problem.getExtensionValue("k2"));
170+
assertEquals("v3", problem.getExtensionValue("k3"));
171+
assertEquals("v4", problem.getExtensionValue("k4"));
172+
assertEquals("v5", problem.getExtensionValue("k5"));
173+
}
174+
175+
@Test
176+
void givenDuplicateKeys_whenCreatingProblem_thenLastValueWins() {
177+
Problem problem =
178+
new Problem(
179+
URI.create("https://example.com/error"),
180+
"Test Error",
181+
400,
182+
"Test detail",
183+
URI.create("https://example.com/instance"),
184+
"duplicateKey",
185+
"firstValue",
186+
"duplicateKey",
187+
"secondValue");
188+
189+
assertEquals(1, problem.getExtensions().size());
190+
assertEquals("secondValue", problem.getExtensionValue("duplicateKey"));
191+
}
192+
193+
@Test
194+
void givenExtensionArrayConstructor_whenCreatingProblem_thenExtensionsBuiltFromArray() {
195+
Problem.Extension ext1 = Problem.extension("key1", "value1");
196+
Problem.Extension ext2 = Problem.extension("key2", "value2");
197+
198+
Problem problem =
199+
new Problem(
200+
URI.create("https://example.com/error"),
201+
"Test Error",
202+
400,
203+
"Test detail",
204+
URI.create("https://example.com/instance"),
205+
ext1,
206+
ext2);
207+
208+
assertEquals(2, problem.getExtensions().size());
209+
assertEquals("value1", problem.getExtensionValue("key1"));
210+
assertEquals("value2", problem.getExtensionValue("key2"));
211+
}
212+
213+
@Test
214+
void givenSetExtensionConstructor_whenCreatingProblem_thenExtensionsBuiltFromSet() {
215+
java.util.Set<Problem.Extension> extensions =
216+
java.util.Set.of(Problem.extension("key1", "value1"), Problem.extension("key2", "value2"));
217+
218+
Problem problem =
219+
new Problem(
220+
URI.create("https://example.com/error"),
221+
"Test Error",
222+
400,
223+
"Test detail",
224+
URI.create("https://example.com/instance"),
225+
extensions);
226+
227+
assertEquals(2, problem.getExtensions().size());
228+
assertEquals("value1", problem.getExtensionValue("key1"));
229+
assertEquals("value2", problem.getExtensionValue("key2"));
230+
}
231+
232+
@Test
233+
void givenEmptyExtensionArray_whenCreatingProblem_thenNoExtensions() {
234+
Problem.Extension[] emptyExtensions = new Problem.Extension[0];
235+
236+
Problem problem =
237+
new Problem(
238+
URI.create("https://example.com/error"),
239+
"Test Error",
240+
400,
241+
"Test detail",
242+
URI.create("https://example.com/instance"),
243+
emptyExtensions);
244+
245+
assertTrue(problem.getExtensions().isEmpty());
246+
}
247+
248+
@Test
249+
void givenEmptyExtensionSet_whenCreatingProblem_thenNoExtensions() {
250+
java.util.Set<Problem.Extension> emptyExtensions = java.util.Set.of();
251+
252+
Problem problem =
253+
new Problem(
254+
URI.create("https://example.com/error"),
255+
"Test Error",
256+
400,
257+
"Test detail",
258+
URI.create("https://example.com/instance"),
259+
emptyExtensions);
260+
261+
assertTrue(problem.getExtensions().isEmpty());
262+
}
263+
264+
@Test
265+
void givenExtensionsWithNullValues_whenCreatingProblem_thenNullPointerExceptionThrown() {
266+
Problem.Extension extensionWithNullValue = Problem.extension("key1", null);
267+
268+
assertThrows(
269+
NullPointerException.class,
270+
() ->
271+
new Problem(
272+
URI.create("https://example.com/error"),
273+
"Test Error",
274+
400,
275+
"Test detail",
276+
URI.create("https://example.com/instance"),
277+
extensionWithNullValue));
278+
}
279+
280+
@Test
281+
void givenMapWithNullValues_whenCreatingProblem_thenNullPointerExceptionThrown() {
282+
Map<String, Object> mapWithNullValue = new java.util.HashMap<>();
283+
mapWithNullValue.put("key1", "value1");
284+
mapWithNullValue.put("key2", null);
285+
286+
assertThrows(
287+
NullPointerException.class,
288+
() ->
289+
new Problem(
290+
URI.create("https://example.com/error"),
291+
"Test Error",
292+
400,
293+
"Test detail",
294+
URI.create("https://example.com/instance"),
295+
mapWithNullValue));
296+
}
297+
}

0 commit comments

Comments
 (0)