Repository navigation
Expand file tree
/
Copy pathAbstractA2ARequestHandlerTest.java
More file actions
251 lines (217 loc) · 8.68 KB
/
Copy pathAbstractA2ARequestHandlerTest.java
File metadata and controls
251 lines (217 loc) · 8.68 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
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.quarkus.arc.profile.IfBuildProfile;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import io.a2a.client.http.A2AHttpClient;
import io.a2a.client.http.A2AHttpResponse;
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.InMemoryQueueManager;
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.AgentCapabilities;
import io.a2a.spec.AgentCard;
import io.a2a.spec.Event;
import io.a2a.spec.JSONRPCError;
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.a2a.util.Utils;
public class AbstractA2ARequestHandlerTest {
protected static final AgentCard CARD = createAgentCard(true, true, true);
protected static final Task MINIMAL_TASK = new Task.Builder()
.id("task-123")
.contextId("session-xyz")
.status(new TaskStatus(TaskState.SUBMITTED))
.build();
protected static final Message MESSAGE = new 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";
protected AgentExecutor executor;
protected TaskStore taskStore;
protected RequestHandler requestHandler;
protected AgentExecutorMethod agentExecutorExecute;
protected AgentExecutorMethod agentExecutorCancel;
protected InMemoryQueueManager queueManager;
protected TestHttpClient httpClient;
protected final Executor internalExecutor = Executors.newCachedThreadPool();
@BeforeEach
public void init() {
executor = new AgentExecutor() {
@Override
public void execute(RequestContext context, EventQueue eventQueue) throws JSONRPCError {
if (agentExecutorExecute != null) {
agentExecutorExecute.invoke(context, eventQueue);
}
}
@Override
public void cancel(RequestContext context, EventQueue eventQueue) throws JSONRPCError {
if (agentExecutorCancel != null) {
agentExecutorCancel.invoke(context, eventQueue);
}
}
};
InMemoryTaskStore inMemoryTaskStore = new InMemoryTaskStore();
taskStore = inMemoryTaskStore;
queueManager = new InMemoryQueueManager(inMemoryTaskStore);
httpClient = new TestHttpClient();
PushNotificationConfigStore pushConfigStore = new InMemoryPushNotificationConfigStore();
PushNotificationSender pushSender = new BasePushNotificationSender(pushConfigStore, httpClient);
requestHandler = DefaultRequestHandler.create(
executor, taskStore, queueManager, pushConfigStore, pushSender, internalExecutor);
}
@AfterEach
public void cleanup() {
agentExecutorExecute = null;
agentExecutorCancel = null;
}
protected static AgentCard createAgentCard(boolean streaming, boolean pushNotifications, boolean stateTransitionHistory) {
String preferredTransport = loadPreferredTransportFromProperties();
AgentCard.Builder builder = new AgentCard.Builder()
.name("test-card")
.description("A test agent card")
.url("http://example.com")
.version("1.0")
.documentationUrl("http://example.com/docs")
.capabilities(new AgentCapabilities.Builder()
.streaming(streaming)
.pushNotifications(pushNotifications)
.stateTransitionHistory(stateTransitionHistory)
.build())
.defaultInputModes(new ArrayList<>())
.defaultOutputModes(new ArrayList<>())
.preferredTransport(preferredTransport)
.skills(new ArrayList<>())
.protocolVersion("0.2.5");
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 JSONRPCError;
}
/**
* 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<Task> tasks = 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 {
StreamingEventKind kind = Utils.unmarshalStreamingEventKindFrom(body);
if (kind instanceof Task task) {
tasks.add(task);
}
try {
return new A2AHttpResponse() {
@Override
public int status() {
return 200;
}
@Override
public boolean success() {
return true;
}
@Override
public String body() {
return "";
}
};
} finally {
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;
}
}
}
}