Skip to content

Commit 7292fc3

Browse files
committed
refactor: Simplify TestHttpTransport by inheriting from JdkA2AHttpTransport.
Signed-off-by: Sun Yuhan <[email protected]>
1 parent a7026d0 commit 7292fc3

File tree

3 files changed

+6
-138
lines changed

3 files changed

+6
-138
lines changed

client/src/test/java/io/a2a/client/A2ACardResolverTest.java

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,14 @@
88
import static org.junit.jupiter.api.Assertions.assertTrue;
99

1010
import java.io.IOException;
11-
import java.util.Map;
1211
import java.util.concurrent.CompletableFuture;
1312
import java.util.function.Consumer;
1413

15-
import com.fasterxml.jackson.core.JsonProcessingException;
16-
import com.fasterxml.jackson.core.type.TypeReference;
17-
import io.a2a.spec.Event;
18-
import io.a2a.spec.JSONRPCRequest;
19-
import io.a2a.spec.JSONRPCResponse;
2014
import io.a2a.transport.http.A2AHttpResponse;
2115
import io.a2a.spec.A2AClientError;
2216
import io.a2a.spec.A2AClientJSONError;
2317
import io.a2a.spec.AgentCard;
24-
import io.a2a.transport.http.A2AHttpTransport;
18+
import io.a2a.transport.http.JdkA2AHttpTransport;
2519
import org.junit.jupiter.api.Test;
2620

