From 4c3dff46fe0bd2cc6e6ce1cbe08c0128aa9dde2f Mon Sep 17 00:00:00 2001 From: roost-io <8110509+mgdevstack@users.noreply.github.com> Date: Thu, 7 May 2026 13:13:25 +0530 Subject: [PATCH] Unit test generated by RoostGPT Using AI Model gpt-5 --- core/pom.xml | 21 +- .../adk/models/ClaudeGenerateContentTest.java | 271 ++++++++++++++++++ pom.xml | 79 +++-- 3 files changed, 347 insertions(+), 24 deletions(-) create mode 100644 core/src/test/java/com/google/adk/models/ClaudeGenerateContentTest.java 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/models/ClaudeGenerateContentTest.java b/core/src/test/java/com/google/adk/models/ClaudeGenerateContentTest.java new file mode 100644 index 000000000..7a2dd3f45 --- /dev/null +++ b/core/src/test/java/com/google/adk/models/ClaudeGenerateContentTest.java @@ -0,0 +1,271 @@ +/* + * 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=generateContent_bc4a182290 +ROOST_METHOD_SIG_HASH=generateContent_c08b9769fe + +Scenario 1: Builds messages from text-only contents and invokes Anthropic once + +Details: + TestName: buildsMessageParamsFromTextOnlyContents + Description: Verifies that when LlmRequest contains Content instances with only text Part(s), the method maps them into Anthropic MessageParam(s), builds MessageCreateParams correctly, calls anthropicClient.messages().create exactly once, and returns a Flowable that emits exactly one LlmResponse. + +Execution: + Arrange: Create an LlmRequest with a non-empty contents() list where each Content has role "user" and parts containing text only. Mock anthropicClient.messages().create to return a valid Message with a simple text ContentBlock. + Act: Call generateContent with stream=false. + Assert: Capture the MessageCreateParams passed to anthropicClient.messages().create and assert that: + - messages contains entries equal in count to LlmRequest.contents(). + - each MessageParam contains text blocks reflecting the corresponding input text parts. + - the Flowable emits exactly one LlmResponse and completes. +Validation: + Confirms that text parts are correctly propagated to Anthropic as text message blocks and that the method performs a single, synchronous Anthropic call, then wraps the converted response in a single-emission Flowable. + + +*/ + +// ********RoostGPT******** + +package com.google.adk.models; + +import com.anthropic.client.AnthropicClient; +import com.anthropic.models.messages.Message; +import com.anthropic.models.messages.MessageCreateParams; +import com.anthropic.models.messages.MessageParam; +import com.anthropic.models.messages.MessageParam.Role; +import com.anthropic.models.messages.TextBlockParam; +import com.fasterxml.jackson.core.type.TypeReference; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.genai.types.Content; +import com.google.genai.types.GenerateContentConfig; +import com.google.genai.types.Part; +import io.reactivex.rxjava3.core.Flowable; +import io.reactivex.rxjava3.subscribers.TestSubscriber; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +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.Answers; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; +import org.junit.jupiter.api.*; +import com.anthropic.models.messages.ContentBlock; +import com.anthropic.models.messages.ContentBlockParam; +import com.anthropic.models.messages.Tool; +import com.anthropic.models.messages.ToolChoice; +import com.anthropic.models.messages.ToolChoiceAuto; +import com.anthropic.models.messages.ToolResultBlockParam; +import com.anthropic.models.messages.ToolUnion; +import com.anthropic.models.messages.ToolUseBlockParam; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import com.google.genai.types.FunctionCall; +import com.google.genai.types.FunctionDeclaration; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Tests for Claude.generateContent method. + */ +@ExtendWith(MockitoExtension.class) +public abstract class ClaudeGenerateContentTest extends BaseLlm { + + @Mock(answer = Answers.RETURNS_DEEP_STUBS) + private AnthropicClient anthropicClient; + + @Captor + private ArgumentCaptor paramsCaptor; + + private Claude claude; + + public ClaudeGenerateContentTest() { + super("test-model"); // TODO: Update model name if needed for your environment + } + + @BeforeEach + public void setUp() { + // Default maxTokens provided via constructor for predictability + this.claude = new Claude("test-model", anthropicClient, 1024); + } + + @Test + @Tag("valid") + public void public void testBuildsMessageParamsFromTextOnlyContents() { + // Arrange + List parts1 = ImmutableList.of( + Part.builder().text("Hello").build(), + Part.builder().text("How are you?").build() + ); + List parts2 = ImmutableList.of( + Part.builder().text("Second message").build() + ); + Content content1 = Content.builder().role("user").parts(ImmutableList.copyOf(parts1)).build(); + Content content2 = Content.builder().role("user").parts(ImmutableList.copyOf(parts2)).build(); + LlmRequest llmRequest = LlmRequest.builder() + .model("test-model") + .contents(ImmutableList.of(content1, content2)) + .build(); + Message mockedMessage = mock(Message.class); + when(anthropicClient.messages().create(any(MessageCreateParams.class))).thenReturn(mockedMessage); + // Act + Flowable flowable = claude.generateContent(llmRequest, false); + // Assert + verify(anthropicClient.messages(), times(1)).create(paramsCaptor.capture()); + MessageCreateParams capturedParams = paramsCaptor.getValue(); + // Validate that the number of messages matches the number of contents + List messageParams = capturedParams.messages(); + assertEquals(2, (int) messageParams.size(), "Expected two message params to be created"); + // Validate that each MessageParam contains corresponding text blocks + List> expectedTexts = new ArrayList<>(); + expectedTexts.add(parts1.stream().map(p -> p.text().orElse("")).collect(Collectors.toList())); + expectedTexts.add(parts2.stream().map(p -> p.text().orElse("")).collect(Collectors.toList())); + for (int i = 0; i < messageParams.size(); i++) { + MessageParam mp = messageParams.get(i); + // Role should map to USER for "user" + assertEquals(Role.USER, mp.role(), "Expected role USER for user content"); + // Extract text values from content blocks + List actualTexts = mp.content().stream() + .filter(cb -> cb.isText()) + .map(cb -> cb.asText()) + .map(TextBlockParam::text) + .collect(Collectors.toList()); + assertEquals(expectedTexts.get(i), actualTexts, "Text blocks should match input parts"); + } + // System text should be empty since not provided + assertEquals("", capturedParams.system(), "Expected empty system text when not provided"); + // Flowable should emit exactly one LlmResponse and complete + TestSubscriber ts = new TestSubscriber<>(); + flowable.subscribe(ts); + ts.assertNoErrors(); + ts.assertValueCount(1); + ts.assertComplete(); + // Additional sanity: emitted LlmResponse object is non-null + List emitted = ts.values(); + assertNotNull(emitted.get(0), "Emitted LlmResponse should not be null"); + } + + @Test + @Tag("boundary") + public void public void testHandlesEmptyContentsAndCallsAnthropicOnce() { + // Arrange + LlmRequest llmRequest = LlmRequest.builder() + .model("test-model") + .contents(ImmutableList.of()) + .build(); + Message mockedMessage = mock(Message.class); + when(anthropicClient.messages().create(any(MessageCreateParams.class))).thenReturn(mockedMessage); + // Act + Flowable flowable = claude.generateContent(llmRequest, false); + // Assert + verify(anthropicClient.messages(), times(1)).create(paramsCaptor.capture()); + MessageCreateParams capturedParams = paramsCaptor.getValue(); + assertNotNull(capturedParams, "Captured params should not be null"); + assertEquals(0, (int) capturedParams.messages().size(), "Expected zero message params for empty contents"); + TestSubscriber ts = new TestSubscriber<>(); + flowable.subscribe(ts); + ts.assertNoErrors(); + ts.assertValueCount(1); + ts.assertComplete(); + } + + @Test + @Tag("valid") + public void public void testAppliesSystemInstructionWhenPresent() { + // Arrange + Content systemInstruction = Content.builder() + .parts(ImmutableList.of( + Part.builder().text("You are a helpful system").build(), + Part.builder().text("Follow the rules").build() + )) + .build(); + GenerateContentConfig config = GenerateContentConfig.builder() + .systemInstruction(systemInstruction) + .build(); + Content userContent = Content.builder() + .role("user") + .parts(ImmutableList.of(Part.builder().text("Tell me a joke").build())) + .build(); + LlmRequest llmRequest = LlmRequest.builder() + .model("test-model") + .config(config) + .contents(ImmutableList.of(userContent)) + .build(); + Message mockedMessage = mock(Message.class); + when(anthropicClient.messages().create(any(MessageCreateParams.class))).thenReturn(mockedMessage); + // Act + Flowable flowable = claude.generateContent(llmRequest, false); + // Assert + verify(anthropicClient.messages(), times(1)).create(paramsCaptor.capture()); + MessageCreateParams capturedParams = paramsCaptor.getValue(); + String expectedSystem = "You are a helpful system\nFollow the rules"; + assertEquals(expectedSystem, capturedParams.system(), "System instruction should be correctly concatenated"); + TestSubscriber ts = new TestSubscriber<>(); + flowable.subscribe(ts); + ts.assertNoErrors(); + ts.assertValueCount(1); + ts.assertComplete(); + } + + @Test + @Tag("valid") + public void public void testRoleMappingAssistantAndModelToAssistant() { + // Arrange + Content assistantContent = Content.builder() + .role("assistant") + .parts(ImmutableList.of(Part.builder().text("Assistant reply").build())) + .build(); + Content modelContent = Content.builder() + .role("model") + .parts(ImmutableList.of(Part.builder().text("Model style reply").build())) + .build(); + LlmRequest llmRequest = LlmRequest.builder() + .model("test-model") + .contents(ImmutableList.of(assistantContent, modelContent)) + .build(); + Message mockedMessage = mock(Message.class); + when(anthropicClient.messages().create(any(MessageCreateParams.class))).thenReturn(mockedMessage); + // Act + Flowable flowable = claude.generateContent(llmRequest, false); + // Assert + verify(anthropicClient.messages(), times(1)).create(paramsCaptor.capture()); + MessageCreateParams capturedParams = paramsCaptor.getValue(); + List messageParams = capturedParams.messages(); + assertEquals(2, (int) messageParams.size(), "Expected two message params"); + for (MessageParam mp : messageParams) { + assertEquals(Role.ASSISTANT, mp.role(), "assistant/model roles should map to ASSISTANT"); + } + TestSubscriber ts = new TestSubscriber<>(); + flowable.subscribe(ts); + ts.assertNoErrors(); + ts.assertValueCount(1); + ts.assertComplete(); + } + +} \ No newline at end of file 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