Skip to content

Commit e79e10f

Browse files
committed
feat(test): Add JSON parsing tests and refactor StringHelper utility methods
1 parent 425fbf9 commit e79e10f

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

src/main/java/com/github/codeboyzhou/mcp/declarative/util/StringHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ public static boolean isBlank(String str) {
4646
* @return the original string if it is not blank, otherwise the default value
4747
*/
4848
public static String defaultIfBlank(String str, String defaultValue) {
49-
return str == null || str.isBlank() ? defaultValue : str;
49+
return isBlank(str) ? defaultValue : str;
5050
}
5151
}

src/test/java/com/github/codeboyzhou/mcp/declarative/util/JacksonHelperTest.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,25 @@ void testToJsonString_shouldSucceed() {
3434

3535
@Test
3636
void testToJsonString_shouldThrowException() {
37-
CircularReference circularRef = new CircularReference();
3837
assertThrows(
39-
McpServerJsonProcessingException.class, () -> JacksonHelper.toJsonString(circularRef));
38+
McpServerJsonProcessingException.class,
39+
() -> JacksonHelper.toJsonString(new CircularReference()));
40+
}
41+
42+
@Test
43+
void testFromJson_shouldSucceed() {
44+
String json = "{\"name\":\"test\",\"age\":25}";
45+
Person person = JacksonHelper.fromJson(json, Person.class);
46+
assertEquals("test", person.name);
47+
assertEquals(25, person.age);
48+
}
49+
50+
@Test
51+
void testFromJson_shouldThrowException() {
52+
String json = "{\"name\":\"test\",\"age\":25}";
53+
assertThrows(
54+
McpServerJsonProcessingException.class,
55+
() -> JacksonHelper.fromJson(json, CircularReference.class));
4056
}
4157

4258
@Test

src/test/java/com/github/codeboyzhou/mcp/declarative/util/StringHelperTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ void testConstructor_shouldThrowException() {
1111
assertThrows(UnsupportedOperationException.class, StringHelper::new);
1212
}
1313

14+
@Test
15+
void testIsBlank_shouldReturnTrueWhenStrIsBlank() {
16+
assertTrue(StringHelper.isBlank(StringHelper.EMPTY));
17+
assertTrue(StringHelper.isBlank(StringHelper.SPACE));
18+
}
19+
20+
@Test
21+
void testIsBlank_shouldReturnFalseWhenStrIsNotBlank() {
22+
assertFalse(StringHelper.isBlank("test"));
23+
}
24+
1425
@Test
1526
void testDefaultIfBlank_shouldReturnDefaultValueWhenStrIsNull() {
1627
assertEquals("default", StringHelper.defaultIfBlank(null, "default"));

0 commit comments

Comments
 (0)