|
| 1 | +package com.google.adk.a2a; |
| 2 | + |
| 3 | +import static com.google.common.truth.Truth.assertThat; |
| 4 | + |
| 5 | +import com.google.adk.agents.BaseAgent; |
| 6 | +import com.google.adk.agents.InvocationContext; |
| 7 | +import com.google.adk.artifacts.InMemoryArtifactService; |
| 8 | +import com.google.adk.events.Event; |
| 9 | +import com.google.adk.memory.InMemoryMemoryService; |
| 10 | +import com.google.adk.sessions.InMemorySessionService; |
| 11 | +import com.google.common.collect.ImmutableList; |
| 12 | +import com.google.genai.types.Content; |
| 13 | +import com.google.genai.types.Part; |
| 14 | +import io.a2a.spec.Message; |
| 15 | +import io.a2a.spec.TextPart; |
| 16 | +import io.reactivex.rxjava3.core.Flowable; |
| 17 | +import io.reactivex.rxjava3.core.Single; |
| 18 | +import java.time.Duration; |
| 19 | +import java.util.List; |
| 20 | +import java.util.UUID; |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.runner.RunWith; |
| 23 | +import org.junit.runners.JUnit4; |
| 24 | + |
| 25 | +@RunWith(JUnit4.class) |
| 26 | +public class A2ASendMessageExecutorAdvancedTest { |
| 27 | + |
| 28 | + @Test |
| 29 | + public void execute_withCustomStrategy_usesStrategy() { |
| 30 | + InMemorySessionService sessionService = new InMemorySessionService(); |
| 31 | + |
| 32 | + A2ASendMessageExecutor executor = new A2ASendMessageExecutor(sessionService, "test-app"); |
| 33 | + |
| 34 | + A2ASendMessageExecutor.AgentExecutionStrategy customStrategy = |
| 35 | + (userId, sessionId, userContent, runConfig, invocationId) -> { |
| 36 | + Event customEvent = |
| 37 | + Event.builder() |
| 38 | + .id(UUID.randomUUID().toString()) |
| 39 | + .invocationId(invocationId) |
| 40 | + .author("agent") |
| 41 | + .content( |
| 42 | + Content.builder() |
| 43 | + .role("model") |
| 44 | + .parts( |
| 45 | + ImmutableList.of( |
| 46 | + Part.builder().text("Custom strategy response").build())) |
| 47 | + .build()) |
| 48 | + .build(); |
| 49 | + return Single.just(ImmutableList.of(customEvent)); |
| 50 | + }; |
| 51 | + |
| 52 | + Message request = |
| 53 | + new Message.Builder() |
| 54 | + .messageId("msg-1") |
| 55 | + .contextId("ctx-1") |
| 56 | + .role(Message.Role.USER) |
| 57 | + .parts(List.of(new TextPart("Test"))) |
| 58 | + .build(); |
| 59 | + |
| 60 | + Message response = executor.execute(request, customStrategy).blockingGet(); |
| 61 | + |
| 62 | + assertThat(response).isNotNull(); |
| 63 | + assertThat(response.getParts()).isNotEmpty(); |
| 64 | + assertThat(((TextPart) response.getParts().get(0)).getText()) |
| 65 | + .contains("Custom strategy response"); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void execute_withNullMessage_generatesDefaultContext() { |
| 70 | + BaseAgent agent = createSimpleAgent(); |
| 71 | + InMemorySessionService sessionService = new InMemorySessionService(); |
| 72 | + |
| 73 | + A2ASendMessageExecutor executor = |
| 74 | + new A2ASendMessageExecutor( |
| 75 | + agent, |
| 76 | + "test-app", |
| 77 | + Duration.ofSeconds(30), |
| 78 | + sessionService, |
| 79 | + new InMemoryArtifactService(), |
| 80 | + new InMemoryMemoryService()); |
| 81 | + |
| 82 | + Message response = executor.execute(null).blockingGet(); |
| 83 | + |
| 84 | + assertThat(response).isNotNull(); |
| 85 | + assertThat(response.getContextId()).isNotNull(); |
| 86 | + assertThat(response.getContextId()).isNotEmpty(); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void execute_withEmptyContextId_generatesNewContext() { |
| 91 | + BaseAgent agent = createSimpleAgent(); |
| 92 | + InMemorySessionService sessionService = new InMemorySessionService(); |
| 93 | + |
| 94 | + A2ASendMessageExecutor executor = |
| 95 | + new A2ASendMessageExecutor( |
| 96 | + agent, |
| 97 | + "test-app", |
| 98 | + Duration.ofSeconds(30), |
| 99 | + sessionService, |
| 100 | + new InMemoryArtifactService(), |
| 101 | + new InMemoryMemoryService()); |
| 102 | + |
| 103 | + Message request = |
| 104 | + new Message.Builder() |
| 105 | + .messageId("msg-1") |
| 106 | + .role(Message.Role.USER) |
| 107 | + .parts(List.of(new TextPart("Test"))) |
| 108 | + .build(); |
| 109 | + |
| 110 | + Message response = executor.execute(request).blockingGet(); |
| 111 | + |
| 112 | + assertThat(response).isNotNull(); |
| 113 | + assertThat(response.getContextId()).isNotNull(); |
| 114 | + assertThat(response.getContextId()).isNotEmpty(); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void execute_withProvidedContextId_preservesContext() { |
| 119 | + BaseAgent agent = createSimpleAgent(); |
| 120 | + InMemorySessionService sessionService = new InMemorySessionService(); |
| 121 | + |
| 122 | + A2ASendMessageExecutor executor = |
| 123 | + new A2ASendMessageExecutor( |
| 124 | + agent, |
| 125 | + "test-app", |
| 126 | + Duration.ofSeconds(30), |
| 127 | + sessionService, |
| 128 | + new InMemoryArtifactService(), |
| 129 | + new InMemoryMemoryService()); |
| 130 | + |
| 131 | + String contextId = "my-custom-context"; |
| 132 | + Message request = |
| 133 | + new Message.Builder() |
| 134 | + .messageId("msg-1") |
| 135 | + .contextId(contextId) |
| 136 | + .role(Message.Role.USER) |
| 137 | + .parts(List.of(new TextPart("Test"))) |
| 138 | + .build(); |
| 139 | + |
| 140 | + Message response = executor.execute(request).blockingGet(); |
| 141 | + |
| 142 | + assertThat(response).isNotNull(); |
| 143 | + assertThat(response.getContextId()).isEqualTo(contextId); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void execute_multipleRequests_maintainsSession() { |
| 148 | + BaseAgent agent = createSimpleAgent(); |
| 149 | + InMemorySessionService sessionService = new InMemorySessionService(); |
| 150 | + |
| 151 | + A2ASendMessageExecutor executor = |
| 152 | + new A2ASendMessageExecutor( |
| 153 | + agent, |
| 154 | + "test-app", |
| 155 | + Duration.ofSeconds(30), |
| 156 | + sessionService, |
| 157 | + new InMemoryArtifactService(), |
| 158 | + new InMemoryMemoryService()); |
| 159 | + |
| 160 | + String contextId = "persistent-context"; |
| 161 | + |
| 162 | + Message request1 = |
| 163 | + new Message.Builder() |
| 164 | + .messageId("msg-1") |
| 165 | + .contextId(contextId) |
| 166 | + .role(Message.Role.USER) |
| 167 | + .parts(List.of(new TextPart("First message"))) |
| 168 | + .build(); |
| 169 | + |
| 170 | + Message response1 = executor.execute(request1).blockingGet(); |
| 171 | + assertThat(response1.getContextId()).isEqualTo(contextId); |
| 172 | + |
| 173 | + Message request2 = |
| 174 | + new Message.Builder() |
| 175 | + .messageId("msg-2") |
| 176 | + .contextId(contextId) |
| 177 | + .role(Message.Role.USER) |
| 178 | + .parts(List.of(new TextPart("Second message"))) |
| 179 | + .build(); |
| 180 | + |
| 181 | + Message response2 = executor.execute(request2).blockingGet(); |
| 182 | + assertThat(response2.getContextId()).isEqualTo(contextId); |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + public void execute_withoutRunnerConfig_throwsException() { |
| 187 | + InMemorySessionService sessionService = new InMemorySessionService(); |
| 188 | + |
| 189 | + A2ASendMessageExecutor executor = new A2ASendMessageExecutor(sessionService, "test-app"); |
| 190 | + |
| 191 | + Message request = |
| 192 | + new Message.Builder() |
| 193 | + .messageId("msg-1") |
| 194 | + .contextId("ctx-1") |
| 195 | + .role(Message.Role.USER) |
| 196 | + .parts(List.of(new TextPart("Test"))) |
| 197 | + .build(); |
| 198 | + |
| 199 | + try { |
| 200 | + executor.execute(request).blockingGet(); |
| 201 | + assertThat(false).isTrue(); |
| 202 | + } catch (IllegalStateException e) { |
| 203 | + assertThat(e.getMessage()).contains("Runner-based handle invoked without configured runner"); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + @Test |
| 208 | + public void execute_errorInStrategy_returnsErrorResponse() { |
| 209 | + InMemorySessionService sessionService = new InMemorySessionService(); |
| 210 | + |
| 211 | + A2ASendMessageExecutor executor = new A2ASendMessageExecutor(sessionService, "test-app"); |
| 212 | + |
| 213 | + A2ASendMessageExecutor.AgentExecutionStrategy failingStrategy = |
| 214 | + (userId, sessionId, userContent, runConfig, invocationId) -> { |
| 215 | + return Single.error(new RuntimeException("Strategy failed")); |
| 216 | + }; |
| 217 | + |
| 218 | + Message request = |
| 219 | + new Message.Builder() |
| 220 | + .messageId("msg-1") |
| 221 | + .contextId("ctx-1") |
| 222 | + .role(Message.Role.USER) |
| 223 | + .parts(List.of(new TextPart("Test"))) |
| 224 | + .build(); |
| 225 | + |
| 226 | + Message response = executor.execute(request, failingStrategy).blockingGet(); |
| 227 | + |
| 228 | + assertThat(response).isNotNull(); |
| 229 | + assertThat(response.getParts()).isNotEmpty(); |
| 230 | + assertThat(((TextPart) response.getParts().get(0)).getText()).contains("Error:"); |
| 231 | + assertThat(((TextPart) response.getParts().get(0)).getText()).contains("Strategy failed"); |
| 232 | + } |
| 233 | + |
| 234 | + private BaseAgent createSimpleAgent() { |
| 235 | + return new BaseAgent("test", "test agent", ImmutableList.of(), null, null) { |
| 236 | + @Override |
| 237 | + protected Flowable<Event> runAsyncImpl(InvocationContext ctx) { |
| 238 | + return Flowable.just( |
| 239 | + Event.builder() |
| 240 | + .content( |
| 241 | + Content.builder() |
| 242 | + .role("model") |
| 243 | + .parts( |
| 244 | + ImmutableList.of( |
| 245 | + com.google.genai.types.Part.builder().text("Response").build())) |
| 246 | + .build()) |
| 247 | + .build()); |
| 248 | + } |
| 249 | + |
| 250 | + @Override |
| 251 | + protected Flowable<Event> runLiveImpl(InvocationContext ctx) { |
| 252 | + return Flowable.empty(); |
| 253 | + } |
| 254 | + }; |
| 255 | + } |
| 256 | +} |
0 commit comments