|
| 1 | +package io.modelcontextprotocol.kotlin.sdk.integration.kotlin |
| 2 | + |
| 3 | +import io.modelcontextprotocol.kotlin.sdk.ReadResourceRequest |
| 4 | +import io.modelcontextprotocol.kotlin.sdk.ReadResourceResult |
| 5 | +import io.modelcontextprotocol.kotlin.sdk.ServerCapabilities |
| 6 | +import io.modelcontextprotocol.kotlin.sdk.SubscribeRequest |
| 7 | +import io.modelcontextprotocol.kotlin.sdk.TextResourceContents |
| 8 | +import io.modelcontextprotocol.kotlin.sdk.UnsubscribeRequest |
| 9 | +import io.modelcontextprotocol.kotlin.sdk.integration.utils.TestUtils.runTest |
| 10 | +import kotlin.test.Ignore |
| 11 | +import org.junit.jupiter.api.Test |
| 12 | +import kotlin.test.assertEquals |
| 13 | +import kotlin.test.assertNotNull |
| 14 | +import kotlin.test.assertTrue |
| 15 | + |
| 16 | +class ResourceIntegrationTest : KotlinTestBase() { |
| 17 | + |
| 18 | + override val port = 3005 |
| 19 | + private val testResourceUri = "test://example.txt" |
| 20 | + private val testResourceName = "Test Resource" |
| 21 | + private val testResourceDescription = "A test resource for integration testing" |
| 22 | + private val testResourceContent = "This is the content of the test resource." |
| 23 | + |
| 24 | + override fun configureServerCapabilities(): ServerCapabilities = ServerCapabilities( |
| 25 | + resources = ServerCapabilities.Resources( |
| 26 | + subscribe = true, |
| 27 | + listChanged = true, |
| 28 | + ), |
| 29 | + ) |
| 30 | + |
| 31 | + override fun configureServer() { |
| 32 | + server.addResource( |
| 33 | + uri = testResourceUri, |
| 34 | + name = testResourceName, |
| 35 | + description = testResourceDescription, |
| 36 | + mimeType = "text/plain", |
| 37 | + ) { request -> |
| 38 | + ReadResourceResult( |
| 39 | + contents = listOf( |
| 40 | + TextResourceContents( |
| 41 | + text = testResourceContent, |
| 42 | + uri = request.uri, |
| 43 | + mimeType = "text/plain", |
| 44 | + ), |
| 45 | + ), |
| 46 | + ) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + fun testListResources() = runTest { |
| 52 | + val result = client.listResources() |
| 53 | + |
| 54 | + assertNotNull(result, "List resources result should not be null") |
| 55 | + assertTrue(result.resources.isNotEmpty(), "Resources list should not be empty") |
| 56 | + |
| 57 | + val testResource = result.resources.find { it.uri == testResourceUri } |
| 58 | + assertNotNull(testResource, "Test resource should be in the list") |
| 59 | + assertEquals(testResourceName, testResource.name, "Resource name should match") |
| 60 | + assertEquals(testResourceDescription, testResource.description, "Resource description should match") |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + fun testReadResource() = runTest { |
| 65 | + val result = client.readResource(ReadResourceRequest(uri = testResourceUri)) |
| 66 | + |
| 67 | + assertNotNull(result, "Read resource result should not be null") |
| 68 | + assertTrue(result.contents.isNotEmpty(), "Resource contents should not be empty") |
| 69 | + |
| 70 | + val content = result.contents.firstOrNull() as? TextResourceContents |
| 71 | + assertNotNull(content, "Resource content should be TextResourceContents") |
| 72 | + assertEquals(testResourceContent, content.text, "Resource content should match") |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + @Ignore("Blocked by https://github.com/modelcontextprotocol/kotlin-sdk/issues/249") |
| 77 | + fun testSubscribeAndUnsubscribe() { |
| 78 | + runTest { |
| 79 | + val subscribeResult = client.subscribeResource(SubscribeRequest(uri = testResourceUri)) |
| 80 | + assertNotNull(subscribeResult, "Subscribe result should not be null") |
| 81 | + |
| 82 | + val unsubscribeResult = client.unsubscribeResource(UnsubscribeRequest(uri = testResourceUri)) |
| 83 | + assertNotNull(unsubscribeResult, "Unsubscribe result should not be null") |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments