|
| 1 | +package org.a2aproject.sdk.tests.multitenancy.grpc; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 7 | + |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | +import java.util.UUID; |
| 11 | +import java.util.concurrent.TimeUnit; |
| 12 | +import java.util.function.BiConsumer; |
| 13 | + |
| 14 | +import jakarta.inject.Inject; |
| 15 | + |
| 16 | +import org.a2aproject.sdk.client.Client; |
| 17 | +import org.a2aproject.sdk.client.ClientEvent; |
| 18 | +import org.a2aproject.sdk.client.TaskEvent; |
| 19 | +import org.a2aproject.sdk.client.transport.grpc.GrpcTransport; |
| 20 | +import org.a2aproject.sdk.client.transport.grpc.GrpcTransportConfigBuilder; |
| 21 | +import org.a2aproject.sdk.spec.AgentCapabilities; |
| 22 | +import org.a2aproject.sdk.spec.AgentCard; |
| 23 | +import org.a2aproject.sdk.spec.AgentInterface; |
| 24 | +import org.a2aproject.sdk.spec.Message; |
| 25 | +import org.a2aproject.sdk.spec.MessageSendParams; |
| 26 | +import org.a2aproject.sdk.spec.Task; |
| 27 | +import org.a2aproject.sdk.spec.TextPart; |
| 28 | +import org.a2aproject.sdk.spec.TransportProtocol; |
| 29 | +import io.grpc.ManagedChannel; |
| 30 | +import io.grpc.ManagedChannelBuilder; |
| 31 | +import io.quarkus.security.spi.runtime.AuthorizationController; |
| 32 | +import io.quarkus.test.junit.QuarkusTest; |
| 33 | +import org.eclipse.microprofile.config.ConfigProvider; |
| 34 | +import org.jspecify.annotations.Nullable; |
| 35 | +import org.junit.jupiter.api.AfterAll; |
| 36 | +import org.junit.jupiter.api.BeforeAll; |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | + |
| 39 | +/** |
| 40 | + * Integration tests for multi-tenant executor routing on a gRPC server. |
| 41 | + * Verifies that gRPC SendMessage requests with a {@code tenant} field are routed |
| 42 | + * to the correct {@code AgentExecutor} bean qualified with {@code @Tenant}. |
| 43 | + */ |
| 44 | +@QuarkusTest |
| 45 | +public class MultiTenantGrpcTest { |
| 46 | + |
| 47 | + @Inject |
| 48 | + AuthorizationController authorizationController; |
| 49 | + |
| 50 | + private static ManagedChannel channel; |
| 51 | + private static String grpcTarget; |
| 52 | + |
| 53 | + @BeforeAll |
| 54 | + public static void setupChannel() { |
| 55 | + int port = ConfigProvider.getConfig().getValue("quarkus.http.port", Integer.class); |
| 56 | + grpcTarget = "localhost:" + port; |
| 57 | + channel = ManagedChannelBuilder.forTarget(grpcTarget).usePlaintext().build(); |
| 58 | + } |
| 59 | + |
| 60 | + @AfterAll |
| 61 | + public static void closeChannel() { |
| 62 | + channel.shutdownNow(); |
| 63 | + try { |
| 64 | + channel.awaitTermination(10, TimeUnit.SECONDS); |
| 65 | + } catch (InterruptedException e) { |
| 66 | + Thread.currentThread().interrupt(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void authorizationControllerIsTestAlternative() { |
| 72 | + assertInstanceOf(TestAuthorizationController.class, authorizationController, |
| 73 | + "TestAuthorizationController CDI alternative must be active"); |
| 74 | + assertFalse(authorizationController.isAuthorizationEnabled(), |
| 75 | + "Authorization must be disabled for multitenancy tests"); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void knownTenantRoutesToAcmeExecutor() throws Exception { |
| 80 | + assertEquals("acme", sendMessageAndGetArtifactText("acme")); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void secondTenantRoutesToBetaExecutor() throws Exception { |
| 85 | + assertEquals("beta", sendMessageAndGetArtifactText("beta")); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void unknownTenantFallsBackToDefault() throws Exception { |
| 90 | + assertEquals("default", sendMessageAndGetArtifactText("unknown-corp")); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void nullTenantUsesDefault() throws Exception { |
| 95 | + assertEquals("default", sendMessageAndGetArtifactText(null)); |
| 96 | + } |
| 97 | + |
| 98 | + private String sendMessageAndGetArtifactText(@Nullable String tenant) throws Exception { |
| 99 | + AgentCard card = buildGrpcAgentCard(); |
| 100 | + MessageSendParams params = buildParams(tenant); |
| 101 | + |
| 102 | + List<Task> tasks = new ArrayList<>(); |
| 103 | + List<BiConsumer<ClientEvent, AgentCard>> consumers = List.of((event, agentCard) -> { |
| 104 | + if (event instanceof TaskEvent te) { |
| 105 | + tasks.add(te.getTask()); |
| 106 | + } |
| 107 | + }); |
| 108 | + try (Client client = Client.builder(card) |
| 109 | + .withTransport(GrpcTransport.class, new GrpcTransportConfigBuilder() |
| 110 | + .channelFactory(target -> channel)) |
| 111 | + .build()) { |
| 112 | + client.sendMessage(params, consumers, null, null); |
| 113 | + } |
| 114 | + |
| 115 | + assertEquals(1, tasks.size(), "Expected exactly one Task in the response"); |
| 116 | + Task task = tasks.get(0); |
| 117 | + assertNotNull(task.artifacts(), "Expected artifacts in task"); |
| 118 | + assertFalse(task.artifacts().isEmpty(), "Expected at least one artifact"); |
| 119 | + return ((TextPart) task.artifacts().get(0).parts().get(0)).text(); |
| 120 | + } |
| 121 | + |
| 122 | + private static MessageSendParams buildParams(@Nullable String tenant) { |
| 123 | + Message message = Message.builder() |
| 124 | + .messageId(UUID.randomUUID().toString()) |
| 125 | + .role(Message.Role.ROLE_USER) |
| 126 | + .parts(List.of(new TextPart("hello"))) |
| 127 | + .build(); |
| 128 | + MessageSendParams.Builder builder = MessageSendParams.builder().message(message); |
| 129 | + if (tenant != null) { |
| 130 | + builder.tenant(tenant); |
| 131 | + } |
| 132 | + return builder.build(); |
| 133 | + } |
| 134 | + |
| 135 | + private static AgentCard buildGrpcAgentCard() { |
| 136 | + return AgentCard.builder() |
| 137 | + .name("test-agent") |
| 138 | + .description("test") |
| 139 | + .version("1.0.0") |
| 140 | + .defaultInputModes(List.of("text")) |
| 141 | + .defaultOutputModes(List.of("text")) |
| 142 | + .capabilities(AgentCapabilities.builder().streaming(false).build()) |
| 143 | + .skills(List.of()) |
| 144 | + .supportedInterfaces(List.of(new AgentInterface(TransportProtocol.GRPC.asString(), grpcTarget))) |
| 145 | + .build(); |
| 146 | + } |
| 147 | +} |
0 commit comments