|
| 1 | +package com.rogervinas |
| 2 | + |
| 3 | +import com.nhaarman.mockitokotlin2.argumentCaptor |
| 4 | +import com.nhaarman.mockitokotlin2.whenever |
| 5 | +import com.rogervinas.tools.BookingService |
| 6 | +import io.modelcontextprotocol.client.McpClient |
| 7 | +import io.modelcontextprotocol.client.McpSyncClient |
| 8 | +import io.modelcontextprotocol.client.transport.HttpClientSseClientTransport |
| 9 | +import io.modelcontextprotocol.spec.McpSchema.CallToolRequest |
| 10 | +import io.modelcontextprotocol.spec.McpSchema.TextContent |
| 11 | +import org.assertj.core.api.Assertions.assertThat |
| 12 | +import org.junit.jupiter.api.AfterAll |
| 13 | +import org.junit.jupiter.api.MethodOrderer |
| 14 | +import org.junit.jupiter.api.Order |
| 15 | +import org.junit.jupiter.api.Test |
| 16 | +import org.junit.jupiter.api.TestInstance |
| 17 | +import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS |
| 18 | +import org.junit.jupiter.api.TestMethodOrder |
| 19 | +import org.mockito.Mockito.doReturn |
| 20 | +import org.springframework.boot.test.context.SpringBootTest |
| 21 | +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT |
| 22 | +import org.springframework.boot.test.web.server.LocalServerPort |
| 23 | +import org.springframework.test.context.bean.override.mockito.MockitoBean |
| 24 | +import java.time.LocalDate |
| 25 | +import java.util.function.Consumer |
| 26 | + |
| 27 | + |
| 28 | +@SpringBootTest(webEnvironment = RANDOM_PORT) |
| 29 | +@TestInstance(PER_CLASS) |
| 30 | +@TestMethodOrder(MethodOrderer.OrderAnnotation::class) |
| 31 | +class McpServerApplicationTest { |
| 32 | + |
| 33 | + @LocalServerPort |
| 34 | + val port: Int = 0 |
| 35 | + |
| 36 | + val client: McpSyncClient by lazy { |
| 37 | + McpClient.sync(HttpClientSseClientTransport("http://localhost:$port")).build().apply { |
| 38 | + initialize() |
| 39 | + ping() |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + @AfterAll |
| 44 | + fun closeClient() { |
| 45 | + client.closeGracefully() |
| 46 | + } |
| 47 | + |
| 48 | + @MockitoBean |
| 49 | + lateinit var bookingService: BookingService |
| 50 | + |
| 51 | + @Test |
| 52 | + @Order(0) |
| 53 | + fun `should list tools`() { |
| 54 | + assertThat(client.listTools().tools).singleElement().satisfies(Consumer { |
| 55 | + assertThat(it.name).isEqualTo("book") |
| 56 | + assertThat(it.description).isEqualTo("make a reservation for accommodation for a given city and date") |
| 57 | + }) |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + fun `should book`() { |
| 62 | + val bookResult = "Booking is done!" |
| 63 | + val cityCaptor = argumentCaptor<String>() |
| 64 | + val checkinDateCaptor = argumentCaptor<LocalDate>() |
| 65 | + val checkoutDateCaptor = argumentCaptor<LocalDate>() |
| 66 | + doReturn(bookResult) |
| 67 | + .whenever(bookingService) |
| 68 | + .book(cityCaptor.capture(), checkinDateCaptor.capture(), checkoutDateCaptor.capture()) |
| 69 | + |
| 70 | + val city = "Barcelona" |
| 71 | + val checkinDate = "2025-04-15" |
| 72 | + val checkoutDate = "2025-04-18" |
| 73 | + val result = client.callTool( |
| 74 | + CallToolRequest( |
| 75 | + "book", |
| 76 | + mapOf( |
| 77 | + "city" to city, |
| 78 | + "checkinDate" to checkinDate, |
| 79 | + "checkoutDate" to checkoutDate |
| 80 | + ) |
| 81 | + ) |
| 82 | + ) |
| 83 | + |
| 84 | + assertThat(result.isError).isFalse() |
| 85 | + assertThat(result.content).singleElement().isInstanceOfSatisfying(TextContent::class.java) { |
| 86 | + // TODO why is text double quoted? |
| 87 | + assertThat(it.text).isEqualTo("\"$bookResult\"") |
| 88 | + } |
| 89 | + assertThat(cityCaptor.allValues).singleElement().isEqualTo(city) |
| 90 | + assertThat(checkinDateCaptor.allValues).singleElement().isEqualTo(LocalDate.parse(checkinDate)) |
| 91 | + assertThat(checkoutDateCaptor.allValues).singleElement().isEqualTo(LocalDate.parse(checkoutDate)) |
| 92 | + } |
| 93 | +} |
0 commit comments