|
17 | 17 | import io.modelcontextprotocol.spec.McpServerSession; |
18 | 18 | import org.junit.jupiter.api.BeforeEach; |
19 | 19 | import org.junit.jupiter.api.Test; |
| 20 | +import org.junit.jupiter.params.ParameterizedTest; |
| 21 | +import org.junit.jupiter.params.provider.ValueSource; |
20 | 22 | import org.mockito.Mock; |
21 | 23 | import org.mockito.MockitoAnnotations; |
22 | 24 | import reactor.core.publisher.Mono; |
|
29 | 31 | import static org.mockito.Mockito.never; |
30 | 32 | import static org.mockito.Mockito.times; |
31 | 33 | import static org.mockito.Mockito.verify; |
| 34 | +import static org.mockito.Mockito.verifyNoInteractions; |
32 | 35 | import static org.mockito.Mockito.when; |
33 | 36 |
|
34 | 37 | /** |
@@ -85,6 +88,54 @@ void testListRootsWithSinglePage() { |
85 | 88 | }).verifyComplete(); |
86 | 89 | } |
87 | 90 |
|
| 91 | + @ParameterizedTest |
| 92 | + @ValueSource(booleans = { false, true }) |
| 93 | + void testListRootsWithoutCapabilities(boolean paginated) { |
| 94 | + exchange = new McpAsyncServerExchange("testSessionId", mockSession, null, clientInfo, |
| 95 | + McpTransportContext.EMPTY); |
| 96 | + when(mockSession.sendRequest(eq(McpSchema.METHOD_ROOTS_LIST), any(McpSchema.PaginatedRequest.class), |
| 97 | + any(TypeRef.class))) |
| 98 | + .thenReturn(Mono.just(McpSchema.ListRootsResult.builder(List.of()).build())); |
| 99 | + |
| 100 | + Mono<McpSchema.ListRootsResult> result = paginated ? exchange.listRoots("cursor") : exchange.listRoots(); |
| 101 | + verifyNoInteractions(mockSession); |
| 102 | + StepVerifier.create(result) |
| 103 | + .verifyErrorSatisfies(error -> assertThat(error).isInstanceOf(IllegalStateException.class) |
| 104 | + .hasMessage("Client must be initialized. Call the initialize method first!")); |
| 105 | + verifyNoInteractions(mockSession); |
| 106 | + } |
| 107 | + |
| 108 | + @ParameterizedTest |
| 109 | + @ValueSource(booleans = { false, true }) |
| 110 | + void testListRootsWithoutRootsCapability(boolean paginated) { |
| 111 | + exchange = new McpAsyncServerExchange("testSessionId", mockSession, |
| 112 | + McpSchema.ClientCapabilities.builder().build(), clientInfo, McpTransportContext.EMPTY); |
| 113 | + when(mockSession.sendRequest(eq(McpSchema.METHOD_ROOTS_LIST), any(McpSchema.PaginatedRequest.class), |
| 114 | + any(TypeRef.class))) |
| 115 | + .thenReturn(Mono.just(McpSchema.ListRootsResult.builder(List.of()).build())); |
| 116 | + |
| 117 | + Mono<McpSchema.ListRootsResult> result = paginated ? exchange.listRoots("cursor") : exchange.listRoots(); |
| 118 | + verifyNoInteractions(mockSession); |
| 119 | + StepVerifier.create(result) |
| 120 | + .verifyErrorSatisfies(error -> assertThat(error).isInstanceOf(IllegalStateException.class) |
| 121 | + .hasMessage("Client must be configured with root listing capabilities")); |
| 122 | + verifyNoInteractions(mockSession); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + void testListRootsWithoutListChangedSupport() { |
| 127 | + exchange = new McpAsyncServerExchange("testSessionId", mockSession, |
| 128 | + McpSchema.ClientCapabilities.builder().roots(false).build(), clientInfo, McpTransportContext.EMPTY); |
| 129 | + McpSchema.ListRootsResult result = McpSchema.ListRootsResult.builder(List.of()).build(); |
| 130 | + when(mockSession.sendRequest(eq(McpSchema.METHOD_ROOTS_LIST), eq(new McpSchema.PaginatedRequest("cursor")), |
| 131 | + any(TypeRef.class))) |
| 132 | + .thenReturn(Mono.just(result)); |
| 133 | + |
| 134 | + StepVerifier.create(exchange.listRoots("cursor")).expectNext(result).verifyComplete(); |
| 135 | + verify(mockSession).sendRequest(eq(McpSchema.METHOD_ROOTS_LIST), eq(new McpSchema.PaginatedRequest("cursor")), |
| 136 | + any(TypeRef.class)); |
| 137 | + } |
| 138 | + |
88 | 139 | @Test |
89 | 140 | void testListRootsWithMultiplePages() { |
90 | 141 |
|
|
0 commit comments