|
| 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