Repository navigation
Expand file tree
/
Copy pathAbstractA2ARequestHandlerTest.java
More file actions
273 lines (235 loc) · 9.87 KB
/
Copy pathAbstractA2ARequestHandlerTest.java
File metadata and controls
273 lines (235 loc) · 9.87 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
package io.a2a.server.requesthandlers;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
import jakarta.enterprise.context.Dependent;
import io.a2a.client.http.A2AHttpClient;
import io.a2a.client.http.A2AHttpResponse;
import io.a2a.jsonrpc.common.json.JsonProcessingException;
import io.a2a.jsonrpc.common.json.JsonUtil;
import io.a2a.server.agentexecution.AgentExecutor;
import io.a2a.server.agentexecution.RequestContext;
import io.a2a.server.events.EventQueue;
import io.a2a.server.events.EventQueueItem;
import io.a2a.server.events.EventQueueUtil;
import io.a2a.server.events.InMemoryQueueManager;
import io.a2a.server.events.MainEventBus;
import io.a2a.server.events.MainEventBusProcessor;
import io.a2a.server.tasks.BasePushNotificationSender;
import io.a2a.server.tasks.InMemoryPushNotificationConfigStore;
import io.a2a.server.tasks.InMemoryTaskStore;
import io.a2a.server.tasks.PushNotificationConfigStore;
import io.a2a.server.tasks.PushNotificationSender;
import io.a2a.server.tasks.TaskStore;
import io.a2a.spec.A2AError;
import io.a2a.spec.AgentCapabilities;
import io.a2a.spec.AgentCard;
import io.a2a.spec.AgentInterface;
import io.a2a.spec.Event;
import io.a2a.spec.Message;
import io.a2a.spec.StreamingEventKind;
import io.a2a.spec.Task;
import io.a2a.spec.TaskState;
import io.a2a.spec.TaskStatus;
import io.a2a.spec.TextPart;
import io.quarkus.arc.profile.IfBuildProfile;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
public class AbstractA2ARequestHandlerTest {
protected static final AgentCard CARD = createAgentCard(true, true);
protected static final Task MINIMAL_TASK = Task.builder()
.id("task-123")
.contextId("session-xyz")
.status(new TaskStatus(TaskState.SUBMITTED))
.build();
protected static final Message MESSAGE = Message.builder()
.messageId("111")
.role(Message.Role.AGENT)
.parts(new TextPart("test message"))
.build();
private static final String PREFERRED_TRANSPORT = "preferred-transport";
private static final String A2A_REQUESTHANDLER_TEST_PROPERTIES = "/a2a-requesthandler-test.properties";
private static final PushNotificationSender NOOP_PUSHNOTIFICATION_SENDER = task -> {};
protected AgentExecutor executor;
protected TaskStore taskStore;
protected RequestHandler requestHandler;
protected AgentExecutorMethod agentExecutorExecute;
protected AgentExecutorMethod agentExecutorCancel;
protected InMemoryQueueManager queueManager;
protected TestHttpClient httpClient;
protected MainEventBus mainEventBus;
protected MainEventBusProcessor mainEventBusProcessor;
protected final Executor internalExecutor = Executors.newCachedThreadPool();
@BeforeEach
public void init() {
executor = new AgentExecutor() {
@Override
public void execute(RequestContext context, EventQueue eventQueue) throws A2AError {
if (agentExecutorExecute != null) {
agentExecutorExecute.invoke(context, eventQueue);
}
}
@Override
public void cancel(RequestContext context, EventQueue eventQueue) throws A2AError {
if (agentExecutorCancel != null) {
agentExecutorCancel.invoke(context, eventQueue);
}
}
};
InMemoryTaskStore inMemoryTaskStore = new InMemoryTaskStore();
taskStore = inMemoryTaskStore;
// Create push notification components BEFORE MainEventBusProcessor
httpClient = new TestHttpClient();
PushNotificationConfigStore pushConfigStore = new InMemoryPushNotificationConfigStore();
PushNotificationSender pushSender = new BasePushNotificationSender(pushConfigStore, httpClient);
// Create MainEventBus and MainEventBusProcessor (production code path)
mainEventBus = new MainEventBus();
queueManager = new InMemoryQueueManager(inMemoryTaskStore, mainEventBus);
mainEventBusProcessor = new MainEventBusProcessor(mainEventBus, taskStore, pushSender, queueManager);
EventQueueUtil.start(mainEventBusProcessor);
requestHandler = DefaultRequestHandler.create(
executor, taskStore, queueManager, pushConfigStore, mainEventBusProcessor, internalExecutor, internalExecutor);
}
@AfterEach
public void cleanup() {
agentExecutorExecute = null;
agentExecutorCancel = null;
// Stop MainEventBusProcessor background thread
if (mainEventBusProcessor != null) {
EventQueueUtil.stop(mainEventBusProcessor);
}
}
protected static AgentCard createAgentCard(boolean streaming, boolean pushNotifications) {
String preferredTransport = loadPreferredTransportFromProperties();
AgentCard.Builder builder = AgentCard.builder()
.name("test-card")
.description("A test agent card")
.supportedInterfaces(Collections.singletonList(new AgentInterface(preferredTransport, "http://example.com")))
.version("1.0")
.documentationUrl("http://example.com/docs")
.capabilities(AgentCapabilities.builder()
.streaming(streaming)
.pushNotifications(pushNotifications)
.build())
.defaultInputModes(new ArrayList<>())
.defaultOutputModes(new ArrayList<>())
.skills(new ArrayList<>());
return builder.build();
}
private static String loadPreferredTransportFromProperties() {
URL url = AbstractA2ARequestHandlerTest.class.getResource(A2A_REQUESTHANDLER_TEST_PROPERTIES);
Assertions.assertNotNull(url);
Properties properties = new Properties();
try {
try (InputStream in = url.openStream()){
properties.load(in);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
String preferredTransport = properties.getProperty(PREFERRED_TRANSPORT);
Assertions.assertNotNull(preferredTransport);
return preferredTransport;
}
protected interface AgentExecutorMethod {
void invoke(RequestContext context, EventQueue eventQueue) throws A2AError;
}
/**
* Helper method to wrap events in EventQueueItem for tests.
* Creates a simple wrapper that marks events as non-replicated (local events).
*/
protected static EventQueueItem wrapEvent(Event event) {
return new EventQueueItem() {
@Override
public Event getEvent() {
return event;
}
@Override
public boolean isReplicated() {
return false;
}
};
}
@Dependent
@IfBuildProfile("test")
protected static class TestHttpClient implements A2AHttpClient {
public final List<StreamingEventKind> events = Collections.synchronizedList(new ArrayList<>());
public volatile CountDownLatch latch;
@Override
public GetBuilder createGet() {
return null;
}
@Override
public PostBuilder createPost() {
return new TestHttpClient.TestPostBuilder();
}
@Override
public DeleteBuilder createDelete() {
return null;
}
class TestPostBuilder implements A2AHttpClient.PostBuilder {
private volatile String body;
@Override
public PostBuilder body(String body) {
this.body = body;
return this;
}
@Override
public A2AHttpResponse post() throws IOException, InterruptedException {
try {
// Parse StreamResponse format to extract the streaming event
// The body contains a wrapper with one of: task, message, statusUpdate, artifactUpdate
StreamingEventKind event = JsonUtil.fromJson(body, StreamingEventKind.class);
events.add(event);
return new A2AHttpResponse() {
@Override
public int status() {
return 200;
}
@Override
public boolean success() {
return true;
}
@Override
public String body() {
return "";
}
};
} catch (JsonProcessingException e) {
throw new IOException("Failed to parse StreamingEventKind JSON", e);
} finally {
if (latch != null) {
latch.countDown();
}
}
}
@Override
public CompletableFuture<Void> postAsyncSSE(Consumer<String> messageConsumer, Consumer<Throwable> errorConsumer, Runnable completeRunnable) throws IOException, InterruptedException {
return null;
}
@Override
public PostBuilder url(String s) {
return this;
}
@Override
public PostBuilder addHeader(String name, String value) {
return this;
}
@Override
public PostBuilder addHeaders(Map<String, String> headers) {
return this;
}
}
}
}