Skip to content

Commit 5a5a5fe

Browse files
committed
#290 - Add Kotlin extensions for fluent R2dbcEntityTemplate API.
1 parent 1e64c64 commit 5a5a5fe

9 files changed

+601
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2018-2020 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 org.springframework.data.r2dbc.core
17+
18+
import kotlinx.coroutines.reactive.awaitSingle
19+
20+
/**
21+
* Extensions for [ReactiveDeleteOperation].
22+
*
23+
* @author Mark Paluch
24+
* @since 1.1
25+
*/
26+
27+
/**
28+
* Extension for [ReactiveDeleteOperation.delete] leveraging reified type parameters.
29+
*/
30+
inline fun <reified T : Any> ReactiveDeleteOperation.delete(): ReactiveDeleteOperation.ReactiveDelete =
31+
delete(T::class.java)
32+
33+
/**
34+
* Coroutines variant of [ReactiveDeleteOperation.TerminatingDelete.all].
35+
*/
36+
suspend fun ReactiveDeleteOperation.TerminatingDelete.allAndAwait(): Int =
37+
all().awaitSingle()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2020 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 org.springframework.data.r2dbc.core
17+
18+
import kotlinx.coroutines.reactive.awaitSingle
19+
20+
/**
21+
* Extensions for [ReactiveInsertOperation].
22+
*
23+
* @author Mark Paluch
24+
* @since 1.1
25+
*/
26+
27+
/**
28+
* Extension for [ReactiveInsertOperation.insert] leveraging reified type parameters.
29+
*/
30+
inline fun <reified T : Any> ReactiveInsertOperation.insert(): ReactiveInsertOperation.ReactiveInsert<T> =
31+
insert(T::class.java)
32+
33+
/**
34+
* Coroutines variant of [ReactiveInsertOperation.TerminatingInsert.using].
35+
*/
36+
suspend inline fun <reified T : Any> ReactiveInsertOperation.TerminatingInsert<T>.usingAndAwait(o: T): T =
37+
using(o).awaitSingle()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2020 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 org.springframework.data.r2dbc.core
17+
18+
import kotlinx.coroutines.flow.Flow
19+
import kotlinx.coroutines.reactive.asFlow
20+
import kotlinx.coroutines.reactive.awaitFirstOrNull
21+
import kotlinx.coroutines.reactive.awaitSingle
22+
23+
/**
24+
* Extensions for [ReactiveSelectOperation].
25+
*
26+
* @author Mark Paluch
27+
* @since 1.1
28+
*/
29+
30+
/**
31+
* Extension for [ReactiveSelectOperation.select] leveraging reified type parameters.
32+
*/
33+
inline fun <reified T : Any> ReactiveSelectOperation.select(): ReactiveSelectOperation.ReactiveSelect<T> =
34+
select(T::class.java)
35+
36+
/**
37+
* Extension for [ReactiveSelectOperation.SelectWithProjection. as] leveraging reified type parameters.
38+
*/
39+
inline fun <reified T : Any> ReactiveSelectOperation.SelectWithProjection<*>.asType(): ReactiveSelectOperation.SelectWithQuery<T> =
40+
`as`(T::class.java)
41+
42+
/**
43+
* Non-nullable Coroutines variant of [ReactiveSelectOperation.TerminatingSelect.one].
44+
*/
45+
suspend inline fun <reified T : Any> ReactiveSelectOperation.TerminatingSelect<T>.awaitOne(): T =
46+
one().awaitSingle()
47+
48+
/**
49+
* Nullable Coroutines variant of [ReactiveSelectOperation.TerminatingSelect.one].
50+
*/
51+
suspend inline fun <reified T : Any> ReactiveSelectOperation.TerminatingSelect<T>.awaitOneOrNull(): T? =
52+
one().awaitFirstOrNull()
53+
54+
/**
55+
* Non-nullable Coroutines variant of [ReactiveSelectOperation.TerminatingSelect.first].
56+
*/
57+
suspend inline fun <reified T : Any> ReactiveSelectOperation.TerminatingSelect<T>.awaitFirst(): T =
58+
first().awaitSingle()
59+
60+
/**
61+
* Nullable Coroutines variant of [ReactiveSelectOperation.TerminatingSelect.first].
62+
*/
63+
suspend inline fun <reified T : Any> ReactiveSelectOperation.TerminatingSelect<T>.awaitFirstOrNull(): T? =
64+
first().awaitFirstOrNull()
65+
66+
/**
67+
* Coroutines variant of [ReactiveSelectOperation.TerminatingSelect.count].
68+
*/
69+
suspend fun <T : Any> ReactiveSelectOperation.TerminatingSelect<T>.awaitCount(): Long =
70+
count().awaitSingle()
71+
72+
/**
73+
* Coroutines variant of [ReactiveSelectOperation.TerminatingSelect.exists].
74+
*/
75+
suspend fun <T : Any> ReactiveSelectOperation.TerminatingSelect<T>.awaitExists(): Boolean =
76+
exists().awaitSingle()
77+
78+
/**
79+
* Coroutines [Flow] variant of [ReactiveSelectOperation.TerminatingSelect.all].
80+
*/
81+
fun <T : Any> ReactiveSelectOperation.TerminatingSelect<T>.flow(): Flow<T> =
82+
all().asFlow()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2020 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 org.springframework.data.r2dbc.core
17+
18+
import kotlinx.coroutines.reactive.awaitSingle
19+
import org.springframework.data.r2dbc.query.Update
20+
21+
/**
22+
* Extensions for [ReactiveUpdateOperation].
23+
*
24+
* @author Mark Paluch
25+
* @since 1.1
26+
*/
27+
28+
/**
29+
* Extension for [ReactiveUpdateOperation.update] leveraging reified type parameters.
30+
*/
31+
inline fun <reified T : Any> ReactiveUpdateOperation.update(): ReactiveUpdateOperation.ReactiveUpdate =
32+
update(T::class.java)
33+
34+
/**
35+
* Coroutines variant of [ReactiveUpdateOperation.TerminatingUpdate.apply].
36+
*/
37+
suspend fun ReactiveUpdateOperation.TerminatingUpdate.applyAndAwait(update: Update): Int = apply(update).awaitSingle()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright 2020 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 org.springframework.data.r2dbc.core
17+
18+
data class Person(val id: String)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2020 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 org.springframework.data.r2dbc.core
17+
18+
import io.mockk.every
19+
import io.mockk.mockk
20+
import io.mockk.verify
21+
import kotlinx.coroutines.runBlocking
22+
import org.assertj.core.api.Assertions.assertThat
23+
import org.junit.Test
24+
import reactor.core.publisher.Mono
25+
26+
/**
27+
* Unit tests for [ReactiveDeleteOperationExtensions].
28+
*
29+
* @author Mark Paluch
30+
*/
31+
class ReactiveDeleteOperationExtensionsUnitTests {
32+
33+
val operations = mockk<FluentR2dbcOperations>(relaxed = true)
34+
35+
@Test // gh-290
36+
fun `delete() with reified type parameter extension should call its Java counterpart`() {
37+
38+
operations.delete<Person>()
39+
verify { operations.delete(Person::class.java) }
40+
}
41+
42+
@Test // gh-290
43+
fun allAndAwait() {
44+
45+
val delete = mockk<ReactiveDeleteOperation.TerminatingDelete>()
46+
every { delete.all() } returns Mono.just(42)
47+
48+
runBlocking {
49+
assertThat(delete.allAndAwait()).isEqualTo(42)
50+
}
51+
52+
verify {
53+
delete.all()
54+
}
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2020 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 org.springframework.data.r2dbc.core
17+
18+
import io.mockk.every
19+
import io.mockk.mockk
20+
import io.mockk.verify
21+
import kotlinx.coroutines.runBlocking
22+
import org.assertj.core.api.Assertions.assertThat
23+
import org.junit.Test
24+
import reactor.core.publisher.Mono
25+
26+
/**
27+
* Unit tests for [ReactiveInsertOperationExtensions].
28+
*
29+
* @author Mark Paluch
30+
*/
31+
class ReactiveInsertOperationExtensionsUnitTests {
32+
33+
val operations = mockk<FluentR2dbcOperations>(relaxed = true)
34+
35+
@Test // gh-290
36+
fun `insert() with reified type parameter extension should call its Java counterpart`() {
37+
38+
operations.insert<Person>()
39+
verify { operations.insert(Person::class.java) }
40+
}
41+
42+
@Test // gh-290
43+
fun oneAndAwait() {
44+
45+
val insert = mockk<ReactiveInsertOperation.TerminatingInsert<String>>()
46+
every { insert.using("foo") } returns Mono.just("bar")
47+
48+
runBlocking {
49+
assertThat(insert.usingAndAwait("foo")).isEqualTo("bar")
50+
}
51+
52+
verify {
53+
insert.using("foo")
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)