Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion mcp/mcp-annotations-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,26 @@
<version>${project.parent.version}</version>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>

</dependencies>


</project>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* Copyright 2025-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.ai.mcp.annotation.spring;

import java.lang.reflect.Method;
import java.util.Arrays;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension;

import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.AopUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;

/**
* Unit Tests for {@link AnnotationProviderUtil}.
*
* @author Sun Yuhan
*/
@ExtendWith(MockitoExtension.class)
class AnnotationProviderUtilTests {

@Test
void beanMethodsWithNormalClassReturnsSortedMethods() {
TestClass testBean = new TestClass();

Method[] methods = AnnotationProviderUtil.beanMethods(testBean);

assertThat(methods).isNotNull();
assertThat(methods.length).isEqualTo(3);

assertThat(methods[0].getName()).isEqualTo("aaaMethod");
assertThat(methods[1].getName()).isEqualTo("bbbMethod");
assertThat(methods[2].getName()).isEqualTo("cccMethod");

Arrays.stream(methods).forEach(method -> assertThat(method.getDeclaringClass()).isEqualTo(TestClass.class));
}

@Test
void beanMethodsWithAopProxyReturnsTargetClassMethods() {
TestClass target = new TestClass();
ProxyFactory proxyFactory = new ProxyFactory(target);
Object proxy = proxyFactory.getProxy();

Method[] methods = AnnotationProviderUtil.beanMethods(proxy);

assertThat(methods).isNotNull();
assertThat(methods.length).isEqualTo(3);

Arrays.stream(methods).forEach(method -> assertThat(method.getDeclaringClass()).isEqualTo(TestClass.class));
}

@Test
void beanMethodsWithMockedAopProxyReturnsTargetClassMethods() {
Object proxy = mock(Object.class);

try (MockedStatic<AopUtils> mockedAopUtils = mockStatic(AopUtils.class)) {
mockedAopUtils.when(() -> AopUtils.isAopProxy(proxy)).thenReturn(true);
mockedAopUtils.when(() -> AopUtils.getTargetClass(proxy)).thenReturn(TestClass.class);

Method[] methods = AnnotationProviderUtil.beanMethods(proxy);

assertThat(methods).isNotNull();
assertThat(methods.length).isEqualTo(3);

mockedAopUtils.verify(() -> AopUtils.isAopProxy(proxy));
mockedAopUtils.verify(() -> AopUtils.getTargetClass(proxy));
}
}

@Test
void beanMethodsWithNoDeclaredMethodsReturnsEmptyArray() {
NoMethodClass testBean = new NoMethodClass();

Method[] methods = AnnotationProviderUtil.beanMethods(testBean);

assertThat(methods).isNotNull();
assertThat(methods).isEmpty();
}

@Test
void beanMethodsWithOverloadedMethodsReturnsCorrectlySortedMethods() {
OverloadedMethodClass testBean = new OverloadedMethodClass();

Method[] methods = AnnotationProviderUtil.beanMethods(testBean);

assertThat(methods).isNotNull();
assertThat(methods.length).isEqualTo(3);

assertThat(methods[0].getName()).isEqualTo("overloadedMethod");
assertThat(methods[0].getParameterCount()).isEqualTo(0);

assertThat(methods[1].getName()).isEqualTo("overloadedMethod");
assertThat(methods[1].getParameterCount()).isEqualTo(1);

assertThat(methods[2].getName()).isEqualTo("simpleMethod");
}

static class TestClass {

public void cccMethod() {
}

public void aaaMethod() {
}

public void bbbMethod() {
}

}

static class NoMethodClass {

}

static class OverloadedMethodClass {

public void simpleMethod() {
}

public void overloadedMethod(String param) {
}

public void overloadedMethod() {
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
/*
* Copyright 2025-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.ai.mcp.annotation.spring;

import java.util.ArrayList;
import java.util.List;

import io.modelcontextprotocol.server.McpServerFeatures;
import io.modelcontextprotocol.server.McpStatelessServerFeatures;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springaicommunity.mcp.method.changed.prompt.AsyncPromptListChangedSpecification;
import org.springaicommunity.mcp.method.changed.resource.AsyncResourceListChangedSpecification;
import org.springaicommunity.mcp.method.changed.tool.AsyncToolListChangedSpecification;
import org.springaicommunity.mcp.method.elicitation.AsyncElicitationSpecification;
import org.springaicommunity.mcp.method.logging.AsyncLoggingSpecification;
import org.springaicommunity.mcp.method.progress.AsyncProgressSpecification;
import org.springaicommunity.mcp.method.sampling.AsyncSamplingSpecification;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Unit Tests for {@link AsyncMcpAnnotationProviders}.
*
* @author Sun Yuhan
*/
@ExtendWith(MockitoExtension.class)
class AsyncMcpAnnotationProvidersTests {

@Test
void testLoggingSpecifications() {
List<Object> loggingObjects = new ArrayList<>();
loggingObjects.add(new Object());

List<AsyncLoggingSpecification> result = AsyncMcpAnnotationProviders.loggingSpecifications(loggingObjects);

assertNotNull(result);
}

@Test
void testLoggingSpecificationsWithEmptyList() {
List<Object> loggingObjects = new ArrayList<>();

List<AsyncLoggingSpecification> result = AsyncMcpAnnotationProviders.loggingSpecifications(loggingObjects);

assertNotNull(result);
assertTrue(result.isEmpty());
}

@Test
void testSamplingSpecifications() {
List<Object> samplingObjects = new ArrayList<>();
samplingObjects.add(new Object());

List<AsyncSamplingSpecification> result = AsyncMcpAnnotationProviders.samplingSpecifications(samplingObjects);

assertNotNull(result);
}

@Test
void testElicitationSpecifications() {
List<Object> elicitationObjects = new ArrayList<>();
elicitationObjects.add(new Object());

List<AsyncElicitationSpecification> result = AsyncMcpAnnotationProviders
.elicitationSpecifications(elicitationObjects);

assertNotNull(result);
}

@Test
void testProgressSpecifications() {
List<Object> progressObjects = new ArrayList<>();
progressObjects.add(new Object());

List<AsyncProgressSpecification> result = AsyncMcpAnnotationProviders.progressSpecifications(progressObjects);

assertNotNull(result);
}

@Test
void testToolSpecifications() {
List<Object> toolObjects = new ArrayList<>();
toolObjects.add(new Object());

List<McpServerFeatures.AsyncToolSpecification> result = AsyncMcpAnnotationProviders
.toolSpecifications(toolObjects);

assertNotNull(result);
}

@Test
void testStatelessToolSpecifications() {

List<Object> toolObjects = new ArrayList<>();
toolObjects.add(new Object());

List<McpStatelessServerFeatures.AsyncToolSpecification> result = AsyncMcpAnnotationProviders
.statelessToolSpecifications(toolObjects);

assertNotNull(result);
}

@Test
void testCompleteSpecifications() {
List<Object> completeObjects = new ArrayList<>();
completeObjects.add(new Object());

List<McpServerFeatures.AsyncCompletionSpecification> result = AsyncMcpAnnotationProviders
.completeSpecifications(completeObjects);

assertNotNull(result);
}

@Test
void testStatelessCompleteSpecifications() {
List<Object> completeObjects = new ArrayList<>();
completeObjects.add(new Object());

List<McpStatelessServerFeatures.AsyncCompletionSpecification> result = AsyncMcpAnnotationProviders
.statelessCompleteSpecifications(completeObjects);

assertNotNull(result);
}

@Test
void testPromptSpecifications() {
List<Object> promptObjects = new ArrayList<>();
promptObjects.add(new Object());

List<McpServerFeatures.AsyncPromptSpecification> result = AsyncMcpAnnotationProviders
.promptSpecifications(promptObjects);

assertNotNull(result);
}

@Test
void testStatelessPromptSpecifications() {
List<Object> promptObjects = new ArrayList<>();
promptObjects.add(new Object());

List<McpStatelessServerFeatures.AsyncPromptSpecification> result = AsyncMcpAnnotationProviders
.statelessPromptSpecifications(promptObjects);

assertNotNull(result);
}

@Test
void testResourceSpecifications() {
List<Object> resourceObjects = new ArrayList<>();
resourceObjects.add(new Object());

List<McpServerFeatures.AsyncResourceSpecification> result = AsyncMcpAnnotationProviders
.resourceSpecifications(resourceObjects);

assertNotNull(result);
}

@Test
void testStatelessResourceSpecifications() {
List<Object> resourceObjects = new ArrayList<>();
resourceObjects.add(new Object());

List<McpStatelessServerFeatures.AsyncResourceSpecification> result = AsyncMcpAnnotationProviders
.statelessResourceSpecifications(resourceObjects);

assertNotNull(result);
}

@Test
void testResourceListChangedSpecifications() {
List<Object> resourceListChangedObjects = new ArrayList<>();
resourceListChangedObjects.add(new Object());

List<AsyncResourceListChangedSpecification> result = AsyncMcpAnnotationProviders
.resourceListChangedSpecifications(resourceListChangedObjects);

assertNotNull(result);
}

@Test
void testToolListChangedSpecifications() {
List<Object> toolListChangedObjects = new ArrayList<>();
toolListChangedObjects.add(new Object());

List<AsyncToolListChangedSpecification> result = AsyncMcpAnnotationProviders
.toolListChangedSpecifications(toolListChangedObjects);

assertNotNull(result);
}

@Test
void testPromptListChangedSpecifications() {
List<Object> promptListChangedObjects = new ArrayList<>();
promptListChangedObjects.add(new Object());

List<AsyncPromptListChangedSpecification> result = AsyncMcpAnnotationProviders
.promptListChangedSpecifications(promptListChangedObjects);

assertNotNull(result);
}

}
Loading