|
1 | 1 | package io.a2a.grpc.utils; |
2 | 2 |
|
3 | | -import static io.a2a.grpc.utils.JSONRPCUtils.ERROR_MESSAGE; |
4 | | -import static io.a2a.spec.A2AMethods.GET_TASK_PUSH_NOTIFICATION_CONFIG_METHOD; |
5 | | -import static io.a2a.spec.A2AMethods.SET_TASK_PUSH_NOTIFICATION_CONFIG_METHOD; |
6 | | -import static org.junit.jupiter.api.Assertions.assertEquals; |
7 | | -import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
8 | | -import static org.junit.jupiter.api.Assertions.assertNotNull; |
9 | | -import static org.junit.jupiter.api.Assertions.assertThrows; |
10 | | -import static org.junit.jupiter.api.Assertions.fail; |
11 | | - |
| 3 | +import com.google.gson.JsonObject; |
| 4 | +import com.google.gson.JsonParser; |
12 | 5 | import com.google.gson.JsonSyntaxException; |
13 | | - |
14 | 6 | import io.a2a.grpc.Role; |
15 | 7 | import io.a2a.jsonrpc.common.json.InvalidParamsJsonMappingException; |
16 | 8 | import io.a2a.jsonrpc.common.json.JsonMappingException; |
17 | 9 | import io.a2a.jsonrpc.common.json.JsonProcessingException; |
18 | | -import io.a2a.jsonrpc.common.wrappers.A2ARequest; |
19 | | -import io.a2a.jsonrpc.common.wrappers.GetTaskPushNotificationConfigRequest; |
20 | | -import io.a2a.jsonrpc.common.wrappers.GetTaskPushNotificationConfigResponse; |
21 | | -import io.a2a.jsonrpc.common.wrappers.CreateTaskPushNotificationConfigRequest; |
22 | | -import io.a2a.jsonrpc.common.wrappers.CreateTaskPushNotificationConfigResponse; |
23 | | -import io.a2a.jsonrpc.common.wrappers.SendMessageRequest; |
| 10 | +import io.a2a.jsonrpc.common.wrappers.*; |
24 | 11 | import io.a2a.spec.InvalidParamsError; |
25 | 12 | import io.a2a.spec.JSONParseError; |
26 | | -import io.a2a.spec.Message; |
27 | 13 | import io.a2a.spec.PushNotificationConfig; |
28 | 14 | import io.a2a.spec.TaskPushNotificationConfig; |
29 | 15 | import org.junit.jupiter.api.Test; |
30 | 16 |
|
| 17 | +import static io.a2a.grpc.utils.JSONRPCUtils.ERROR_MESSAGE; |
| 18 | +import static io.a2a.spec.A2AMethods.GET_TASK_PUSH_NOTIFICATION_CONFIG_METHOD; |
| 19 | +import static io.a2a.spec.A2AMethods.SET_TASK_PUSH_NOTIFICATION_CONFIG_METHOD; |
| 20 | +import static org.junit.jupiter.api.Assertions.*; |
| 21 | + |
31 | 22 | public class JSONRPCUtilsTest { |
32 | 23 |
|
33 | 24 | @Test |
@@ -388,6 +379,130 @@ public void testParseErrorResponse_InvalidParams() throws Exception { |
388 | 379 | assertEquals("Invalid params", response.getError().getMessage()); |
389 | 380 | } |
390 | 381 |
|
| 382 | + // ── toJsonRPCRequest serialization ──────────────────────────────────────── |
| 383 | + |
| 384 | + @Test |
| 385 | + public void testToJsonRPCRequest_TextPart_NullMetadata_OnlyTextFieldInPart() { |
| 386 | + io.a2a.grpc.SendMessageRequest request = io.a2a.grpc.SendMessageRequest.newBuilder() |
| 387 | + .setMessage(io.a2a.grpc.Message.newBuilder() |
| 388 | + .setMessageId("msg-1") |
| 389 | + .setRole(Role.ROLE_USER) |
| 390 | + .addParts(io.a2a.grpc.Part.newBuilder() |
| 391 | + .setText("hello") |
| 392 | + .build()) |
| 393 | + .build()) |
| 394 | + .build(); |
| 395 | + |
| 396 | + String json = JSONRPCUtils.toJsonRPCRequest("req-1", "SendMessage", request); |
| 397 | + |
| 398 | + JsonObject part = JsonParser.parseString(json).getAsJsonObject() |
| 399 | + .get("params").getAsJsonObject() |
| 400 | + .get("message").getAsJsonObject() |
| 401 | + .get("parts").getAsJsonArray() |
| 402 | + .get(0).getAsJsonObject(); |
| 403 | + |
| 404 | + assertEquals(1, part.size(), "TextPart with no metadata should only have 'text' field"); |
| 405 | + assertTrue(part.has("text")); |
| 406 | + assertFalse(part.has("filename"), "filename must not appear for TextPart"); |
| 407 | + assertFalse(part.has("mediaType"), "mediaType must not appear for TextPart"); |
| 408 | + assertFalse(part.has("metadata"), "metadata must not appear when not set"); |
| 409 | + } |
| 410 | + |
| 411 | + @Test |
| 412 | + public void testToJsonRPCRequest_TextPart_WithMetadata_OnlyTextAndMetadataFields() { |
| 413 | + io.a2a.grpc.SendMessageRequest request = io.a2a.grpc.SendMessageRequest.newBuilder() |
| 414 | + .setMessage(io.a2a.grpc.Message.newBuilder() |
| 415 | + .setMessageId("msg-1") |
| 416 | + .setRole(Role.ROLE_USER) |
| 417 | + .addParts(io.a2a.grpc.Part.newBuilder() |
| 418 | + .setText("hello") |
| 419 | + .setMetadata(com.google.protobuf.Struct.newBuilder() |
| 420 | + .putFields("key", |
| 421 | + com.google.protobuf.Value.newBuilder() |
| 422 | + .setStringValue("value") |
| 423 | + .build()) |
| 424 | + .build()) |
| 425 | + .build()) |
| 426 | + .build()) |
| 427 | + .build(); |
| 428 | + |
| 429 | + String json = JSONRPCUtils.toJsonRPCRequest("req-1", "SendMessage", request); |
| 430 | + |
| 431 | + JsonObject part = JsonParser.parseString(json).getAsJsonObject() |
| 432 | + .get("params").getAsJsonObject() |
| 433 | + .get("message").getAsJsonObject() |
| 434 | + .get("parts").getAsJsonArray() |
| 435 | + .get(0).getAsJsonObject(); |
| 436 | + |
| 437 | + assertEquals(2, part.size(), "TextPart with metadata should only have 'text' and 'metadata' fields"); |
| 438 | + assertTrue(part.has("text")); |
| 439 | + assertTrue(part.has("metadata")); |
| 440 | + assertFalse(part.has("filename"), "filename must not appear for TextPart"); |
| 441 | + assertFalse(part.has("mediaType"), "mediaType must not appear for TextPart"); |
| 442 | + } |
| 443 | + |
| 444 | + // ── toJsonRPCResultResponse serialization ───────────────────────────────── |
| 445 | + |
| 446 | + @Test |
| 447 | + public void testToJsonRPCResultResponse_TextPart_NullMetadata_OnlyTextFieldInPart() { |
| 448 | + io.a2a.grpc.SendMessageResponse response = io.a2a.grpc.SendMessageResponse.newBuilder() |
| 449 | + .setMessage(io.a2a.grpc.Message.newBuilder() |
| 450 | + .setMessageId("msg-1") |
| 451 | + .setRole(Role.ROLE_AGENT) |
| 452 | + .addParts(io.a2a.grpc.Part.newBuilder() |
| 453 | + .setText("hi there") |
| 454 | + .build()) |
| 455 | + .build()) |
| 456 | + .build(); |
| 457 | + |
| 458 | + String json = JSONRPCUtils.toJsonRPCResultResponse("req-1", response); |
| 459 | + |
| 460 | + JsonObject part = JsonParser.parseString(json).getAsJsonObject() |
| 461 | + .get("result").getAsJsonObject() |
| 462 | + .get("message").getAsJsonObject() |
| 463 | + .get("parts").getAsJsonArray() |
| 464 | + .get(0).getAsJsonObject(); |
| 465 | + |
| 466 | + assertEquals(1, part.size(), "TextPart with no metadata should only have 'text' field"); |
| 467 | + assertTrue(part.has("text")); |
| 468 | + assertFalse(part.has("filename"), "filename must not appear for TextPart"); |
| 469 | + assertFalse(part.has("mediaType"), "mediaType must not appear for TextPart"); |
| 470 | + assertFalse(part.has("metadata"), "metadata must not appear when not set"); |
| 471 | + } |
| 472 | + |
| 473 | + @Test |
| 474 | + public void testToJsonRPCResultResponse_TextPart_WithMetadata_OnlyTextAndMetadataFields() { |
| 475 | + io.a2a.grpc.SendMessageResponse response = io.a2a.grpc.SendMessageResponse.newBuilder() |
| 476 | + .setMessage(io.a2a.grpc.Message.newBuilder() |
| 477 | + .setMessageId("msg-1") |
| 478 | + .setRole(Role.ROLE_AGENT) |
| 479 | + .addParts(io.a2a.grpc.Part.newBuilder() |
| 480 | + .setText("hi there") |
| 481 | + .setMetadata(com.google.protobuf.Struct.newBuilder() |
| 482 | + .putFields("tag", |
| 483 | + com.google.protobuf.Value.newBuilder() |
| 484 | + .setStringValue("reply") |
| 485 | + .build()) |
| 486 | + .build()) |
| 487 | + .build()) |
| 488 | + .build()) |
| 489 | + .build(); |
| 490 | + |
| 491 | + String json = JSONRPCUtils.toJsonRPCResultResponse("req-1", response); |
| 492 | + |
| 493 | + JsonObject part = JsonParser.parseString(json).getAsJsonObject() |
| 494 | + .get("result").getAsJsonObject() |
| 495 | + .get("message").getAsJsonObject() |
| 496 | + .get("parts").getAsJsonArray() |
| 497 | + .get(0).getAsJsonObject(); |
| 498 | + |
| 499 | + assertEquals(2, part.size(), "TextPart with metadata should only have 'text' and 'metadata' fields"); |
| 500 | + assertTrue(part.has("text")); |
| 501 | + assertTrue(part.has("metadata")); |
| 502 | + assertFalse(part.has("filename"), "filename must not appear for TextPart"); |
| 503 | + assertFalse(part.has("mediaType"), "mediaType must not appear for TextPart"); |
| 504 | + } |
| 505 | + |
391 | 506 | @Test |
392 | 507 | public void testParseErrorResponse_ParseError() throws Exception { |
393 | 508 | String errorResponse = """ |
|
0 commit comments