2721
public class A2ACardResolverTest {
@@ -111,7 +105,7 @@ public void testGetAgentCardRequestError() throws Exception {
111105
assertTrue(msg.contains("503"));
112106
}
113107

114-
private static class TestHttpTransport implements A2AHttpTransport {
108+
private static class TestHttpTransport extends JdkA2AHttpTransport {
115109
int status = 200;
116110
String body;
117111
String url;
@@ -126,51 +120,6 @@ public PostBuilder createPost() {
126120
return null;
127121
}
128122

129-
@Override
130-
public AgentCard getAgentCard(String method, Map<String, String> authInfo) throws A2AClientError {
131-
GetBuilder builder = createGet()
132-
.url(method)
133-
.addHeader("Content-Type", "application/json");
134-
135-
if (authInfo != null) {
136-
for (Map.Entry<String, String> entry : authInfo.entrySet()) {
137-
builder.addHeader(entry.getKey(), entry.getValue());
138-
}
139-
}
140-
141-
String body;
142-
try {
143-
A2AHttpResponse response = builder.get();
144-
if (!response.success()) {
145-
throw new A2AClientError("Failed to obtain agent card: " + response.status());
146-
}
147-
body = response.body();
148-
} catch (IOException | InterruptedException e) {
149-
throw new A2AClientError("Failed to obtain agent card", e);
150-
}
151-
152-
try {
153-
return unmarshalFrom(body, AGENT_CARD_TYPE_REFERENCE);
154-
} catch (JsonProcessingException e) {
155-
throw new A2AClientJSONError("Could not unmarshal agent card response", e);
156-
}
157-
}
158-
159-
@Override
160-
public void sendEvent(Event event, String method) throws IOException, InterruptedException {
161-
162-
}
163-
164-
@Override
165-
public <T extends JSONRPCResponse<?>> T sendMessage(JSONRPCRequest<?> request, String operation, TypeReference<T> responseTypeRef) throws IOException, InterruptedException {
166-
return null;
167-
}
168-
169-
@Override
170-
public <T extends JSONRPCResponse<?>> CompletableFuture<Void> sendMessageStreaming(JSONRPCRequest<?> request, String operation, TypeReference<T> responseTypeRef, Consumer<T> responseConsumer, Consumer<Throwable> errorConsumer, Runnable completeRunnable) throws IOException, InterruptedException {
171-
return null;
172-
}
173-
174123
class TestGetBuilder implements GetBuilder {
175124

176125
@Override

sdk-server-common/src/test/java/io/a2a/server/requesthandlers/JSONRPCHandlerTest.java

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package io.a2a.server.requesthandlers;
22

3-
import static io.a2a.util.Utils.OBJECT_MAPPER;
4-
import static io.a2a.util.Utils.unmarshalFrom;
53
import static org.junit.jupiter.api.Assertions.assertEquals;
64
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
75
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -14,7 +12,6 @@
1412
import java.util.ArrayList;
1513
import java.util.Collections;
1614
import java.util.List;
17-
import java.util.Map;
1815
import java.util.concurrent.CompletableFuture;
1916
import java.util.concurrent.CountDownLatch;
2017
import java.util.concurrent.Executor;
@@ -24,14 +21,8 @@
2421
import java.util.concurrent.atomic.AtomicReference;
2522
import java.util.function.Consumer;
2623

27-
import com.fasterxml.jackson.core.JsonProcessingException;
28-
import com.fasterxml.jackson.core.type.TypeReference;
29-
import io.a2a.spec.A2AClientError;
30-
import io.a2a.spec.A2AServerException;
3124
import io.a2a.spec.InternalError;
32-
import io.a2a.spec.JSONRPCRequest;
33-
import io.a2a.spec.JSONRPCResponse;
34-
import io.a2a.transport.http.A2AHttpTransport;
25+
import io.a2a.transport.http.JdkA2AHttpTransport;
3526
import jakarta.enterprise.context.Dependent;
3627

3728
import io.a2a.transport.http.A2AHttpResponse;
@@ -1273,7 +1264,7 @@ private interface AgentExecutorMethod {
12731264

12741265
@Dependent
12751266
@IfBuildProfile("test")
1276-
private static class TestHttpTransport implements A2AHttpTransport {
1267+
private static class TestHttpTransport extends JdkA2AHttpTransport {
12771268
final List<Task> tasks = Collections.synchronizedList(new ArrayList<>());
12781269
volatile CountDownLatch latch;
12791270

@@ -1287,51 +1278,6 @@ public PostBuilder createPost() {
12871278
return new TestPostBuilder();
12881279
}
12891280

1290-
@Override
1291-
public AgentCard getAgentCard(String method, Map<String, String> authInfo) throws A2AClientError {
1292-
return null;
1293-
}
1294-
1295-
@Override
1296-
public void sendEvent(Event event, String method) throws IOException, InterruptedException {
1297-
String body = Utils.OBJECT_MAPPER.writeValueAsString(event);
1298-
createPost().url(method).body(body).post();
1299-
}
1300-
1301-
@Override
1302-
public <T extends JSONRPCResponse<?>> T sendMessage(JSONRPCRequest<?> request, String operation, TypeReference<T> responseTypeRef) throws IOException, InterruptedException {
1303-
PostBuilder postBuilder = createPostBuilder(request, operation);
1304-
A2AHttpResponse response = postBuilder.post();
1305-
1306-
if (!response.success()) {
1307-
throw new IOException("Request failed " + response.status());
1308-
}
1309-
1310-
return unmarshalResponse(response.body(), responseTypeRef);
1311-
}
1312-
1313-
@Override
1314-
public <T extends JSONRPCResponse<?>> CompletableFuture<Void> sendMessageStreaming(JSONRPCRequest<?> request, String operation, TypeReference<T> responseTypeRef, Consumer<T> responseConsumer, Consumer<Throwable> errorConsumer, Runnable completeRunnable) throws IOException, InterruptedException {
1315-
return null;
1316-
}
1317-
1318-
private PostBuilder createPostBuilder(JSONRPCRequest<?> request, String method) throws JsonProcessingException {
1319-
return createPost()
1320-
.url(method)
1321-
.addHeader("Content-Type", "application/json")
1322-
.body(OBJECT_MAPPER.writeValueAsString(request));
1323-
}
1324-
1325-
private <T extends JSONRPCResponse<?>> T unmarshalResponse(String response, TypeReference<T> typeReference)
1326-
throws A2AServerException, JsonProcessingException {
1327-
T value = unmarshalFrom(response, typeReference);
1328-
JSONRPCError error = value.getError();
1329-
if (error != null) {
1330-
throw new A2AServerException(error.getMessage() + (error.getData() != null ? ": " + error.getData() : ""));
1331-
}
1332-
return value;
1333-
}
1334-
13351281
class TestPostBuilder implements PostBuilder {
13361282
private volatile String body;
13371283
@Override

tests/server-common/src/test/java/io/a2a/server/apps/common/TestHttpTransport.java

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,11 @@
44
import java.util.ArrayList;
55
import java.util.Collections;
66
import java.util.List;
7-
import java.util.Map;
87
import java.util.concurrent.CompletableFuture;
98
import java.util.concurrent.CountDownLatch;
109
import java.util.function.Consumer;
1110

12-
import com.fasterxml.jackson.core.type.TypeReference;
13-
import io.a2a.spec.A2AClientError;
14-
import io.a2a.spec.AgentCard;
15-
import io.a2a.spec.Event;
16-
import io.a2a.spec.JSONRPCRequest;
17-
import io.a2a.spec.JSONRPCResponse;
18-
import io.a2a.transport.http.A2AHttpTransport;
11+
import io.a2a.transport.http.JdkA2AHttpTransport;
1912
import jakarta.enterprise.context.Dependent;
2013
import jakarta.enterprise.inject.Alternative;
2114

@@ -25,7 +18,7 @@
2518

2619
@Dependent
2720
@Alternative
28-
public class TestHttpTransport implements A2AHttpTransport {
21+
public class TestHttpTransport extends JdkA2AHttpTransport {
2922
final List<Task> tasks = Collections.synchronizedList(new ArrayList<>());
3023
volatile CountDownLatch latch;
3124

@@ -39,26 +32,6 @@ public PostBuilder createPost() {
3932
return new TestPostBuilder();
4033
}
4134

42-
@Override
43-
public AgentCard getAgentCard(String method, Map<String, String> authInfo) throws A2AClientError {
44-
return null;
45-
}
46-
47-
@Override
48-
public void sendEvent(Event event, String method) throws IOException, InterruptedException {
49-
50-
}
51-
52-
@Override
53-
public <T extends JSONRPCResponse<?>> T sendMessage(JSONRPCRequest<?> request, String operation, TypeReference<T> responseTypeRef) throws IOException, InterruptedException {
54-
return null;
55-
}
56-
57-
@Override
58-
public <T extends JSONRPCResponse<?>> CompletableFuture<Void> sendMessageStreaming(JSONRPCRequest<?> request, String operation, TypeReference<T> responseTypeRef, Consumer<T> responseConsumer, Consumer<Throwable> errorConsumer, Runnable completeRunnable) throws IOException, InterruptedException {
59-
return null;
60-
}
61-
6235
class TestPostBuilder implements PostBuilder {
6336
private volatile String body;
6437
@Override

0 commit comments

Comments
 (0)