|
| 1 | +package com.caplin.integration.datasourcex.spring.internal |
| 2 | + |
| 3 | +import com.caplin.datasource.DataSource |
| 4 | +import com.caplin.integration.datasourcex.reactive.api.ContainerEvent |
| 5 | +import com.caplin.integration.datasourcex.reactive.api.ContainerEvent.RowEvent.Upsert |
| 6 | +import com.caplin.integration.datasourcex.spring.annotations.DataMessageMapping |
| 7 | +import com.caplin.integration.datasourcex.spring.annotations.DataMessageMapping.Type.MAPPING |
| 8 | +import com.caplin.integration.datasourcex.spring.annotations.DataMessageMapping.Type.RECORD_GENERIC |
| 9 | +import com.caplin.integration.datasourcex.spring.annotations.DataService |
| 10 | +import com.caplin.integration.datasourcex.spring.annotations.IngressDestinationVariable |
| 11 | +import com.caplin.integration.datasourcex.spring.annotations.IngressToken.USER_ID |
| 12 | +import io.kotest.assertions.nondeterministic.eventually |
| 13 | +import io.kotest.core.spec.style.FunSpec |
| 14 | +import io.kotest.extensions.spring.SpringExtension |
| 15 | +import io.kotest.matchers.collections.shouldContain |
| 16 | +import io.kotest.matchers.shouldBe |
| 17 | +import kotlin.time.Duration.Companion.seconds |
| 18 | +import kotlinx.coroutines.awaitCancellation |
| 19 | +import kotlinx.coroutines.flow.Flow |
| 20 | +import kotlinx.coroutines.flow.flow |
| 21 | +import kotlinx.coroutines.flow.flowOf |
| 22 | +import kotlinx.coroutines.flow.map |
| 23 | +import org.springframework.beans.factory.annotation.Autowired |
| 24 | +import org.springframework.boot.autoconfigure.ImportAutoConfiguration |
| 25 | +import org.springframework.boot.test.context.SpringBootTest |
| 26 | +import org.springframework.context.annotation.Bean |
| 27 | +import org.springframework.context.annotation.Configuration |
| 28 | +import org.springframework.messaging.handler.annotation.Payload |
| 29 | +import org.springframework.test.context.TestPropertySource |
| 30 | +import tools.jackson.databind.ObjectMapper |
| 31 | +import tools.jackson.module.kotlin.jacksonMapperBuilder |
| 32 | + |
| 33 | +/** |
| 34 | + * End-to-end test of the Spring Boot starter: a real application context wires the |
| 35 | + * autoconfiguration, which binds the `@DataService` controller's `@DataMessageMapping` methods onto |
| 36 | + * a (faked) DataSource. Simulating peer requests drives the whole stack — message handler, argument |
| 37 | + * resolution, the controller, and the return-value handler — across every supported subject shape. |
| 38 | + */ |
| 39 | +@SpringBootTest( |
| 40 | + classes = |
| 41 | + [DataSourceEndToEndTest.TestConfig::class, DataSourceEndToEndTest.TestController::class] |
| 42 | +) |
| 43 | +@ImportAutoConfiguration( |
| 44 | + DataSourceAutoConfiguration::class, |
| 45 | + DataSourceServerAutoConfiguration::class, |
| 46 | +) |
| 47 | +@TestPropertySource(properties = ["caplin.datasource.managed.discovery.hostname=localhost"]) |
| 48 | +class DataSourceEndToEndTest : FunSpec() { |
| 49 | + |
| 50 | + @Autowired private lateinit var fake: FakeDataSource |
| 51 | + |
| 52 | + init { |
| 53 | + extension(SpringExtension()) |
| 54 | + |
| 55 | + test("active JSON subject publishes the controller's flow") { |
| 56 | + fake.request("/price/USD") |
| 57 | + eventually(TIMEOUT) { |
| 58 | + fake.publishedJson shouldContain ("/price/USD" to mapOf("ccy" to "USD", "bid" to "1.23")) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + test("active generic-record subject publishes the controller's flow as record fields") { |
| 63 | + fake.request("/record/EUR") |
| 64 | + eventually(TIMEOUT) { |
| 65 | + fake.publishedRecords shouldContain |
| 66 | + ("/record/EUR" to mapOf("ccy" to "EUR", "bid" to "1.10")) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + test("active mapping subject publishes the remapped subject") { |
| 71 | + fake.request("/map/abc") |
| 72 | + eventually(TIMEOUT) { fake.publishedMappings shouldContain ("/map/abc" to "/real/abc") } |
| 73 | + } |
| 74 | + |
| 75 | + test("a streaming subject publishes each update in order then NotFound on completion") { |
| 76 | + fake.request("/ticks") |
| 77 | + eventually(TIMEOUT) { |
| 78 | + fake.publishedJson.filter { it.first == "/ticks" }.map { it.second } shouldBe |
| 79 | + listOf(mapOf("seq" to "1"), mapOf("seq" to "2"), mapOf("seq" to "3")) |
| 80 | + fake.erroredSubjects shouldContain "/ticks" |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + test("a failing flow publishes a SubjectError and nothing else") { |
| 85 | + fake.request("/broken") |
| 86 | + eventually(TIMEOUT) { fake.erroredSubjects shouldContain "/broken" } |
| 87 | + fake.publishedJson.any { it.first == "/broken" } shouldBe false |
| 88 | + } |
| 89 | + |
| 90 | + test("a container subject publishes element structure and serves row data on request") { |
| 91 | + fake.request("/book/X") |
| 92 | + eventually(TIMEOUT) { |
| 93 | + fake.publishedContainers shouldContain ("/book/X" to listOf("/book/X-items/1")) |
| 94 | + } |
| 95 | + |
| 96 | + fake.request("/book/X-items/1") |
| 97 | + eventually(TIMEOUT) { |
| 98 | + fake.publishedRecords shouldContain ("/book/X-items/1" to mapOf("px" to "100")) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + test("a request-stream channel returns a response derived from the client's message") { |
| 103 | + val responses = fake.openChannel("/echo/42") |
| 104 | + fake.sendToChannel("/echo/42", mapOf("ping" to "1")) |
| 105 | + eventually(TIMEOUT) { responses shouldContain mapOf("ping" to "1", "echoedBy" to "42") } |
| 106 | + } |
| 107 | + |
| 108 | + test("a bidirectional channel responds to each client message") { |
| 109 | + val responses = fake.openChannel("/chat/7") |
| 110 | + fake.sendToChannel("/chat/7", mapOf("m" to "a")) |
| 111 | + fake.sendToChannel("/chat/7", mapOf("m" to "b")) |
| 112 | + eventually(TIMEOUT) { |
| 113 | + responses shouldContain mapOf("m" to "a", "by" to "7") |
| 114 | + responses shouldContain mapOf("m" to "b", "by" to "7") |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + test("a fire-and-forget channel returns an OK acknowledgement") { |
| 119 | + val responses = fake.openChannel("/submit") |
| 120 | + fake.sendToChannel("/submit", mapOf("x" to "1")) |
| 121 | + eventually(TIMEOUT) { responses shouldContain mapOf("Result" to "ok") } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @DataService |
| 126 | + class TestController { |
| 127 | + @DataMessageMapping("/price/{ccy}") |
| 128 | + fun price(@IngressDestinationVariable(token = USER_ID, value = "ccy") ccy: String): Flow<Any> = |
| 129 | + flowOf(mapOf("ccy" to ccy, "bid" to "1.23")) |
| 130 | + |
| 131 | + @DataMessageMapping("/record/{ccy}", type = RECORD_GENERIC) |
| 132 | + fun record( |
| 133 | + @IngressDestinationVariable(token = USER_ID, value = "ccy") ccy: String |
| 134 | + ): Flow<Map<String, String>> = flowOf(mapOf("ccy" to ccy, "bid" to "1.10")) |
| 135 | + |
| 136 | + @DataMessageMapping("/map/{id}", type = MAPPING) |
| 137 | + fun map(@IngressDestinationVariable(token = USER_ID, value = "id") id: String): Flow<String> = |
| 138 | + flowOf("/real/$id") |
| 139 | + |
| 140 | + @DataMessageMapping("/ticks") |
| 141 | + fun ticks(): Flow<Any> = flowOf(mapOf("seq" to "1"), mapOf("seq" to "2"), mapOf("seq" to "3")) |
| 142 | + |
| 143 | + @DataMessageMapping("/broken") fun broken(): Flow<Any> = flow { error("boom") } |
| 144 | + |
| 145 | + @DataMessageMapping("/book/{id}", type = RECORD_GENERIC) |
| 146 | + fun book( |
| 147 | + @IngressDestinationVariable(token = USER_ID, value = "id") id: String |
| 148 | + ): Flow<ContainerEvent<Map<String, String>>> = flow { |
| 149 | + emit(Upsert("1", mapOf("px" to "100"))) |
| 150 | + awaitCancellation() |
| 151 | + } |
| 152 | + |
| 153 | + @DataMessageMapping("/echo/{id}") |
| 154 | + fun echo( |
| 155 | + @IngressDestinationVariable(token = USER_ID, value = "id") id: String, |
| 156 | + @Payload request: Map<String, String>, |
| 157 | + ): Flow<Map<String, String>> = flowOf(request + ("echoedBy" to id)) |
| 158 | + |
| 159 | + @DataMessageMapping("/chat/{id}") |
| 160 | + fun chat( |
| 161 | + @IngressDestinationVariable(token = USER_ID, value = "id") id: String, |
| 162 | + @Payload messages: Flow<Map<String, String>>, |
| 163 | + ): Flow<Map<String, String>> = messages.map { it + ("by" to id) } |
| 164 | + |
| 165 | + @DataMessageMapping("/submit") |
| 166 | + fun submit(@Payload request: Map<String, String>) { |
| 167 | + // fire-and-forget: side effects only; the starter returns an OK acknowledgement |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + @Configuration(proxyBeanMethods = false) |
| 172 | + class TestConfig { |
| 173 | + private val fake = FakeDataSource() |
| 174 | + |
| 175 | + @Bean fun fakeDataSource(): FakeDataSource = fake |
| 176 | + |
| 177 | + @Bean fun dataSource(): DataSource = fake.dataSource |
| 178 | + |
| 179 | + @Bean fun objectMapper(): ObjectMapper = jacksonMapperBuilder().build() |
| 180 | + } |
| 181 | + |
| 182 | + private companion object { |
| 183 | + val TIMEOUT = 5.seconds |
| 184 | + } |
| 185 | +} |
0 commit comments