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
21 changes: 13 additions & 8 deletions core/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0"?>
<!--
Copyright 2025 Google LLC

Expand All @@ -16,19 +16,15 @@
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.google.adk</groupId>
<artifactId>google-adk-parent</artifactId>
<version>0.4.1-SNAPSHOT</version><!-- {x-version-update:google-adk:current} -->
<version>0.4.1-SNAPSHOT</version>
<!-- {x-version-update:google-adk:current} -->
</parent>

<artifactId>google-adk</artifactId>
<name>Agent Development Kit</name>
<description>Agent Development Kit: an open-source, code-first toolkit designed to simplify building, evaluating, and deploying advanced AI agents anywhere.</description>



<dependencies>
<dependency>
<groupId>com.anthropic</groupId>
Expand Down Expand Up @@ -201,6 +197,15 @@
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-maven-plugin</artifactId>
<version>0.0.40</version>
<!--Plugin added by RoostGPT-->
</plugin>
</plugins>
<pluginManagement>
<plugins/>
</pluginManagement>
</build>
</project>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
/*
* 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 unit-java-adk using AI Type Azure Open AI and AI Model gpt-5

ROOST_METHOD_HASH=addSessionToMemory_8a630adf5c
ROOST_METHOD_SIG_HASH=addSessionToMemory_3e3003d0a0

Scenario 1: Successfully stores a session with events that have non-empty parts

Details:
TestName: storesSessionWithEventsHavingNonEmptyParts
Description: Verifies that addSessionToMemory stores a session whose events include content with present, non-empty parts. Confirms that the returned Completable completes without errors and that the stored events are discoverable via searchMemory.

Execution:
Arrange: Create a Session with appName "appA", userId "user1", session id "s1", and events where at least one event has content().isPresent() and parts().isPresent() with a non-empty list containing a Part with non-empty text like "hello world".
Act: Invoke addSessionToMemory(session) and await the Completable to complete. Then call searchMemory("appA", "user1", "hello") to retrieve memories.
Assert: Assert the Completable completes successfully. Assert that searchMemory returns at least one MemoryEntry with content containing the word "hello".

Validation:
Confirms that events meeting the filter (content present, parts present and non-empty) are retained and can be retrieved through searchMemory. Ensures the Completable signals completion, indicating the write to memory occurred.

*/

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

package com.google.adk.memory;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.google.adk.events.Event;
import com.google.adk.sessions.Session;
import com.google.genai.types.Content;
import com.google.genai.types.Part;
import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.core.Single;
import java.time.Instant;
import java.util.List;
import java.util.Locale;
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;

