|
| 1 | +package org.a2aproject.sdk.client.transport.grpc; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 5 | + |
| 6 | +import java.util.List; |
| 7 | +import java.util.concurrent.TimeUnit; |
| 8 | +import java.util.concurrent.atomic.AtomicReference; |
| 9 | + |
| 10 | +import io.grpc.ManagedChannel; |
| 11 | +import io.grpc.Server; |
| 12 | +import io.grpc.inprocess.InProcessChannelBuilder; |
| 13 | +import io.grpc.inprocess.InProcessServerBuilder; |
| 14 | +import io.grpc.stub.StreamObserver; |
| 15 | +import org.a2aproject.sdk.grpc.A2AServiceGrpc; |
| 16 | +import org.a2aproject.sdk.grpc.GetTaskPushNotificationConfigRequest; |
| 17 | +import org.a2aproject.sdk.spec.AgentCapabilities; |
| 18 | +import org.a2aproject.sdk.spec.AgentCard; |
| 19 | +import org.a2aproject.sdk.spec.AgentInterface; |
| 20 | +import org.a2aproject.sdk.spec.GetTaskPushNotificationConfigParams; |
| 21 | +import org.a2aproject.sdk.spec.TaskPushNotificationConfig; |
| 22 | +import org.jspecify.annotations.Nullable; |
| 23 | +import org.junit.jupiter.api.AfterEach; |
| 24 | +import org.junit.jupiter.api.BeforeEach; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.junit.jupiter.api.Timeout; |
| 27 | + |
| 28 | +@Timeout(5) |
| 29 | +class GrpcTransportTest { |
| 30 | + |
| 31 | + private static final String TASK_ID = "task-1"; |
| 32 | + private static final String CALLBACK_URL = "https://example.com/callback"; |
| 33 | + private static final AgentCard CARD = AgentCard.builder() |
| 34 | + .name("Test Agent") |
| 35 | + .description("Agent for gRPC transport tests") |
| 36 | + .version("1.0.0") |
| 37 | + .supportedInterfaces(List.of(new AgentInterface("GRPC", "http://localhost"))) |
| 38 | + .capabilities(AgentCapabilities.builder().pushNotifications(true).build()) |
| 39 | + .defaultInputModes(List.of("text")) |
| 40 | + .defaultOutputModes(List.of("text")) |
| 41 | + .skills(List.of()) |
| 42 | + .build(); |
| 43 | + |
| 44 | + private final AtomicReference<GetTaskPushNotificationConfigRequest> receivedRequest = new AtomicReference<>(); |
| 45 | + private Server server; |
| 46 | + private ManagedChannel channel; |
| 47 | + |
| 48 | + @BeforeEach |
| 49 | + void setUp() throws Exception { |
| 50 | + String serverName = InProcessServerBuilder.generateName(); |
| 51 | + server = InProcessServerBuilder.forName(serverName) |
| 52 | + .directExecutor() |
| 53 | + .addService(new A2AServiceGrpc.A2AServiceImplBase() { |
| 54 | + @Override |
| 55 | + public void getTaskPushNotificationConfig(GetTaskPushNotificationConfigRequest request, |
| 56 | + StreamObserver<org.a2aproject.sdk.grpc.TaskPushNotificationConfig> responseObserver) { |
| 57 | + receivedRequest.set(request); |
| 58 | + responseObserver.onNext(org.a2aproject.sdk.grpc.TaskPushNotificationConfig.newBuilder() |
| 59 | + .setTaskId(request.getTaskId()) |
| 60 | + .setId(request.getId().isEmpty() ? request.getTaskId() : request.getId()) |
| 61 | + .setUrl(CALLBACK_URL) |
| 62 | + .build()); |
| 63 | + responseObserver.onCompleted(); |
| 64 | + } |
| 65 | + }) |
| 66 | + .build() |
| 67 | + .start(); |
| 68 | + channel = InProcessChannelBuilder.forName(serverName).directExecutor().build(); |
| 69 | + } |
| 70 | + |
| 71 | + @AfterEach |
| 72 | + void tearDown() throws InterruptedException { |
| 73 | + channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS); |
| 74 | + server.shutdownNow().awaitTermination(5, TimeUnit.SECONDS); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void testGetPushNotificationConfigWithOmittedId() throws Exception { |
| 79 | + assertGetPushNotificationConfig(new GetTaskPushNotificationConfigParams(TASK_ID), null, "", ""); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void testGetPushNotificationConfigWithOmittedIdViaBuilder() throws Exception { |
| 84 | + assertGetPushNotificationConfig(GetTaskPushNotificationConfigParams.builder().taskId(TASK_ID).build(), |
| 85 | + null, "", ""); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void testGetPushNotificationConfigWithEmptyId() throws Exception { |
| 90 | + assertGetPushNotificationConfig(new GetTaskPushNotificationConfigParams(TASK_ID, ""), null, "", ""); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void testGetPushNotificationConfigWithExplicitIdAndDefaultTenant() throws Exception { |
| 95 | + assertGetPushNotificationConfig(new GetTaskPushNotificationConfigParams(TASK_ID, "config-1"), |
| 96 | + "default-tenant", "config-1", "default-tenant"); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void testGetPushNotificationConfigWithOmittedIdAndRequestTenant() throws Exception { |
| 101 | + assertGetPushNotificationConfig(new GetTaskPushNotificationConfigParams(TASK_ID, null, "request-tenant"), |
| 102 | + "default-tenant", "", "request-tenant"); |
| 103 | + } |
| 104 | + |
| 105 | + private void assertGetPushNotificationConfig(GetTaskPushNotificationConfigParams params, |
| 106 | + @Nullable String defaultTenant, String expectedId, String expectedTenant) throws Exception { |
| 107 | + GrpcTransport transport = new GrpcTransport(channel, CARD, defaultTenant, null); |
| 108 | + TaskPushNotificationConfig result = transport.getTaskPushNotificationConfiguration(params, null); |
| 109 | + |
| 110 | + GetTaskPushNotificationConfigRequest request = receivedRequest.get(); |
| 111 | + assertNotNull(request); |
| 112 | + assertEquals(TASK_ID, request.getTaskId()); |
| 113 | + assertEquals(expectedId, request.getId()); |
| 114 | + assertEquals(expectedTenant, request.getTenant()); |
| 115 | + assertEquals(expectedId.isEmpty() ? TASK_ID : expectedId, result.id()); |
| 116 | + assertEquals(TASK_ID, result.taskId()); |
| 117 | + assertEquals(CALLBACK_URL, result.url()); |
| 118 | + } |
| 119 | +} |
0 commit comments