|
| 1 | +package org.a2aproject.sdk.client.transport.rest; |
| 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 java.util.List; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +import org.a2aproject.sdk.client.http.A2AHttpHeaders; |
| 12 | +import org.a2aproject.sdk.client.http.A2AHttpResponse; |
| 13 | +import org.a2aproject.sdk.spec.A2AClientException; |
| 14 | +import org.a2aproject.sdk.spec.A2AClientHTTPError; |
| 15 | +import org.a2aproject.sdk.spec.TaskNotFoundError; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | + |
| 18 | +public class RestErrorMapperTest { |
| 19 | + |
| 20 | + @Test |
| 21 | + public void testEmptyBodyFallbackIncludesHeaders() { |
| 22 | + Map<String, List<String>> headers = Map.of("Retry-After", List.of("60")); |
| 23 | + A2AClientException ex = RestErrorMapper.mapRestError("", 429, headers); |
| 24 | + |
| 25 | + assertInstanceOf(A2AClientHTTPError.class, ex.getCause()); |
| 26 | + A2AClientHTTPError httpError = (A2AClientHTTPError) ex.getCause(); |
| 27 | + assertEquals(429, httpError.getCode()); |
| 28 | + assertEquals(List.of("60"), httpError.getResponseHeaders().get("Retry-After")); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void testNullBodyFallbackIncludesHeaders() { |
| 33 | + Map<String, List<String>> headers = Map.of("WWW-Authenticate", List.of("Bearer")); |
| 34 | + A2AClientException ex = RestErrorMapper.mapRestError(null, 401, headers); |
| 35 | + |
| 36 | + assertInstanceOf(A2AClientHTTPError.class, ex.getCause()); |
| 37 | + A2AClientHTTPError httpError = (A2AClientHTTPError) ex.getCause(); |
| 38 | + assertEquals(401, httpError.getCode()); |
| 39 | + assertEquals(List.of("Bearer"), httpError.getResponseHeaders().get("WWW-Authenticate")); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testUnrecognizedGoogleErrorFormatIncludesHeaders() { |
| 44 | + String body = "{\"error\": {\"code\": 503, \"message\": \"Service Unavailable\"}}"; |
| 45 | + Map<String, List<String>> headers = Map.of("Retry-After", List.of("30")); |
| 46 | + A2AClientException ex = RestErrorMapper.mapRestError(body, 503, headers); |
| 47 | + |
| 48 | + assertInstanceOf(A2AClientHTTPError.class, ex.getCause()); |
| 49 | + A2AClientHTTPError httpError = (A2AClientHTTPError) ex.getCause(); |
| 50 | + assertEquals(503, httpError.getCode()); |
| 51 | + assertEquals(List.of("30"), httpError.getResponseHeaders().get("Retry-After")); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testRecognizedA2AErrorDoesNotWrapInHttpError() { |
| 56 | + String body = "{\"error\": {\"code\": 404, \"status\": \"NOT_FOUND\", \"message\": \"Task not found\", " + |
| 57 | + "\"details\": [{\"reason\": \"TASK_NOT_FOUND\"}]}}"; |
| 58 | + A2AClientException ex = RestErrorMapper.mapRestError(body, 404, Map.of()); |
| 59 | + |
| 60 | + assertInstanceOf(TaskNotFoundError.class, ex.getCause()); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testMapRestErrorFromA2AHttpResponse() { |
| 65 | + Map<String, List<String>> headerMap = Map.of("X-Custom", List.of("value")); |
| 66 | + A2AHttpResponse response = new A2AHttpResponse() { |
| 67 | + @Override |
| 68 | + public int status() { |
| 69 | + return 500; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public boolean success() { |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public String body() { |
| 79 | + return ""; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public A2AHttpHeaders headers() { |
| 84 | + return new A2AHttpHeaders() { |
| 85 | + @Override |
| 86 | + public String firstValue(String name) { |
| 87 | + List<String> values = headerMap.get(name); |
| 88 | + return values != null && !values.isEmpty() ? values.get(0) : null; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public List<String> allValues(String name) { |
| 93 | + return headerMap.getOrDefault(name, List.of()); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public Map<String, List<String>> toMap() { |
| 98 | + return headerMap; |
| 99 | + } |
| 100 | + }; |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + A2AClientException ex = RestErrorMapper.mapRestError(response); |
| 105 | + assertInstanceOf(A2AClientHTTPError.class, ex.getCause()); |
| 106 | + A2AClientHTTPError httpError = (A2AClientHTTPError) ex.getCause(); |
| 107 | + assertEquals(500, httpError.getCode()); |
| 108 | + assertEquals(List.of("value"), httpError.getResponseHeaders().get("X-Custom")); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testHeaderLookupIsCaseInsensitive() { |
| 113 | + Map<String, List<String>> headers = Map.of("Retry-After", List.of("60")); |
| 114 | + A2AClientException ex = RestErrorMapper.mapRestError("", 429, headers); |
| 115 | + |
| 116 | + A2AClientHTTPError httpError = (A2AClientHTTPError) ex.getCause(); |
| 117 | + assertEquals(List.of("60"), httpError.getResponseHeaders().get("retry-after")); |
| 118 | + assertEquals(List.of("60"), httpError.getResponseHeaders().get("RETRY-AFTER")); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testTwoArgOverloadDefaultsToEmptyHeaders() { |
| 123 | + A2AClientException ex = RestErrorMapper.mapRestError("", 500); |
| 124 | + |
| 125 | + assertInstanceOf(A2AClientHTTPError.class, ex.getCause()); |
| 126 | + A2AClientHTTPError httpError = (A2AClientHTTPError) ex.getCause(); |
| 127 | + assertNotNull(httpError.getResponseHeaders()); |
| 128 | + assertTrue(httpError.getResponseHeaders().isEmpty()); |
| 129 | + } |
| 130 | +} |
0 commit comments