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