|
| 1 | +package org.springframework.ai.autoconfigure.mcp.server; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 7 | +import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; |
| 8 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 9 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 10 | +import org.springframework.context.annotation.Configuration; |
| 11 | +import org.springframework.web.reactive.function.server.RouterFunction; |
| 12 | + |
| 13 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 14 | + |
| 15 | +import io.modelcontextprotocol.server.transport.WebFluxSseServerTransport; |
| 16 | + |
| 17 | +class McpWebFluxServerAutoConfigurationTests { |
| 18 | + |
| 19 | + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 20 | + .withConfiguration(AutoConfigurations.of( |
| 21 | + McpWebFluxServerAutoConfiguration.class, |
| 22 | + JacksonAutoConfiguration.class, |
| 23 | + TestConfiguration.class)); // Add test configuration |
| 24 | + |
| 25 | + @Test |
| 26 | + void shouldConfigureWebFluxTransportWithCustomObjectMapper() { |
| 27 | + this.contextRunner.run((context) -> { |
| 28 | + assertThat(context).hasSingleBean(WebFluxSseServerTransport.class); |
| 29 | + assertThat(context).hasSingleBean(RouterFunction.class); |
| 30 | + assertThat(context).hasSingleBean(McpServerProperties.class); |
| 31 | + |
| 32 | + WebFluxSseServerTransport transport = context.getBean(WebFluxSseServerTransport.class); |
| 33 | + ObjectMapper objectMapper = context.getBean(ObjectMapper.class); |
| 34 | + |
| 35 | + // Verify that the ObjectMapper is configured to ignore unknown properties |
| 36 | + assertThat(objectMapper.getDeserializationConfig().isEnabled( |
| 37 | + com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) |
| 38 | + .isFalse(); |
| 39 | + |
| 40 | + // Test with a JSON payload containing unknown fields |
| 41 | + String jsonWithUnknownField = """ |
| 42 | + { |
| 43 | + "tools": ["tool1", "tool2"], |
| 44 | + "name": "test", |
| 45 | + "unknownField": "value" |
| 46 | + } |
| 47 | + """; |
| 48 | + |
| 49 | + // This should not throw an exception |
| 50 | + TestMessage message = objectMapper.readValue(jsonWithUnknownField, TestMessage.class); |
| 51 | + assertThat(message.getName()).isEqualTo("test"); |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + // Test configuration to enable McpServerProperties |
| 56 | + @Configuration |
| 57 | + @EnableConfigurationProperties(McpServerProperties.class) |
| 58 | + static class TestConfiguration { |
| 59 | + } |
| 60 | + |
| 61 | + // Test class to simulate the actual message structure |
| 62 | + static class TestMessage { |
| 63 | + private String name; |
| 64 | + |
| 65 | + public String getName() { |
| 66 | + return name; |
| 67 | + } |
| 68 | + |
| 69 | + public void setName(String name) { |
| 70 | + this.name = name; |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments