zhou zhou
15 小时以前 82624affb0251b75b62b35567d3eb260c06efe78
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.vincent.rsf.server.ai.service.impl.mcp;
 
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vincent.rsf.server.ai.config.AiDefaults;
import com.vincent.rsf.server.ai.dto.AiMcpToolPreviewDto;
import com.vincent.rsf.server.ai.entity.AiMcpMount;
import com.vincent.rsf.server.ai.mapper.AiMcpMountMapper;
import com.vincent.rsf.server.ai.service.BuiltinMcpToolRegistry;
import com.vincent.rsf.server.ai.service.McpMountRuntimeFactory;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.ai.tool.definition.ToolDefinition;
import org.springframework.ai.tool.metadata.ToolMetadata;
 
import java.util.List;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
 
@ExtendWith(MockitoExtension.class)
class AiMcpAdminServiceTest {
 
    @Mock
    private AiMcpMountMapper aiMcpMountMapper;
    @Mock
    private BuiltinMcpToolRegistry builtinMcpToolRegistry;
    @Mock
    private McpMountRuntimeFactory mcpMountRuntimeFactory;
 
    @Test
    void shouldMergeGovernedCatalogIntoPreviewResult() {
        AiMcpAdminService aiMcpAdminService = new AiMcpAdminService(
                aiMcpMountMapper,
                builtinMcpToolRegistry,
                mcpMountRuntimeFactory,
                new ObjectMapper()
        );
        AiMcpMount mount = new AiMcpMount()
                .setTransportType(AiDefaults.MCP_TRANSPORT_BUILTIN)
                .setBuiltinCode(AiDefaults.MCP_BUILTIN_RSF_WMS)
                .setName("builtin");
        ToolCallback callback = mock(ToolCallback.class);
        ToolDefinition toolDefinition = mock(ToolDefinition.class);
        ToolMetadata toolMetadata = mock(ToolMetadata.class);
        McpMountRuntimeFactory.McpMountRuntime runtime = mock(McpMountRuntimeFactory.McpMountRuntime.class);
        when(toolDefinition.name()).thenReturn("rsf_query_task");
        when(toolDefinition.description()).thenReturn("desc");
        when(toolDefinition.inputSchema()).thenReturn("{schema}");
        when(toolMetadata.returnDirect()).thenReturn(Boolean.FALSE);
        when(callback.getToolDefinition()).thenReturn(toolDefinition);
        when(callback.getToolMetadata()).thenReturn(toolMetadata);
        when(runtime.getToolCallbacks()).thenReturn(new ToolCallback[]{callback});
        when(runtime.getErrors()).thenReturn(List.of());
        when(mcpMountRuntimeFactory.create(List.of(mount), 1L)).thenReturn(runtime);
        when(builtinMcpToolRegistry.listBuiltinToolCatalog(AiDefaults.MCP_BUILTIN_RSF_WMS)).thenReturn(List.of(
                AiMcpToolPreviewDto.builder()
                        .name("rsf_query_task")
                        .toolGroup("任务查询")
                        .toolPurpose("purpose")
                        .queryBoundary("boundary")
                        .exampleQuestions(List.of("q1"))
                        .build()
        ));
 
        List<AiMcpToolPreviewDto> result = aiMcpAdminService.previewTools(mount, 1L);
 
        assertEquals(1, result.size());
        assertEquals("任务查询", result.get(0).getToolGroup());
        assertEquals("purpose", result.get(0).getToolPurpose());
        assertEquals("{schema}", result.get(0).getInputSchema());
    }
}