forked from modelcontextprotocol/java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONRPCRequestMcpValidationTest.java
More file actions
90 lines (73 loc) · 2.82 KB
/
JSONRPCRequestMcpValidationTest.java
File metadata and controls
90 lines (73 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* Copyright 2024-2024 the original author or authors.
*/
package io.modelcontextprotocol.spec;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* Tests for MCP-specific validation of JSONRPCRequest ID requirements.
*
* @author Christian Tzolov
*/
public class JSONRPCRequestMcpValidationTest {
@Test
public void testValidStringId() {
assertDoesNotThrow(() -> {
var request = new McpSchema.JSONRPCRequest("2.0", "test/method", "string-id", null);
assertEquals("string-id", request.id());
});
}
@Test
public void testValidIntegerId() {
assertDoesNotThrow(() -> {
var request = new McpSchema.JSONRPCRequest("2.0", "test/method", 123, null);
assertEquals(123, request.id());
});
}
@Test
public void testValidLongId() {
assertDoesNotThrow(() -> {
var request = new McpSchema.JSONRPCRequest("2.0", "test/method", 123L, null);
assertEquals(123L, request.id());
});
}
@Test
public void testNullIdThrowsException() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
new McpSchema.JSONRPCRequest("2.0", "test/method", null, null);
});
assertTrue(exception.getMessage().contains("MCP requests MUST include an ID"));
assertTrue(exception.getMessage().contains("null IDs are not allowed"));
}
@Test
public void testDoubleIdTypeThrowsException() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
new McpSchema.JSONRPCRequest("2.0", "test/method", 123.45, null);
});
assertTrue(exception.getMessage().contains("MCP requests MUST have an ID that is either a string or integer"));
assertTrue(exception.getMessage().contains("Double"));
}
@Test
public void testBooleanIdThrowsException() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
new McpSchema.JSONRPCRequest("2.0", "test/method", true, null);
});
assertTrue(exception.getMessage().contains("MCP requests MUST have an ID that is either a string or integer"));
assertTrue(exception.getMessage().contains("Boolean"));
}
@Test
public void testArrayIdThrowsException() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
new McpSchema.JSONRPCRequest("2.0", "test/method", new String[] { "array" }, null);
});
assertTrue(exception.getMessage().contains("MCP requests MUST have an ID that is either a string or integer"));
}
@Test
public void testObjectIdThrowsException() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
new McpSchema.JSONRPCRequest("2.0", "test/method", new Object(), null);
});
assertTrue(exception.getMessage().contains("MCP requests MUST have an ID that is either a string or integer"));
assertTrue(exception.getMessage().contains("Object"));
}
}