refactor(tests): improve notification assertions in WebFluxSseIntegrationTests

Replace index-based assertions with content-based lookups using a notification map.
This change makes the tests more resilient by removing the dependency on
notification order, which is important for asynchronous messaging tests.

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
This commit is contained in:
Christian Tzolov 2025-04-10 16:54:24 +02:00
commit 63724f17a4

View file

@ -10,6 +10,7 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.stream.Collectors;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.modelcontextprotocol.client.McpClient;
@ -596,20 +597,24 @@ public class WebFluxSseIntegrationTests {
// Should have received 3 notifications (1 NOTICE and 2 ERROR)
assertThat(receivedNotifications).hasSize(3);
Map<String, McpSchema.LoggingMessageNotification> notificationMap = receivedNotifications.stream()
.collect(Collectors.toMap(n -> n.data(), n -> n));
// First notification should be NOTICE level
assertThat(receivedNotifications.get(0).level()).isEqualTo(McpSchema.LoggingLevel.NOTICE);
assertThat(receivedNotifications.get(0).logger()).isEqualTo("test-logger");
assertThat(receivedNotifications.get(0).data()).isEqualTo("Notice message");
assertThat(notificationMap.get("Notice message").level()).isEqualTo(McpSchema.LoggingLevel.NOTICE);
assertThat(notificationMap.get("Notice message").logger()).isEqualTo("test-logger");
assertThat(notificationMap.get("Notice message").data()).isEqualTo("Notice message");
// Second notification should be ERROR level
assertThat(receivedNotifications.get(1).level()).isEqualTo(McpSchema.LoggingLevel.ERROR);
assertThat(receivedNotifications.get(1).logger()).isEqualTo("test-logger");
assertThat(receivedNotifications.get(1).data()).isEqualTo("Error message");
assertThat(notificationMap.get("Error message").level()).isEqualTo(McpSchema.LoggingLevel.ERROR);
assertThat(notificationMap.get("Error message").logger()).isEqualTo("test-logger");
assertThat(notificationMap.get("Error message").data()).isEqualTo("Error message");
// Third notification should be ERROR level
assertThat(receivedNotifications.get(2).level()).isEqualTo(McpSchema.LoggingLevel.ERROR);
assertThat(receivedNotifications.get(2).logger()).isEqualTo("test-logger");
assertThat(receivedNotifications.get(2).data()).isEqualTo("Another error message");
assertThat(notificationMap.get("Another error message").level())
.isEqualTo(McpSchema.LoggingLevel.ERROR);
assertThat(notificationMap.get("Another error message").logger()).isEqualTo("test-logger");
assertThat(notificationMap.get("Another error message").data()).isEqualTo("Another error message");
});
}
mcpServer.close();