Skip to content

Commit 330f887

Browse files
committed
Update unit tests
1 parent 8e7d086 commit 330f887

File tree

4 files changed

+181
-16
lines changed

4 files changed

+181
-16
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.github.malczuuu.problem4j.core;
2+
3+
import java.util.HashMap;
4+
5+
final class MapUtils {
6+
7+
/**
8+
* Creates a map from the given arguments in similar style to Java 9's Map.of().
9+
*
10+
* @param args an even number of arguments representing key-value pairs
11+
* @return a HashMap containing the provided key-value pairs
12+
*/
13+
static HashMap<String, Object> mapOf(Object... args) {
14+
if (args.length % 2 != 0) {
15+
throw new IllegalArgumentException("args length must be even");
16+
}
17+
18+
HashMap<String, Object> map = new HashMap<>();
19+
for (int i = 0; i < args.length; i += 2) {
20+
map.put((String) args[i], args[i + 1]);
21+
}
22+
return map;
23+
}
24+
25+
private MapUtils() {}
26+
}

src/test/java/io/github/malczuuu/problem4j/core/ProblemBuilderImplTests.java

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
package io.github.malczuuu.problem4j.core;
22

3+
import static io.github.malczuuu.problem4j.core.MapUtils.mapOf;
34
import static org.assertj.core.api.Assertions.assertThat;
45
import static org.assertj.core.api.Assertions.assertThatThrownBy;
56

67
import java.net.URI;
78
import java.util.Arrays;
89
import java.util.Collection;
10+
import java.util.Collections;
911
import java.util.HashMap;
1012
import java.util.Map;
1113
import org.junit.jupiter.api.Test;
1214

