| 
 | 1 | +/*  | 
 | 2 | + * Copyright 2023-2025 the original author or authors.  | 
 | 3 | + *  | 
 | 4 | + * Licensed under the Apache License, Version 2.0 (the "License");  | 
 | 5 | + * you may not use this file except in compliance with the License.  | 
 | 6 | + * You may obtain a copy of the License at  | 
 | 7 | + *  | 
 | 8 | + *      https://www.apache.org/licenses/LICENSE-2.0  | 
 | 9 | + *  | 
 | 10 | + * Unless required by applicable law or agreed to in writing, software  | 
 | 11 | + * distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 13 | + * See the License for the specific language governing permissions and  | 
 | 14 | + * limitations under the License.  | 
 | 15 | + */  | 
 | 16 | + | 
 | 17 | +package org.springframework.ai.model.tool;  | 
 | 18 | + | 
 | 19 | +import java.util.List;  | 
 | 20 | +import java.util.Map;  | 
 | 21 | + | 
 | 22 | +import io.micrometer.observation.ObservationRegistry;  | 
 | 23 | +import org.junit.jupiter.api.Test;  | 
 | 24 | + | 
 | 25 | +import org.springframework.ai.chat.messages.AssistantMessage;  | 
 | 26 | +import org.springframework.ai.chat.messages.UserMessage;  | 
 | 27 | +import org.springframework.ai.chat.model.ChatResponse;  | 
 | 28 | +import org.springframework.ai.chat.model.Generation;  | 
 | 29 | +import org.springframework.ai.chat.prompt.Prompt;  | 
 | 30 | +import org.springframework.ai.tool.ToolCallback;  | 
 | 31 | +import org.springframework.ai.tool.definition.DefaultToolDefinition;  | 
 | 32 | +import org.springframework.ai.tool.definition.ToolDefinition;  | 
 | 33 | +import org.springframework.ai.tool.metadata.ToolMetadata;  | 
 | 34 | + | 
 | 35 | +import static org.assertj.core.api.Assertions.assertThat;  | 
 | 36 | +import static org.assertj.core.api.Assertions.assertThatNoException;  | 
 | 37 | + | 
 | 38 | +/**  | 
 | 39 | + * Tests for {@link DefaultToolCallingManager} with empty/null arguments handling.  | 
 | 40 | + *  | 
 | 41 | + */  | 
 | 42 | +class DefaultToolCallingManagerTest {  | 
 | 43 | + | 
 | 44 | +	@Test  | 
 | 45 | +	void shouldHandleNullArgumentsInStreamMode() {  | 
 | 46 | +		// Create a mock tool callback  | 
 | 47 | +		ToolCallback mockToolCallback = new ToolCallback() {  | 
 | 48 | +			@Override  | 
 | 49 | +			public ToolDefinition getToolDefinition() {  | 
 | 50 | +				return DefaultToolDefinition.builder()  | 
 | 51 | +					.name("testTool")  | 
 | 52 | +					.description("A test tool")  | 
 | 53 | +					.inputSchema("{}")  | 
 | 54 | +					.build();  | 
 | 55 | +			}  | 
 | 56 | + | 
 | 57 | +			@Override  | 
 | 58 | +			public ToolMetadata getToolMetadata() {  | 
 | 59 | +				return ToolMetadata.builder().build();  | 
 | 60 | +			}  | 
 | 61 | + | 
 | 62 | +			@Override  | 
 | 63 | +			public String call(String toolInput) {  | 
 | 64 | +				// Verify the input is not null or empty  | 
 | 65 | +				assertThat(toolInput).isNotNull();  | 
 | 66 | +				assertThat(toolInput).isNotEmpty();  | 
 | 67 | +				return "{\"result\": \"success\"}";  | 
 | 68 | +			}  | 
 | 69 | +		};  | 
 | 70 | + | 
 | 71 | +		// Create a ToolCall with empty parameters  | 
 | 72 | +		AssistantMessage.ToolCall toolCall = new AssistantMessage.ToolCall("1", "function", "testTool", null);  | 
 | 73 | + | 
 | 74 | +		// Create a ChatResponse  | 
 | 75 | +		AssistantMessage assistantMessage = new AssistantMessage("", Map.of(), List.of(toolCall));  | 
 | 76 | +		Generation generation = new Generation(assistantMessage);  | 
 | 77 | +		ChatResponse chatResponse = new ChatResponse(List.of(generation));  | 
 | 78 | + | 
 | 79 | +		// Create a Prompt with tool callbacks  | 
 | 80 | +		Prompt prompt = new Prompt(List.of(new UserMessage("test")));  | 
 | 81 | + | 
 | 82 | +		// Mock the tool callbacks resolution by creating a custom ToolCallbackResolver  | 
 | 83 | +		DefaultToolCallingManager managerWithCallback = DefaultToolCallingManager.builder()  | 
 | 84 | +			.observationRegistry(ObservationRegistry.NOOP)  | 
 | 85 | +			.toolCallbackResolver(toolName -> {  | 
 | 86 | +				if ("testTool".equals(toolName)) {  | 
 | 87 | +					return mockToolCallback;  | 
 | 88 | +				}  | 
 | 89 | +				return null;  | 
 | 90 | +			})  | 
 | 91 | +			.build();  | 
 | 92 | + | 
 | 93 | +		// Verify that no exception is thrown  | 
 | 94 | +		assertThatNoException().isThrownBy(() -> managerWithCallback.executeToolCalls(prompt, chatResponse));  | 
 | 95 | +	}  | 
 | 96 | + | 
 | 97 | +	@Test  | 
 | 98 | +	void shouldHandleEmptyArgumentsInStreamMode() {  | 
 | 99 | +		// Create a mock tool callback  | 
 | 100 | +		ToolCallback mockToolCallback = new ToolCallback() {  | 
 | 101 | +			@Override  | 
 | 102 | +			public ToolDefinition getToolDefinition() {  | 
 | 103 | +				return DefaultToolDefinition.builder()  | 
 | 104 | +					.name("testTool")  | 
 | 105 | +					.description("A test tool")  | 
 | 106 | +					.inputSchema("{}")  | 
 | 107 | +					.build();  | 
 | 108 | +			}  | 
 | 109 | + | 
 | 110 | +			@Override  | 
 | 111 | +			public ToolMetadata getToolMetadata() {  | 
 | 112 | +				return ToolMetadata.builder().build();  | 
 | 113 | +			}  | 
 | 114 | + | 
 | 115 | +			@Override  | 
 | 116 | +			public String call(String toolInput) {  | 
 | 117 | +				// Verify the input is not null or empty  | 
 | 118 | +				assertThat(toolInput).isNotNull();  | 
 | 119 | +				assertThat(toolInput).isNotEmpty();  | 
 | 120 | +				return "{\"result\": \"success\"}";  | 
 | 121 | +			}  | 
 | 122 | +		};  | 
 | 123 | + | 
 | 124 | +		// Create a ToolCall with empty parameters  | 
 | 125 | +		AssistantMessage.ToolCall toolCall = new AssistantMessage.ToolCall("1", "function", "testTool", "");  | 
 | 126 | + | 
 | 127 | +		// Create a ChatResponse  | 
 | 128 | +		AssistantMessage assistantMessage = new AssistantMessage("", Map.of(), List.of(toolCall));  | 
 | 129 | +		Generation generation = new Generation(assistantMessage);  | 
 | 130 | +		ChatResponse chatResponse = new ChatResponse(List.of(generation));  | 
 | 131 | + | 
 | 132 | +		// Create a Prompt with tool callbacks  | 
 | 133 | +		Prompt prompt = new Prompt(List.of(new UserMessage("test")));  | 
 | 134 | + | 
 | 135 | +		// Mock the tool callbacks resolution by creating a custom ToolCallbackResolver  | 
 | 136 | +		DefaultToolCallingManager managerWithCallback = DefaultToolCallingManager.builder()  | 
 | 137 | +			.observationRegistry(ObservationRegistry.NOOP)  | 
 | 138 | +			.toolCallbackResolver(toolName -> {  | 
 | 139 | +				if ("testTool".equals(toolName)) {  | 
 | 140 | +					return mockToolCallback;  | 
 | 141 | +				}  | 
 | 142 | +				return null;  | 
 | 143 | +			})  | 
 | 144 | +			.build();  | 
 | 145 | + | 
 | 146 | +		// Verify that no exception is thrown  | 
 | 147 | +		assertThatNoException().isThrownBy(() -> managerWithCallback.executeToolCalls(prompt, chatResponse));  | 
 | 148 | +	}  | 
 | 149 | + | 
 | 150 | +}  | 
0 commit comments