|
| 1 | +/* |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby.internal.mcp; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.*; |
| 9 | +import static org.mockito.Mockito.*; |
| 10 | + |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +import io.jooby.Jooby; |
| 15 | +import io.jooby.Router; |
| 16 | +import io.jooby.StatusCode; |
| 17 | +import io.jooby.mcp.McpChain; |
| 18 | +import io.jooby.mcp.McpOperation; |
| 19 | +import io.modelcontextprotocol.common.McpTransportContext; |
| 20 | +import io.modelcontextprotocol.spec.McpError; |
| 21 | +import io.modelcontextprotocol.spec.McpSchema; |
| 22 | + |
| 23 | +public class McpExecutorTest { |
| 24 | + |
| 25 | + private Jooby app; |
| 26 | + private Router router; |
| 27 | + private McpExecutor executor; |
| 28 | + private McpTransportContext transportContext; |
| 29 | + private McpChain chain; |
| 30 | + private McpOperation operation; |
| 31 | + |
| 32 | + @BeforeEach |
| 33 | + void setUp() { |
| 34 | + app = mock(Jooby.class); |
| 35 | + router = mock(Router.class); |
| 36 | + when(app.getRouter()).thenReturn(router); |
| 37 | + |
| 38 | + executor = new McpExecutor(app); |
| 39 | + transportContext = mock(McpTransportContext.class); |
| 40 | + chain = mock(McpChain.class); |
| 41 | + operation = mock(McpOperation.class); |
| 42 | + |
| 43 | + // Setup default operation behavior |
| 44 | + when(operation.getClassName()).thenReturn(getClass().getName()); |
| 45 | + when(operation.getId()).thenReturn("test-op"); |
| 46 | + |
| 47 | + // Global router default to prevent NPE during logging |
| 48 | + when(router.errorCode(any())).thenReturn(StatusCode.SERVER_ERROR); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void testInvokeSuccess() throws Throwable { |
| 53 | + Object result = new Object(); |
| 54 | + when(chain.proceed(any(), eq(transportContext), eq(operation))).thenReturn(result); |
| 55 | + |
| 56 | + Object actual = executor.invoke(null, transportContext, operation, chain); |
| 57 | + |
| 58 | + assertEquals(result, actual); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void testInvokeFatalError() throws Throwable { |
| 63 | + // OutOfMemoryError triggers SneakyThrows.isFatal |
| 64 | + OutOfMemoryError fatal = new OutOfMemoryError(); |
| 65 | + when(chain.proceed(any(), any(), any())).thenThrow(fatal); |
| 66 | + |
| 67 | + // This should now propagate the fatal error after logging |
| 68 | + assertThrows( |
| 69 | + OutOfMemoryError.class, () -> executor.invoke(null, transportContext, operation, chain)); |
| 70 | + |
| 71 | + verify(operation).exception(fatal); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void testInvokeToolError() throws Throwable { |
| 76 | + Exception error = new Exception("tool-failure"); |
| 77 | + when(chain.proceed(any(), any(), any())).thenThrow(error); |
| 78 | + when(operation.isTool()).thenReturn(true); |
| 79 | + |
| 80 | + Object result = executor.invoke(null, transportContext, operation, chain); |
| 81 | + |
| 82 | + assertTrue(result instanceof McpSchema.CallToolResult); |
| 83 | + McpSchema.CallToolResult toolResult = (McpSchema.CallToolResult) result; |
| 84 | + assertTrue(toolResult.isError()); |
| 85 | + assertEquals("tool-failure", ((McpSchema.TextContent) toolResult.content().get(0)).text()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void testInvokeMcpErrorRethrow() throws Throwable { |
| 90 | + McpSchema.JSONRPCResponse.JSONRPCError jsonError = |
| 91 | + new McpSchema.JSONRPCResponse.JSONRPCError(-32000, "mcp-error", null); |
| 92 | + McpError mcpError = new McpError(jsonError); |
| 93 | + |
| 94 | + when(chain.proceed(any(), any(), any())).thenThrow(mcpError); |
| 95 | + |
| 96 | + // Should rethrow the same McpError |
| 97 | + McpError actual = |
| 98 | + assertThrows( |
| 99 | + McpError.class, () -> executor.invoke(null, transportContext, operation, chain)); |
| 100 | + |
| 101 | + assertEquals(jsonError, actual.getJsonRpcError()); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void testStatusCodeMapping() throws Throwable { |
| 106 | + checkMapping(new Exception(), StatusCode.NOT_FOUND, McpSchema.ErrorCodes.RESOURCE_NOT_FOUND); |
| 107 | + checkMapping(new Exception(), StatusCode.BAD_REQUEST, McpSchema.ErrorCodes.INVALID_PARAMS); |
| 108 | + checkMapping(new Exception(), StatusCode.CONFLICT, McpSchema.ErrorCodes.INVALID_PARAMS); |
| 109 | + checkMapping(new Exception(), StatusCode.FORBIDDEN, McpSchema.ErrorCodes.INTERNAL_ERROR); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void testIsServerErrorBranching() { |
| 114 | + assertTrue(McpExecutor.isServerError(McpSchema.ErrorCodes.INTERNAL_ERROR)); |
| 115 | + assertTrue(McpExecutor.isServerError(-32701)); |
| 116 | + assertFalse(McpExecutor.isServerError(-32600)); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + void testToolErrorWithNullMessage() throws Throwable { |
| 121 | + Exception error = new Exception((String) null); |
| 122 | + when(chain.proceed(any(), any(), any())).thenThrow(error); |
| 123 | + when(operation.isTool()).thenReturn(true); |
| 124 | + |
| 125 | + Object result = executor.invoke(null, transportContext, operation, chain); |
| 126 | + McpSchema.CallToolResult toolResult = (McpSchema.CallToolResult) result; |
| 127 | + assertEquals( |
| 128 | + "Unknown error occurred", ((McpSchema.TextContent) toolResult.content().get(0)).text()); |
| 129 | + } |
| 130 | + |
| 131 | + private void checkMapping(Throwable t, StatusCode joobyCode, int expectedMcpCode) |
| 132 | + throws Throwable { |
| 133 | + reset(chain, router); |
| 134 | + when(chain.proceed(any(), any(), any())).thenThrow(t); |
| 135 | + when(router.errorCode(t)).thenReturn(joobyCode); |
| 136 | + |
| 137 | + McpError ex = |
| 138 | + assertThrows( |
| 139 | + McpError.class, () -> executor.invoke(null, transportContext, operation, chain)); |
| 140 | + assertEquals(expectedMcpCode, ex.getJsonRpcError().code()); |
| 141 | + } |
| 142 | +} |
0 commit comments