10
10
import java .util .Map ;
11
11
12
12
import com .fasterxml .jackson .core .type .TypeReference ;
13
+ import io .modelcontextprotocol .spec .DefaultMcpTransportContext ;
13
14
import io .modelcontextprotocol .spec .McpError ;
14
15
import io .modelcontextprotocol .spec .McpSchema ;
15
16
import io .modelcontextprotocol .spec .McpServerSession ;
@@ -54,7 +55,8 @@ void setUp() {
54
55
55
56
clientInfo = new McpSchema .Implementation ("test-client" , "1.0.0" );
56
57
57
- exchange = new McpAsyncServerExchange (mockSession , clientCapabilities , clientInfo );
58
+ exchange = new McpAsyncServerExchange ("testSessionId" , mockSession , clientCapabilities , clientInfo ,
59
+ new DefaultMcpTransportContext ());
58
60
}
59
61
60
62
@ Test
@@ -219,132 +221,72 @@ void testLoggingNotificationWithNullMessage() {
219
221
}
220
222
221
223
@ Test
222
- void testLoggingNotificationWithAllowedLevel () {
224
+ void testSetMinLoggingLevelWithNullValue () {
225
+ assertThatThrownBy (() -> exchange .setMinLoggingLevel (null )).isInstanceOf (IllegalArgumentException .class )
226
+ .hasMessage ("minLoggingLevel must not be null" );
227
+ }
223
228
229
+ @ Test
230
+ void testLoggingNotificationWithAllowedLevel () {
224
231
McpSchema .LoggingMessageNotification notification = McpSchema .LoggingMessageNotification .builder ()
225
232
.level (McpSchema .LoggingLevel .ERROR )
226
233
.logger ("test-logger" )
227
234
.data ("Test error message" )
228
235
.build ();
229
236
237
+ when (mockSession .isNotificationForLevelAllowed (any ())).thenReturn (Boolean .TRUE );
230
238
when (mockSession .sendNotification (eq (McpSchema .METHOD_NOTIFICATION_MESSAGE ), eq (notification )))
231
239
.thenReturn (Mono .empty ());
232
240
233
241
StepVerifier .create (exchange .loggingNotification (notification )).verifyComplete ();
234
242
235
- // Verify that sendNotification was called exactly once
243
+ verify ( mockSession , times ( 1 )). isNotificationForLevelAllowed ( eq ( McpSchema . LoggingLevel . ERROR ));
236
244
verify (mockSession , times (1 )).sendNotification (eq (McpSchema .METHOD_NOTIFICATION_MESSAGE ), eq (notification ));
237
245
}
238
246
239
247
@ Test
240
248
void testLoggingNotificationWithFilteredLevel () {
241
- // Given - Set minimum level to WARNING, send DEBUG message
242
- exchange .setMinLoggingLevel (McpSchema .LoggingLevel .WARNING );
249
+ exchange . setMinLoggingLevel ( McpSchema . LoggingLevel . DEBUG );
250
+ verify ( mockSession , times ( 1 )) .setMinLoggingLevel (eq ( McpSchema .LoggingLevel .DEBUG ) );
243
251
244
252
McpSchema .LoggingMessageNotification debugNotification = McpSchema .LoggingMessageNotification .builder ()
245
253
.level (McpSchema .LoggingLevel .DEBUG )
246
254
.logger ("test-logger" )
247
255
.data ("Debug message that should be filtered" )
248
256
.build ();
249
257
250
- // When & Then - Should complete without sending notification
251
- StepVerifier .create (exchange .loggingNotification (debugNotification )).verifyComplete ();
252
-
253
- // Verify that sendNotification was never called for filtered DEBUG level
254
- verify (mockSession , never ()).sendNotification (eq (McpSchema .METHOD_NOTIFICATION_MESSAGE ), eq (debugNotification ));
255
- }
256
-
257
- @ Test
258
- void testLoggingNotificationLevelFiltering () {
259
- // Given - Set minimum level to WARNING
260
- exchange .setMinLoggingLevel (McpSchema .LoggingLevel .WARNING );
261
-
262
- // Test DEBUG (should be filtered)
263
- McpSchema .LoggingMessageNotification debugNotification = McpSchema .LoggingMessageNotification .builder ()
264
- .level (McpSchema .LoggingLevel .DEBUG )
265
- .logger ("test-logger" )
266
- .data ("Debug message" )
267
- .build ();
258
+ when (mockSession .isNotificationForLevelAllowed (eq (McpSchema .LoggingLevel .DEBUG ))).thenReturn (Boolean .TRUE );
259
+ when (mockSession .sendNotification (eq (McpSchema .METHOD_NOTIFICATION_MESSAGE ), eq (debugNotification )))
260
+ .thenReturn (Mono .empty ());
268
261
269
262
StepVerifier .create (exchange .loggingNotification (debugNotification )).verifyComplete ();
270
263
271
- // Verify that sendNotification was never called for DEBUG level
272
- verify (mockSession , never ()).sendNotification (eq (McpSchema .METHOD_NOTIFICATION_MESSAGE ), eq (debugNotification ));
273
-
274
- // Test INFO (should be filtered)
275
- McpSchema .LoggingMessageNotification infoNotification = McpSchema .LoggingMessageNotification .builder ()
276
- .level (McpSchema .LoggingLevel .INFO )
277
- .logger ("test-logger" )
278
- .data ("Info message" )
279
- .build ();
280
-
281
- StepVerifier .create (exchange .loggingNotification (infoNotification )).verifyComplete ();
282
-
283
- // Verify that sendNotification was never called for INFO level
284
- verify (mockSession , never ()).sendNotification (eq (McpSchema .METHOD_NOTIFICATION_MESSAGE ), eq (infoNotification ));
285
-
286
- reset (mockSession );
264
+ verify (mockSession , times (1 )).isNotificationForLevelAllowed (eq (McpSchema .LoggingLevel .DEBUG ));
265
+ verify (mockSession , times (1 )).sendNotification (eq (McpSchema .METHOD_NOTIFICATION_MESSAGE ),
266
+ eq (debugNotification ));
287
267
288
- // Test WARNING (should be sent)
289
268
McpSchema .LoggingMessageNotification warningNotification = McpSchema .LoggingMessageNotification .builder ()
290
269
.level (McpSchema .LoggingLevel .WARNING )
291
270
.logger ("test-logger" )
292
- .data ("Warning message" )
271
+ .data ("Debug message that should be filtered " )
293
272
.build ();
294
273
295
- when (mockSession .sendNotification (eq (McpSchema .METHOD_NOTIFICATION_MESSAGE ), eq (warningNotification )))
296
- .thenReturn (Mono .empty ());
297
-
298
274
StepVerifier .create (exchange .loggingNotification (warningNotification )).verifyComplete ();
299
275
300
- // Verify that sendNotification was called exactly once for WARNING level
301
- verify (mockSession , times ( 1 )).sendNotification (eq (McpSchema .METHOD_NOTIFICATION_MESSAGE ),
276
+ verify ( mockSession , times ( 1 )). isNotificationForLevelAllowed ( eq ( McpSchema . LoggingLevel . WARNING ));
277
+ verify (mockSession , never ( )).sendNotification (eq (McpSchema .METHOD_NOTIFICATION_MESSAGE ),
302
278
eq (warningNotification ));
303
-
304
- // Test ERROR (should be sent)
305
- McpSchema .LoggingMessageNotification errorNotification = McpSchema .LoggingMessageNotification .builder ()
306
- .level (McpSchema .LoggingLevel .ERROR )
307
- .logger ("test-logger" )
308
- .data ("Error message" )
309
- .build ();
310
-
311
- when (mockSession .sendNotification (eq (McpSchema .METHOD_NOTIFICATION_MESSAGE ), eq (errorNotification )))
312
- .thenReturn (Mono .empty ());
313
-
314
- StepVerifier .create (exchange .loggingNotification (errorNotification )).verifyComplete ();
315
-
316
- // Verify that sendNotification was called exactly once for ERROR level
317
- verify (mockSession , times (1 )).sendNotification (eq (McpSchema .METHOD_NOTIFICATION_MESSAGE ),
318
- eq (errorNotification ));
319
- }
320
-
321
- @ Test
322
- void testLoggingNotificationWithDefaultLevel () {
323
-
324
- McpSchema .LoggingMessageNotification infoNotification = McpSchema .LoggingMessageNotification .builder ()
325
- .level (McpSchema .LoggingLevel .INFO )
326
- .logger ("test-logger" )
327
- .data ("Info message" )
328
- .build ();
329
-
330
- when (mockSession .sendNotification (eq (McpSchema .METHOD_NOTIFICATION_MESSAGE ), eq (infoNotification )))
331
- .thenReturn (Mono .empty ());
332
-
333
- StepVerifier .create (exchange .loggingNotification (infoNotification )).verifyComplete ();
334
-
335
- // Verify that sendNotification was called exactly once for default level
336
- verify (mockSession , times (1 )).sendNotification (eq (McpSchema .METHOD_NOTIFICATION_MESSAGE ), eq (infoNotification ));
337
279
}
338
280
339
281
@ Test
340
282
void testLoggingNotificationWithSessionError () {
341
-
342
283
McpSchema .LoggingMessageNotification notification = McpSchema .LoggingMessageNotification .builder ()
343
284
.level (McpSchema .LoggingLevel .ERROR )
344
285
.logger ("test-logger" )
345
286
.data ("Test error message" )
346
287
.build ();
347
288
289
+ when (mockSession .isNotificationForLevelAllowed (any ())).thenReturn (Boolean .TRUE );
348
290
when (mockSession .sendNotification (eq (McpSchema .METHOD_NOTIFICATION_MESSAGE ), eq (notification )))
349
291
.thenReturn (Mono .error (new RuntimeException ("Session error" )));
350
292
@@ -353,44 +295,6 @@ void testLoggingNotificationWithSessionError() {
353
295
});
354
296
}
355
297
356
- @ Test
357
- void testSetMinLoggingLevelWithNullValue () {
358
- // When & Then
359
- assertThatThrownBy (() -> exchange .setMinLoggingLevel (null )).isInstanceOf (IllegalArgumentException .class )
360
- .hasMessage ("minLoggingLevel must not be null" );
361
- }
362
-
363
- @ Test
364
- void testLoggingLevelHierarchy () {
365
- // Test all logging levels to ensure proper hierarchy
366
- McpSchema .LoggingLevel [] levels = { McpSchema .LoggingLevel .DEBUG , McpSchema .LoggingLevel .INFO ,
367
- McpSchema .LoggingLevel .NOTICE , McpSchema .LoggingLevel .WARNING , McpSchema .LoggingLevel .ERROR ,
368
- McpSchema .LoggingLevel .CRITICAL , McpSchema .LoggingLevel .ALERT , McpSchema .LoggingLevel .EMERGENCY };
369
-
370
- // Set minimum level to WARNING
371
- exchange .setMinLoggingLevel (McpSchema .LoggingLevel .WARNING );
372
-
373
- for (McpSchema .LoggingLevel level : levels ) {
374
- McpSchema .LoggingMessageNotification notification = McpSchema .LoggingMessageNotification .builder ()
375
- .level (level )
376
- .logger ("test-logger" )
377
- .data ("Test message for " + level )
378
- .build ();
379
-
380
- if (level .level () >= McpSchema .LoggingLevel .WARNING .level ()) {
381
- // Should be sent
382
- when (mockSession .sendNotification (eq (McpSchema .METHOD_NOTIFICATION_MESSAGE ), eq (notification )))
383
- .thenReturn (Mono .empty ());
384
-
385
- StepVerifier .create (exchange .loggingNotification (notification )).verifyComplete ();
386
- }
387
- else {
388
- // Should be filtered (completes without sending)
389
- StepVerifier .create (exchange .loggingNotification (notification )).verifyComplete ();
390
- }
391
- }
392
- }
393
-
394
298
// ---------------------------------------
395
299
// Create Elicitation Tests
396
300
// ---------------------------------------
0 commit comments