|
4 | 4 |
|
5 | 5 | package io.modelcontextprotocol.client; |
6 | 6 |
|
| 7 | +import static org.assertj.core.api.Assertions.assertThat; |
| 8 | +import static org.assertj.core.api.Assertions.assertThatCode; |
| 9 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 10 | +import static org.assertj.core.api.Assertions.fail; |
| 11 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 12 | + |
7 | 13 | import java.time.Duration; |
| 14 | +import java.util.ArrayList; |
8 | 15 | import java.util.Map; |
9 | 16 | import java.util.Objects; |
10 | 17 | import java.util.concurrent.atomic.AtomicBoolean; |
|
13 | 20 | import java.util.function.Consumer; |
14 | 21 | import java.util.function.Function; |
15 | 22 |
|
| 23 | +import org.junit.jupiter.api.AfterEach; |
| 24 | +import org.junit.jupiter.api.BeforeEach; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.junit.jupiter.params.ParameterizedTest; |
| 27 | +import org.junit.jupiter.params.provider.ValueSource; |
| 28 | + |
16 | 29 | import io.modelcontextprotocol.spec.McpClientTransport; |
17 | 30 | import io.modelcontextprotocol.spec.McpError; |
18 | 31 | import io.modelcontextprotocol.spec.McpSchema; |
| 32 | +import io.modelcontextprotocol.spec.McpSchema.BlobResourceContents; |
19 | 33 | import io.modelcontextprotocol.spec.McpSchema.CallToolRequest; |
20 | 34 | import io.modelcontextprotocol.spec.McpSchema.ClientCapabilities; |
21 | 35 | import io.modelcontextprotocol.spec.McpSchema.CreateMessageRequest; |
|
24 | 38 | import io.modelcontextprotocol.spec.McpSchema.ElicitResult; |
25 | 39 | import io.modelcontextprotocol.spec.McpSchema.GetPromptRequest; |
26 | 40 | import io.modelcontextprotocol.spec.McpSchema.Prompt; |
| 41 | +import io.modelcontextprotocol.spec.McpSchema.ReadResourceResult; |
27 | 42 | import io.modelcontextprotocol.spec.McpSchema.Resource; |
| 43 | +import io.modelcontextprotocol.spec.McpSchema.ResourceContents; |
28 | 44 | import io.modelcontextprotocol.spec.McpSchema.Root; |
29 | 45 | import io.modelcontextprotocol.spec.McpSchema.SubscribeRequest; |
| 46 | +import io.modelcontextprotocol.spec.McpSchema.TextResourceContents; |
30 | 47 | import io.modelcontextprotocol.spec.McpSchema.Tool; |
31 | 48 | import io.modelcontextprotocol.spec.McpSchema.UnsubscribeRequest; |
32 | 49 | import io.modelcontextprotocol.spec.McpTransport; |
33 | | -import org.junit.jupiter.api.AfterEach; |
34 | | -import org.junit.jupiter.api.BeforeEach; |
35 | | -import org.junit.jupiter.api.Disabled; |
36 | | -import org.junit.jupiter.api.Test; |
37 | | -import org.junit.jupiter.params.ParameterizedTest; |
38 | | -import org.junit.jupiter.params.provider.ValueSource; |
39 | 50 | import reactor.core.publisher.Flux; |
40 | 51 | import reactor.core.publisher.Mono; |
41 | 52 | import reactor.test.StepVerifier; |
42 | 53 |
|
43 | | -import static org.assertj.core.api.Assertions.assertThat; |
44 | | -import static org.assertj.core.api.Assertions.assertThatCode; |
45 | | -import static org.assertj.core.api.Assertions.assertThatThrownBy; |
46 | | -import static org.assertj.core.api.Assertions.fail; |
47 | | -import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
48 | | - |
49 | 54 | /** |
50 | 55 | * Test suite for the {@link McpAsyncClient} that can be used with different |
51 | 56 | * {@link McpTransport} implementations. |
@@ -406,18 +411,59 @@ void testRemoveNonExistentRoot() { |
406 | 411 | } |
407 | 412 |
|
408 | 413 | @Test |
409 | | - @Disabled |
410 | 414 | void testReadResource() { |
411 | | - withClient(createMcpTransport(), mcpAsyncClient -> { |
412 | | - StepVerifier.create(mcpAsyncClient.listResources()).consumeNextWith(resources -> { |
413 | | - if (!resources.resources().isEmpty()) { |
414 | | - Resource firstResource = resources.resources().get(0); |
415 | | - StepVerifier.create(mcpAsyncClient.readResource(firstResource)).consumeNextWith(result -> { |
416 | | - assertThat(result).isNotNull(); |
417 | | - assertThat(result.contents()).isNotNull(); |
418 | | - }).verifyComplete(); |
| 415 | + withClient(createMcpTransport(), client -> { |
| 416 | + Flux<McpSchema.ReadResourceResult> resources = client.initialize() |
| 417 | + .then(client.listResources(null)) |
| 418 | + .flatMapMany(r -> Flux.fromIterable(r.resources())) |
| 419 | + .flatMap(r -> client.readResource(r)); |
| 420 | + |
| 421 | + StepVerifier.create(resources).recordWith(ArrayList::new).consumeRecordedWith(readResourceResults -> { |
| 422 | + |
| 423 | + for (ReadResourceResult result : readResourceResults) { |
| 424 | + |
| 425 | + assertThat(result).isNotNull(); |
| 426 | + assertThat(result.contents()).isNotNull().isNotEmpty(); |
| 427 | + |
| 428 | + // Validate each content item |
| 429 | + for (ResourceContents content : result.contents()) { |
| 430 | + assertThat(content).isNotNull(); |
| 431 | + assertThat(content.uri()).isNotNull().isNotEmpty(); |
| 432 | + assertThat(content.mimeType()).isNotNull().isNotEmpty(); |
| 433 | + |
| 434 | + // Validate content based on its type with more comprehensive |
| 435 | + // checks |
| 436 | + switch (content.mimeType()) { |
| 437 | + case "text/plain" -> { |
| 438 | + TextResourceContents textContent = assertInstanceOf(TextResourceContents.class, |
| 439 | + content); |
| 440 | + assertThat(textContent.text()).isNotNull().isNotEmpty(); |
| 441 | + assertThat(textContent.uri()).isNotEmpty(); |
| 442 | + } |
| 443 | + case "application/octet-stream" -> { |
| 444 | + BlobResourceContents blobContent = assertInstanceOf(BlobResourceContents.class, |
| 445 | + content); |
| 446 | + assertThat(blobContent.blob()).isNotNull().isNotEmpty(); |
| 447 | + assertThat(blobContent.uri()).isNotNull().isNotEmpty(); |
| 448 | + // Validate base64 encoding format |
| 449 | + assertThat(blobContent.blob()).matches("^[A-Za-z0-9+/]*={0,2}$"); |
| 450 | + } |
| 451 | + default -> { |
| 452 | + |
| 453 | + // Still validate basic properties |
| 454 | + if (content instanceof TextResourceContents textContent) { |
| 455 | + assertThat(textContent.text()).isNotNull(); |
| 456 | + } |
| 457 | + else if (content instanceof BlobResourceContents blobContent) { |
| 458 | + assertThat(blobContent.blob()).isNotNull(); |
| 459 | + } |
| 460 | + } |
| 461 | + } |
| 462 | + } |
419 | 463 | } |
420 | | - }).verifyComplete(); |
| 464 | + }) |
| 465 | + .expectNextCount(10) // Expect 10 elements |
| 466 | + .verifyComplete(); |
421 | 467 | }); |
422 | 468 | } |
423 | 469 |
|
|
0 commit comments