|
| 1 | +/* |
| 2 | + * Copyright StreamsHub authors. |
| 3 | + * License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). |
| 4 | + */ |
| 5 | +package io.streamshub.mcp.common.observability; |
| 6 | + |
| 7 | +import io.quarkiverse.mcp.server.McpConnection; |
| 8 | +import io.quarkiverse.mcp.server.RawMessage; |
| 9 | +import io.quarkiverse.mcp.server.RequestId; |
| 10 | +import org.junit.jupiter.api.BeforeEach; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.mockito.Mockito; |
| 13 | + |
| 14 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 15 | +import static org.mockito.Mockito.verify; |
| 16 | +import static org.mockito.Mockito.when; |
| 17 | + |
| 18 | +/** |
| 19 | + * Unit tests for {@link McpTrafficLogger}. |
| 20 | + */ |
| 21 | +class McpTrafficLoggerTest { |
| 22 | + |
| 23 | + private McpTrafficLogger logger; |
| 24 | + private RawMessage message; |
| 25 | + private McpConnection connection; |
| 26 | + |
| 27 | + McpTrafficLoggerTest() { |
| 28 | + } |
| 29 | + |
| 30 | + @BeforeEach |
| 31 | + void setUp() { |
| 32 | + logger = new McpTrafficLogger(); |
| 33 | + message = Mockito.mock(RawMessage.class); |
| 34 | + connection = Mockito.mock(McpConnection.class); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + void testIsEnabledReturnsTrue() { |
| 39 | + assertTrue(logger.isEnabled(), "McpTrafficLogger should always be enabled"); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void testOnMessageReceivedWithRequestMessage() { |
| 44 | + RequestId requestId = Mockito.mock(RequestId.class); |
| 45 | + when(requestId.toString()).thenReturn("123"); |
| 46 | + when(message.method()).thenReturn("tools/call"); |
| 47 | + when(message.id()).thenReturn(requestId); |
| 48 | + when(message.asPrettyString()).thenReturn("{\n \"method\": \"tools/call\"\n}"); |
| 49 | + when(connection.id()).thenReturn("conn-abc"); |
| 50 | + |
| 51 | + logger.onMessageReceived(message, connection); |
| 52 | + |
| 53 | + // Verify the logger accessed message properties |
| 54 | + verify(message).method(); |
| 55 | + verify(message).id(); |
| 56 | + verify(message).asPrettyString(); |
| 57 | + verify(connection).id(); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void testOnMessageReceivedWithNotification() { |
| 62 | + when(message.method()).thenReturn("notifications/message"); |
| 63 | + when(message.id()).thenReturn(null); // Notifications have no ID |
| 64 | + when(message.asPrettyString()).thenReturn("{\n \"method\": \"notifications/message\"\n}"); |
| 65 | + when(connection.id()).thenReturn("conn-xyz"); |
| 66 | + |
| 67 | + logger.onMessageReceived(message, connection); |
| 68 | + |
| 69 | + // Verify the logger accessed message properties |
| 70 | + verify(message).method(); |
| 71 | + verify(message).id(); |
| 72 | + verify(message).asPrettyString(); |
| 73 | + verify(connection).id(); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void testOnMessageReceivedWithResponse() { |
| 78 | + RequestId requestId = Mockito.mock(RequestId.class); |
| 79 | + when(requestId.toString()).thenReturn("456"); |
| 80 | + when(message.method()).thenReturn(null); // Responses have no method |
| 81 | + when(message.id()).thenReturn(requestId); |
| 82 | + when(message.asPrettyString()).thenReturn("{\n \"result\": {...}\n}"); |
| 83 | + when(connection.id()).thenReturn("conn-def"); |
| 84 | + |
| 85 | + logger.onMessageReceived(message, connection); |
| 86 | + |
| 87 | + // Verify the logger accessed message properties |
| 88 | + verify(message).method(); |
| 89 | + verify(message).id(); |
| 90 | + verify(message).asPrettyString(); |
| 91 | + verify(connection).id(); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void testOnMessageSentWithRequestMessage() { |
| 96 | + RequestId requestId = Mockito.mock(RequestId.class); |
| 97 | + when(requestId.toString()).thenReturn("789"); |
| 98 | + when(message.method()).thenReturn("tools/list"); |
| 99 | + when(message.id()).thenReturn(requestId); |
| 100 | + when(message.asPrettyString()).thenReturn("{\n \"method\": \"tools/list\"\n}"); |
| 101 | + when(connection.id()).thenReturn("conn-ghi"); |
| 102 | + |
| 103 | + logger.onMessageSent(message, connection); |
| 104 | + |
| 105 | + // Verify the logger accessed message properties |
| 106 | + verify(message).method(); |
| 107 | + verify(message).id(); |
| 108 | + verify(message).asPrettyString(); |
| 109 | + verify(connection).id(); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void testOnMessageSentWithNullPrettyString() { |
| 114 | + when(message.method()).thenReturn("tools/call"); |
| 115 | + when(message.id()).thenReturn(null); |
| 116 | + when(message.asPrettyString()).thenReturn(null); |
| 117 | + when(connection.id()).thenReturn("conn-jkl"); |
| 118 | + |
| 119 | + logger.onMessageSent(message, connection); |
| 120 | + |
| 121 | + // Verify null is handled - formatMessage still called asPrettyString() |
| 122 | + verify(message).method(); |
| 123 | + verify(message).id(); |
| 124 | + verify(message).asPrettyString(); |
| 125 | + verify(connection).id(); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + void testOnMessageSentWithEmptyPrettyString() { |
| 130 | + when(message.method()).thenReturn("tools/call"); |
| 131 | + when(message.id()).thenReturn(null); |
| 132 | + when(message.asPrettyString()).thenReturn(""); |
| 133 | + when(connection.id()).thenReturn("conn-mno"); |
| 134 | + |
| 135 | + logger.onMessageSent(message, connection); |
| 136 | + |
| 137 | + // Verify empty string is handled gracefully |
| 138 | + verify(message).method(); |
| 139 | + verify(message).id(); |
| 140 | + verify(message).asPrettyString(); |
| 141 | + verify(connection).id(); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + void testOnMessageReceivedWithBlankPrettyString() { |
| 146 | + when(message.method()).thenReturn("initialize"); |
| 147 | + when(message.id()).thenReturn(null); |
| 148 | + when(message.asPrettyString()).thenReturn(" "); |
| 149 | + when(connection.id()).thenReturn("conn-pqr"); |
| 150 | + |
| 151 | + logger.onMessageReceived(message, connection); |
| 152 | + |
| 153 | + // Verify blank string is handled gracefully (falls back to "n/a") |
| 154 | + verify(message).method(); |
| 155 | + verify(message).id(); |
| 156 | + verify(message).asPrettyString(); |
| 157 | + verify(connection).id(); |
| 158 | + } |
| 159 | +} |
0 commit comments