diff --git a/core/pom.xml b/core/pom.xml index fe65715f3..7d2032c7e 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -1,4 +1,4 @@ - + 4.0.0 - com.google.adk google-adk-parent - 0.4.1-SNAPSHOT + 0.4.1-SNAPSHOT + - google-adk Agent Development Kit Agent Development Kit: an open-source, code-first toolkit designed to simplify building, evaluating, and deploying advanced AI agents anywhere. - - - com.anthropic @@ -201,6 +197,15 @@ maven-compiler-plugin + + io.spring.javaformat + spring-javaformat-maven-plugin + 0.0.40 + + + + + - + \ No newline at end of file diff --git a/core/src/test/java/com/google/adk/memory/InMemoryMemoryServiceAddSessionToMemoryTest.java b/core/src/test/java/com/google/adk/memory/InMemoryMemoryServiceAddSessionToMemoryTest.java new file mode 100644 index 000000000..90300ef9e --- /dev/null +++ b/core/src/test/java/com/google/adk/memory/InMemoryMemoryServiceAddSessionToMemoryTest.java @@ -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 searchSingle = service.searchMemory(appName, userId, "hello"); + SearchMemoryResponse response = searchSingle.blockingGet(); + // Assert + assertNotNull(response, "SearchMemoryResponse should not be null"); + List 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"); + } +} diff --git a/pom.xml b/pom.xml index 6009c7316..24fd6ece6 100644 --- a/pom.xml +++ b/pom.xml @@ -1,4 +1,4 @@ - + - + 4.0.0 - com.google.adk google-adk-parent - 0.4.1-SNAPSHOT + 0.4.1-SNAPSHOT + pom - Google Agent Development Kit Maven Parent POM https://github.com/google/adk-java Google Agent Development Kit (ADK) for Java - core dev @@ -39,12 +35,10 @@ a2a a2a/webservice - 17 ${java.version} UTF-8 - 1.11.0 3.4.1 1.49.0 @@ -73,7 +67,6 @@ 3.9.0 5.4.3 - @@ -112,7 +105,6 @@ pom import - com.anthropic @@ -274,9 +266,21 @@ assertj-core ${assertj.version} + + org.mockito + mockito-junit-jupiter + 2.23.4 + test + + + + io.spring.javaformat + spring-javaformat-formatter + 0.0.40 + + - @@ -324,8 +328,7 @@ plain - + **/*Test.java @@ -469,6 +472,36 @@ + + org.apache.maven.plugins + maven-surefire-plugin + 3.2.5 + + + + org.apache.maven.plugins + maven-surefire-report-plugin + 3.2.5 + + testReport + + + + + org.apache.maven.plugins + maven-site-plugin + 2.1 + + testReport + + + + + io.spring.javaformat + spring-javaformat-maven-plugin + 0.0.40 + + @@ -528,7 +561,6 @@ - The Apache License, Version 2.0 @@ -558,4 +590,19 @@ https://central.sonatype.com/repository/maven-snapshots/ + + + org.mockito + mockito-junit-jupiter + 2.23.4 + test + + + + io.spring.javaformat + spring-javaformat-formatter + 0.0.40 + + + \ No newline at end of file