public class InMemoryMemoryServiceAddSessionToMemoryTest {

private InMemoryMemoryService service;

@BeforeEach
public void setUp() {
service = new InMemoryMemoryService();
}

@Test
@Tag("integration")
@Tag("valid")
public void testStoresSessionWithEventsHavingNonEmptyParts() {
// Arrange
String appName = "appA";
String userId = "user1";
String sessionId = "s1";
long ts = 1_700_000_000L; // example epoch seconds (deterministic, not live)
// Mock Part with non-empty text "hello world"
Part part = mock(Part.class);
when(part.text()).thenReturn(Optional.of("hello world"));
// Mock Content with non-empty parts list
Content content = mock(Content.class);
when(content.parts()).thenReturn(Optional.of(List.of(part)));
// Mock Event with content present and timestamp set
Event event = mock(Event.class);
when(event.content()).thenReturn(Optional.of(content));
when(event.author()).thenReturn("user");
when(event.timestamp()).thenReturn(ts);
// Mock Session
Session session = mock(Session.class);
when(session.appName()).thenReturn(appName);
when(session.userId()).thenReturn(userId);
when(session.id()).thenReturn(sessionId);
when(session.events()).thenReturn(List.of(event));
// Act
Completable addCompletable = service.addSessionToMemory(session);
addCompletable.test().assertComplete().assertNoErrors();
Single<SearchMemoryResponse> searchSingle = service.searchMemory(appName, userId, "hello");
SearchMemoryResponse response = searchSingle.blockingGet();
// Assert
assertNotNull(response, "SearchMemoryResponse should not be null");
List<MemoryEntry> memories = response.memories();
assertNotNull(memories, "Memories list should not be null");
assertTrue((int) memories.size() > 0, "Expected at least one memory to be returned");
boolean containsHelloContent =
memories.stream()
.anyMatch(
m -> {
Content c = m.content();
return c != null
&& c.parts().isPresent()
&& c.parts().get().stream()
.map(p -> p.text().orElse("").toLowerCase(Locale.ROOT))
.anyMatch(t -> t.contains("hello"));
});
assertTrue(containsHelloContent, "Expected at least one memory containing the word 'hello'");
// Also validate timestamp formatting is deterministic for the inserted event
String expectedIso = Instant.ofEpochSecond(ts).toString();
boolean timestampMatches = memories.stream().anyMatch(m -> expectedIso.equals(m.timestamp()));
assertTrue(
timestampMatches, "Expected at least one memory with the expected ISO-8601 timestamp");
}

@Test
@Tag("boundary")
public void testDoesNotStoreEventsWhenPartsAreEmpty() {
// Arrange
String appName = "appA";
String userId = "user1";
String sessionId = "s2";
// Mock Content with empty parts
Content content = mock(Content.class);
when(content.parts()).thenReturn(Optional.of(List.of()));
// Mock Event with content present but parts empty
Event event = mock(Event.class);
when(event.content()).thenReturn(Optional.of(content));
when(event.author()).thenReturn("user");
when(event.timestamp()).thenReturn(123L);
// Mock Session
Session session = mock(Session.class);
when(session.appName()).thenReturn(appName);
when(session.userId()).thenReturn(userId);
when(session.id()).thenReturn(sessionId);
when(session.events()).thenReturn(List.of(event));
// Act
Completable addCompletable = service.addSessionToMemory(session);
addCompletable.test().assertComplete().assertNoErrors();
// Query for any word; since parts were empty, there should be no matches
SearchMemoryResponse response = service.searchMemory(appName, userId, "anything").blockingGet();
// Assert
assertNotNull(response, "SearchMemoryResponse should not be null");
assertEquals(0, (int) response.memories().size(), "Expected no memories for empty parts");
}

@Test
@Tag("invalid")
public void testIgnoresEventsWithNoContent() {
// Arrange
String appName = "appA";
String userId = "user1";
String sessionId = "s3";
// Mock Event with no content
Event event = mock(Event.class);
when(event.content()).thenReturn(Optional.empty());
when(event.author()).thenReturn("user");
when(event.timestamp()).thenReturn(456L);
// Mock Session
Session session = mock(Session.class);
when(session.appName()).thenReturn(appName);
when(session.userId()).thenReturn(userId);
when(session.id()).thenReturn(sessionId);
when(session.events()).thenReturn(List.of(event));
// Act
Completable addCompletable = service.addSessionToMemory(session);
addCompletable.test().assertComplete().assertNoErrors();
// Assert: no memories should be found for any query
SearchMemoryResponse response = service.searchMemory(appName, userId, "query").blockingGet();
assertNotNull(response, "SearchMemoryResponse should not be null");
assertEquals(
0, (int) response.memories().size(), "Expected no memories when event has no content");
}

@Test
@Tag("integration")
public void testUserAndAppIsolationAndMultipleSessions() {
// Arrange
String appName = "appA";
String user1 = "user1";
String user2 = "user2";
// Event for user1: text "alpha"
Part partAlpha = mock(Part.class);
when(partAlpha.text()).thenReturn(Optional.of("alpha"));
Content contentAlpha = mock(Content.class);
when(contentAlpha.parts()).thenReturn(Optional.of(List.of(partAlpha)));
Event eventAlpha = mock(Event.class);
when(eventAlpha.content()).thenReturn(Optional.of(contentAlpha));
when(eventAlpha.author()).thenReturn("author1");
when(eventAlpha.timestamp()).thenReturn(1000L);
Session sessionUser1 = mock(Session.class);
when(sessionUser1.appName()).thenReturn(appName);
when(sessionUser1.userId()).thenReturn(user1);
when(sessionUser1.id()).thenReturn("s1");
when(sessionUser1.events()).thenReturn(List.of(eventAlpha));
// Event for user2: text "beta"
Part partBeta = mock(Part.class);
when(partBeta.text()).thenReturn(Optional.of("beta"));
Content contentBeta = mock(Content.class);
when(contentBeta.parts()).thenReturn(Optional.of(List.of(partBeta)));
Event eventBeta = mock(Event.class);
when(eventBeta.content()).thenReturn(Optional.of(contentBeta));
when(eventBeta.author()).thenReturn("author2");
when(eventBeta.timestamp()).thenReturn(2000L);
Session sessionUser2 = mock(Session.class);
when(sessionUser2.appName()).thenReturn(appName);
when(sessionUser2.userId()).thenReturn(user2);
when(sessionUser2.id()).thenReturn("s2");
when(sessionUser2.events()).thenReturn(List.of(eventBeta));
// Act
service.addSessionToMemory(sessionUser1).test().assertComplete().assertNoErrors();
service.addSessionToMemory(sessionUser2).test().assertComplete().assertNoErrors();
// Assert: searching user1 for "alpha" should return results
SearchMemoryResponse r1 = service.searchMemory(appName, user1, "alpha").blockingGet();
assertNotNull(r1, "Response should not be null");
assertTrue((int) r1.memories().size() > 0, "Expected memories for user1 query 'alpha'");
// Assert: searching user1 for "beta" should return no results (isolation)
SearchMemoryResponse r2 = service.searchMemory(appName, user1, "beta").blockingGet();
assertNotNull(r2, "Response should not be null");
assertEquals(0, (int) r2.memories().size(), "Expected no memories for user1 query 'beta'");
}

@Test
@Tag("integration")
public void testOverwriteExistingSessionIdReplacesEvents() {
// Arrange
String appName = "appA";
String userId = "user1";
String sessionId = "sOverwrite";
// First event: "foo"
Part partFoo = mock(Part.class);
when(partFoo.text()).thenReturn(Optional.of("foo"));
Content contentFoo = mock(Content.class);
when(contentFoo.parts()).thenReturn(Optional.of(List.of(partFoo)));
Event eventFoo = mock(Event.class);
when(eventFoo.content()).thenReturn(Optional.of(contentFoo));
when(eventFoo.author()).thenReturn("authorFoo");
when(eventFoo.timestamp()).thenReturn(3000L);
Session sessionFoo = mock(Session.class);
when(sessionFoo.appName()).thenReturn(appName);
when(sessionFoo.userId()).thenReturn(userId);
when(sessionFoo.id()).thenReturn(sessionId);
when(sessionFoo.events()).thenReturn(List.of(eventFoo));
// Second event: "bar" with the same session id (overwrite)
Part partBar = mock(Part.class);
when(partBar.text()).thenReturn(Optional.of("bar"));
Content contentBar = mock(Content.class);
when(contentBar.parts()).thenReturn(Optional.of(List.of(partBar)));
Event eventBar = mock(Event.class);
when(eventBar.content()).thenReturn(Optional.of(contentBar));
when(eventBar.author()).thenReturn("authorBar");
when(eventBar.timestamp()).thenReturn(4000L);
Session sessionBar = mock(Session.class);
when(sessionBar.appName()).thenReturn(appName);
when(sessionBar.userId()).thenReturn(userId);
when(sessionBar.id()).thenReturn(sessionId); // same session id
when(sessionBar.events()).thenReturn(List.of(eventBar));
// Act: add first, then overwrite with second
service.addSessionToMemory(sessionFoo).test().assertComplete().assertNoErrors();
service.addSessionToMemory(sessionBar).test().assertComplete().assertNoErrors();
// Assert: "foo" should no longer be found
SearchMemoryResponse rFoo = service.searchMemory(appName, userId, "foo").blockingGet();
assertNotNull(rFoo, "Response should not be null");
assertEquals(0, (int) rFoo.memories().size(), "Expected no memories for 'foo' after overwrite");
// Assert: "bar" should be found
SearchMemoryResponse rBar = service.searchMemory(appName, userId, "bar").blockingGet();
assertNotNull(rBar, "Response should not be null");
assertTrue((int) rBar.memories().size() > 0, "Expected memories for 'bar' after overwrite");
}
}
Loading