|
1 | 1 | package io.a2a.client; |
2 | 2 |
|
3 | | - |
4 | 3 | import java.util.Collections; |
5 | 4 | import java.util.List; |
6 | 5 |
|
|
22 | 21 |
|
23 | 22 | public class ClientBuilderTest { |
24 | 23 |
|
25 | | - private AgentCard card = AgentCard.builder() |
26 | | - .name("Hello World Agent") |
| 24 | + private static AgentCard buildCard(List<AgentInterface> interfaces) { |
| 25 | + return AgentCard.builder() |
| 26 | + .name("Hello World Agent") |
27 | 27 | .description("Just a hello world agent") |
28 | 28 | .version("1.0.0") |
29 | 29 | .documentationUrl("http://example.com/docs") |
30 | 30 | .capabilities(AgentCapabilities.builder() |
31 | 31 | .streaming(true) |
32 | 32 | .pushNotifications(true) |
33 | 33 | .build()) |
34 | | - .defaultInputModes(Collections.singletonList("text")) |
35 | | - .defaultOutputModes(Collections.singletonList("text")) |
36 | | - .skills(Collections.singletonList(AgentSkill.builder() |
37 | | - .id("hello_world") |
38 | | - .name("Returns hello world") |
39 | | - .description("just returns hello world") |
40 | | - .tags(Collections.singletonList("hello world")) |
41 | | - .examples(List.of("hi", "hello world")) |
42 | | - .build())) |
43 | | - .supportedInterfaces(List.of( |
44 | | - new AgentInterface(TransportProtocol.JSONRPC.asString(), "http://localhost:9999"))) |
45 | | - .build(); |
| 34 | + .defaultInputModes(Collections.singletonList("text")) |
| 35 | + .defaultOutputModes(Collections.singletonList("text")) |
| 36 | + .skills(Collections.singletonList(AgentSkill.builder() |
| 37 | + .id("hello_world") |
| 38 | + .name("Returns hello world") |
| 39 | + .description("just returns hello world") |
| 40 | + .tags(Collections.singletonList("hello world")) |
| 41 | + .examples(List.of("hi", "hello world")) |
| 42 | + .build())) |
| 43 | + .supportedInterfaces(interfaces) |
| 44 | + .build(); |
| 45 | + } |
| 46 | + |
| 47 | + private final AgentCard card = buildCard(List.of( |
| 48 | + new AgentInterface(TransportProtocol.JSONRPC.asString(), "http://localhost:9999"))); |
| 49 | + |
| 50 | + private final AgentCard cardWithTenant = buildCard(List.of( |
| 51 | + new AgentInterface(TransportProtocol.JSONRPC.asString(), "http://localhost:9999", "/default-tenant"))); |
| 52 | + |
| 53 | + private final AgentCard cardWithMultipleInterfaces = buildCard(List.of( |
| 54 | + new AgentInterface(TransportProtocol.GRPC.asString(), "http://localhost:9998", "/grpc-tenant", "1.0"), |
| 55 | + new AgentInterface(TransportProtocol.JSONRPC.asString(), "http://localhost:9999", "/jsonrpc-tenant", "1.0"))); |
46 | 56 |
|
47 | 57 | @Test |
48 | 58 | public void shouldNotFindCompatibleTransport() throws A2AClientException { |
@@ -91,4 +101,75 @@ public void shouldCreateClient_differentConfigurations() throws A2AClientExcepti |
91 | 101 |
|
92 | 102 | Assertions.assertNotNull(client); |
93 | 103 | } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void shouldPreserveTenantFromAgentInterface() throws A2AClientException { |
| 107 | + ClientBuilder builder = Client |
| 108 | + .builder(cardWithTenant) |
| 109 | + .withTransport(JSONRPCTransport.class, new JSONRPCTransportConfigBuilder()); |
| 110 | + |
| 111 | + AgentInterface selectedInterface = builder.findBestClientTransport(); |
| 112 | + |
| 113 | + Assertions.assertEquals("/default-tenant", selectedInterface.tenant()); |
| 114 | + Assertions.assertEquals("http://localhost:9999", selectedInterface.url()); |
| 115 | + Assertions.assertEquals(TransportProtocol.JSONRPC.asString(), selectedInterface.protocolBinding()); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void shouldPreserveProtocolVersionFromAgentInterface() throws A2AClientException { |
| 120 | + ClientBuilder builder = Client |
| 121 | + .builder(cardWithMultipleInterfaces) |
| 122 | + .withTransport(JSONRPCTransport.class, new JSONRPCTransportConfigBuilder()); |
| 123 | + |
| 124 | + AgentInterface selectedInterface = builder.findBestClientTransport(); |
| 125 | + |
| 126 | + Assertions.assertEquals("/jsonrpc-tenant", selectedInterface.tenant()); |
| 127 | + Assertions.assertEquals("1.0", selectedInterface.protocolVersion()); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void shouldSelectCorrectInterfaceWithServerPreference() throws A2AClientException { |
| 132 | + // Server preference (default): iterates server interfaces in order, picks first that client supports |
| 133 | + // cardWithMultipleInterfaces has [GRPC, JSONRPC] - GRPC is first |
| 134 | + // Client supports both GRPC and JSONRPC, so GRPC should be selected (server's first choice) |
| 135 | + ClientBuilder builder = Client |
| 136 | + .builder(cardWithMultipleInterfaces) |
| 137 | + .withTransport(GrpcTransport.class, new GrpcTransportConfigBuilder().channelFactory(s -> null)) |
| 138 | + .withTransport(JSONRPCTransport.class, new JSONRPCTransportConfigBuilder()); |
| 139 | + |
| 140 | + AgentInterface selectedInterface = builder.findBestClientTransport(); |
| 141 | + |
| 142 | + Assertions.assertEquals(TransportProtocol.GRPC.asString(), selectedInterface.protocolBinding()); |
| 143 | + Assertions.assertEquals("http://localhost:9998", selectedInterface.url()); |
| 144 | + Assertions.assertEquals("/grpc-tenant", selectedInterface.tenant()); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void shouldSelectCorrectInterfaceWithClientPreference() throws A2AClientException { |
| 149 | + // Client preference: iterates client transports in registration order, picks first that server supports |
| 150 | + // Client registers [JSONRPC, GRPC] - JSONRPC is first |
| 151 | + // Server supports both, so JSONRPC should be selected (client's first choice) |
| 152 | + ClientBuilder builder = Client |
| 153 | + .builder(cardWithMultipleInterfaces) |
| 154 | + .clientConfig(new ClientConfig.Builder().setUseClientPreference(true).build()) |
| 155 | + .withTransport(JSONRPCTransport.class, new JSONRPCTransportConfigBuilder()) |
| 156 | + .withTransport(GrpcTransport.class, new GrpcTransportConfigBuilder().channelFactory(s -> null)); |
| 157 | + |
| 158 | + AgentInterface selectedInterface = builder.findBestClientTransport(); |
| 159 | + |
| 160 | + Assertions.assertEquals(TransportProtocol.JSONRPC.asString(), selectedInterface.protocolBinding()); |
| 161 | + Assertions.assertEquals("http://localhost:9999", selectedInterface.url()); |
| 162 | + Assertions.assertEquals("/jsonrpc-tenant", selectedInterface.tenant()); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + public void shouldPreserveEmptyTenant() throws A2AClientException { |
| 167 | + ClientBuilder builder = Client |
| 168 | + .builder(card) |
| 169 | + .withTransport(JSONRPCTransport.class, new JSONRPCTransportConfigBuilder()); |
| 170 | + |
| 171 | + AgentInterface selectedInterface = builder.findBestClientTransport(); |
| 172 | + |
| 173 | + Assertions.assertEquals("", selectedInterface.tenant()); |
| 174 | + } |
94 | 175 | } |
0 commit comments