|
| 1 | +/* |
| 2 | + * Copyright 2024-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 com.alibaba.cloud.ai.mcp.gateway.nacos.callback; |
| 18 | + |
| 19 | +import com.alibaba.cloud.ai.mcp.gateway.core.utils.SpringBeanUtils; |
| 20 | +import com.alibaba.cloud.ai.mcp.gateway.nacos.definition.NacosMcpGatewayToolDefinition; |
| 21 | +import com.alibaba.cloud.ai.mcp.nacos.service.NacosMcpOperationService; |
| 22 | +import com.alibaba.nacos.api.ai.model.mcp.McpServerRemoteServiceConfig; |
| 23 | +import com.fasterxml.jackson.databind.JsonNode; |
| 24 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 25 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 26 | +import org.junit.jupiter.api.AfterEach; |
| 27 | +import org.junit.jupiter.api.BeforeEach; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.mockito.Mockito; |
| 30 | +import org.springframework.context.support.GenericApplicationContext; |
| 31 | +import org.springframework.web.reactive.function.client.WebClient; |
| 32 | + |
| 33 | +import java.lang.reflect.Method; |
| 34 | +import java.util.Collections; |
| 35 | +import java.util.Map; |
| 36 | + |
| 37 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 38 | + |
| 39 | +/** |
| 40 | + * Tests for NacosMcpGatewayToolCallback response template processing |
| 41 | + * @author saladday |
| 42 | + */ |
| 43 | +class NacosMcpGatewayToolCallbackTest { |
| 44 | + |
| 45 | + private GenericApplicationContext applicationContext; |
| 46 | + |
| 47 | + private final ObjectMapper objectMapper = new ObjectMapper(); |
| 48 | + |
| 49 | + @BeforeEach |
| 50 | + void setUp() { |
| 51 | + applicationContext = new GenericApplicationContext(); |
| 52 | + applicationContext.registerBean(WebClient.Builder.class, WebClient::builder); |
| 53 | + applicationContext.registerBean(NacosMcpOperationService.class, () -> Mockito.mock(NacosMcpOperationService.class)); |
| 54 | + applicationContext.refresh(); |
| 55 | + SpringBeanUtils.getInstance().setApplicationContext(applicationContext); |
| 56 | + } |
| 57 | + |
| 58 | + @AfterEach |
| 59 | + void tearDown() { |
| 60 | + if (applicationContext != null) { |
| 61 | + applicationContext.close(); |
| 62 | + } |
| 63 | + SpringBeanUtils.getInstance().setApplicationContext(null); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void bodyTemplateWithRootPlaceholderReturnsRawResponse() throws Exception { |
| 68 | + NacosMcpGatewayToolDefinition definition = new NacosMcpGatewayToolDefinition(); |
| 69 | + definition.setName("test-tool"); |
| 70 | + definition.setDescription("test tool"); |
| 71 | + definition.setProtocol("http"); |
| 72 | + definition.setRemoteServerConfig(new McpServerRemoteServiceConfig()); |
| 73 | + |
| 74 | + NacosMcpGatewayToolCallback callback = new NacosMcpGatewayToolCallback(definition); |
| 75 | + |
| 76 | + ObjectNode responseTemplate = objectMapper.createObjectNode(); |
| 77 | + responseTemplate.put("body", "{{.}}"); |
| 78 | + |
| 79 | + JsonNode templateNode = responseTemplate; |
| 80 | + |
| 81 | + Method processResponse = NacosMcpGatewayToolCallback.class |
| 82 | + .getDeclaredMethod("processResponse", String.class, JsonNode.class, Map.class); |
| 83 | + processResponse.setAccessible(true); |
| 84 | + |
| 85 | + String response = "20.5"; |
| 86 | + String result = (String) processResponse.invoke(callback, response, templateNode, Collections.emptyMap()); |
| 87 | + |
| 88 | + assertEquals(response, result); |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments