Skip to content

Commit 4f0b76e

Browse files
christophstroblmp911de
authored andcommitted
#521 - Add example for Kotlin Coroutines and Flow support.
Original pull request: #522.
1 parent 9080685 commit 4f0b76e

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

mongodb/kotlin/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,16 @@ Using the `Criteria` extensions allows to write type-safe queries via an ideomat
6363
operations.find<Person>(Query(Person::firstname isEqualTo "Tyrion"))
6464
```
6565

66+
## Coroutines and Flow support
67+
68+
```kotlin
69+
runBlocking {
70+
operations.find<Person>(Query(where("firstname").isEqualTo("Tyrion"))).awaitSingle()
71+
}
72+
```
73+
74+
```kotlin
75+
runBlocking {
76+
operations.findAll<Person>().asFlow().toList()
77+
}
78+
```

mongodb/kotlin/pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@
1111
<artifactId>spring-data-mongodb-kotlin</artifactId>
1212
<name>Spring Data MongoDB - Kotlin features</name>
1313

14+
<properties>
15+
<kotlin-coroutines>1.3.0-RC2</kotlin-coroutines>
16+
</properties>
17+
1418
<dependencies>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
22+
</dependency>
1523
<dependency>
1624
<groupId>org.jetbrains.kotlin</groupId>
1725
<artifactId>kotlin-stdlib-jdk8</artifactId>
@@ -20,6 +28,21 @@
2028
<groupId>org.jetbrains.kotlin</groupId>
2129
<artifactId>kotlin-reflect</artifactId>
2230
</dependency>
31+
<dependency>
32+
<groupId>org.jetbrains.kotlinx</groupId>
33+
<artifactId>kotlinx-coroutines-core</artifactId>
34+
<version>${kotlin-coroutines}</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.jetbrains.kotlinx</groupId>
38+
<artifactId>kotlinx-coroutines-reactor</artifactId>
39+
<version>${kotlin-coroutines}</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>io.projectreactor</groupId>
43+
<artifactId>reactor-test</artifactId>
44+
<scope>test</scope>
45+
</dependency>
2346
</dependencies>
2447

2548
<build>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.mongodb.people
17+
18+
import kotlinx.coroutines.flow.toList
19+
import kotlinx.coroutines.reactive.awaitSingle
20+
import kotlinx.coroutines.reactive.flow.asFlow
21+
import kotlinx.coroutines.runBlocking
22+
import org.assertj.core.api.Assertions.assertThat
23+
import org.junit.Before
24+
import org.junit.Test
25+
import org.junit.runner.RunWith
26+
import org.springframework.beans.factory.annotation.Autowired
27+
import org.springframework.boot.test.context.SpringBootTest
28+
import org.springframework.data.mongodb.core.*
29+
import org.springframework.data.mongodb.core.query.Criteria.where
30+
import org.springframework.data.mongodb.core.query.Query
31+
import org.springframework.data.mongodb.core.query.isEqualTo
32+
import org.springframework.test.context.junit4.SpringRunner
33+
import reactor.test.StepVerifier
34+
35+
/**
36+
*
37+
* @author Christoph Strobl
38+
*/
39+
@RunWith(SpringRunner::class)
40+
@SpringBootTest
41+
class FlowAndCoroutinesTests {
42+
43+
@Autowired
44+
lateinit var operations: ReactiveMongoOperations
45+
46+
@Before
47+
fun before() {
48+
StepVerifier.create(operations.dropCollection<Person>()).verifyComplete()
49+
}
50+
51+
@Test
52+
fun `find - the coroutine way`() {
53+
54+
StepVerifier.create(operations.insert<Person>().one(Person("Tyrion", "Lannister"))).expectNextCount(1).verifyComplete()
55+
56+
assertThat(
57+
runBlocking {
58+
operations.find<Person>(Query(where("firstname").isEqualTo("Tyrion"))).awaitSingle()
59+
}
60+
).extracting("firstname").isEqualTo("Tyrion")
61+
}
62+
63+
@Test
64+
fun `find - the flow way`() {
65+
66+
StepVerifier.create(operations.insert<Person>().one(Person("Tyrion", "Lannister"))).expectNextCount(1).verifyComplete()
67+
StepVerifier.create(operations.insert<Person>().one(Person("Cersei", "Lannister"))).expectNextCount(1).verifyComplete()
68+
69+
assertThat(
70+
runBlocking {
71+
operations.findAll<Person>().asFlow().toList()
72+
}
73+
).extracting("firstname").containsExactlyInAnyOrder("Tyrion", "Cersei")
74+
}
75+
}

0 commit comments

Comments
 (0)