#521 - Add example for Kotlin Coroutines and Flow support.

Original pull request: #522.
This commit is contained in:
Christoph Strobl
2019-08-12 12:22:18 +02:00
committed by Mark Paluch
parent 9080685591
commit 4f0b76ee5d
3 changed files with 111 additions and 0 deletions

View File

@@ -63,3 +63,16 @@ Using the `Criteria` extensions allows to write type-safe queries via an ideomat
operations.find<Person>(Query(Person::firstname isEqualTo "Tyrion"))
```
## Coroutines and Flow support
```kotlin
runBlocking {
operations.find<Person>(Query(where("firstname").isEqualTo("Tyrion"))).awaitSingle()
}
```
```kotlin
runBlocking {
operations.findAll<Person>().asFlow().toList()
}
```

View File

@@ -11,7 +11,15 @@
<artifactId>spring-data-mongodb-kotlin</artifactId>
<name>Spring Data MongoDB - Kotlin features</name>
<properties>
<kotlin-coroutines>1.3.0-RC2</kotlin-coroutines>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
@@ -20,6 +28,21 @@
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-core</artifactId>
<version>${kotlin-coroutines}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-reactor</artifactId>
<version>${kotlin-coroutines}</version>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@@ -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<Person>()).verifyComplete()
}
@Test
fun `find - the coroutine way`() {
StepVerifier.create(operations.insert<Person>().one(Person("Tyrion", "Lannister"))).expectNextCount(1).verifyComplete()
assertThat(
runBlocking {
operations.find<Person>(Query(where("firstname").isEqualTo("Tyrion"))).awaitSingle()
}
).extracting("firstname").isEqualTo("Tyrion")
}
@Test
fun `find - the flow way`() {
StepVerifier.create(operations.insert<Person>().one(Person("Tyrion", "Lannister"))).expectNextCount(1).verifyComplete()
StepVerifier.create(operations.insert<Person>().one(Person("Cersei", "Lannister"))).expectNextCount(1).verifyComplete()
assertThat(
runBlocking {
operations.findAll<Person>().asFlow().toList()
}
).extracting("firstname").containsExactlyInAnyOrder("Tyrion", "Cersei")
}
}