Skip to content

Commit 94ff7d7

Browse files
tomsun28maeste
andauthored
test: add unit test case for Assert and Utils (#132)
# Description Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [x] Follow the [`CONTRIBUTING` Guide](../CONTRIBUTING.md). - [x] Make your Pull Request title in the <https://www.conventionalcommits.org/> specification. - Important Prefixes for [release-please](https://github.com/googleapis/release-please): - `fix:` which represents bug fixes, and correlates to a [SemVer](https://semver.org/) patch. - `feat:` represents a new feature, and correlates to a SemVer minor. - `feat!:`, or `fix!:`, `refactor!:`, etc., which represent a breaking change (indicated by the `!`) and will result in a SemVer major. - [x] Ensure the tests pass - [ ] Appropriate READMEs were updated (if necessary) Fixes #<issue_number_goes_here> 🦕 --------- Co-authored-by: Stefano Maestri <[email protected]>
1 parent 558e695 commit 94ff7d7

File tree

2 files changed

+269
-0
lines changed

2 files changed

+269
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package io.a2a.util;
2+
3+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
import org.junit.jupiter.api.Test;
7+
8+
/**
9+
* Tests for Assert utility class
10+
*/
11+
class AssertTest {
12+
13+
@Test
14+
void testCheckNotNullParam_WithValidValue() {
15+
// Test normal case
16+
String testValue = "test";
17+
String result = Assert.checkNotNullParam("testParam", testValue);
18+
assertEquals(testValue, result);
19+
}
20+
21+
@Test
22+
void testCheckNotNullParam_WithNullValue() {
23+
// Test that null value throws exception
24+
assertThrows(IllegalArgumentException.class, () -> {
25+
Assert.checkNotNullParam("testParam", null);
26+
});
27+
}
28+
29+
@Test
30+
void testCheckNotNullParam_WithNullName() {
31+
// Test that null name parameter throws exception
32+
assertThrows(IllegalArgumentException.class, () -> {
33+
Assert.checkNotNullParam(null, "testValue");
34+
});
35+
}
36+
37+
@Test
38+
void testCheckNotNullParam_WithEmptyName() {
39+
// Test empty string name parameter
40+
String testValue = "test";
41+
String result = Assert.checkNotNullParam("", testValue);
42+
assertEquals(testValue, result);
43+
}
44+
45+
@Test
46+
void testIsNullOrStringOrInteger_WithNull() {
47+
// Test null value
48+
assertDoesNotThrow(() -> {
49+
Assert.isNullOrStringOrInteger(null);
50+
});
51+
}
52+
53+
@Test
54+
void testIsNullOrStringOrInteger_WithString() {
55+
// Test String type
56+
assertDoesNotThrow(() -> {
57+
Assert.isNullOrStringOrInteger("test");
58+
});
59+
}
60+
61+
@Test
62+
void testIsNullOrStringOrInteger_WithInteger() {
63+
// Test Integer type
64+
assertDoesNotThrow(() -> {
65+
Assert.isNullOrStringOrInteger(123);
66+
});
67+
}
68+
69+
@Test
70+
void testIsNullOrStringOrInteger_WithLong() {
71+
// Test that Long type throws exception
72+
assertThrows(IllegalArgumentException.class, () -> {
73+
Assert.isNullOrStringOrInteger(123L);
74+
});
75+
}
76+
77+
@Test
78+
void testIsNullOrStringOrInteger_WithDouble() {
79+
// Test that Double type throws exception
80+
assertThrows(IllegalArgumentException.class, () -> {
81+
Assert.isNullOrStringOrInteger(123.45);
82+
});
83+
}
84+
85+
@Test
86+
void testIsNullOrStringOrInteger_WithBoolean() {
87+
// Test that Boolean type throws exception
88+
assertThrows(IllegalArgumentException.class, () -> {
89+
Assert.isNullOrStringOrInteger(true);
90+
});
91+
}
92+
93+
@Test
94+
void testIsNullOrStringOrInteger_WithObject() {
95+
// Test that Object type throws exception
96+
assertThrows(IllegalArgumentException.class, () -> {
97+
Assert.isNullOrStringOrInteger(new Object());
98+
});
99+
}
100+
}
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
package io.a2a.util;
2+
3+
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertNotNull;
6+
import static org.junit.jupiter.api.Assertions.assertNull;
7+
import static org.junit.jupiter.api.Assertions.assertThrows;
8+
import static org.junit.jupiter.api.Assertions.assertTrue;
9+
import com.fasterxml.jackson.core.type.TypeReference;
10+
import org.junit.jupiter.api.Test;
11+
12+
import java.util.List;
13+
import java.util.Map;
14+
15+
/**
16+
* Tests for Utils utility class
17+
*/
18+
class UtilsTest {
19+
20+
@Test
21+
void testDefaultIfNull_WithNullValue() {
22+
// Test that null value returns default value
23+
String result = Utils.defaultIfNull(null, "default");
24+
assertEquals("default", result);
25+
}
26+
27+
@Test
28+
void testDefaultIfNull_WithNonNullValue() {
29+
// Test that non-null value returns original value
30+
String value = "test";
31+
String result = Utils.defaultIfNull(value, "default");
32+
assertEquals(value, result);
33+
}
34+
35+
@Test
36+
void testDefaultIfNull_WithNullDefault() {
37+
// Test case where default value is null
38+
String value = "test";
39+
String result = Utils.defaultIfNull(value, null);
40+
assertEquals(value, result);
41+
}
42+
43+
@Test
44+
void testDefaultIfNull_WithBothNull() {
45+
// Test case where both value and default are null
46+
String result = Utils.defaultIfNull(null, null);
47+
assertNull(result);
48+
}
49+
50+
@Test
51+
void testDefaultIfNull_WithDifferentTypes() {
52+
// Test with different types
53+
Integer intValue = 123;
54+
Integer result = Utils.defaultIfNull(intValue, 456);
55+
assertEquals(intValue, result);
56+
}
57+
58+
@Test
59+
void testUnmarshalFrom_WithValidJson() throws Exception {
60+
// Test with valid JSON string
61+
String json = "{\"name\":\"test\",\"value\":123}";
62+
TypeReference<Map<String, Object>> typeRef = new TypeReference<Map<String, Object>>() {};
63+
64+
Map<String, Object> result = Utils.unmarshalFrom(json, typeRef);
65+
66+
assertNotNull(result);
67+
assertEquals("test", result.get("name"));
68+
assertEquals(123, result.get("value"));
69+
}
70+
71+
@Test
72+
void testUnmarshalFrom_WithArrayJson() throws Exception {
73+
// Test with array JSON
74+
String json = "[\"item1\",\"item2\",\"item3\"]";
75+
TypeReference<List<String>> typeRef = new TypeReference<List<String>>() {};
76+
77+
List<String> result = Utils.unmarshalFrom(json, typeRef);
78+
79+
assertNotNull(result);
80+
assertEquals(3, result.size());
81+
assertEquals("item1", result.get(0));
82+
assertEquals("item2", result.get(1));
83+
assertEquals("item3", result.get(2));
84+
}
85+
86+
@Test
87+
void testUnmarshalFrom_WithInvalidJson() {
88+
// Test with invalid JSON string
89+
String invalidJson = "{invalid json}";
90+
TypeReference<Map<String, Object>> typeRef = new TypeReference<Map<String, Object>>() {};
91+
92+
assertThrows(Exception.class, () -> {
93+
Utils.unmarshalFrom(invalidJson, typeRef);
94+
});
95+
}
96+
97+
@Test
98+
void testUnmarshalFrom_WithEmptyJson() throws Exception {
99+
// Test with empty JSON object
100+
String json = "{}";
101+
TypeReference<Map<String, Object>> typeRef = new TypeReference<Map<String, Object>>() {};
102+
103+
Map<String, Object> result = Utils.unmarshalFrom(json, typeRef);
104+
105+
assertNotNull(result);
106+
assertTrue(result.isEmpty());
107+
}
108+
109+
@Test
110+
void testRethrow_WithRuntimeException() {
111+
// Test rethrowing RuntimeException
112+
RuntimeException originalException = new RuntimeException("test exception");
113+
114+
assertThrows(RuntimeException.class, () -> {
115+
Utils.rethrow(originalException);
116+
});
117+
}
118+
119+
@Test
120+
void testRethrow_WithCheckedException() {
121+
// Test rethrowing checked exception
122+
Exception originalException = new Exception("test checked exception");
123+
124+
assertThrows(Exception.class, () -> {
125+
Utils.rethrow(originalException);
126+
});
127+
}
128+
129+
@Test
130+
void testRethrow_WithCustomException() {
131+
// Test rethrowing custom exception
132+
IllegalArgumentException originalException = new IllegalArgumentException("test illegal argument");
133+
134+
assertThrows(IllegalArgumentException.class, () -> {
135+
Utils.rethrow(originalException);
136+
});
137+
}
138+
139+
@Test
140+
void testObjectMapper_IsNotNull() {
141+
// Test that OBJECT_MAPPER is not null
142+
assertNotNull(Utils.OBJECT_MAPPER);
143+
}
144+
145+
@Test
146+
void testObjectMapper_CanSerializeAndDeserialize() throws Exception {
147+
// Test that OBJECT_MAPPER can serialize and deserialize properly
148+
TestObject original = new TestObject("test", 123);
149+
150+
String json = Utils.OBJECT_MAPPER.writeValueAsString(original);
151+
TestObject deserialized = Utils.OBJECT_MAPPER.readValue(json, TestObject.class);
152+
153+
assertEquals(original.name, deserialized.name);
154+
assertEquals(original.value, deserialized.value);
155+
}
156+
157+
// Simple class for testing
158+
static class TestObject {
159+
public String name;
160+
public int value;
161+
162+
public TestObject() {}
163+
164+
public TestObject(String name, int value) {
165+
this.name = name;
166+
this.value = value;
167+
}
168+
}
169+
}

0 commit comments

Comments
 (0)