This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
package io.shinhanlife.dap.mcg.presentation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.shinhanlife.dap.lib.mcp.security.SecurityProperties;
|
||||
import io.shinhanlife.dap.lib.adapter.dto.JsonRpcResponse;
|
||||
import io.shinhanlife.dap.mcg.config.GatewayFallbackProperties;
|
||||
import io.shinhanlife.dap.mcg.registry.RedisRegistryService;
|
||||
import io.shinhanlife.dap.mcg.service.ExecuteService;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.data.redis.RedisConnectionFailureException;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
class McpRouterControllerTest {
|
||||
|
||||
@Test
|
||||
void returnsEmptyToolListWhenRedisIsUnavailableAndNoFallbackIsConfigured() {
|
||||
RedisRegistryService registryService = org.mockito.Mockito.mock(RedisRegistryService.class);
|
||||
when(registryService.getAllTools()).thenThrow(new RedisConnectionFailureException("Redis unavailable"));
|
||||
|
||||
McpRouterController controller = new McpRouterController(
|
||||
registryService,
|
||||
org.mockito.Mockito.mock(ExecuteService.class),
|
||||
org.mockito.Mockito.mock(SecurityProperties.class),
|
||||
new ObjectMapper(),
|
||||
new GatewayFallbackProperties());
|
||||
|
||||
ResponseEntity<JsonRpcResponse> response = assertDoesNotThrow(() -> controller.listTools(null));
|
||||
Map<?, ?> result = (Map<?, ?>) response.getBody().getResult();
|
||||
|
||||
assertEquals(List.of(), result.get("tools"));
|
||||
}
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
package io.shinhanlife.dap.lib.mcp;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.shinhanlife.dap.lib.annotation.GrowToolHint;
|
||||
import io.shinhanlife.dap.lib.config.McpProperties;
|
||||
import io.shinhanlife.dap.lib.util.ToolSchemaResolver;
|
||||
import io.shinhanlife.dap.lib.validation.ToolArgumentSchemaValidator;
|
||||
import java.util.Map;
|
||||
import java.util.Arrays;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springaicommunity.mcp.annotation.McpTool;
|
||||
import org.springaicommunity.mcp.annotation.McpToolParam;
|
||||
import org.springframework.context.support.StaticApplicationContext;
|
||||
|
||||
class McpToolExecutionServiceTest {
|
||||
|
||||
@Test
|
||||
void returnsNotFoundWhenNoToolMatchesTheRequestedName() {
|
||||
McpToolExecutionService service = serviceWith(new Object());
|
||||
ToolExecutionResult result = service.execute("missing.cmm.tool.inquiry", null, Map.of());
|
||||
assertEquals(404, result.statusCode());
|
||||
assertEquals("TOOL_NOT_FOUND", ((Map<?, ?>) result.body()).get("code"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void convertsArgumentsToDtoAndExecutesTheMatchedTool() {
|
||||
McpToolExecutionService service = serviceWith(new EchoTool());
|
||||
ToolExecutionResult result = service.execute("sample.cmm.value.echo",
|
||||
headers(Map.of(
|
||||
"requestId", "request-1",
|
||||
"guid", "guid-1",
|
||||
"mcpSessionId", "session-1",
|
||||
"employeeNo", "employee-1",
|
||||
"virtualEmployeeNo", "virtual-1",
|
||||
"headerRequestId", "request-1",
|
||||
"traceId", "guid-1",
|
||||
"encryptedEmployeeId", "employee-1")),
|
||||
Map.of("value", "hello"));
|
||||
assertEquals(200, result.statusCode());
|
||||
assertEquals("hello", ((Map<?, ?>) result.body()).get("value"));
|
||||
assertEquals("request-1", result.headers().get("x-request-id"));
|
||||
assertEquals("guid-1", result.headers().get("guid"));
|
||||
assertEquals("session-1", result.headers().get("mcp-session-id"));
|
||||
}
|
||||
|
||||
private McpRequestHeaders headers(Map<String, String> values) {
|
||||
try {
|
||||
Class<?>[] types = Arrays.stream(McpRequestHeaders.class.getRecordComponents())
|
||||
.map(component -> component.getType())
|
||||
.toArray(Class<?>[]::new);
|
||||
Object[] arguments = Arrays.stream(McpRequestHeaders.class.getRecordComponents())
|
||||
.map(component -> values.get(component.getName()))
|
||||
.toArray();
|
||||
return McpRequestHeaders.class.getDeclaredConstructor(types).newInstance(arguments);
|
||||
} catch (ReflectiveOperationException exception) {
|
||||
throw new AssertionError(exception);
|
||||
}
|
||||
}
|
||||
|
||||
private McpToolExecutionService serviceWith(Object toolBean) {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
StaticApplicationContext context = new StaticApplicationContext();
|
||||
context.getBeanFactory().registerSingleton("toolBean", toolBean);
|
||||
context.refresh();
|
||||
McpToolMethodRegistry registry = new McpToolMethodRegistry(context, new McpProperties());
|
||||
registry.initialize();
|
||||
return new McpToolExecutionService(registry, objectMapper,
|
||||
new ToolArgumentSchemaValidator(objectMapper), new ToolSchemaResolver(objectMapper));
|
||||
}
|
||||
|
||||
static class EchoTool {
|
||||
@McpTool(name = "sample.cmm.value.echo", description = "Echoes a value")
|
||||
@GrowToolHint
|
||||
public Map<String, Object> execute(EchoRequest request) {
|
||||
return Map.of("value", request.value);
|
||||
}
|
||||
}
|
||||
|
||||
static class EchoRequest {
|
||||
@McpToolParam(description = "Value", required = true)
|
||||
private String value;
|
||||
public String getValue() { return value; }
|
||||
public void setValue(String value) { this.value = value; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user