Skip to content

Commit a364943

Browse files
Add more tests
1 parent 236ae89 commit a364943

2 files changed

Lines changed: 327 additions & 0 deletions

File tree

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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.jackson2;
23+
24+
import static org.junit.jupiter.api.Assertions.assertEquals;
25+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
26+
import static org.junit.jupiter.api.Assertions.assertNull;
27+
import static org.junit.jupiter.api.Assertions.assertTrue;
28+
29+
import com.fasterxml.jackson.core.JsonProcessingException;
30+
import com.fasterxml.jackson.databind.json.JsonMapper;
31+
import io.github.problem4j.core.Problem;
32+
import java.util.List;
33+
import org.junit.jupiter.api.Test;
34+
35+
class ProblemBridgeTest {
36+
37+
@Test
38+
void givenTypeAsNumber_whenDeserializing_thenTypeShouldBeBlank() throws JsonProcessingException {
39+
JsonMapper mapper = JsonMapper.builder().addModule(new ProblemModule()).build();
40+
41+
String json = "{\"type\":123,\"title\":\"test\",\"status\":400}";
42+
43+
Problem problem = mapper.readValue(json, Problem.class);
44+
45+
assertEquals(Problem.BLANK_TYPE, problem.getType());
46+
assertEquals("test", problem.getTitle());
47+
assertEquals(400, problem.getStatus());
48+
}
49+
50+
@Test
51+
void givenInstanceAsNumber_whenDeserializing_thenInstanceShouldBeNull()
52+
throws JsonProcessingException {
53+
JsonMapper mapper = JsonMapper.builder().addModule(new ProblemModule()).build();
54+
55+
String json = "{\"title\":\"test\",\"status\":400,\"instance\":123}";
56+
57+
Problem problem = mapper.readValue(json, Problem.class);
58+
59+
assertNull(problem.getInstance());
60+
}
61+
62+
@Test
63+
void givenStatusAsNull_whenDeserializing_thenStatusShouldBeZero() throws JsonProcessingException {
64+
JsonMapper mapper = JsonMapper.builder().addModule(new ProblemModule()).build();
65+
66+
String json = "{\"title\":\"test\",\"status\":null}";
67+
68+
Problem problem = mapper.readValue(json, Problem.class);
69+
70+
assertEquals(0, problem.getStatus());
71+
}
72+
73+
@Test
74+
void givenExtensionWithNullName_whenSetting_thenExtensionShouldBeIgnored() {
75+
ProblemBridge bridge = new ProblemBridge();
76+
77+
bridge.setExtension(null, "value");
78+
79+
assertTrue(bridge.getExtensions().isEmpty());
80+
}
81+
82+
@Test
83+
void givenExtensionWithNullValue_whenSetting_thenExtensionShouldBeIgnored() {
84+
ProblemBridge bridge = new ProblemBridge();
85+
86+
bridge.setExtension("key", null);
87+
88+
assertTrue(bridge.getExtensions().isEmpty());
89+
}
90+
91+
@Test
92+
@SuppressWarnings("unchecked")
93+
void givenDuplicateExtension_whenDeserializing_thenValuesShouldBeMergedIntoList()
94+
throws JsonProcessingException {
95+
JsonMapper mapper = JsonMapper.builder().addModule(new ProblemModule()).build();
96+
97+
String json =
98+
"{\"title\":\"test\",\"status\":400,"
99+
+ "\"custom\":\"first\",\"custom\":\"second\",\"custom\":\"third\"}";
100+
101+
Problem problem = mapper.readValue(json, Problem.class);
102+
103+
Object custom = problem.getExtensionValue("custom");
104+
assertInstanceOf(List.class, custom);
105+
assertEquals(List.of("first", "second", "third"), custom);
106+
}
107+
108+
@Test
109+
void givenTypeAsBoolean_whenDeserializing_thenTypeShouldBeBlank() throws JsonProcessingException {
110+
JsonMapper mapper = JsonMapper.builder().addModule(new ProblemModule()).build();
111+
112+
String json = "{\"type\":true,\"title\":\"test\",\"status\":400}";
113+
114+
Problem problem = mapper.readValue(json, Problem.class);
115+
116+
assertEquals(Problem.BLANK_TYPE, problem.getType());
117+
}
118+
119+
@Test
120+
void givenInstanceAsBoolean_whenDeserializing_thenInstanceShouldBeNull()
121+
throws JsonProcessingException {
122+
JsonMapper mapper = JsonMapper.builder().addModule(new ProblemModule()).build();
123+
124+
String json = "{\"title\":\"test\",\"status\":400,\"instance\":true}";
125+
126+
Problem problem = mapper.readValue(json, Problem.class);
127+
128+
assertNull(problem.getInstance());
129+
}
130+
131+
@Test
132+
void givenTypeAsObject_whenDeserializing_thenTypeShouldBeBlank() throws JsonProcessingException {
133+
JsonMapper mapper = JsonMapper.builder().addModule(new ProblemModule()).build();
134+
135+
String json = "{\"type\":{\"nested\":\"value\"},\"title\":\"test\",\"status\":400}";
136+
137+
Problem problem = mapper.readValue(json, Problem.class);
138+
139+
assertEquals(Problem.BLANK_TYPE, problem.getType());
140+
}
141+
142+
@Test
143+
void givenInstanceAsArray_whenDeserializing_thenInstanceShouldBeNull()
144+
throws JsonProcessingException {
145+
JsonMapper mapper = JsonMapper.builder().addModule(new ProblemModule()).build();
146+
147+
String json = "{\"title\":\"test\",\"status\":400,\"instance\":[1,2,3]}";
148+
149+
Problem problem = mapper.readValue(json, Problem.class);
150+
151+
assertNull(problem.getInstance());
152+
}
153+
154+
@Test
155+
void givenReservedNameAsExtension_whenSetting_thenExtensionShouldBeIgnored() {
156+
ProblemBridge bridge = new ProblemBridge();
157+
158+
bridge.setExtension("type", "someValue");
159+
bridge.setExtension("title", "someValue");
160+
bridge.setExtension("status", "someValue");
161+
bridge.setExtension("detail", "someValue");
162+
bridge.setExtension("instance", "someValue");
163+
164+
assertTrue(bridge.getExtensions().isEmpty());
165+
}
166+
}
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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.jackson3;
23+
24+
import static org.junit.jupiter.api.Assertions.assertEquals;
25+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
26+
import static org.junit.jupiter.api.Assertions.assertNull;
27+
import static org.junit.jupiter.api.Assertions.assertTrue;
28+
29+
import io.github.problem4j.core.Problem;
30+
import java.util.List;
31+
import org.junit.jupiter.api.Test;
32+
import tools.jackson.databind.json.JsonMapper;
33+
34+
class ProblemBridgeTest {
35+
36+
@Test
37+
void givenTypeAsNumber_whenDeserializing_thenTypeShouldBeBlank() {
38+
JsonMapper mapper = JsonMapper.builder().addModule(new ProblemJacksonModule()).build();
39+
40+
String json = "{\"type\":123,\"title\":\"test\",\"status\":400}";
41+
42+
Problem problem = mapper.readValue(json, Problem.class);
43+
44+
assertEquals(Problem.BLANK_TYPE, problem.getType());
45+
assertEquals("test", problem.getTitle());
46+
assertEquals(400, problem.getStatus());
47+
}
48+
49+
@Test
50+
void givenInstanceAsNumber_whenDeserializing_thenInstanceShouldBeNull() {
51+
JsonMapper mapper = JsonMapper.builder().addModule(new ProblemJacksonModule()).build();
52+
53+
String json = "{\"title\":\"test\",\"status\":400,\"instance\":123}";
54+
55+
Problem problem = mapper.readValue(json, Problem.class);
56+
57+
assertNull(problem.getInstance());
58+
}
59+
60+
@Test
61+
void givenStatusAsNull_whenDeserializing_thenStatusShouldBeZero() {
62+
JsonMapper mapper = JsonMapper.builder().addModule(new ProblemJacksonModule()).build();
63+
64+
String json = "{\"title\":\"test\",\"status\":null}";
65+
66+
Problem problem = mapper.readValue(json, Problem.class);
67+
68+
assertEquals(0, problem.getStatus());
69+
}
70+
71+
@Test
72+
void givenExtensionWithNullName_whenSetting_thenExtensionShouldBeIgnored() {
73+
ProblemBridge bridge = new ProblemBridge();
74+
75+
bridge.setExtension(null, "value");
76+
77+
assertTrue(bridge.getExtensions().isEmpty());
78+
}
79+
80+
@Test
81+
void givenExtensionWithNullValue_whenSetting_thenExtensionShouldBeIgnored() {
82+
ProblemBridge bridge = new ProblemBridge();
83+
84+
bridge.setExtension("key", null);
85+
86+
assertTrue(bridge.getExtensions().isEmpty());
87+
}
88+
89+
@Test
90+
@SuppressWarnings("unchecked")
91+
void givenDuplicateExtension_whenDeserializing_thenValuesShouldBeMergedIntoList() {
92+
JsonMapper mapper = JsonMapper.builder().addModule(new ProblemJacksonModule()).build();
93+
94+
String json =
95+
"{\"title\":\"test\",\"status\":400,"
96+
+ "\"custom\":\"first\",\"custom\":\"second\",\"custom\":\"third\"}";
97+
98+
Problem problem = mapper.readValue(json, Problem.class);
99+
100+
Object custom = problem.getExtensionValue("custom");
101+
assertInstanceOf(List.class, custom);
102+
assertEquals(List.of("first", "second", "third"), (List<String>) custom);
103+
}
104+
105+
@Test
106+
void givenTypeAsBoolean_whenDeserializing_thenTypeShouldBeBlank() {
107+
JsonMapper mapper = JsonMapper.builder().addModule(new ProblemJacksonModule()).build();
108+
109+
String json = "{\"type\":true,\"title\":\"test\",\"status\":400}";
110+
111+
Problem problem = mapper.readValue(json, Problem.class);
112+
113+
assertEquals(Problem.BLANK_TYPE, problem.getType());
114+
}
115+
116+
@Test
117+
void givenInstanceAsBoolean_whenDeserializing_thenInstanceShouldBeNull() {
118+
JsonMapper mapper = JsonMapper.builder().addModule(new ProblemJacksonModule()).build();
119+
120+
String json = "{\"title\":\"test\",\"status\":400,\"instance\":true}";
121+
122+
Problem problem = mapper.readValue(json, Problem.class);
123+
124+
assertNull(problem.getInstance());
125+
}
126+
127+
@Test
128+
void givenTypeAsObject_whenDeserializing_thenTypeShouldBeBlank() {
129+
JsonMapper mapper = JsonMapper.builder().addModule(new ProblemJacksonModule()).build();
130+
131+
String json = "{\"type\":{\"nested\":\"value\"},\"title\":\"test\",\"status\":400}";
132+
133+
Problem problem = mapper.readValue(json, Problem.class);
134+
135+
assertEquals(Problem.BLANK_TYPE, problem.getType());
136+
}
137+
138+
@Test
139+
void givenInstanceAsArray_whenDeserializing_thenInstanceShouldBeNull() {
140+
JsonMapper mapper = JsonMapper.builder().addModule(new ProblemJacksonModule()).build();
141+
142+
String json = "{\"title\":\"test\",\"status\":400,\"instance\":[1,2,3]}";
143+
144+
Problem problem = mapper.readValue(json, Problem.class);
145+
146+
assertNull(problem.getInstance());
147+
}
148+
149+
@Test
150+
void givenReservedNameAsExtension_whenSetting_thenExtensionShouldBeIgnored() {
151+
ProblemBridge bridge = new ProblemBridge();
152+
153+
bridge.setExtension("type", "someValue");
154+
bridge.setExtension("title", "someValue");
155+
bridge.setExtension("status", "someValue");
156+
bridge.setExtension("detail", "someValue");
157+
bridge.setExtension("instance", "someValue");
158+
159+
assertTrue(bridge.getExtensions().isEmpty());
160+
}
161+
}

0 commit comments

Comments
 (0)