|
| 1 | +package io.a2a.client.transport.grpc; |
| 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.assertTrue; |
| 7 | + |
| 8 | +import io.a2a.spec.A2AClientException; |
| 9 | +import io.a2a.spec.ContentTypeNotSupportedError; |
| 10 | +import io.a2a.spec.ExtendedCardNotConfiguredError; |
| 11 | +import io.a2a.spec.ExtensionSupportRequiredError; |
| 12 | +import io.a2a.spec.InvalidAgentResponseError; |
| 13 | +import io.a2a.spec.InvalidParamsError; |
| 14 | +import io.a2a.spec.InvalidRequestError; |
| 15 | +import io.a2a.spec.JSONParseError; |
| 16 | +import io.a2a.spec.MethodNotFoundError; |
| 17 | +import io.a2a.spec.PushNotificationNotSupportedError; |
| 18 | +import io.a2a.spec.TaskNotCancelableError; |
| 19 | +import io.a2a.spec.TaskNotFoundError; |
| 20 | +import io.a2a.spec.UnsupportedOperationError; |
| 21 | +import io.a2a.spec.VersionNotSupportedError; |
| 22 | +import io.grpc.Status; |
| 23 | +import io.grpc.StatusRuntimeException; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +/** |
| 27 | + * Tests for GrpcErrorMapper - verifies correct unmarshalling of gRPC errors to A2A error types |
| 28 | + */ |
| 29 | +public class GrpcErrorMapperTest { |
| 30 | + |
| 31 | + @Test |
| 32 | + public void testExtensionSupportRequiredErrorUnmarshalling() { |
| 33 | + // Create a gRPC StatusRuntimeException with ExtensionSupportRequiredError in description |
| 34 | + String errorMessage = "ExtensionSupportRequiredError: Extension required: https://example.com/test-extension"; |
| 35 | + StatusRuntimeException grpcException = Status.FAILED_PRECONDITION |
| 36 | + .withDescription(errorMessage) |
| 37 | + .asRuntimeException(); |
| 38 | + |
| 39 | + // Map the gRPC error to A2A error |
| 40 | + A2AClientException result = GrpcErrorMapper.mapGrpcError(grpcException); |
| 41 | + |
| 42 | + // Verify the result |
| 43 | + assertNotNull(result); |
| 44 | + assertNotNull(result.getCause()); |
| 45 | + assertInstanceOf(ExtensionSupportRequiredError.class, result.getCause()); |
| 46 | + |
| 47 | + ExtensionSupportRequiredError extensionError = (ExtensionSupportRequiredError) result.getCause(); |
| 48 | + assertNotNull(extensionError.getMessage()); |
| 49 | + assertTrue(extensionError.getMessage().contains("https://example.com/test-extension")); |
| 50 | + assertTrue(result.getMessage().contains(errorMessage)); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testVersionNotSupportedErrorUnmarshalling() { |
| 55 | + // Create a gRPC StatusRuntimeException with VersionNotSupportedError in description |
| 56 | + String errorMessage = "VersionNotSupportedError: Version 2.0 is not supported"; |
| 57 | + StatusRuntimeException grpcException = Status.FAILED_PRECONDITION |
| 58 | + .withDescription(errorMessage) |
| 59 | + .asRuntimeException(); |
| 60 | + |
| 61 | + // Map the gRPC error to A2A error |
| 62 | + A2AClientException result = GrpcErrorMapper.mapGrpcError(grpcException); |
| 63 | + |
| 64 | + // Verify the result |
| 65 | + assertNotNull(result); |
| 66 | + assertNotNull(result.getCause()); |
| 67 | + assertInstanceOf(VersionNotSupportedError.class, result.getCause()); |
| 68 | + |
| 69 | + VersionNotSupportedError versionError = (VersionNotSupportedError) result.getCause(); |
| 70 | + assertNotNull(versionError.getMessage()); |
| 71 | + assertTrue(versionError.getMessage().contains("Version 2.0 is not supported")); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testExtendedCardNotConfiguredErrorUnmarshalling() { |
| 76 | + // Create a gRPC StatusRuntimeException with ExtendedCardNotConfiguredError in description |
| 77 | + String errorMessage = "ExtendedCardNotConfiguredError: Extended card not configured for this agent"; |
| 78 | + StatusRuntimeException grpcException = Status.FAILED_PRECONDITION |
| 79 | + .withDescription(errorMessage) |
| 80 | + .asRuntimeException(); |
| 81 | + |
| 82 | + // Map the gRPC error to A2A error |
| 83 | + A2AClientException result = GrpcErrorMapper.mapGrpcError(grpcException); |
| 84 | + |
| 85 | + // Verify the result |
| 86 | + assertNotNull(result); |
| 87 | + assertNotNull(result.getCause()); |
| 88 | + assertInstanceOf(ExtendedCardNotConfiguredError.class, result.getCause()); |
| 89 | + |
| 90 | + ExtendedCardNotConfiguredError extendedCardError = (ExtendedCardNotConfiguredError) result.getCause(); |
| 91 | + assertNotNull(extendedCardError.getMessage()); |
| 92 | + assertTrue(extendedCardError.getMessage().contains("Extended card not configured")); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testTaskNotFoundErrorUnmarshalling() { |
| 97 | + // Create a gRPC StatusRuntimeException with TaskNotFoundError in description |
| 98 | + String errorMessage = "TaskNotFoundError: Task task-123 not found"; |
| 99 | + StatusRuntimeException grpcException = Status.NOT_FOUND |
| 100 | + .withDescription(errorMessage) |
| 101 | + .asRuntimeException(); |
| 102 | + |
| 103 | + // Map the gRPC error to A2A error |
| 104 | + A2AClientException result = GrpcErrorMapper.mapGrpcError(grpcException); |
| 105 | + |
| 106 | + // Verify the result |
| 107 | + assertNotNull(result); |
| 108 | + assertNotNull(result.getCause()); |
| 109 | + assertInstanceOf(TaskNotFoundError.class, result.getCause()); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void testUnsupportedOperationErrorUnmarshalling() { |
| 114 | + // Create a gRPC StatusRuntimeException with UnsupportedOperationError in description |
| 115 | + String errorMessage = "UnsupportedOperationError: Operation not supported"; |
| 116 | + StatusRuntimeException grpcException = Status.UNIMPLEMENTED |
| 117 | + .withDescription(errorMessage) |
| 118 | + .asRuntimeException(); |
| 119 | + |
| 120 | + // Map the gRPC error to A2A error |
| 121 | + A2AClientException result = GrpcErrorMapper.mapGrpcError(grpcException); |
| 122 | + |
| 123 | + // Verify the result |
| 124 | + assertNotNull(result); |
| 125 | + assertNotNull(result.getCause()); |
| 126 | + assertInstanceOf(UnsupportedOperationError.class, result.getCause()); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void testInvalidParamsErrorUnmarshalling() { |
| 131 | + // Create a gRPC StatusRuntimeException with InvalidParamsError in description |
| 132 | + String errorMessage = "InvalidParamsError: Invalid parameters provided"; |
| 133 | + StatusRuntimeException grpcException = Status.INVALID_ARGUMENT |
| 134 | + .withDescription(errorMessage) |
| 135 | + .asRuntimeException(); |
| 136 | + |
| 137 | + // Map the gRPC error to A2A error |
| 138 | + A2AClientException result = GrpcErrorMapper.mapGrpcError(grpcException); |
| 139 | + |
| 140 | + // Verify the result |
| 141 | + assertNotNull(result); |
| 142 | + assertNotNull(result.getCause()); |
| 143 | + assertInstanceOf(InvalidParamsError.class, result.getCause()); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void testContentTypeNotSupportedErrorUnmarshalling() { |
| 148 | + // Create a gRPC StatusRuntimeException with ContentTypeNotSupportedError in description |
| 149 | + String errorMessage = "ContentTypeNotSupportedError: Content type application/xml not supported"; |
| 150 | + StatusRuntimeException grpcException = Status.FAILED_PRECONDITION |
| 151 | + .withDescription(errorMessage) |
| 152 | + .asRuntimeException(); |
| 153 | + |
| 154 | + // Map the gRPC error to A2A error |
| 155 | + A2AClientException result = GrpcErrorMapper.mapGrpcError(grpcException); |
| 156 | + |
| 157 | + // Verify the result |
| 158 | + assertNotNull(result); |
| 159 | + assertNotNull(result.getCause()); |
| 160 | + assertInstanceOf(ContentTypeNotSupportedError.class, result.getCause()); |
| 161 | + |
| 162 | + ContentTypeNotSupportedError contentTypeError = (ContentTypeNotSupportedError) result.getCause(); |
| 163 | + assertNotNull(contentTypeError.getMessage()); |
| 164 | + assertTrue(contentTypeError.getMessage().contains("Content type application/xml not supported")); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + public void testFallbackToStatusCodeMapping() { |
| 169 | + // Create a gRPC StatusRuntimeException without specific error type in description |
| 170 | + StatusRuntimeException grpcException = Status.NOT_FOUND |
| 171 | + .withDescription("Generic not found error") |
| 172 | + .asRuntimeException(); |
| 173 | + |
| 174 | + // Map the gRPC error to A2A error |
| 175 | + A2AClientException result = GrpcErrorMapper.mapGrpcError(grpcException); |
| 176 | + |
| 177 | + // Verify fallback to status code mapping |
| 178 | + assertNotNull(result); |
| 179 | + assertNotNull(result.getCause()); |
| 180 | + assertInstanceOf(TaskNotFoundError.class, result.getCause()); |
| 181 | + } |
| 182 | + |
| 183 | + @Test |
| 184 | + public void testCustomErrorPrefix() { |
| 185 | + // Create a gRPC StatusRuntimeException |
| 186 | + String errorMessage = "ExtensionSupportRequiredError: Extension required: https://example.com/ext"; |
| 187 | + StatusRuntimeException grpcException = Status.FAILED_PRECONDITION |
| 188 | + .withDescription(errorMessage) |
| 189 | + .asRuntimeException(); |
| 190 | + |
| 191 | + // Map with custom error prefix |
| 192 | + String customPrefix = "Custom Error: "; |
| 193 | + A2AClientException result = GrpcErrorMapper.mapGrpcError(grpcException, customPrefix); |
| 194 | + |
| 195 | + // Verify custom prefix is used |
| 196 | + assertNotNull(result); |
| 197 | + assertTrue(result.getMessage().startsWith(customPrefix)); |
| 198 | + assertInstanceOf(ExtensionSupportRequiredError.class, result.getCause()); |
| 199 | + } |
| 200 | +} |
0 commit comments