Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,394 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// ********RoostGPT********
/*
Test generated by RoostGPT for test june-java-unit using AI Type AWS Bedrock Runtime AI and AI Model global.anthropic.claude-sonnet-4-6

ROOST_METHOD_HASH=agent_9b91251448
ROOST_METHOD_SIG_HASH=agent_877597f15f

Scenario 1: Return Agent When Agent Is Set via Builder

Details:
TestName: agentReturnsCorrectAgentWhenSetViaBuilder
Description: Verifies that the `agent()` method returns the exact `BaseAgent` instance
that was provided to the `InvocationContext` through the builder pattern.

Execution:
Arrange:
- Create a mock of `BaseAgent`.
- Create a mock of `Session`.
- Create a mock of `BaseSessionService`.
- Create a mock of `BaseArtifactService`.
- Build an `InvocationContext` using `InvocationContext.builder()`
with `.agent(mockAgent)`, `.session(mockSession)`,
`.sessionService(mockSessionService)`, `.artifactService(mockArtifactService)`.
Act:
- Call `invocationContext.agent()` on the built context.
Assert:
- Assert that the returned value is the same instance as `mockAgent`
using `assertSame(mockAgent, invocationContext.agent())`.

Validation:
Confirms that `agent()` correctly retrieves and returns the agent reference stored
during construction. This is fundamental to the InvocationContext's role as a
carrier of invocation state — the agent being invoked must be reliably accessible.

*/

// ********RoostGPT********

