Repository navigation
Expand file tree
/
Copy pathJSONRPCUtilsTest.java
More file actions
409 lines (367 loc) · 15.5 KB
/
Copy pathJSONRPCUtilsTest.java
File metadata and controls
409 lines (367 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package io.a2a.grpc.utils;
import static io.a2a.grpc.utils.JSONRPCUtils.ERROR_MESSAGE;
import static io.a2a.spec.A2AMethods.GET_TASK_PUSH_NOTIFICATION_CONFIG_METHOD;
import static io.a2a.spec.A2AMethods.SET_TASK_PUSH_NOTIFICATION_CONFIG_METHOD;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
import com.google.gson.JsonSyntaxException;
import io.a2a.jsonrpc.common.json.InvalidParamsJsonMappingException;
import io.a2a.jsonrpc.common.json.JsonMappingException;
import io.a2a.jsonrpc.common.json.JsonProcessingException;
import io.a2a.jsonrpc.common.wrappers.A2ARequest;
import io.a2a.jsonrpc.common.wrappers.GetTaskPushNotificationConfigRequest;
import io.a2a.jsonrpc.common.wrappers.GetTaskPushNotificationConfigResponse;
import io.a2a.jsonrpc.common.wrappers.CreateTaskPushNotificationConfigRequest;
import io.a2a.jsonrpc.common.wrappers.CreateTaskPushNotificationConfigResponse;
import io.a2a.spec.InvalidParamsError;
import io.a2a.spec.JSONParseError;
import io.a2a.spec.PushNotificationConfig;
import io.a2a.spec.TaskPushNotificationConfig;
import org.junit.jupiter.api.Test;
public class JSONRPCUtilsTest {
@Test
public void testParseCreateTaskPushNotificationConfigRequest_ValidProtoFormat() throws JsonProcessingException {
String validRequest = """
{
"jsonrpc": "2.0",
"method": "CreateTaskPushNotificationConfig",
"id": "1",
"params": {
"taskId": "task-123",
"configId": "config-456",
"tenant": "",
"config": {
"url": "https://example.com/callback",
"authentication": {
"scheme": "jwt"
}
}
}
}
""";
A2ARequest<?> request = JSONRPCUtils.parseRequestBody(validRequest, null);
assertNotNull(request);
assertInstanceOf(CreateTaskPushNotificationConfigRequest.class, request);
CreateTaskPushNotificationConfigRequest setRequest = (CreateTaskPushNotificationConfigRequest) request;
assertEquals("2.0", setRequest.getJsonrpc());
assertEquals(1, setRequest.getId());
assertEquals("CreateTaskPushNotificationConfig", setRequest.getMethod());
TaskPushNotificationConfig config = setRequest.getParams();
assertNotNull(config);
assertEquals("task-123", config.taskId());
assertNotNull(config.pushNotificationConfig());
assertEquals("https://example.com/callback", config.pushNotificationConfig().url());
}
@Test
public void testParseGetTaskPushNotificationConfigRequest_ValidProtoFormat() throws JsonProcessingException {
String validRequest = """
{
"jsonrpc": "2.0",
"method": "GetTaskPushNotificationConfig",
"id": "2",
"params": {
"taskId": "task-123",
"id": "config-456"
}
}
""";
A2ARequest<?> request = JSONRPCUtils.parseRequestBody(validRequest, null);
assertNotNull(request);
assertInstanceOf(GetTaskPushNotificationConfigRequest.class, request);
GetTaskPushNotificationConfigRequest getRequest = (GetTaskPushNotificationConfigRequest) request;
assertEquals("2.0", getRequest.getJsonrpc());
assertEquals(2, getRequest.getId());
assertEquals("GetTaskPushNotificationConfig", getRequest.getMethod());
assertNotNull(getRequest.getParams());
assertEquals("task-123", getRequest.getParams().id());
}
@Test
public void testParseMalformedJSON_ThrowsJsonSyntaxException() {
String malformedRequest = """
{
"jsonrpc": "2.0",
"method": "CreateTaskPushNotificationConfig",
"params": {
"parent": "tasks/task-123"
"""; // Missing closing braces
JsonSyntaxException exception = assertThrows(JsonSyntaxException.class, () -> {
JSONRPCUtils.parseRequestBody(malformedRequest, null);
});
assertEquals("java.io.EOFException: End of input at line 6 column 1 path $.params.parent", exception.getMessage());
}
@Test
public void testParseInvalidParams_ThrowsInvalidParamsJsonMappingException() {
String invalidParamsRequest = """
{
"jsonrpc": "2.0",
"method": "CreateTaskPushNotificationConfig",
"id": "3",
"params": "not_a_dict"
}
""";
InvalidParamsJsonMappingException exception = assertThrows(
InvalidParamsJsonMappingException.class,
() -> JSONRPCUtils.parseRequestBody(invalidParamsRequest, null)
);
assertEquals(3, exception.getId());
}
@Test
public void testParseInvalidProtoStructure_ThrowsInvalidParamsJsonMappingException() {
String invalidStructure = """
{
"jsonrpc": "2.0",
"method": "CreateTaskPushNotificationConfig",
"id": "4",
"params": {
"invalid_field": "value"
}
}
""";
InvalidParamsJsonMappingException exception = assertThrows(
InvalidParamsJsonMappingException.class,
() -> JSONRPCUtils.parseRequestBody(invalidStructure, null)
);
assertEquals(4, exception.getId());
assertEquals(ERROR_MESSAGE.formatted("invalid_field in message a2a.v1.CreateTaskPushNotificationConfigRequest"), exception.getMessage());
}
@Test
public void testParseNumericalTimestampThrowsInvalidParamsJsonMappingException() {
String valideRequest = """
{
"jsonrpc": "2.0",
"method": "ListTasks",
"id": "1",
"params": {
"statusTimestampAfter": "2023-10-27T10:00:00Z"
}
}
""";
String invalidRequest = """
{
"jsonrpc": "2.0",
"method": "ListTasks",
"id": "2",
"params": {
"statusTimestampAfter": "1"
}
}
""";
try {
A2ARequest<?> request = JSONRPCUtils.parseRequestBody(valideRequest, null);
assertEquals(1, request.getId());
} catch (JsonProcessingException e) {
fail(e);
}
InvalidParamsJsonMappingException exception = assertThrows(
InvalidParamsJsonMappingException.class,
() -> JSONRPCUtils.parseRequestBody(invalidRequest, null)
);
assertEquals(2, exception.getId());
}
@Test
public void testParseMissingField_ThrowsInvalidParamsError() throws JsonMappingException {
String missingRoleMessage= """
{
"jsonrpc":"2.0",
"method":"SendMessage",
"id": "18",
"params":{
"message":{
"messageId":"message-1234",
"contextId":"context-1234",
"parts":[
{
"text":"tell me a joke"
}
],
"metadata":{
}
}
}
}""";
InvalidParamsJsonMappingException exception = assertThrows(
InvalidParamsJsonMappingException.class,
() -> JSONRPCUtils.parseRequestBody(missingRoleMessage, null)
);
assertEquals(18, exception.getId());
}
@Test
public void testParseUnknownField_ThrowsJsonMappingException() throws JsonMappingException {
String unkownFieldMessage= """
{
"jsonrpc":"2.0",
"method":"SendMessage",
"id": "18",
"params":{
"message":{
"role": "ROLE_AGENT",
"unknown":"field",
"messageId":"message-1234",
"contextId":"context-1234",
"parts":[
{
"text":"tell me a joke"
}
],
"metadata":{
}
}
}
}""";
JsonMappingException exception = assertThrows(
JsonMappingException.class,
() -> JSONRPCUtils.parseRequestBody(unkownFieldMessage, null)
);
assertEquals(ERROR_MESSAGE.formatted("unknown in message a2a.v1.Message"), exception.getMessage());
}
@Test
public void testParseInvalidTypeWithNullId_UsesEmptyStringSentinel() throws Exception {
// Test the low-level convertProtoBufExceptionToJsonProcessingException with null ID
// This tests the sentinel value logic directly
com.google.protobuf.InvalidProtocolBufferException protoException =
new com.google.protobuf.InvalidProtocolBufferException("Expected ENUM but found \"INVALID_VALUE\"");
// Use reflection to call the private method for testing
java.lang.reflect.Method method = JSONRPCUtils.class.getDeclaredMethod(
"convertProtoBufExceptionToJsonProcessingException",
com.google.protobuf.InvalidProtocolBufferException.class,
Object.class
);
method.setAccessible(true);
JsonProcessingException exception = (JsonProcessingException) method.invoke(
null,
protoException,
null // null ID
);
// Should be InvalidParamsJsonMappingException with empty string sentinel
assertInstanceOf(InvalidParamsJsonMappingException.class, exception,
"Expected InvalidParamsJsonMappingException for proto error");
InvalidParamsJsonMappingException invalidParamsException = (InvalidParamsJsonMappingException) exception;
assertEquals("", invalidParamsException.getId(),
"Expected empty string sentinel when ID is null");
}
@Test
public void testParseInvalidTypeWithValidId_PreservesId() throws Exception {
// Test the low-level convertProtoBufExceptionToJsonProcessingException with valid ID
com.google.protobuf.InvalidProtocolBufferException protoException =
new com.google.protobuf.InvalidProtocolBufferException("Expected ENUM but found \"INVALID_VALUE\"");
// Use reflection to call the private method for testing
java.lang.reflect.Method method = JSONRPCUtils.class.getDeclaredMethod(
"convertProtoBufExceptionToJsonProcessingException",
com.google.protobuf.InvalidProtocolBufferException.class,
Object.class
);
method.setAccessible(true);
JsonProcessingException exception = (JsonProcessingException) method.invoke(
null,
protoException,
42 // Valid ID
);
// Should be InvalidParamsJsonMappingException with preserved ID
assertInstanceOf(InvalidParamsJsonMappingException.class, exception,
"Expected InvalidParamsJsonMappingException for proto error");
InvalidParamsJsonMappingException invalidParamsException = (InvalidParamsJsonMappingException) exception;
assertEquals(42, invalidParamsException.getId(),
"Expected actual ID to be preserved when present");
}
@Test
public void testGenerateCreateTaskPushNotificationConfigResponse_Success() throws Exception {
TaskPushNotificationConfig config = new TaskPushNotificationConfig(
"task-123",
PushNotificationConfig.builder()
.url("https://example.com/callback")
.id("config-456")
.build(),
"tenant"
);
String responseJson = """
{
"jsonrpc": "2.0",
"id": "1",
"result": {
"taskId": "task-123",
"id": "config-456",
"pushNotificationConfig": {
"url": "https://example.com/callback",
"id": "config-456"
}
}
}
""";
CreateTaskPushNotificationConfigResponse response =
(CreateTaskPushNotificationConfigResponse) JSONRPCUtils.parseResponseBody(responseJson, SET_TASK_PUSH_NOTIFICATION_CONFIG_METHOD);
assertNotNull(response);
assertEquals(1, response.getId());
assertNotNull(response.getResult());
assertEquals("task-123", response.getResult().taskId());
assertEquals("https://example.com/callback", response.getResult().pushNotificationConfig().url());
}
@Test
public void testGenerateGetTaskPushNotificationConfigResponse_Success() throws Exception {
String responseJson = """
{
"jsonrpc": "2.0",
"id": "2",
"result": {
"taskId": "task-123",
"id": "config-456",
"pushNotificationConfig": {
"url": "https://example.com/callback",
"id": "config-456"
}
}
}
""";
GetTaskPushNotificationConfigResponse response =
(GetTaskPushNotificationConfigResponse) JSONRPCUtils.parseResponseBody(responseJson, GET_TASK_PUSH_NOTIFICATION_CONFIG_METHOD);
assertNotNull(response);
assertEquals(2, response.getId());
assertNotNull(response.getResult());
assertEquals("task-123", response.getResult().taskId());
assertEquals("https://example.com/callback", response.getResult().pushNotificationConfig().url());
}
@Test
public void testParseErrorResponse_InvalidParams() throws Exception {
String errorResponse = """
{
"jsonrpc": "2.0",
"id": "5",
"error": {
"code": -32602,
"message": "Invalid params"
}
}
""";
CreateTaskPushNotificationConfigResponse response =
(CreateTaskPushNotificationConfigResponse) JSONRPCUtils.parseResponseBody(errorResponse, SET_TASK_PUSH_NOTIFICATION_CONFIG_METHOD);
assertNotNull(response);
assertEquals(5, response.getId());
assertNotNull(response.getError());
assertInstanceOf(InvalidParamsError.class, response.getError());
assertEquals(-32602, response.getError().getCode());
assertEquals("Invalid params", response.getError().getMessage());
}
@Test
public void testParseErrorResponse_ParseError() throws Exception {
String errorResponse = """
{
"jsonrpc": "2.0",
"id": 6,
"error": {
"code": -32700,
"message": "Parse error"
}
}
""";
CreateTaskPushNotificationConfigResponse response =
(CreateTaskPushNotificationConfigResponse) JSONRPCUtils.parseResponseBody(errorResponse, SET_TASK_PUSH_NOTIFICATION_CONFIG_METHOD);
assertNotNull(response);
assertEquals(6, response.getId());
assertNotNull(response.getError());
assertInstanceOf(JSONParseError.class, response.getError());
assertEquals(-32700, response.getError().getCode());
assertEquals("Parse error", response.getError().getMessage());
}
}