Skip to content

Commit 5524c63

Browse files
committed
Add comprehensive test coverage for JsonParser
Co-authored-by: Oleksandr Klymenko <[email protected]> Signed-off-by: Oleksandr Klymenko <[email protected]>
1 parent 13de219 commit 5524c63

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

spring-ai-model/src/test/java/org/springframework/ai/util/json/JsonParserTests.java

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,118 @@ void doesNotDoubleSerializeValidJsonString() {
278278
assertThat(input).isEqualTo(result);
279279
}
280280

281+
@Test
282+
void shouldThrowExceptionWhenJsonIsMalformed() {
283+
var json = "{ value }";
284+
assertThatThrownBy(() -> JsonParser.fromJson(json, TestRecord.class)).isInstanceOf(IllegalStateException.class)
285+
.hasMessageContaining("Conversion from JSON to")
286+
.hasMessageContaining("failed");
287+
}
288+
289+
@Test
290+
void shouldThrowExceptionWhenJsonIsMalformedWithType() {
291+
var json = "{ value: }";
292+
assertThatThrownBy(() -> JsonParser.fromJson(json, (Type) TestRecord.class))
293+
.isInstanceOf(IllegalStateException.class)
294+
.hasMessageContaining("Conversion from JSON to")
295+
.hasMessageContaining("failed");
296+
}
297+
298+
@Test
299+
void shouldThrowExceptionWhenJsonIsMalformedWithTypeReference() {
300+
var json = "[ 1, 2, }";
301+
assertThatThrownBy(() -> JsonParser.fromJson(json, new TypeReference<java.util.List<Integer>>() {
302+
})).isInstanceOf(IllegalStateException.class)
303+
.hasMessageContaining("Conversion from JSON to")
304+
.hasMessageContaining("failed");
305+
}
306+
307+
@Test
308+
void fromJsonToListWithTypeReference() {
309+
var json = "[1, 2, 3]";
310+
var list = JsonParser.fromJson(json, new TypeReference<java.util.List<Integer>>() {
311+
});
312+
assertThat(list).containsExactly(1, 2, 3);
313+
}
314+
315+
@Test
316+
void fromJsonToMapWithTypeReference() {
317+
var json = """
318+
{
319+
"stringValue": "string",
320+
"intValue": "1"
321+
}
322+
""";
323+
var map = JsonParser.fromJson(json, new TypeReference<java.util.Map<String, String>>() {
324+
});
325+
assertThat(map).containsEntry("stringValue", "string").containsEntry("intValue", "1");
326+
}
327+
328+
@Test
329+
void shouldThrowExceptionWhenValueIsNullInToTypedObject() {
330+
assertThatThrownBy(() -> JsonParser.toTypedObject(null, String.class))
331+
.isInstanceOf(IllegalArgumentException.class)
332+
.hasMessage("value cannot be null");
333+
}
334+
335+
@Test
336+
void shouldThrowExceptionWhenTypeIsNullInToTypedObject() {
337+
assertThatThrownBy(() -> JsonParser.toTypedObject("test", null)).isInstanceOf(IllegalArgumentException.class)
338+
.hasMessage("type cannot be null");
339+
}
340+
341+
@Test
342+
void fromObjectToPrimitiveInt() {
343+
var value = JsonParser.toTypedObject("1", int.class);
344+
assertThat(value).isInstanceOf(Integer.class);
345+
assertThat(value).isEqualTo(1);
346+
}
347+
348+
@Test
349+
void fromObjectToPrimitiveLong() {
350+
var value = JsonParser.toTypedObject("1", long.class);
351+
assertThat(value).isInstanceOf(Long.class);
352+
assertThat(value).isEqualTo(1L);
353+
}
354+
355+
@Test
356+
void fromObjectToPrimitiveBoolean() {
357+
var value = JsonParser.toTypedObject("true", boolean.class);
358+
assertThat(value).isInstanceOf(Boolean.class);
359+
assertThat(value).isEqualTo(true);
360+
}
361+
362+
@Test
363+
void shouldThrowExceptionWhenConversionFails() {
364+
assertThatThrownBy(() -> JsonParser.toTypedObject("not_a_number", Integer.class))
365+
.isInstanceOf(NumberFormatException.class);
366+
}
367+
368+
@Test
369+
void fromEmptyJsonObject() {
370+
var json = "{}";
371+
var object = JsonParser.fromJson(json, TestRecord.class);
372+
assertThat(object).isNotNull();
373+
assertThat(object.name).isNull();
374+
assertThat(object.age).isNull();
375+
}
376+
377+
@Test
378+
void fromEmptyJsonArray() {
379+
var json = "[]";
380+
var list = JsonParser.fromJson(json, new TypeReference<java.util.List<String>>() {
381+
});
382+
assertThat(list).isEmpty();
383+
}
384+
385+
@Test
386+
void toTypedObjectWithNonParsableJsonString() {
387+
var invalidJsonString = "not json at all";
388+
assertThatThrownBy(() -> JsonParser.toTypedObject(invalidJsonString, TestRecord.class))
389+
.isInstanceOf(IllegalStateException.class)
390+
.hasMessageContaining("Conversion from JSON to");
391+
}
392+
281393
record TestRecord(String name, Integer age) {
282394
}
283395

0 commit comments

Comments
 (0)