Skip to content

Commit 4ddac8b

Browse files
authored
test: add ToProto and FromProto tests (#341)
# Description This PR adds new unit tests to improve coverage for `ProtoUtils.ToProto` and `ProtoUtils.FromProto`. ### Tests Added - `testFromProtoPartUnsupportedType`: verifies invalid proto parts throw `InvalidParamsError`. - `testTaskQueryParamsInvalidName`: makes sure invalid task name formats are handled gracefully. - `testToProtoPartUnsupportedType`: checks handling of unsupported `Part` types during conversion. Fixes #304
1 parent e81b35a commit 4ddac8b

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.a2a.grpc.utils;
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.assertNotNull;
7+
import static org.junit.jupiter.api.Assertions.assertThrows;
8+
9+
public class FromProtoTest {
10+
@Test
11+
public void testFromProtoPartUnsupportedType() {
12+
io.a2a.grpc.Part emptyPart = io.a2a.grpc.Part.newBuilder().build();
13+
io.a2a.grpc.Message invalidMessage = io.a2a.grpc.Message.newBuilder()
14+
.addContent(emptyPart)
15+
.build();
16+
io.a2a.spec.InvalidParamsError exception = assertThrows(
17+
io.a2a.spec.InvalidParamsError.class,
18+
() -> ProtoUtils.FromProto.message(invalidMessage)
19+
);
20+
21+
assertEquals("Invalid parameters", exception.getMessage());
22+
23+
}
24+
25+
@Test
26+
public void testTaskQueryParamsInvalidName() {
27+
io.a2a.grpc.GetTaskRequest request = io.a2a.grpc.GetTaskRequest.newBuilder()
28+
.setName("invalid-name-format")
29+
.build();
30+
31+
var result = ProtoUtils.FromProto.taskQueryParams(request);
32+
33+
assertNotNull(result);
34+
assertEquals("invalid-name-format", result.id());
35+
}
36+
37+
}

spec-grpc/src/test/java/io/a2a/grpc/utils/ToProtoTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,28 @@ public void convertTaskTimestampStatus() {
289289
assertNotNull(status.timestamp());
290290
assertEquals(expectedTimestamp, status.timestamp());
291291
}
292+
293+
@Test
294+
public void testToProtoPartUnsupportedType() {
295+
class FakePart extends io.a2a.spec.Part<Object> {
296+
@Override
297+
public io.a2a.spec.Part.Kind getKind() { return null; }
298+
@Override
299+
public Map<String, Object> getMetadata() { return Map.of(); }
300+
}
301+
302+
FakePart fakePart = new FakePart();
303+
304+
io.a2a.spec.Message message = new io.a2a.spec.Message.Builder()
305+
.messageId("msg-unsupported")
306+
.contextId("ctx-unsupported")
307+
.role(io.a2a.spec.Message.Role.USER)
308+
.parts(List.of(fakePart))
309+
.build();
310+
io.a2a.grpc.Message result = ProtoUtils.ToProto.message(message);
311+
312+
assertNotNull(result);
313+
assertEquals(1, result.getContentCount());
314+
}
315+
292316
}

0 commit comments

Comments
 (0)