|
| 1 | +/* |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby.internal.mcp.transport; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 10 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 11 | +import static org.mockito.Mockito.mock; |
| 12 | +import static org.mockito.Mockito.never; |
| 13 | +import static org.mockito.Mockito.verify; |
| 14 | +import static org.mockito.Mockito.when; |
| 15 | + |
| 16 | +import java.lang.reflect.Field; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.BeforeEach; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 21 | +import org.mockito.Mock; |
| 22 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 23 | +import org.slf4j.Logger; |
| 24 | + |
| 25 | +import io.jooby.Context; |
| 26 | +import io.modelcontextprotocol.json.McpJsonMapper; |
| 27 | +import io.modelcontextprotocol.server.McpTransportContextExtractor; |
| 28 | +import io.modelcontextprotocol.spec.McpServerSession; |
| 29 | +import reactor.core.publisher.Mono; |
| 30 | + |
| 31 | +@ExtendWith(MockitoExtension.class) |
| 32 | +class AbstractMcpTransportProviderTest { |
| 33 | + |
| 34 | + @Mock McpJsonMapper jsonMapper; |
| 35 | + @Mock McpTransportContextExtractor<Context> contextExtractor; |
| 36 | + @Mock McpServerSession session1; |
| 37 | + @Mock McpServerSession session2; |
| 38 | + @Mock Logger mockLogger; |
| 39 | + |
| 40 | + private AbstractMcpTransportProvider provider; |
| 41 | + |
| 42 | + @BeforeEach |
| 43 | + void setup() throws Exception { |
| 44 | + // Create a concrete instance of the abstract class for testing |
| 45 | + provider = |
| 46 | + new AbstractMcpTransportProvider(jsonMapper, contextExtractor) { |
| 47 | + @Override |
| 48 | + protected String transportName() { |
| 49 | + return "test-transport"; |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + // Inject the mock SLF4J Logger using reflection to verify logging branches |
| 54 | + Field logField = AbstractMcpTransportProvider.class.getDeclaredField("log"); |
| 55 | + logField.setAccessible(true); |
| 56 | + logField.set(provider, mockLogger); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void testSetSessionFactory() throws Exception { |
| 61 | + McpServerSession.Factory factory = mock(McpServerSession.Factory.class); |
| 62 | + provider.setSessionFactory(factory); |
| 63 | + |
| 64 | + Field factoryField = AbstractMcpTransportProvider.class.getDeclaredField("sessionFactory"); |
| 65 | + factoryField.setAccessible(true); |
| 66 | + assertEquals(factory, factoryField.get(provider)); |
| 67 | + } |
| 68 | + |
| 69 | + // --- NOTIFY CLIENTS TESTS --- |
| 70 | + |
| 71 | + @Test |
| 72 | + void testNotifyClients_EmptySessions() { |
| 73 | + provider.notifyClients("method", "params").block(); |
| 74 | + |
| 75 | + verify(mockLogger).debug("No active {} sessions to broadcast a message to", "test-transport"); |
| 76 | + verify(mockLogger, never()) |
| 77 | + .debug("Attempting to broadcast to {} active {} sessions", 0, "test-transport"); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void testNotifyClients_Populated_DebugEnabled() { |
| 82 | + when(mockLogger.isDebugEnabled()).thenReturn(true); |
| 83 | + provider.sessions.put("sess-1", session1); |
| 84 | + when(session1.sendNotification("method", "params")).thenReturn(Mono.empty()); |
| 85 | + |
| 86 | + provider.notifyClients("method", "params").block(); |
| 87 | + |
| 88 | + // Verify debug branch was entered |
| 89 | + verify(mockLogger) |
| 90 | + .debug("Attempting to broadcast to {} active {} sessions", 1, "test-transport"); |
| 91 | + verify(session1).sendNotification("method", "params"); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void testNotifyClients_Populated_DebugDisabled() { |
| 96 | + when(mockLogger.isDebugEnabled()).thenReturn(false); |
| 97 | + provider.sessions.put("sess-1", session1); |
| 98 | + when(session1.sendNotification("method", "params")).thenReturn(Mono.empty()); |
| 99 | + |
| 100 | + provider.notifyClients("method", "params").block(); |
| 101 | + |
| 102 | + // Verify debug branch was skipped |
| 103 | + verify(mockLogger, never()) |
| 104 | + .debug("Attempting to broadcast to {} active {} sessions", 1, "test-transport"); |
| 105 | + verify(session1).sendNotification("method", "params"); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + void testNotifyClients_HandlesNotificationErrorGracefully() { |
| 110 | + // We don't care about debug mode for this test |
| 111 | + when(mockLogger.isDebugEnabled()).thenReturn(false); |
| 112 | + |
| 113 | + provider.sessions.put("sess-1", session1); |
| 114 | + when(session1.getId()).thenReturn("sess-1-id"); |
| 115 | + |
| 116 | + // Simulate an error occurring during the send operation |
| 117 | + RuntimeException simulatedError = new RuntimeException("Simulated I/O failure"); |
| 118 | + when(session1.sendNotification("method", "params")).thenReturn(Mono.error(simulatedError)); |
| 119 | + |
| 120 | + // The onErrorComplete() should swallow the error, so block() will not throw an exception |
| 121 | + provider.notifyClients("method", "params").block(); |
| 122 | + |
| 123 | + // Verify the doOnError block correctly logged the failure |
| 124 | + verify(mockLogger) |
| 125 | + .error( |
| 126 | + "Failed to send a message to {} session {}: {}", |
| 127 | + "test-transport", |
| 128 | + "sess-1-id", |
| 129 | + "Simulated I/O failure"); |
| 130 | + } |
| 131 | + |
| 132 | + // --- CLOSE GRACEFULLY TESTS --- |
| 133 | + |
| 134 | + @Test |
| 135 | + void testCloseGracefully_DebugEnabled() { |
| 136 | + when(mockLogger.isDebugEnabled()).thenReturn(true); |
| 137 | + |
| 138 | + provider.sessions.put("sess-1", session1); |
| 139 | + provider.sessions.put("sess-2", session2); |
| 140 | + |
| 141 | + when(session1.closeGracefully()).thenReturn(Mono.empty()); |
| 142 | + when(session2.closeGracefully()).thenReturn(Mono.empty()); |
| 143 | + |
| 144 | + assertFalse(provider.isClosing.get()); // Initially false |
| 145 | + |
| 146 | + provider.closeGracefully().block(); |
| 147 | + |
| 148 | + // Verify doFirst actions |
| 149 | + assertTrue(provider.isClosing.get()); |
| 150 | + verify(mockLogger) |
| 151 | + .debug("Initiating graceful shutdown for {} {} sessions", 2, "test-transport"); |
| 152 | + |
| 153 | + // Verify flatMap actions |
| 154 | + verify(session1).closeGracefully(); |
| 155 | + verify(session2).closeGracefully(); |
| 156 | + |
| 157 | + // Verify doFinally actions |
| 158 | + assertTrue(provider.sessions.isEmpty()); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + void testCloseGracefully_DebugDisabled() { |
| 163 | + when(mockLogger.isDebugEnabled()).thenReturn(false); |
| 164 | + |
| 165 | + provider.sessions.put("sess-1", session1); |
| 166 | + when(session1.closeGracefully()).thenReturn(Mono.empty()); |
| 167 | + |
| 168 | + provider.closeGracefully().block(); |
| 169 | + |
| 170 | + // Verify the debug logging branch was bypassed |
| 171 | + verify(mockLogger, never()) |
| 172 | + .debug("Initiating graceful shutdown for {} {} sessions", 1, "test-transport"); |
| 173 | + |
| 174 | + // Verify state still updated properly |
| 175 | + assertTrue(provider.isClosing.get()); |
| 176 | + assertTrue(provider.sessions.isEmpty()); |
| 177 | + verify(session1).closeGracefully(); |
| 178 | + } |
| 179 | +} |
0 commit comments