From 7403651ba36552d55f81e3c5769c08f081a285cf Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Mon, 15 Jul 2019 14:17:39 +0200 Subject: [PATCH] #103 - Coroutines extensions throw proper EmptyResultDataAccessException. RowsFetchSpec.awaitOne() and RowsFetchSpec.awaitFirst() now throw EmptyResultDataAccessException instead of NoSuchElementException to consistently use Spring DAO exceptions. --- .../data/r2dbc/core/RowsFetchSpecExtensions.kt | 14 ++++++++------ .../r2dbc/core/RowsFetchSpecExtensionsTests.kt | 10 ++++++---- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/org/springframework/data/r2dbc/core/RowsFetchSpecExtensions.kt b/src/main/kotlin/org/springframework/data/r2dbc/core/RowsFetchSpecExtensions.kt index 69fdb22..20f37b9 100644 --- a/src/main/kotlin/org/springframework/data/r2dbc/core/RowsFetchSpecExtensions.kt +++ b/src/main/kotlin/org/springframework/data/r2dbc/core/RowsFetchSpecExtensions.kt @@ -18,16 +18,17 @@ package org.springframework.data.r2dbc.core import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.reactive.awaitFirstOrNull -import kotlinx.coroutines.reactive.awaitSingle import kotlinx.coroutines.reactive.flow.asFlow +import org.springframework.dao.EmptyResultDataAccessException /** * Non-nullable Coroutines variant of [RowsFetchSpec.one]. * * @author Sebastien Deleuze */ -suspend fun RowsFetchSpec.awaitOne(): T = - one().awaitSingle() +suspend fun RowsFetchSpec.awaitOne(): T { + return one().awaitFirstOrNull() ?: throw EmptyResultDataAccessException(1) +} /** * Nullable Coroutines variant of [RowsFetchSpec.one]. @@ -42,8 +43,9 @@ suspend fun RowsFetchSpec.awaitOneOrNull(): T? = * * @author Sebastien Deleuze */ -suspend fun RowsFetchSpec.awaitFirst(): T = - first().awaitSingle() +suspend fun RowsFetchSpec.awaitFirst(): T { + return first().awaitFirstOrNull() ?: throw EmptyResultDataAccessException(1) +} /** * Nullable Coroutines variant of [RowsFetchSpec.first]. @@ -62,4 +64,4 @@ suspend fun RowsFetchSpec.awaitFirstOrNull(): T? = * @author Sebastien Deleuze */ @FlowPreview -fun RowsFetchSpec.flow(batchSize: Int = 1): Flow = all().asFlow(batchSize) +fun RowsFetchSpec.flow(batchSize: Int = 1): Flow = all().asFlow(batchSize) diff --git a/src/test/kotlin/org/springframework/data/r2dbc/core/RowsFetchSpecExtensionsTests.kt b/src/test/kotlin/org/springframework/data/r2dbc/core/RowsFetchSpecExtensionsTests.kt index 38cc646..ff2b614 100644 --- a/src/test/kotlin/org/springframework/data/r2dbc/core/RowsFetchSpecExtensionsTests.kt +++ b/src/test/kotlin/org/springframework/data/r2dbc/core/RowsFetchSpecExtensionsTests.kt @@ -24,6 +24,7 @@ import kotlinx.coroutines.runBlocking import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatExceptionOfType import org.junit.Test +import org.springframework.dao.EmptyResultDataAccessException import reactor.core.publisher.Flux import reactor.core.publisher.Mono @@ -31,6 +32,7 @@ import reactor.core.publisher.Mono * Unit tests for [RowsFetchSpec] extensions. * * @author Sebastien Deleuze + * @author Mark Paluch */ class RowsFetchSpecExtensionsTests { @@ -49,13 +51,13 @@ class RowsFetchSpecExtensionsTests { } } - @Test // gh-63 + @Test // gh-63, gh-103 fun awaitOneWithNull() { val spec = mockk>() every { spec.one() } returns Mono.empty() - assertThatExceptionOfType(NoSuchElementException::class.java).isThrownBy { + assertThatExceptionOfType(EmptyResultDataAccessException::class.java).isThrownBy { runBlocking { spec.awaitOne() } } @@ -109,13 +111,13 @@ class RowsFetchSpecExtensionsTests { } } - @Test // gh-63 + @Test // gh-63, gh-103 fun awaitFirstWithNull() { val spec = mockk>() every { spec.first() } returns Mono.empty() - assertThatExceptionOfType(NoSuchElementException::class.java).isThrownBy { + assertThatExceptionOfType(EmptyResultDataAccessException::class.java).isThrownBy { runBlocking { spec.awaitFirst() } }