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