Repository navigation
Expand file tree
/
Copy pathResourceTemplateManagementTests.java
More file actions
313 lines (247 loc) · 12 KB
/
Copy pathResourceTemplateManagementTests.java
File metadata and controls
313 lines (247 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* Copyright 2024-2024 the original author or authors.
*/
package io.modelcontextprotocol.server;
import java.time.Duration;
import java.util.List;
import io.modelcontextprotocol.MockMcpServerTransport;
import io.modelcontextprotocol.MockMcpServerTransportProvider;
import io.modelcontextprotocol.spec.McpSchema;
import io.modelcontextprotocol.spec.McpSchema.JSONRPCNotification;
import io.modelcontextprotocol.spec.McpSchema.ReadResourceResult;
import io.modelcontextprotocol.spec.McpSchema.ResourceTemplate;
import io.modelcontextprotocol.spec.McpSchema.ServerCapabilities;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
/**
* Test suite for Resource Template Management functionality. Tests the new
* addResourceTemplate() and removeResourceTemplate() methods, as well as the Map-based
* resource template storage.
*
* @author Christian Tzolov
*/
public class ResourceTemplateManagementTests {
private static final String TEST_TEMPLATE_URI = "test://resource/{param}";
private static final String TEST_TEMPLATE_NAME = "test-template";
private MockMcpServerTransportProvider mockTransportProvider;
private McpAsyncServer mcpAsyncServer;
@BeforeEach
void setUp() {
mockTransportProvider = new MockMcpServerTransportProvider(new MockMcpServerTransport());
}
@AfterEach
void tearDown() {
if (mcpAsyncServer != null) {
assertThatCode(() -> mcpAsyncServer.closeGracefully().block(Duration.ofSeconds(10)))
.doesNotThrowAnyException();
}
}
// ---------------------------------------
// Async Resource Template Tests
// ---------------------------------------
@Test
void testAddResourceTemplate() {
mcpAsyncServer = McpServer.async(mockTransportProvider)
.serverInfo("test-server", "1.0.0")
.capabilities(ServerCapabilities.builder().resources(true, false).build())
.build();
ResourceTemplate template = ResourceTemplate.builder(TEST_TEMPLATE_URI, TEST_TEMPLATE_NAME)
.description("Test resource template")
.mimeType("text/plain")
.build();
McpServerFeatures.AsyncResourceTemplateSpecification specification = new McpServerFeatures.AsyncResourceTemplateSpecification(
template, (exchange, req) -> Mono.just(ReadResourceResult.builder(List.of()).build()));
StepVerifier.create(mcpAsyncServer.addResourceTemplate(specification)).verifyComplete();
}
@Test
void testAddResourceTemplateWithoutCapability() {
// Create a server without resource capabilities
McpAsyncServer serverWithoutResources = McpServer.async(mockTransportProvider)
.serverInfo("test-server", "1.0.0")
.build();
ResourceTemplate template = ResourceTemplate.builder(TEST_TEMPLATE_URI, TEST_TEMPLATE_NAME)
.description("Test resource template")
.mimeType("text/plain")
.build();
McpServerFeatures.AsyncResourceTemplateSpecification specification = new McpServerFeatures.AsyncResourceTemplateSpecification(
template, (exchange, req) -> Mono.just(ReadResourceResult.builder(List.of()).build()));
StepVerifier.create(serverWithoutResources.addResourceTemplate(specification)).verifyErrorSatisfies(error -> {
assertThat(error).isInstanceOf(IllegalStateException.class)
.hasMessageContaining("Server must be configured with resource capabilities");
});
assertThatCode(() -> serverWithoutResources.closeGracefully().block(Duration.ofSeconds(10)))
.doesNotThrowAnyException();
}
@Test
void testRemoveResourceTemplate() {
ResourceTemplate template = ResourceTemplate.builder(TEST_TEMPLATE_URI, TEST_TEMPLATE_NAME)
.description("Test resource template")
.mimeType("text/plain")
.build();
McpServerFeatures.AsyncResourceTemplateSpecification specification = new McpServerFeatures.AsyncResourceTemplateSpecification(
template, (exchange, req) -> Mono.just(ReadResourceResult.builder(List.of()).build()));
mcpAsyncServer = McpServer.async(mockTransportProvider)
.serverInfo("test-server", "1.0.0")
.capabilities(ServerCapabilities.builder().resources(true, false).build())
.resourceTemplates(specification)
.build();
StepVerifier.create(mcpAsyncServer.removeResourceTemplate(TEST_TEMPLATE_URI)).verifyComplete();
}
@Test
void testRemoveResourceTemplateWithoutCapability() {
// Create a server without resource capabilities
McpAsyncServer serverWithoutResources = McpServer.async(mockTransportProvider)
.serverInfo("test-server", "1.0.0")
.build();
StepVerifier.create(serverWithoutResources.removeResourceTemplate(TEST_TEMPLATE_URI))
.verifyErrorSatisfies(error -> {
assertThat(error).isInstanceOf(IllegalStateException.class)
.hasMessageContaining("Server must be configured with resource capabilities");
});
assertThatCode(() -> serverWithoutResources.closeGracefully().block(Duration.ofSeconds(10)))
.doesNotThrowAnyException();
}
@Test
void testRemoveNonexistentResourceTemplate() {
mcpAsyncServer = McpServer.async(mockTransportProvider)
.serverInfo("test-server", "1.0.0")
.capabilities(ServerCapabilities.builder().resources(true, false).build())
.build();
// Removing a non-existent resource template should complete successfully (no
// error)
// as per the new implementation that just logs a warning
StepVerifier.create(mcpAsyncServer.removeResourceTemplate("nonexistent://template/{id}")).verifyComplete();
}
@Test
void testRemoveResourceTemplateNotifiesListChanged() {
ResourceTemplate template = ResourceTemplate.builder(TEST_TEMPLATE_URI, TEST_TEMPLATE_NAME).build();
McpServerFeatures.AsyncResourceTemplateSpecification specification = new McpServerFeatures.AsyncResourceTemplateSpecification(
template, (exchange, req) -> Mono.just(ReadResourceResult.builder(List.of()).build()));
mcpAsyncServer = McpServer.async(mockTransportProvider)
.serverInfo("test-server", "1.0.0")
.capabilities(ServerCapabilities.builder().resources(false, true).build())
.resourceTemplates(specification)
.build();
StepVerifier.create(mcpAsyncServer.removeResourceTemplate(TEST_TEMPLATE_URI)).verifyComplete();
assertThat(mockTransportProvider.getTransport().getAllSentMessages()).singleElement()
.isInstanceOfSatisfying(JSONRPCNotification.class, notification -> assertThat(notification.method())
.isEqualTo(McpSchema.METHOD_NOTIFICATION_RESOURCES_LIST_CHANGED));
}
@Test
void testRemoveNonexistentResourceTemplateDoesNotNotify() {
mcpAsyncServer = McpServer.async(mockTransportProvider)
.serverInfo("test-server", "1.0.0")
.capabilities(ServerCapabilities.builder().resources(false, true).build())
.build();
StepVerifier.create(mcpAsyncServer.removeResourceTemplate("nonexistent://template/{id}")).verifyComplete();
assertThat(mockTransportProvider.getTransport().getAllSentMessages()).isEmpty();
}
@Test
void testReplaceExistingResourceTemplate() {
ResourceTemplate originalTemplate = ResourceTemplate.builder(TEST_TEMPLATE_URI, TEST_TEMPLATE_NAME)
.description("Original template")
.mimeType("text/plain")
.build();
ResourceTemplate updatedTemplate = ResourceTemplate.builder(TEST_TEMPLATE_URI, TEST_TEMPLATE_NAME)
.description("Updated template")
.mimeType("application/json")
.build();
McpServerFeatures.AsyncResourceTemplateSpecification originalSpec = new McpServerFeatures.AsyncResourceTemplateSpecification(
originalTemplate, (exchange, req) -> Mono.just(ReadResourceResult.builder(List.of()).build()));
McpServerFeatures.AsyncResourceTemplateSpecification updatedSpec = new McpServerFeatures.AsyncResourceTemplateSpecification(
updatedTemplate, (exchange, req) -> Mono.just(ReadResourceResult.builder(List.of()).build()));
mcpAsyncServer = McpServer.async(mockTransportProvider)
.serverInfo("test-server", "1.0.0")
.capabilities(ServerCapabilities.builder().resources(true, false).build())
.resourceTemplates(originalSpec)
.build();
// Adding a resource template with the same URI should replace the existing one
StepVerifier.create(mcpAsyncServer.addResourceTemplate(updatedSpec)).verifyComplete();
}
// ---------------------------------------
// Sync Resource Template Tests
// ---------------------------------------
@Test
void testSyncAddResourceTemplate() {
ResourceTemplate template = ResourceTemplate.builder(TEST_TEMPLATE_URI, TEST_TEMPLATE_NAME)
.description("Test resource template")
.mimeType("text/plain")
.build();
McpServerFeatures.SyncResourceTemplateSpecification specification = new McpServerFeatures.SyncResourceTemplateSpecification(
template, (exchange, req) -> ReadResourceResult.builder(List.of()).build());
var mcpSyncServer = McpServer.sync(mockTransportProvider)
.serverInfo("test-server", "1.0.0")
.capabilities(ServerCapabilities.builder().resources(true, false).build())
.build();
assertThatCode(() -> mcpSyncServer.addResourceTemplate(specification)).doesNotThrowAnyException();
assertThatCode(() -> mcpSyncServer.closeGracefully()).doesNotThrowAnyException();
}
@Test
void testSyncRemoveResourceTemplate() {
ResourceTemplate template = ResourceTemplate.builder(TEST_TEMPLATE_URI, TEST_TEMPLATE_NAME)
.description("Test resource template")
.mimeType("text/plain")
.build();
McpServerFeatures.SyncResourceTemplateSpecification specification = new McpServerFeatures.SyncResourceTemplateSpecification(
template, (exchange, req) -> ReadResourceResult.builder(List.of()).build());
var mcpSyncServer = McpServer.sync(mockTransportProvider)
.serverInfo("test-server", "1.0.0")
.capabilities(ServerCapabilities.builder().resources(true, false).build())
.resourceTemplates(specification)
.build();
assertThatCode(() -> mcpSyncServer.removeResourceTemplate(TEST_TEMPLATE_URI)).doesNotThrowAnyException();
assertThatCode(() -> mcpSyncServer.closeGracefully()).doesNotThrowAnyException();
}
// ---------------------------------------
// Map-based Storage Tests
// ---------------------------------------
@Test
void testResourceTemplateMapBasedStorage() {
ResourceTemplate template1 = ResourceTemplate.builder("test://template1/{id}", "template1")
.description("First template")
.mimeType("text/plain")
.build();
ResourceTemplate template2 = ResourceTemplate.builder("test://template2/{id}", "template2")
.description("Second template")
.mimeType("application/json")
.build();
McpServerFeatures.AsyncResourceTemplateSpecification spec1 = new McpServerFeatures.AsyncResourceTemplateSpecification(
template1, (exchange, req) -> Mono.just(ReadResourceResult.builder(List.of()).build()));
McpServerFeatures.AsyncResourceTemplateSpecification spec2 = new McpServerFeatures.AsyncResourceTemplateSpecification(
template2, (exchange, req) -> Mono.just(ReadResourceResult.builder(List.of()).build()));
mcpAsyncServer = McpServer.async(mockTransportProvider)
.serverInfo("test-server", "1.0.0")
.capabilities(ServerCapabilities.builder().resources(true, false).build())
.resourceTemplates(spec1, spec2)
.build();
// Verify both templates are stored (this would be tested through integration
// tests
// or by accessing internal state, but for unit tests we verify no exceptions)
assertThat(mcpAsyncServer).isNotNull();
}
@Test
void testResourceTemplateBuilderWithMap() {
// Test that the new Map-based builder methods work correctly
ResourceTemplate template = ResourceTemplate.builder(TEST_TEMPLATE_URI, TEST_TEMPLATE_NAME)
.description("Test resource template")
.mimeType("text/plain")
.build();
McpServerFeatures.AsyncResourceTemplateSpecification specification = new McpServerFeatures.AsyncResourceTemplateSpecification(
template, (exchange, req) -> Mono.just(ReadResourceResult.builder(List.of()).build()));
// Test varargs builder method
assertThatCode(() -> {
McpServer.async(mockTransportProvider)
.serverInfo("test-server", "1.0.0")
.capabilities(ServerCapabilities.builder().resources(true, false).build())
.resourceTemplates(specification)
.build()
.closeGracefully()
.block(Duration.ofSeconds(10));
}).doesNotThrowAnyException();
}
}