diff --git a/mongodb/kotlin/README.md b/mongodb/kotlin/README.md index ff01904d..4af0900c 100644 --- a/mongodb/kotlin/README.md +++ b/mongodb/kotlin/README.md @@ -63,3 +63,16 @@ Using the `Criteria` extensions allows to write type-safe queries via an ideomat operations.find(Query(Person::firstname isEqualTo "Tyrion")) ``` +## Coroutines and Flow support + +```kotlin +runBlocking { + operations.find(Query(where("firstname").isEqualTo("Tyrion"))).awaitSingle() +} +``` + +```kotlin +runBlocking { + operations.findAll().asFlow().toList() +} +``` diff --git a/mongodb/kotlin/pom.xml b/mongodb/kotlin/pom.xml index e2f6f366..e5d01b66 100644 --- a/mongodb/kotlin/pom.xml +++ b/mongodb/kotlin/pom.xml @@ -11,7 +11,15 @@ spring-data-mongodb-kotlin Spring Data MongoDB - Kotlin features + + 1.3.0-RC2 + + + + org.springframework.boot + spring-boot-starter-data-mongodb-reactive + org.jetbrains.kotlin kotlin-stdlib-jdk8 @@ -20,6 +28,21 @@ org.jetbrains.kotlin kotlin-reflect + + org.jetbrains.kotlinx + kotlinx-coroutines-core + ${kotlin-coroutines} + + + org.jetbrains.kotlinx + kotlinx-coroutines-reactor + ${kotlin-coroutines} + + + io.projectreactor + reactor-test + test + diff --git a/mongodb/kotlin/src/test/kotlin/example/springdata/mongodb/people/FlowAndCoroutinesTests.kt b/mongodb/kotlin/src/test/kotlin/example/springdata/mongodb/people/FlowAndCoroutinesTests.kt new file mode 100644 index 00000000..410bc13c --- /dev/null +++ b/mongodb/kotlin/src/test/kotlin/example/springdata/mongodb/people/FlowAndCoroutinesTests.kt @@ -0,0 +1,75 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example.springdata.mongodb.people + +import kotlinx.coroutines.flow.toList +import kotlinx.coroutines.reactive.awaitSingle +import kotlinx.coroutines.reactive.flow.asFlow +import kotlinx.coroutines.runBlocking +import org.assertj.core.api.Assertions.assertThat +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.data.mongodb.core.* +import org.springframework.data.mongodb.core.query.Criteria.where +import org.springframework.data.mongodb.core.query.Query +import org.springframework.data.mongodb.core.query.isEqualTo +import org.springframework.test.context.junit4.SpringRunner +import reactor.test.StepVerifier + +/** + * + * @author Christoph Strobl + */ +@RunWith(SpringRunner::class) +@SpringBootTest +class FlowAndCoroutinesTests { + + @Autowired + lateinit var operations: ReactiveMongoOperations + + @Before + fun before() { + StepVerifier.create(operations.dropCollection()).verifyComplete() + } + + @Test + fun `find - the coroutine way`() { + + StepVerifier.create(operations.insert().one(Person("Tyrion", "Lannister"))).expectNextCount(1).verifyComplete() + + assertThat( + runBlocking { + operations.find(Query(where("firstname").isEqualTo("Tyrion"))).awaitSingle() + } + ).extracting("firstname").isEqualTo("Tyrion") + } + + @Test + fun `find - the flow way`() { + + StepVerifier.create(operations.insert().one(Person("Tyrion", "Lannister"))).expectNextCount(1).verifyComplete() + StepVerifier.create(operations.insert().one(Person("Cersei", "Lannister"))).expectNextCount(1).verifyComplete() + + assertThat( + runBlocking { + operations.findAll().asFlow().toList() + } + ).extracting("firstname").containsExactlyInAnyOrder("Tyrion", "Cersei") + } +}