|
| 1 | +package com.samples.a2a; |
| 2 | + |
| 3 | +import com.github.tomakehurst.wiremock.WireMockServer; |
| 4 | +import com.samples.a2a.test.A2AClientTestUtils; |
| 5 | +import com.samples.a2a.test.GeminiMockServer; |
| 6 | +import io.a2a.A2A; |
| 7 | +import io.a2a.client.Client; |
| 8 | +import io.a2a.client.ClientEvent; |
| 9 | +import io.a2a.client.MessageEvent; |
| 10 | +import io.a2a.client.TaskUpdateEvent; |
| 11 | +import io.a2a.client.config.ClientConfig; |
| 12 | +import io.a2a.client.http.A2ACardResolver; |
| 13 | +import io.a2a.client.transport.jsonrpc.JSONRPCTransport; |
| 14 | +import io.a2a.client.transport.jsonrpc.JSONRPCTransportConfig; |
| 15 | +import io.a2a.spec.AgentCard; |
| 16 | +import io.a2a.spec.Artifact; |
| 17 | +import io.a2a.spec.Message; |
| 18 | +import io.a2a.spec.Part; |
| 19 | +import io.a2a.spec.TaskArtifactUpdateEvent; |
| 20 | +import io.a2a.spec.TaskStatusUpdateEvent; |
| 21 | +import io.a2a.spec.TextPart; |
| 22 | +import io.a2a.spec.UpdateEvent; |
| 23 | +import io.quarkus.test.junit.QuarkusTest; |
| 24 | +import org.eclipse.microprofile.config.inject.ConfigProperty; |
| 25 | +import org.junit.jupiter.api.*; |
| 26 | + |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.List; |
| 29 | +import java.util.concurrent.CompletableFuture; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | +import java.util.function.BiConsumer; |
| 32 | +import java.util.function.Consumer; |
| 33 | + |
| 34 | +import static org.junit.jupiter.api.Assertions.*; |
| 35 | + |
| 36 | +/** |
| 37 | + * Integration test using A2A Client with streaming approach. |
| 38 | + * Tests the full A2A protocol stack: Client -> AgentExecutor -> AI Service. |
| 39 | + */ |
| 40 | +@QuarkusTest |
| 41 | +public class ContentEditorA2AIntegrationTest { |
| 42 | + private static WireMockServer wireMockServer; |
| 43 | + |
| 44 | + @ConfigProperty(name = "quarkus.http.test-port") |
| 45 | + int serverPort; |
| 46 | + |
| 47 | + @BeforeAll |
| 48 | + public static void setupWireMock() { |
| 49 | + wireMockServer = new WireMockServer(8089); |
| 50 | + wireMockServer.start(); |
| 51 | + } |
| 52 | + |
| 53 | + @AfterEach |
| 54 | + public void resetWireMock() { |
| 55 | + wireMockServer.resetAll(); |
| 56 | + } |
| 57 | + |
| 58 | + @AfterAll |
| 59 | + public static void teardownWireMock() { |
| 60 | + if (wireMockServer != null) { |
| 61 | + wireMockServer.stop(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testContentEditorViaA2AProtocol() throws Exception { |
| 67 | + // Mock Gemini response |
| 68 | + GeminiMockServer.mockTextResponse(wireMockServer, "Content has been polished!"); |
| 69 | + |
| 70 | + // Fetch agent card |
| 71 | + String serverUrl = "http://localhost:" + serverPort; |
| 72 | + AgentCard agentCard = new A2ACardResolver(serverUrl).getAgentCard(); |
| 73 | + |
| 74 | + // Create CompletableFuture to capture async response |
| 75 | + CompletableFuture<String> messageResponse = new CompletableFuture<>(); |
| 76 | + |
| 77 | + // Create event consumers |
| 78 | + List<BiConsumer<ClientEvent, AgentCard>> consumers = A2AClientTestUtils.createEventConsumers(messageResponse); |
| 79 | + |
| 80 | + // Create error handler |
| 81 | + Consumer<Throwable> errorHandler = error -> { |
| 82 | + messageResponse.completeExceptionally(error); |
| 83 | + }; |
| 84 | + |
| 85 | + // Create streaming client |
| 86 | + Client client = Client.builder(agentCard) |
| 87 | + .addConsumers(consumers) |
| 88 | + .streamingErrorHandler(errorHandler) |
| 89 | + .withTransport(JSONRPCTransport.class, new JSONRPCTransportConfig()) |
| 90 | + .clientConfig(new ClientConfig.Builder().build()) |
| 91 | + .build(); |
| 92 | + |
| 93 | + // Send message |
| 94 | + Message message = A2A.toUserMessage("Please edit this content"); |
| 95 | + client.sendMessage(message); |
| 96 | + |
| 97 | + // Wait for response with timeout |
| 98 | + String response = messageResponse.get(10, TimeUnit.SECONDS); |
| 99 | + |
| 100 | + assertNotNull(response, "Should have response"); |
| 101 | + assertTrue(response.length() > 0, "Response should not be empty"); |
| 102 | + } |
| 103 | + |
| 104 | +} |
| 105 | + |
0 commit comments