package com.google.adk.agents;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import com.google.adk.artifacts.BaseArtifactService;
import com.google.adk.memory.BaseMemoryService;
import com.google.adk.plugins.PluginManager;
import com.google.adk.sessions.BaseSessionService;
import com.google.adk.sessions.Session;
import com.google.genai.types.Content;
import java.util.Optional;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class InvocationContextAgentTest {
@Mock private BaseAgent mockAgent;
@Mock private BaseAgent anotherMockAgent;
@Mock private Session mockSession;
@Mock private BaseSessionService mockSessionService;
@Mock private BaseArtifactService mockArtifactService;
@Mock private BaseMemoryService mockMemoryService;
@Mock private PluginManager mockPluginManager;
@Mock private RunConfig mockRunConfig;

@BeforeEach
void setUp() {
lenient().when(mockSession.appName()).thenReturn("testApp");
lenient().when(mockSession.userId()).thenReturn("testUser");
}

@Test
@Tag("valid")
void agentReturnsCorrectAgentWhenSetViaBuilder() {
InvocationContext invocationContext =
InvocationContext.builder()
.sessionService(mockSessionService)
.artifactService(mockArtifactService)
.agent(mockAgent)
.session(mockSession)
.build();
assertSame(mockAgent, invocationContext.agent());
}

@Test
@Tag("valid")
void agentReturnsCorrectAgentWhenSetViaCreateMethod() {
InvocationContext invocationContext =
InvocationContext.create(
mockSessionService,
mockArtifactService,
"test-invocation-id",
mockAgent,
mockSession,
null,
null);
assertSame(mockAgent, invocationContext.agent());
}

@Test
@Tag("valid")
void agentReturnsNullWhenNoAgentSet() {
InvocationContext invocationContext =
InvocationContext.builder()
.sessionService(mockSessionService)
.artifactService(mockArtifactService)
.session(mockSession)
.build();
assertNull(invocationContext.agent());
}

@Test
@Tag("valid")
void agentReturnsSameInstanceAfterMutation() {
InvocationContext invocationContext =
InvocationContext.builder()
.sessionService(mockSessionService)
.artifactService(mockArtifactService)
.agent(mockAgent)
.session(mockSession)
.build();
invocationContext.agent(anotherMockAgent);
assertSame(anotherMockAgent, invocationContext.agent());
}

@Test
@Tag("valid")
void agentReturnsSameInstanceBeforeAndAfterOtherFieldMutation() {
InvocationContext invocationContext =
InvocationContext.builder()
.sessionService(mockSessionService)
.artifactService(mockArtifactService)
.agent(mockAgent)
.session(mockSession)
.invocationId("some-invocation-id")
.build();
invocationContext.setEndInvocation(true);
assertSame(mockAgent, invocationContext.agent());
}

@Test
@Tag("valid")
void agentReturnsCorrectAgentAfterCopyOf() {
InvocationContext original =
InvocationContext.builder()
.sessionService(mockSessionService)
.artifactService(mockArtifactService)
.agent(mockAgent)
.session(mockSession)
.invocationId("inv-id")
.build();
InvocationContext copy = InvocationContext.copyOf(original);
assertSame(mockAgent, copy.agent());
}

@Test
@Tag("valid")
void agentMutationDoesNotAffectCopiedContext() {
InvocationContext original =
InvocationContext.builder()
.sessionService(mockSessionService)
.artifactService(mockArtifactService)
.agent(mockAgent)
.session(mockSession)
.invocationId("inv-id")
.build();
InvocationContext copy = InvocationContext.copyOf(original);
original.agent(anotherMockAgent);
assertSame(mockAgent, copy.agent());
assertSame(anotherMockAgent, original.agent());
}

@Test
@Tag("valid")
void agentReturnsNotNullWhenAgentIsProvided() {
InvocationContext invocationContext =
InvocationContext.builder()
.sessionService(mockSessionService)
.artifactService(mockArtifactService)
.agent(mockAgent)
.session(mockSession)
.build();
assertNotNull(invocationContext.agent());
}

@Test
@Tag("valid")
void agentReturnsSameInstanceWhenSetMultipleTimes() {
InvocationContext invocationContext =
InvocationContext.builder()
.sessionService(mockSessionService)
.artifactService(mockArtifactService)
.agent(mockAgent)
.session(mockSession)
.build();
invocationContext.agent(anotherMockAgent);
invocationContext.agent(mockAgent);
assertSame(mockAgent, invocationContext.agent());
}

@Test
@Tag("integration")
void agentReturnsCorrectAgentInFullyConfiguredContext() {
when(mockAgent.name()).thenReturn("testAgentName");
InvocationContext invocationContext =
InvocationContext.builder()
.sessionService(mockSessionService)
.artifactService(mockArtifactService)
.memoryService(mockMemoryService)
.pluginManager(mockPluginManager)
.agent(mockAgent)
.session(mockSession)
.invocationId("full-invocation-id")
.branch(Optional.of("branch.path"))
.runConfig(mockRunConfig)
.endInvocation(false)
.build();
BaseAgent returnedAgent = invocationContext.agent();
assertSame(mockAgent, returnedAgent);
assertEquals("testAgentName", returnedAgent.name());
}

@Test
@Tag("valid")
void agentReturnsCorrectAgentWhenBuiltWithUserContent() {
Content userContent = mock(Content.class);
InvocationContext invocationContext =
InvocationContext.create(
mockSessionService,
mockArtifactService,
"invocation-id",
mockAgent,
mockSession,
userContent,
null);
assertSame(mockAgent, invocationContext.agent());
}

@Test
@Tag("valid")
void agentReturnsCorrectAgentWithNullUserContent() {
InvocationContext invocationContext =
InvocationContext.create(
mockSessionService,
mockArtifactService,
"invocation-id",
mockAgent,
mockSession,
null,
null);
assertSame(mockAgent, invocationContext.agent());
}

@Test
@Tag("boundary")
void agentIsNullWhenBuilderHasNoAgentSet() {
InvocationContext invocationContext =
InvocationContext.builder()
.sessionService(mockSessionService)
.artifactService(mockArtifactService)
.session(mockSession)
.invocationId("some-id")
.build();
assertNull(invocationContext.agent());
}

@Test
@Tag("boundary")
void agentSetToNullViaSetterReturnsNull() {
InvocationContext invocationContext =
InvocationContext.builder()
.sessionService(mockSessionService)
.artifactService(mockArtifactService)
.agent(mockAgent)
.session(mockSession)
.build();
invocationContext.agent(null);
assertNull(invocationContext.agent());
}

@Test
@Tag("valid")
void agentEqualsChecksAgentField() {
InvocationContext ctx1 =
InvocationContext.builder()
.sessionService(mockSessionService)
.artifactService(mockArtifactService)
.agent(mockAgent)
.session(mockSession)
.invocationId("inv-id")
.build();
InvocationContext ctx2 =
InvocationContext.builder()
.sessionService(mockSessionService)
.artifactService(mockArtifactService)
.agent(mockAgent)
.session(mockSession)
.invocationId("inv-id")
.build();
assertEquals(ctx1.agent(), ctx2.agent());
}

@Test
@Tag("invalid")
void agentReturnsDifferentAgentAfterSetterOverride() {
InvocationContext invocationContext =
InvocationContext.builder()
.sessionService(mockSessionService)
.artifactService(mockArtifactService)
.agent(mockAgent)
.session(mockSession)
.build();
invocationContext.agent(anotherMockAgent);
assertNotSame(mockAgent, invocationContext.agent());
assertSame(anotherMockAgent, invocationContext.agent());
}

@Test
@Tag("valid")
void agentReturnsCorrectAgentInDeprecatedConstructor() {
InvocationContext invocationContext =
new InvocationContext(
mockSessionService,
mockArtifactService,
mockMemoryService,
mockPluginManager,
Optional.empty(),
Optional.empty(),
"inv-id",
mockAgent,
mockSession,
Optional.empty(),
null,
false);
assertSame(mockAgent, invocationContext.agent());
}

@Test
@Tag("valid")
void agentReturnsCorrectAgentInDeprecatedConstructorWithoutPluginManager() {
InvocationContext invocationContext =
new InvocationContext(
mockSessionService,
mockArtifactService,
mockMemoryService,
Optional.empty(),
Optional.empty(),
"inv-id",
mockAgent,
mockSession,
Optional.empty(),
null,
false);
assertSame(mockAgent, invocationContext.agent());
}

@Test
@Tag("integration")
void agentRemainsConsistentThroughMultipleAccesses() {
InvocationContext invocationContext =
InvocationContext.builder()
.sessionService(mockSessionService)
.artifactService(mockArtifactService)
.agent(mockAgent)
.session(mockSession)
.invocationId("inv-id")
.build();
BaseAgent first = invocationContext.agent();
BaseAgent second = invocationContext.agent();
BaseAgent third = invocationContext.agent();
assertSame(first, second);
assertSame(second, third);
assertSame(mockAgent, first);
}
}
Loading