forked from a2aproject/a2a-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractA2ARequestHandlerTest.java
More file actions
189 lines (161 loc) · 6.58 KB
/
AbstractA2ARequestHandlerTest.java
File metadata and controls
189 lines (161 loc) · 6.58 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
package io.a2a.server.requesthandlers;
import jakarta.enterprise.context.Dependent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
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 io.a2a.transport.jsonrpc.client.A2AHttpClient;
import io.a2a.transport.jsonrpc.client.A2AHttpResponse;
import io.a2a.server.agentexecution.AgentExecutor;
import io.a2a.server.agentexecution.RequestContext;
import io.a2a.server.events.EventQueue;
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.JSONRPCError;
import io.a2a.spec.Message;
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;
import io.quarkus.arc.profile.IfBuildProfile;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
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();
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);
}
}
};
taskStore = new InMemoryTaskStore();
queueManager = new InMemoryQueueManager();
httpClient = new TestHttpClient();
PushNotificationConfigStore pushConfigStore = new InMemoryPushNotificationConfigStore();
PushNotificationSender pushSender = new BasePushNotificationSender(pushConfigStore, httpClient);
requestHandler = new DefaultRequestHandler(executor, taskStore, queueManager, pushConfigStore, pushSender, internalExecutor);
}
@AfterEach
public void cleanup() {
agentExecutorExecute = null;
agentExecutorCancel = null;
}
protected static AgentCard createAgentCard(boolean streaming, boolean pushNotifications, boolean stateTransitionHistory) {
return 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<>())
.skills(new ArrayList<>())
.protocolVersion("0.2.5")
.build();
}
protected interface AgentExecutorMethod {
void invoke(RequestContext context, EventQueue eventQueue) throws JSONRPCError;
}
@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();
}
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 {
tasks.add(Utils.OBJECT_MAPPER.readValue(body, Task.TYPE_REFERENCE));
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;
}
}
}
}