15+
/**
16+
* Some of the tests in this class may appear trivial or unnecessary. They are intentionally
17+
* included to explore and validate the behavior of various code coverage analysis tools. These
18+
* tests help ensure that the coverage reports correctly reflect different execution paths, edge
19+
* cases, and instrumentation scenarios.
20+
*/
1321
class ProblemBuilderImplTests {
1422

1523
@Test
@@ -100,6 +108,16 @@ void givenNullNameExtension_shouldIgnoreIt() {
100108
assertThat(problem.getExtensions()).isEmpty();
101109
}
102110

111+
@Test
112+
void givenNullValueExtension_shouldNotIncludeIt() {
113+
Problem problem = Problem.builder().extension("key", null).build();
114+
115+
assertThat(problem.getExtensions()).isEmpty();
116+
assertThat(problem.hasExtension("key")).isFalse();
117+
assertThat(problem.getExtensionValue("key")).isNull();
118+
assertThat(problem.getExtensionMembers()).isEqualTo(mapOf());
119+
}
120+
103121
@Test
104122
void givenNullMapExtension_shouldIgnoreIt() {
105123
Problem problem = Problem.builder().extension((Map<String, Object>) null).build();
@@ -116,7 +134,9 @@ void givenMapExtensionWithNullKey_shouldIgnoreNullKey() {
116134
Problem problem = Problem.builder().extension(map).build();
117135

118136
assertThat(problem.getExtensions()).containsExactly("a");
137+
assertThat(problem.hasExtension("a")).isTrue();
119138
assertThat(problem.getExtensionValue("a")).isEqualTo("b");
139+
assertThat(problem.getExtensionMembers()).isEqualTo(mapOf("a", "b"));
120140
}
121141

122142
@Test
@@ -134,6 +154,9 @@ void givenVarargWithNullElement_shouldIgnoreNullElement() {
134154
.build();
135155

136156
assertThat(problem.getExtensions()).containsExactlyInAnyOrder("a", "b");
157+
assertThat(problem.hasExtension("a")).isTrue();
158+
assertThat(problem.hasExtension("b")).isTrue();
159+
assertThat(problem.getExtensionMembers()).isEqualTo(mapOf("a", 1, "b", 2));
137160
}
138161

139162
@Test
@@ -152,26 +175,29 @@ void givenCollectionWithNullElement_shouldIgnoreNullElement() {
152175
.build();
153176

154177
assertThat(problem.getExtensions()).containsExactlyInAnyOrder("x", "y");
178+
assertThat(problem.hasExtension("x")).isTrue();
179+
assertThat(problem.hasExtension("y")).isTrue();
180+
assertThat(problem.getExtensionMembers()).isEqualTo(mapOf("x", "1", "y", "2"));
155181
}
156182

157183
@Test
158-
void numericStatus_shouldDeriveTitleWhenKnown() {
184+
void givenNumericStatus_shouldDeriveTitleWhenKnown() {
159185
Problem problem = Problem.builder().status(ProblemStatus.MULTI_STATUS.getStatus()).build();
160186

161187
assertThat(problem.getStatus()).isEqualTo(ProblemStatus.MULTI_STATUS.getStatus());
162188
assertThat(problem.getTitle()).isEqualTo(ProblemStatus.MULTI_STATUS.getTitle());
163189
}
164190

165191
@Test
166-
void unknownNumericStatus_shouldNotDeriveTitle() {
192+
void givenUnknownNumericStatus_shouldNotDeriveTitle() {
167193
Problem problem = Problem.builder().status(999).build();
168194

169195
assertThat(problem.getStatus()).isEqualTo(999);
170196
assertThat(problem.getTitle()).isNull();
171197
}
172198

173199
@Test
174-
void typeAndInstance_stringOverloads_shouldAcceptValidUris() {
200+
void givenTypeAndInstance_stringOverloads_shouldAcceptValidUris() {
175201
String t = "http://example.org/type";
176202
String i = "http://example.org/instance";
177203

@@ -182,29 +208,21 @@ void typeAndInstance_stringOverloads_shouldAcceptValidUris() {
182208
}
183209

184210
@Test
185-
void emptyMapExtension_shouldBeIgnored() {
211+
void givenEmptyMapExtension_shouldBeIgnored() {
186212
Map<String, Object> m = new HashMap<>();
187213
Problem problem = Problem.builder().extension(m).build();
188214

189215
assertThat(problem.getExtensions()).isEmpty();
216+
assertThat(problem.getExtensionMembers()).isEqualTo(Collections.emptyMap());
190217
}
191218

192219
@Test
193-
void nullValuedExtension_isPresentButOmittedFromToString() {
194-
Problem problem = Problem.builder().extension("ext", null).build();
195-
196-
assertThat(problem.getExtensions()).containsExactly("ext");
197-
assertThat(problem.getExtensionValue("ext")).isNull();
198-
199-
String s = problem.toString();
200-
assertThat(s).doesNotContain("\"ext\"");
201-
}
202-
203-
@Test
204-
void laterExtensions_shouldOverwriteEarlierValues() {
220+
void givenAssigningTheSameExtensionLater_shouldOverwriteEarlierValues() {
205221
Problem problem =
206222
Problem.builder().extension("k", "v1").extension(Problem.extension("k", "v2")).build();
207223

208224
assertThat(problem.getExtensionValue("k")).isEqualTo("v2");
225+
assertThat(problem.getExtensions()).containsExactly("k");
226+
assertThat(problem.getExtensionMembers()).isEqualTo(mapOf("k", "v2"));
209227
}
210228
}

src/test/java/io/github/malczuuu/problem4j/core/ProblemExceptionTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66

77
import org.junit.jupiter.api.Test;
88

9+
/**
10+
* Some of the tests in this class may appear trivial or unnecessary. They are intentionally
11+
* included to explore and validate the behavior of various code coverage analysis tools. These
12+
* tests help ensure that the coverage reports correctly reflect different execution paths, edge
13+
* cases, and instrumentation scenarios.
14+
*/
915
class ProblemExceptionTests {
1016

1117
@Test

src/test/java/io/github/malczuuu/problem4j/core/ProblemImplTests.java

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package io.github.malczuuu.problem4j.core;
22

3+
import static io.github.malczuuu.problem4j.core.MapUtils.mapOf;
34
import static org.assertj.core.api.Assertions.assertThat;
45

56
import java.net.URI;
67
import java.util.HashMap;
78
import java.util.Map;
89
import org.junit.jupiter.api.Test;
910

11+
/**
12+
* Some of the tests in this class may appear trivial or unnecessary. They are intentionally
13+
* included to explore and validate the behavior of various code coverage analysis tools. These
14+
* tests help ensure that the coverage reports correctly reflect different execution paths, edge
15+
* cases, and instrumentation scenarios.
16+
*/
1017
class ProblemImplTests {
1118

1219
@Test
@@ -114,6 +121,114 @@ void givenStringWithSpecialCharacters_whenToString_thenProperlyEscapes() {
114121
assertThat(result).contains("\"ext\" : \"" + JsonEscape.escape("a\"b\\c\nd") + "\"");
115122
}
116123

124+
@Test
125+
void givenTwoEqualProblems_shouldBeEqual() {
126+
ProblemImpl problem1 =
127+
new ProblemImpl(null, "title", 404, "detail", null, mapOf("key", "value"));
128+
ProblemImpl problem2 =
129+
new ProblemImpl(null, "title", 404, "detail", null, mapOf("key", "value"));
130+
131+
assertThat(problem1).isEqualTo(problem2);
132+
}
133+
134+
@Test
135+
void givenSameProblems_shouldBeEqual() {
136+
Object problem;
137+
Object other;
138+
139+
problem = other = new ProblemImpl(null, "title", 404, "detail", null, mapOf("key", "value"));
140+
141+
assertThat(problem).isEqualTo(other);
142+
}
143+
144+
@Test
145+
void givenTwoDifferentProblems_shouldNotBeEqual() {
146+
ProblemImpl problem1 =
147+
new ProblemImpl(null, "title1", 404, "detail1", null, mapOf("key", "value1"));
148+
ProblemImpl problem2 =
149+
new ProblemImpl(null, "title2", 500, "detail2", null, mapOf("key", "value2"));
150+
151+
assertThat(problem1).isNotEqualTo(problem2);
152+
}
153+
154+
@Test
155+
void givenProblemAndDifferentObject_shouldNotBeEqual() {
156+
Object problem = new ProblemImpl(null, "title1", 404, "detail1", null, mapOf("key", "value"));
157+
Object differentObject = "always wanted to be a problem";
158+
159+
assertThat(problem).isNotEqualTo(differentObject);
160+
}
161+
162+
@Test
163+
void givenTwoEqualProblems_shouldHaveSameHashCode() {
164+
ProblemImpl problem1 =
165+
new ProblemImpl(null, "title", 404, "detail", null, mapOf("key", "value"));
166+
ProblemImpl problem2 =
167+
new ProblemImpl(null, "title", 404, "detail", null, mapOf("key", "value"));
168+
169+
assertThat(problem1.hashCode()).isEqualTo(problem2.hashCode());
170+
}
171+
172+
@Test
173+
void givenTwoDifferentProblems_shouldHaveDifferentHashCodes() {
174+
ProblemImpl problem1 =
175+
new ProblemImpl(null, "title1", 404, "detail1", null, mapOf("key", "value1"));
176+
ProblemImpl problem2 =
177+
new ProblemImpl(null, "title2", 500, "detail2", null, mapOf("key", "value2"));
178+
179+
assertThat(problem1.hashCode()).isNotEqualTo(problem2.hashCode());
180+
}
181+
182+
@Test
183+
void givenTwoEqualExtensions_shouldBeEqual() {
184+
ProblemImpl.ExtensionImpl ext1 = new ProblemImpl.ExtensionImpl("key", "value");
185+
ProblemImpl.ExtensionImpl ext2 = new ProblemImpl.ExtensionImpl("key", "value");
186+
187+
assertThat(ext1).isEqualTo(ext2);
188+
}
189+
190+
@Test
191+
void givenSameExtensions_shouldBeEqual() {
192+
Object ext;
193+
Object other;
194+
195+
ext = other = new ProblemImpl.ExtensionImpl("key", "value");
196+
197+
assertThat(ext).isEqualTo(other);
198+
}
199+
200+
@Test
201+
void givenTwoDifferentExtensions_shouldNotBeEqual() {
202+
ProblemImpl.ExtensionImpl ext1 = new ProblemImpl.ExtensionImpl("key1", "value1");
203+
ProblemImpl.ExtensionImpl ext2 = new ProblemImpl.ExtensionImpl("key2", "value2");
204+
205+
assertThat(ext1).isNotEqualTo(ext2);
206+
}
207+
208+
@Test
209+
void givenExtensionAndDifferentObject_shouldNotBeEqual() {
210+
Object ext = new ProblemImpl.ExtensionImpl("key", "value");
211+
Object differentObject = "always wanted to be a problem";
212+
213+
assertThat(ext).isNotEqualTo(differentObject);
214+
}
215+
216+
@Test
217+
void givenTwoEqualExtensions_shouldHaveSameHashCode() {
218+
ProblemImpl.ExtensionImpl ext1 = new ProblemImpl.ExtensionImpl("key", "value");
219+
ProblemImpl.ExtensionImpl ext2 = new ProblemImpl.ExtensionImpl("key", "value");
220+
221+
assertThat(ext1.hashCode()).isEqualTo(ext2.hashCode());
222+
}
223+
224+
@Test
225+
void givenTwoDifferentExtensions_shouldHaveDifferentHashCodes() {
226+
ProblemImpl.ExtensionImpl ext1 = new ProblemImpl.ExtensionImpl("key1", "value1");
227+
ProblemImpl.ExtensionImpl ext2 = new ProblemImpl.ExtensionImpl("key2", "value2");
228+
229+
assertThat(ext1.hashCode()).isNotEqualTo(ext2.hashCode());
230+
}
231+
117232
private static class DummyObject {
118233
private final String value;
119234

0 commit comments

Comments
 (0)