|
| 1 | +package io.a2a.grpc.utils; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 9 | +import com.google.gson.JsonSyntaxException; |
| 10 | +import io.a2a.spec.GetTaskPushNotificationConfigRequest; |
| 11 | +import io.a2a.spec.GetTaskPushNotificationConfigResponse; |
| 12 | +import io.a2a.spec.InvalidParamsError; |
| 13 | +import io.a2a.spec.InvalidParamsJsonMappingException; |
| 14 | +import io.a2a.spec.JSONParseError; |
| 15 | +import io.a2a.spec.JSONRPCRequest; |
| 16 | +import io.a2a.spec.SetTaskPushNotificationConfigRequest; |
| 17 | +import io.a2a.spec.SetTaskPushNotificationConfigResponse; |
| 18 | +import io.a2a.spec.TaskPushNotificationConfig; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +public class JSONRPCUtilsTest { |
| 22 | + |
| 23 | + @Test |
| 24 | + public void testParseSetTaskPushNotificationConfigRequest_ValidProtoFormat() throws JsonProcessingException { |
| 25 | + String validRequest = """ |
| 26 | + { |
| 27 | + "jsonrpc": "2.0", |
| 28 | + "method": "SetTaskPushNotificationConfig", |
| 29 | + "id": "1", |
| 30 | + "params": { |
| 31 | + "parent": "tasks/task-123", |
| 32 | + "configId": "config-456", |
| 33 | + "config": { |
| 34 | + "name": "tasks/task-123/pushNotificationConfigs/config-456", |
| 35 | + "pushNotificationConfig": { |
| 36 | + "url": "https://example.com/callback", |
| 37 | + "authentication": { |
| 38 | + "schemes": ["jwt"] |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + """; |
| 45 | + |
| 46 | + JSONRPCRequest<?> request = JSONRPCUtils.parseRequestBody(validRequest); |
| 47 | + |
| 48 | + assertNotNull(request); |
| 49 | + assertInstanceOf(SetTaskPushNotificationConfigRequest.class, request); |
| 50 | + SetTaskPushNotificationConfigRequest setRequest = (SetTaskPushNotificationConfigRequest) request; |
| 51 | + assertEquals("2.0", setRequest.getJsonrpc()); |
| 52 | + assertEquals(1, setRequest.getId()); |
| 53 | + assertEquals("SetTaskPushNotificationConfig", setRequest.getMethod()); |
| 54 | + |
| 55 | + TaskPushNotificationConfig config = setRequest.getParams(); |
| 56 | + assertNotNull(config); |
| 57 | + assertEquals("task-123", config.taskId()); |
| 58 | + assertNotNull(config.pushNotificationConfig()); |
| 59 | + assertEquals("https://example.com/callback", config.pushNotificationConfig().url()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testParseGetTaskPushNotificationConfigRequest_ValidProtoFormat() throws JsonProcessingException { |
| 64 | + String validRequest = """ |
| 65 | + { |
| 66 | + "jsonrpc": "2.0", |
| 67 | + "method": "GetTaskPushNotificationConfig", |
| 68 | + "id": "2", |
| 69 | + "params": { |
| 70 | + "name": "tasks/task-123/pushNotificationConfigs/config-456" |
| 71 | + } |
| 72 | + } |
| 73 | + """; |
| 74 | + |
| 75 | + JSONRPCRequest<?> request = JSONRPCUtils.parseRequestBody(validRequest); |
| 76 | + |
| 77 | + assertNotNull(request); |
| 78 | + assertInstanceOf(GetTaskPushNotificationConfigRequest.class, request); |
| 79 | + GetTaskPushNotificationConfigRequest getRequest = (GetTaskPushNotificationConfigRequest) request; |
| 80 | + assertEquals("2.0", getRequest.getJsonrpc()); |
| 81 | + assertEquals(2, getRequest.getId()); |
| 82 | + assertEquals("GetTaskPushNotificationConfig", getRequest.getMethod()); |
| 83 | + assertNotNull(getRequest.getParams()); |
| 84 | + assertEquals("task-123", getRequest.getParams().id()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testParseMalformedJSON_ThrowsJsonSyntaxException() { |
| 89 | + String malformedRequest = """ |
| 90 | + { |
| 91 | + "jsonrpc": "2.0", |
| 92 | + "method": "SetTaskPushNotificationConfig", |
| 93 | + "params": { |
| 94 | + "parent": "tasks/task-123" |
| 95 | + """; // Missing closing braces |
| 96 | + |
| 97 | + assertThrows(JsonSyntaxException.class, () -> { |
| 98 | + JSONRPCUtils.parseRequestBody(malformedRequest); |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testParseInvalidParams_ThrowsInvalidParamsError() { |
| 104 | + String invalidParamsRequest = """ |
| 105 | + { |
| 106 | + "jsonrpc": "2.0", |
| 107 | + "method": "SetTaskPushNotificationConfig", |
| 108 | + "id": "3", |
| 109 | + "params": "not_a_dict" |
| 110 | + } |
| 111 | + """; |
| 112 | + |
| 113 | + InvalidParamsJsonMappingException exception = assertThrows( |
| 114 | + InvalidParamsJsonMappingException.class, |
| 115 | + () -> JSONRPCUtils.parseRequestBody(invalidParamsRequest) |
| 116 | + ); |
| 117 | + assertEquals(3, exception.getId()); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testParseInvalidProtoStructure_ThrowsInvalidParamsError() { |
| 122 | + String invalidStructure = """ |
| 123 | + { |
| 124 | + "jsonrpc": "2.0", |
| 125 | + "method": "SetTaskPushNotificationConfig", |
| 126 | + "id": "4", |
| 127 | + "params": { |
| 128 | + "invalid_field": "value" |
| 129 | + } |
| 130 | + } |
| 131 | + """; |
| 132 | + |
| 133 | + InvalidParamsJsonMappingException exception = assertThrows( |
| 134 | + InvalidParamsJsonMappingException.class, |
| 135 | + () -> JSONRPCUtils.parseRequestBody(invalidStructure) |
| 136 | + ); |
| 137 | + assertEquals(4, exception.getId()); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void testGenerateSetTaskPushNotificationConfigResponse_Success() throws Exception { |
| 142 | + TaskPushNotificationConfig config = new TaskPushNotificationConfig( |
| 143 | + "task-123", |
| 144 | + new io.a2a.spec.PushNotificationConfig.Builder() |
| 145 | + .url("https://example.com/callback") |
| 146 | + .id("config-456") |
| 147 | + .build() |
| 148 | + ); |
| 149 | + |
| 150 | + String responseJson = """ |
| 151 | + { |
| 152 | + "jsonrpc": "2.0", |
| 153 | + "id": "1", |
| 154 | + "result": { |
| 155 | + "name": "tasks/task-123/pushNotificationConfigs/config-456", |
| 156 | + "pushNotificationConfig": { |
| 157 | + "url": "https://example.com/callback", |
| 158 | + "id": "config-456" |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + """; |
| 163 | + |
| 164 | + SetTaskPushNotificationConfigResponse response = |
| 165 | + (SetTaskPushNotificationConfigResponse) JSONRPCUtils.parseResponseBody(responseJson, SetTaskPushNotificationConfigRequest.METHOD); |
| 166 | + |
| 167 | + assertNotNull(response); |
| 168 | + assertEquals(1, response.getId()); |
| 169 | + assertNotNull(response.getResult()); |
| 170 | + assertEquals("task-123", response.getResult().taskId()); |
| 171 | + assertEquals("https://example.com/callback", response.getResult().pushNotificationConfig().url()); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + public void testGenerateGetTaskPushNotificationConfigResponse_Success() throws Exception { |
| 176 | + String responseJson = """ |
| 177 | + { |
| 178 | + "jsonrpc": "2.0", |
| 179 | + "id": "2", |
| 180 | + "result": { |
| 181 | + "name": "tasks/task-123/pushNotificationConfigs/config-456", |
| 182 | + "pushNotificationConfig": { |
| 183 | + "url": "https://example.com/callback", |
| 184 | + "id": "config-456" |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + """; |
| 189 | + |
| 190 | + GetTaskPushNotificationConfigResponse response = |
| 191 | + (GetTaskPushNotificationConfigResponse) JSONRPCUtils.parseResponseBody(responseJson, GetTaskPushNotificationConfigRequest.METHOD); |
| 192 | + |
| 193 | + assertNotNull(response); |
| 194 | + assertEquals(2, response.getId()); |
| 195 | + assertNotNull(response.getResult()); |
| 196 | + assertEquals("task-123", response.getResult().taskId()); |
| 197 | + assertEquals("https://example.com/callback", response.getResult().pushNotificationConfig().url()); |
| 198 | + } |
| 199 | + |
| 200 | + @Test |
| 201 | + public void testParseErrorResponse_InvalidParams() throws Exception { |
| 202 | + String errorResponse = """ |
| 203 | + { |
| 204 | + "jsonrpc": "2.0", |
| 205 | + "id": "5", |
| 206 | + "error": { |
| 207 | + "code": -32602, |
| 208 | + "message": "Invalid params" |
| 209 | + } |
| 210 | + } |
| 211 | + """; |
| 212 | + |
| 213 | + SetTaskPushNotificationConfigResponse response = |
| 214 | + (SetTaskPushNotificationConfigResponse) JSONRPCUtils.parseResponseBody(errorResponse, SetTaskPushNotificationConfigRequest.METHOD); |
| 215 | + |
| 216 | + assertNotNull(response); |
| 217 | + assertEquals(5, response.getId()); |
| 218 | + assertNotNull(response.getError()); |
| 219 | + assertInstanceOf(InvalidParamsError.class, response.getError()); |
| 220 | + assertEquals(-32602, response.getError().getCode()); |
| 221 | + assertEquals("Invalid params", response.getError().getMessage()); |
| 222 | + } |
| 223 | + |
| 224 | + @Test |
| 225 | + public void testParseErrorResponse_ParseError() throws Exception { |
| 226 | + String errorResponse = """ |
| 227 | + { |
| 228 | + "jsonrpc": "2.0", |
| 229 | + "id": 6, |
| 230 | + "error": { |
| 231 | + "code": -32700, |
| 232 | + "message": "Parse error" |
| 233 | + } |
| 234 | + } |
| 235 | + """; |
| 236 | + |
| 237 | + SetTaskPushNotificationConfigResponse response = |
| 238 | + (SetTaskPushNotificationConfigResponse) JSONRPCUtils.parseResponseBody(errorResponse, SetTaskPushNotificationConfigRequest.METHOD); |
| 239 | + |
| 240 | + assertNotNull(response); |
| 241 | + assertEquals(6, response.getId()); |
| 242 | + assertNotNull(response.getError()); |
| 243 | + assertInstanceOf(JSONParseError.class, response.getError()); |
| 244 | + assertEquals(-32700, response.getError().getCode()); |
| 245 | + assertEquals("Parse error", response.getError().getMessage()); |
| 246 | + } |
| 247 | +} |
0 commit comments