|
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.
|
@@ -457,18 +462,59 @@ void testRemoveNonExistentRoot() {
|
457 | 462 | }
|
458 | 463 |
|
459 | 464 | @Test
|
460 |
| - @Disabled |
461 | 465 | void testReadResource() {
|
462 |
| - withClient(createMcpTransport(), mcpAsyncClient -> { |
463 |
| - StepVerifier.create(mcpAsyncClient.listResources()).consumeNextWith(resources -> { |
464 |
| - if (!resources.resources().isEmpty()) { |
465 |
| - Resource firstResource = resources.resources().get(0); |
466 |
| - StepVerifier.create(mcpAsyncClient.readResource(firstResource)).consumeNextWith(result -> { |
467 |
| - assertThat(result).isNotNull(); |
468 |
| - assertThat(result.contents()).isNotNull(); |
469 |
| - }).verifyComplete(); |
| 466 | + withClient(createMcpTransport(), client -> { |
| 467 | + Flux<McpSchema.ReadResourceResult> resources = client.initialize() |
| 468 | + .then(client.listResources(null)) |
| 469 | + .flatMapMany(r -> Flux.fromIterable(r.resources())) |
| 470 | + .flatMap(r -> client.readResource(r)); |
| 471 | + |
| 472 | + StepVerifier.create(resources).recordWith(ArrayList::new).consumeRecordedWith(readResourceResults -> { |
| 473 | + |
| 474 | + for (ReadResourceResult result : readResourceResults) { |
| 475 | + |
| 476 | + assertThat(result).isNotNull(); |
| 477 | + assertThat(result.contents()).isNotNull().isNotEmpty(); |
| 478 | + |
| 479 | + // Validate each content item |
| 480 | + for (ResourceContents content : result.contents()) { |
| 481 | + assertThat(content).isNotNull(); |
| 482 | + assertThat(content.uri()).isNotNull().isNotEmpty(); |
| 483 | + assertThat(content.mimeType()).isNotNull().isNotEmpty(); |
| 484 | + |
| 485 | + // Validate content based on its type with more comprehensive |
| 486 | + // checks |
| 487 | + switch (content.mimeType()) { |
| 488 | + case "text/plain" -> { |
| 489 | + TextResourceContents textContent = assertInstanceOf(TextResourceContents.class, |
| 490 | + content); |
| 491 | + assertThat(textContent.text()).isNotNull().isNotEmpty(); |
| 492 | + assertThat(textContent.uri()).isNotEmpty(); |
| 493 | + } |
| 494 | + case "application/octet-stream" -> { |
| 495 | + BlobResourceContents blobContent = assertInstanceOf(BlobResourceContents.class, |
| 496 | + content); |
| 497 | + assertThat(blobContent.blob()).isNotNull().isNotEmpty(); |
| 498 | + assertThat(blobContent.uri()).isNotNull().isNotEmpty(); |
| 499 | + // Validate base64 encoding format |
| 500 | + assertThat(blobContent.blob()).matches("^[A-Za-z0-9+/]*={0,2}$"); |
| 501 | + } |
| 502 | + default -> { |
| 503 | + |
| 504 | + // Still validate basic properties |
| 505 | + if (content instanceof TextResourceContents textContent) { |
| 506 | + assertThat(textContent.text()).isNotNull(); |
| 507 | + } |
| 508 | + else if (content instanceof BlobResourceContents blobContent) { |
| 509 | + assertThat(blobContent.blob()).isNotNull(); |
| 510 | + } |
| 511 | + } |
| 512 | + } |
| 513 | + } |
470 | 514 | }
|
471 |
| - }).verifyComplete(); |
| 515 | + }) |
| 516 | + .expectNextCount(10) // Expect 10 elements |
| 517 | + .verifyComplete(); |
472 | 518 | });
|
473 | 519 | }
|
474 | 520 |
|
|
0 commit comments