From 43a74cae317d9cae3b837a8bf38591c5417d8fea Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Wed, 3 Apr 2019 08:58:29 +0200 Subject: [PATCH] #86 - Add non-nullable variant to RowsFetchSpec extensions. This commit is a follow-up of gh-63. Original pull request: #86. --- .../r2dbc/function/RowsFetchSpecExtensions.kt | 27 +++++- .../function/RowsFetchSpecExtensionsTests.kt | 96 ++++++++++++++++++- 2 files changed, 116 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/org/springframework/data/r2dbc/function/RowsFetchSpecExtensions.kt b/src/main/kotlin/org/springframework/data/r2dbc/function/RowsFetchSpecExtensions.kt index 88934c96..cb1a1d05 100644 --- a/src/main/kotlin/org/springframework/data/r2dbc/function/RowsFetchSpecExtensions.kt +++ b/src/main/kotlin/org/springframework/data/r2dbc/function/RowsFetchSpecExtensions.kt @@ -16,22 +16,39 @@ package org.springframework.data.r2dbc.function import kotlinx.coroutines.reactive.awaitFirstOrNull +import kotlinx.coroutines.reactive.awaitSingle /** - * Coroutines variant of [RowsFetchSpec.one]. + * Non-nullable Coroutines variant of [RowsFetchSpec.one]. * * @author Sebastien Deleuze */ -suspend fun RowsFetchSpec.awaitOne(): T? +suspend fun RowsFetchSpec.awaitOne(): T + = one().awaitSingle() + +/** + * Nullable Coroutines variant of [RowsFetchSpec.one]. + * + * @author Sebastien Deleuze + */ +suspend fun RowsFetchSpec.awaitOneOrNull(): T? = one().awaitFirstOrNull() /** - * Coroutines variant of [RowsFetchSpec.first]. + * Non-nullable Coroutines variant of [RowsFetchSpec.first]. * * @author Sebastien Deleuze */ -suspend fun RowsFetchSpec.awaitFirst(): T? - = first().awaitFirstOrNull() +suspend fun RowsFetchSpec.awaitFirst(): T + = first().awaitSingle() + +/** + * Nullable Coroutines variant of [RowsFetchSpec.first]. + * + * @author Sebastien Deleuze + */ +suspend fun RowsFetchSpec.awaitFirstOrNull(): T? + = first().awaitFirstOrNull() // TODO Coroutines variant of [RowsFetchSpec.all], depends on [kotlinx.coroutines#254](https://github.com/Kotlin/kotlinx.coroutines/issues/254). // suspend fun RowsFetchSpec.awaitAll() = all()... diff --git a/src/test/kotlin/org/springframework/data/r2dbc/function/RowsFetchSpecExtensionsTests.kt b/src/test/kotlin/org/springframework/data/r2dbc/function/RowsFetchSpecExtensionsTests.kt index 6278572d..78596d4d 100644 --- a/src/test/kotlin/org/springframework/data/r2dbc/function/RowsFetchSpecExtensionsTests.kt +++ b/src/test/kotlin/org/springframework/data/r2dbc/function/RowsFetchSpecExtensionsTests.kt @@ -20,8 +20,10 @@ import io.mockk.mockk import io.mockk.verify import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatExceptionOfType import org.junit.Test import reactor.core.publisher.Mono +import java.lang.NullPointerException /** * Unit tests for [RowsFetchSpec] extensions. @@ -31,7 +33,7 @@ import reactor.core.publisher.Mono class RowsFetchSpecExtensionsTests { @Test // gh-63 - fun awaitOne() { + fun awaitOneWithValue() { val spec = mockk>() every { spec.one() } returns Mono.just("foo") @@ -46,7 +48,52 @@ class RowsFetchSpecExtensionsTests { } @Test // gh-63 - fun awaitFirst() { + fun awaitOneWithNull() { + + val spec = mockk>() + every { spec.one() } returns Mono.empty() + + assertThatExceptionOfType(NoSuchElementException::class.java).isThrownBy { + runBlocking { spec.awaitOne() } + } + + verify { + spec.one() + } + } + + @Test // gh-63 + fun awaitOneOrNullWithValue() { + + val spec = mockk>() + every { spec.one() } returns Mono.just("foo") + + runBlocking { + assertThat(spec.awaitOneOrNull()).isEqualTo("foo") + } + + verify { + spec.one() + } + } + + @Test // gh-63 + fun awaitOneOrNullWithNull() { + + val spec = mockk>() + every { spec.one() } returns Mono.empty() + + runBlocking { + assertThat(spec.awaitOneOrNull()).isNull() + } + + verify { + spec.one() + } + } + + @Test // gh-63 + fun awaitFirstWithValue() { val spec = mockk>() every { spec.first() } returns Mono.just("foo") @@ -59,4 +106,49 @@ class RowsFetchSpecExtensionsTests { spec.first() } } + + @Test // gh-63 + fun awaitFirstWithNull() { + + val spec = mockk>() + every { spec.first() } returns Mono.empty() + + assertThatExceptionOfType(NoSuchElementException::class.java).isThrownBy { + runBlocking { spec.awaitFirst() } + } + + verify { + spec.first() + } + } + + @Test // gh-63 + fun awaitFirstOrNullWithValue() { + + val spec = mockk>() + every { spec.first() } returns Mono.just("foo") + + runBlocking { + assertThat(spec.awaitFirstOrNull()).isEqualTo("foo") + } + + verify { + spec.first() + } + } + + @Test // gh-63 + fun awaitFirstOrNullWithNull() { + + val spec = mockk>() + every { spec.first() } returns Mono.empty() + + runBlocking { + assertThat(spec.awaitFirstOrNull()).isNull() + } + + verify { + spec.first() + } + } }