|
| 1 | +/* |
| 2 | + * Copyright 2025-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.ai.mcp.client.common.autoconfigure.annotations; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import io.modelcontextprotocol.spec.McpSchema; |
| 22 | +import org.junit.jupiter.params.ParameterizedTest; |
| 23 | +import org.junit.jupiter.params.provider.ValueSource; |
| 24 | +import org.springaicommunity.mcp.annotation.McpPromptListChanged; |
| 25 | +import org.springaicommunity.mcp.annotation.McpResourceListChanged; |
| 26 | +import org.springaicommunity.mcp.annotation.McpToolListChanged; |
| 27 | + |
| 28 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 29 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 30 | +import org.springframework.context.annotation.Bean; |
| 31 | +import org.springframework.context.annotation.Configuration; |
| 32 | + |
| 33 | +import static org.assertj.core.api.Assertions.assertThat; |
| 34 | + |
| 35 | +/** |
| 36 | + * Integration tests for MCP client list-changed annotations scanning. |
| 37 | + * |
| 38 | + * <p> |
| 39 | + * This test validates that the annotation scanner correctly identifies and processes |
| 40 | + * {@code @McpToolListChanged}, {@code @McpResourceListChanged}, and |
| 41 | + * {@code @McpPromptListChanged} annotations. |
| 42 | + * |
| 43 | + * @author Fu Jian |
| 44 | + */ |
| 45 | +public class McpClientListChangedAnnotationsScanningIT { |
| 46 | + |
| 47 | + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 48 | + .withConfiguration(AutoConfigurations.of(McpClientAnnotationScannerAutoConfiguration.class, |
| 49 | + McpClientSpecificationFactoryAutoConfiguration.class)); |
| 50 | + |
| 51 | + @ParameterizedTest |
| 52 | + @ValueSource(strings = { "SYNC", "ASYNC" }) |
| 53 | + void shouldScanAllThreeListChangedAnnotations(String clientType) { |
| 54 | + String prefix = clientType.toLowerCase(); |
| 55 | + |
| 56 | + this.contextRunner.withUserConfiguration(AllListChangedConfiguration.class) |
| 57 | + .withPropertyValues("spring.ai.mcp.client.type=" + clientType) |
| 58 | + .run(context -> { |
| 59 | + // Verify all three annotations were scanned |
| 60 | + McpClientAnnotationScannerAutoConfiguration.ClientMcpAnnotatedBeans annotatedBeans = context |
| 61 | + .getBean(McpClientAnnotationScannerAutoConfiguration.ClientMcpAnnotatedBeans.class); |
| 62 | + assertThat(annotatedBeans.getBeansByAnnotation(McpToolListChanged.class)).hasSize(1); |
| 63 | + assertThat(annotatedBeans.getBeansByAnnotation(McpResourceListChanged.class)).hasSize(1); |
| 64 | + assertThat(annotatedBeans.getBeansByAnnotation(McpPromptListChanged.class)).hasSize(1); |
| 65 | + |
| 66 | + // Verify all three specification beans were created |
| 67 | + assertThat(context).hasBean(prefix + "ToolListChangedSpecs"); |
| 68 | + assertThat(context).hasBean(prefix + "ResourceListChangedSpecs"); |
| 69 | + assertThat(context).hasBean(prefix + "PromptListChangedSpecs"); |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + @ParameterizedTest |
| 74 | + @ValueSource(strings = { "SYNC", "ASYNC" }) |
| 75 | + void shouldNotScanAnnotationsWhenScannerDisabled(String clientType) { |
| 76 | + String prefix = clientType.toLowerCase(); |
| 77 | + |
| 78 | + this.contextRunner.withUserConfiguration(AllListChangedConfiguration.class) |
| 79 | + .withPropertyValues("spring.ai.mcp.client.type=" + clientType, |
| 80 | + "spring.ai.mcp.client.annotation-scanner.enabled=false") |
| 81 | + .run(context -> { |
| 82 | + // Verify scanner beans were not created |
| 83 | + assertThat(context).doesNotHaveBean(McpClientAnnotationScannerAutoConfiguration.class); |
| 84 | + assertThat(context).doesNotHaveBean(prefix + "ToolListChangedSpecs"); |
| 85 | + assertThat(context).doesNotHaveBean(prefix + "ResourceListChangedSpecs"); |
| 86 | + assertThat(context).doesNotHaveBean(prefix + "PromptListChangedSpecs"); |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + @Configuration |
| 91 | + static class AllListChangedConfiguration { |
| 92 | + |
| 93 | + @Bean |
| 94 | + TestListChangedHandlers testHandlers() { |
| 95 | + return new TestListChangedHandlers(); |
| 96 | + } |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + static class TestListChangedHandlers { |
| 101 | + |
| 102 | + @McpToolListChanged(clients = "test-client") |
| 103 | + public void onToolListChanged(List<McpSchema.Tool> updatedTools) { |
| 104 | + // Test handler for tool list changes |
| 105 | + } |
| 106 | + |
| 107 | + @McpResourceListChanged(clients = "test-client") |
| 108 | + public void onResourceListChanged(List<McpSchema.Resource> updatedResources) { |
| 109 | + // Test handler for resource list changes |
| 110 | + } |
| 111 | + |
| 112 | + @McpPromptListChanged(clients = "test-client") |
| 113 | + public void onPromptListChanged(List<McpSchema.Prompt> updatedPrompts) { |
| 114 | + // Test handler for prompt list changes |
| 115 | + } |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments