Skip to content

Commit 69e28d7

Browse files
committed
test(util): Add unit tests for utility classes
1 parent d8570c3 commit 69e28d7

File tree

6 files changed

+128
-0
lines changed

6 files changed

+128
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
import com.fasterxml.jackson.core.JsonProcessingException;
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import com.github.codeboyzhou.mcp.declarative.exception.McpServerException;
6+
import org.jetbrains.annotations.VisibleForTesting;
67

78
public final class JsonHelper {
89

910
public static final ObjectMapper MAPPER = new ObjectMapper();
1011

12+
@VisibleForTesting
13+
JsonHelper() {
14+
throw new UnsupportedOperationException("Utility class should not be instantiated");
15+
}
16+
1117
public static String toJson(Object object) {
1218
try {
1319
return MAPPER.writeValueAsString(object);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
package com.github.codeboyzhou.mcp.declarative.util;
22

3+
import org.jetbrains.annotations.VisibleForTesting;
4+
35
public final class StringHelper {
46

57
public static final String EMPTY = "";
68

9+
@VisibleForTesting
10+
StringHelper() {
11+
throw new UnsupportedOperationException("Utility class should not be instantiated");
12+
}
13+
714
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package com.github.codeboyzhou.mcp.declarative.util;
22

33
import com.github.codeboyzhou.mcp.declarative.enums.JsonSchemaDataType;
4+
import org.jetbrains.annotations.VisibleForTesting;
45

56
public final class TypeConverter {
67

8+
@VisibleForTesting
9+
TypeConverter() {
10+
throw new UnsupportedOperationException("Utility class should not be instantiated");
11+
}
12+
713
public static Object convert(Object value, Class<?> targetType) {
814
if (value == null) {
915
return getDefaultValue(targetType);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.github.codeboyzhou.mcp.declarative.util;
2+
3+
import com.github.codeboyzhou.mcp.declarative.exception.McpServerException;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import static org.junit.jupiter.api.Assertions.assertThrows;
9+
10+
class JsonHelperTest {
11+
12+
record TestClass(String name, int age) {
13+
}
14+
15+
@Test
16+
void testNewInstance() {
17+
UnsupportedOperationException e = assertThrows(UnsupportedOperationException.class, JsonHelper::new);
18+
assertEquals("Utility class should not be instantiated", e.getMessage());
19+
}
20+
21+
@Test
22+
void testToJson() {
23+
assertDoesNotThrow(() -> {
24+
TestClass testObject = new TestClass("test", 18);
25+
assertEquals("{\"name\":\"test\",\"age\":18}", JsonHelper.toJson(testObject));
26+
});
27+
28+
McpServerException e = assertThrows(McpServerException.class, () -> JsonHelper.toJson(this));
29+
assertEquals("Error converting object to JSON", e.getMessage());
30+
}
31+
32+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.github.codeboyzhou.mcp.declarative.util;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertThrows;
7+
8+
class StringHelperTest {
9+
10+
@Test
11+
void testNewInstance() {
12+
UnsupportedOperationException e = assertThrows(UnsupportedOperationException.class, StringHelper::new);
13+
assertEquals("Utility class should not be instantiated", e.getMessage());
14+
}
15+
16+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.github.codeboyzhou.mcp.declarative.util;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertNull;
7+
import static org.junit.jupiter.api.Assertions.assertThrows;
8+
9+
class TypeConverterTest {
10+
11+
@Test
12+
void testNewInstance() {
13+
UnsupportedOperationException e = assertThrows(UnsupportedOperationException.class, TypeConverter::new);
14+
assertEquals("Utility class should not be instantiated", e.getMessage());
15+
}
16+
17+
@Test
18+
void testConvertToTargetType() {
19+
assertEquals(StringHelper.EMPTY, TypeConverter.convert(null, String.class));
20+
assertEquals(0, TypeConverter.convert(null, int.class));
21+
assertEquals(0, TypeConverter.convert(null, Integer.class));
22+
assertEquals(0L, TypeConverter.convert(null, long.class));
23+
assertEquals(0L, TypeConverter.convert(null, Long.class));
24+
assertEquals(0.0f, TypeConverter.convert(null, float.class));
25+
assertEquals(0.0f, TypeConverter.convert(null, Float.class));
26+
assertEquals(0.0, TypeConverter.convert(null, double.class));
27+
assertEquals(0.0, TypeConverter.convert(null, Double.class));
28+
assertEquals(false, TypeConverter.convert(null, boolean.class));
29+
assertEquals(false, TypeConverter.convert(null, Boolean.class));
30+
assertNull(TypeConverter.convert(null, TypeConverterTest.class));
31+
32+
assertEquals("123", TypeConverter.convert("123", String.class));
33+
assertEquals(123, TypeConverter.convert("123", int.class));
34+
assertEquals(123, TypeConverter.convert("123", Integer.class));
35+
assertEquals(123L, TypeConverter.convert("123", long.class));
36+
assertEquals(123L, TypeConverter.convert("123", Long.class));
37+
assertEquals(123.0f, TypeConverter.convert("123", float.class));
38+
assertEquals(123.0f, TypeConverter.convert("123", Float.class));
39+
assertEquals(123.0, TypeConverter.convert("123", double.class));
40+
assertEquals(123.0, TypeConverter.convert("123", Double.class));
41+
assertEquals(true, TypeConverter.convert("true", boolean.class));
42+
assertEquals(true, TypeConverter.convert("true", Boolean.class));
43+
assertEquals("123", TypeConverter.convert("123", TypeConverterTest.class));
44+
}
45+
46+
@Test
47+
void testConvertToJsonSchemaType() {
48+
assertEquals(StringHelper.EMPTY, TypeConverter.convert(null, "string"));
49+
assertEquals(0, TypeConverter.convert(null, "integer"));
50+
assertEquals(0.0, TypeConverter.convert(null, "number"));
51+
assertEquals(false, TypeConverter.convert(null, "boolean"));
52+
assertNull(TypeConverter.convert(null, "object"));
53+
54+
assertEquals("123", TypeConverter.convert("123", "string"));
55+
assertEquals(123, TypeConverter.convert("123", "integer"));
56+
assertEquals(123.0, TypeConverter.convert("123", "number"));
57+
assertEquals(true, TypeConverter.convert("true", "boolean"));
58+
assertEquals("123", TypeConverter.convert("123", "object"));
59+
}
60+
61+
}

0 commit comments

Comments
 (0)