diff --git a/mcp-test/src/main/java/io/modelcontextprotocol/client/AbstractMcpAsyncClientTests.java b/mcp-test/src/main/java/io/modelcontextprotocol/client/AbstractMcpAsyncClientTests.java index 18ec06c6..02aa23d8 100644 --- a/mcp-test/src/main/java/io/modelcontextprotocol/client/AbstractMcpAsyncClientTests.java +++ b/mcp-test/src/main/java/io/modelcontextprotocol/client/AbstractMcpAsyncClientTests.java @@ -109,6 +109,17 @@ void tearDown() { onClose(); } + void verifyInitializationTimeout(Function> operation, String action) { + withClient(createMcpTransport(), mcpAsyncClient -> { + StepVerifier.withVirtualTime(() -> operation.apply(mcpAsyncClient)) + .expectSubscription() + .thenAwait(getInitializationTimeout()) + .consumeErrorWith(e -> assertThat(e).isInstanceOf(McpError.class) + .hasMessage("Client must be initialized before " + action)) + .verify(); + }); + } + @Test void testConstructorWithInvalidArguments() { assertThatThrownBy(() -> McpClient.async(null).build()).isInstanceOf(IllegalArgumentException.class) @@ -121,14 +132,7 @@ void testConstructorWithInvalidArguments() { @Test void testListToolsWithoutInitialization() { - withClient(createMcpTransport(), mcpAsyncClient -> { - StepVerifier.withVirtualTime(() -> mcpAsyncClient.listTools(null)) - .expectSubscription() - .thenAwait(getInitializationTimeout()) - .consumeErrorWith(e -> assertThat(e).isInstanceOf(McpError.class) - .hasMessage("Client must be initialized before listing tools")) - .verify(); - }); + verifyInitializationTimeout(client -> client.listTools(null), "listing tools"); } @Test @@ -148,14 +152,7 @@ void testListTools() { @Test void testPingWithoutInitialization() { - withClient(createMcpTransport(), mcpAsyncClient -> { - StepVerifier.withVirtualTime(() -> mcpAsyncClient.ping()) - .expectSubscription() - .thenAwait(getInitializationTimeout()) - .consumeErrorWith(e -> assertThat(e).isInstanceOf(McpError.class) - .hasMessage("Client must be initialized before pinging the " + "server")) - .verify(); - }); + verifyInitializationTimeout(client -> client.ping(), "pinging the server"); } @Test @@ -169,16 +166,8 @@ void testPing() { @Test void testCallToolWithoutInitialization() { - withClient(createMcpTransport(), mcpAsyncClient -> { - CallToolRequest callToolRequest = new CallToolRequest("echo", Map.of("message", ECHO_TEST_MESSAGE)); - - StepVerifier.withVirtualTime(() -> mcpAsyncClient.callTool(callToolRequest)) - .expectSubscription() - .thenAwait(getInitializationTimeout()) - .consumeErrorWith(e -> assertThat(e).isInstanceOf(McpError.class) - .hasMessage("Client must be initialized before calling tools")) - .verify(); - }); + CallToolRequest callToolRequest = new CallToolRequest("echo", Map.of("message", ECHO_TEST_MESSAGE)); + verifyInitializationTimeout(client -> client.callTool(callToolRequest), "calling tools"); } @Test @@ -212,14 +201,7 @@ void testCallToolWithInvalidTool() { @Test void testListResourcesWithoutInitialization() { - withClient(createMcpTransport(), mcpAsyncClient -> { - StepVerifier.withVirtualTime(() -> mcpAsyncClient.listResources(null)) - .expectSubscription() - .thenAwait(getInitializationTimeout()) - .consumeErrorWith(e -> assertThat(e).isInstanceOf(McpError.class) - .hasMessage("Client must be initialized before listing resources")) - .verify(); - }); + verifyInitializationTimeout(client -> client.listResources(null), "listing resources"); } @Test @@ -250,14 +232,7 @@ void testMcpAsyncClientState() { @Test void testListPromptsWithoutInitialization() { - withClient(createMcpTransport(), mcpAsyncClient -> { - StepVerifier.withVirtualTime(() -> mcpAsyncClient.listPrompts(null)) - .expectSubscription() - .thenAwait(getInitializationTimeout()) - .consumeErrorWith(e -> assertThat(e).isInstanceOf(McpError.class) - .hasMessage("Client must be initialized before listing prompts")) - .verify(); - }); + verifyInitializationTimeout(client -> client.listPrompts(null), "listing " + "prompts"); } @Test @@ -281,16 +256,8 @@ void testListPrompts() { @Test void testGetPromptWithoutInitialization() { - withClient(createMcpTransport(), mcpAsyncClient -> { - GetPromptRequest request = new GetPromptRequest("simple_prompt", Map.of()); - - StepVerifier.withVirtualTime(() -> mcpAsyncClient.getPrompt(request)) - .expectSubscription() - .thenAwait(getInitializationTimeout()) - .consumeErrorWith(e -> assertThat(e).isInstanceOf(McpError.class) - .hasMessage("Client must be initialized before getting prompts")) - .verify(); - }); + GetPromptRequest request = new GetPromptRequest("simple_prompt", Map.of()); + verifyInitializationTimeout(client -> client.getPrompt(request), "getting " + "prompts"); } @Test @@ -311,14 +278,8 @@ void testGetPrompt() { @Test void testRootsListChangedWithoutInitialization() { - withClient(createMcpTransport(), mcpAsyncClient -> { - StepVerifier.withVirtualTime(() -> mcpAsyncClient.rootsListChangedNotification()) - .expectSubscription() - .thenAwait(getInitializationTimeout()) - .consumeErrorWith(e -> assertThat(e).isInstanceOf(McpError.class) - .hasMessage("Client must be initialized before sending roots list changed notification")) - .verify(); - }); + verifyInitializationTimeout(client -> client.rootsListChangedNotification(), + "sending roots list changed notification"); } @Test @@ -392,14 +353,7 @@ void testReadResource() { @Test void testListResourceTemplatesWithoutInitialization() { - withClient(createMcpTransport(), mcpAsyncClient -> { - StepVerifier.withVirtualTime(() -> mcpAsyncClient.listResourceTemplates()) - .expectSubscription() - .thenAwait(getInitializationTimeout()) - .consumeErrorWith(e -> assertThat(e).isInstanceOf(McpError.class) - .hasMessage("Client must be initialized before listing resource templates")) - .verify(); - }); + verifyInitializationTimeout(client -> client.listResourceTemplates(), "listing resource templates"); } @Test @@ -492,14 +446,8 @@ void testInitializeWithAllCapabilities() { @Test void testLoggingLevelsWithoutInitialization() { - withClient(createMcpTransport(), - mcpAsyncClient -> StepVerifier - .withVirtualTime(() -> mcpAsyncClient.setLoggingLevel(McpSchema.LoggingLevel.DEBUG)) - .expectSubscription() - .thenAwait(getInitializationTimeout()) - .consumeErrorWith(e -> assertThat(e).isInstanceOf(McpError.class) - .hasMessage("Client must be initialized before setting logging level")) - .verify()); + verifyInitializationTimeout(client -> client.setLoggingLevel(McpSchema.LoggingLevel.DEBUG), + "setting logging level"); } @Test diff --git a/mcp/src/test/java/io/modelcontextprotocol/client/AbstractMcpAsyncClientTests.java b/mcp/src/test/java/io/modelcontextprotocol/client/AbstractMcpAsyncClientTests.java index 06a231ed..f7a0a492 100644 --- a/mcp/src/test/java/io/modelcontextprotocol/client/AbstractMcpAsyncClientTests.java +++ b/mcp/src/test/java/io/modelcontextprotocol/client/AbstractMcpAsyncClientTests.java @@ -11,7 +11,6 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; import java.util.function.Function; -import java.util.function.Supplier; import io.modelcontextprotocol.spec.ClientMcpTransport; import io.modelcontextprotocol.spec.McpError;