77import java .util .List ;
88import java .util .Map ;
99
10- import io .modelcontextprotocol .json .McpJsonMapper ;
11- import io .modelcontextprotocol .json .TypeRef ;
1210import io .modelcontextprotocol .json .schema .JsonSchemaValidator ;
1311import io .modelcontextprotocol .json .schema .JsonSchemaValidator .ValidationResponse ;
14- import io .modelcontextprotocol .spec .McpSchema ;
1512import io .modelcontextprotocol .spec .McpSchema .CallToolResult ;
1613import io .modelcontextprotocol .spec .McpSchema .TextContent ;
1714import io .modelcontextprotocol .spec .McpSchema .Tool ;
2623
2724/**
2825 * Tests for {@link ToolInputValidator}.
26+ *
27+ * @author Andrei Shakirin
2928 */
3029class ToolInputValidatorTests {
3130
32- private final McpJsonMapper jsonMapper = mock (McpJsonMapper .class );
33-
3431 private final JsonSchemaValidator validator = mock (JsonSchemaValidator .class );
3532
36- private final McpSchema . JsonSchema inputSchema = new McpSchema . JsonSchema ( " object" ,
37- Map .of ("name" , Map .of ("type" , "string" )), List .of ("name" ), null , null , null );
33+ private final Map < String , Object > inputSchema = Map . of ( "type" , " object" , "properties " ,
34+ Map .of ("name" , Map .of ("type" , "string" )), "required" , List .of ("name" ));
3835
3936 private final Tool toolWithSchema = Tool .builder ()
4037 .name ("test-tool" )
@@ -46,62 +43,53 @@ class ToolInputValidatorTests {
4643
4744 @ Test
4845 void validate_whenDisabled_returnsNull () {
49- CallToolResult result = ToolInputValidator .validate (toolWithSchema , Map .of ("name" , "test" ), false , jsonMapper ,
50- validator );
46+ CallToolResult result = ToolInputValidator .validate (toolWithSchema , Map .of ("name" , "test" ), false , validator );
5147
5248 assertThat (result ).isNull ();
5349 verify (validator , never ()).validate (any (), any ());
5450 }
5551
5652 @ Test
5753 void validate_whenNoSchema_returnsNull () {
58- CallToolResult result = ToolInputValidator .validate (toolWithoutSchema , Map .of ("name" , "test" ), true , jsonMapper ,
59- validator );
54+ CallToolResult result = ToolInputValidator .validate (toolWithoutSchema , Map .of ("name" , "test" ), true , validator );
6055
6156 assertThat (result ).isNull ();
6257 verify (validator , never ()).validate (any (), any ());
6358 }
6459
6560 @ Test
6661 void validate_whenNoValidator_returnsNull () {
67- CallToolResult result = ToolInputValidator .validate (toolWithSchema , Map .of ("name" , "test" ), true , jsonMapper ,
68- null );
62+ CallToolResult result = ToolInputValidator .validate (toolWithSchema , Map .of ("name" , "test" ), true , null );
6963
7064 assertThat (result ).isNull ();
7165 }
7266
7367 @ Test
74- @ SuppressWarnings ("unchecked" )
7568 void validate_withValidInput_returnsNull () {
76- when (jsonMapper .convertValue (any (), any (TypeRef .class ))).thenReturn (Map .of ("type" , "object" ));
7769 when (validator .validate (any (), any ())).thenReturn (ValidationResponse .asValid (null ));
7870
79- CallToolResult result = ToolInputValidator .validate (toolWithSchema , Map .of ("name" , "test" ), true , jsonMapper ,
80- validator );
71+ CallToolResult result = ToolInputValidator .validate (toolWithSchema , Map .of ("name" , "test" ), true , validator );
8172
8273 assertThat (result ).isNull ();
8374 }
8475
8576 @ Test
86- @ SuppressWarnings ("unchecked" )
8777 void validate_withInvalidInput_returnsErrorResult () {
88- when (jsonMapper .convertValue (any (), any (TypeRef .class ))).thenReturn (Map .of ("type" , "object" ));
8978 when (validator .validate (any (), any ())).thenReturn (ValidationResponse .asInvalid ("missing required: 'name'" ));
9079
91- CallToolResult result = ToolInputValidator .validate (toolWithSchema , Map .of (), true , jsonMapper , validator );
80+ CallToolResult result = ToolInputValidator .validate (toolWithSchema , Map .of (), true , validator );
9281
9382 assertThat (result ).isNotNull ();
9483 assertThat (result .isError ()).isTrue ();
9584 assertThat (((TextContent ) result .content ().get (0 )).text ()).contains ("missing required: 'name'" );
85+ verify (validator ).validate (any (), any ());
9686 }
9787
9888 @ Test
99- @ SuppressWarnings ("unchecked" )
10089 void validate_withNullArguments_usesEmptyMap () {
101- when (jsonMapper .convertValue (any (), any (TypeRef .class ))).thenReturn (Map .of ("type" , "object" ));
10290 when (validator .validate (any (), any ())).thenReturn (ValidationResponse .asValid (null ));
10391
104- CallToolResult result = ToolInputValidator .validate (toolWithSchema , null , true , jsonMapper , validator );
92+ CallToolResult result = ToolInputValidator .validate (toolWithSchema , null , true , validator );
10593
10694 assertThat (result ).isNull ();
10795 verify (validator ).validate (any (), any ());
0 commit comments