Skip to content

Commit 8d1e0e1

Browse files
FelixDesschauder
authored andcommitted
Add Kotlin extensions to JdbcAggregateOperations.
Closes #1961 Original pull request #1962
1 parent 73d6788 commit 8d1e0e1

File tree

3 files changed

+307
-0
lines changed

3 files changed

+307
-0
lines changed

Diff for: spring-data-jdbc/pom.xml

+20
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,13 @@
189189
<scope>test</scope>
190190
</dependency>
191191

192+
<dependency>
193+
<groupId>io.mockk</groupId>
194+
<artifactId>mockk-jvm</artifactId>
195+
<version>${mockk}</version>
196+
<scope>test</scope>
197+
</dependency>
198+
192199
<!-- Testcontainers -->
193200

194201
<dependency>
@@ -239,6 +246,19 @@
239246
<version>${hikari.version}</version>
240247
<scope>test</scope>
241248
</dependency>
249+
250+
<!-- Kotlin extension -->
251+
<dependency>
252+
<groupId>org.jetbrains.kotlin</groupId>
253+
<artifactId>kotlin-stdlib</artifactId>
254+
<optional>true</optional>
255+
</dependency>
256+
257+
<dependency>
258+
<groupId>org.jetbrains.kotlin</groupId>
259+
<artifactId>kotlin-reflect</artifactId>
260+
<optional>true</optional>
261+
</dependency>
242262
</dependencies>
243263

244264
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright 2017-2024 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.jdbc.core
17+
18+
import org.springframework.data.domain.Page
19+
import org.springframework.data.domain.Pageable
20+
import org.springframework.data.domain.Sort
21+
import org.springframework.data.relational.core.query.Query
22+
23+
import java.util.Optional
24+
25+
/**
26+
* Extensions for [JdbcAggregateOperations].
27+
*
28+
* @author Felix Desyatirikov
29+
* @since 3.5
30+
*/
31+
32+
/**
33+
* Extension for [JdbcAggregateOperations.count].
34+
*/
35+
inline fun <reified T> JdbcAggregateOperations.count(): Long =
36+
count(T::class.java)
37+
38+
/**
39+
* Extension for [JdbcAggregateOperations.count] with a query.
40+
*/
41+
inline fun <reified T> JdbcAggregateOperations.count(query: Query): Long =
42+
count(query, T::class.java)
43+
44+
/**
45+
* Extension for [JdbcAggregateOperations.exists].
46+
*/
47+
inline fun <reified T> JdbcAggregateOperations.exists(query: Query): Boolean =
48+
exists(query, T::class.java)
49+
50+
/**
51+
* Extension for [JdbcAggregateOperations.existsById].
52+
*/
53+
inline fun <reified T> JdbcAggregateOperations.existsById(id: Any): Boolean =
54+
existsById(id, T::class.java)
55+
56+
/**
57+
* Extension for [JdbcAggregateOperations.findById].
58+
*/
59+
inline fun <reified T> JdbcAggregateOperations.findById(id: Any): T? =
60+
findById(id, T::class.java)
61+
62+
/**
63+
* Extension for [JdbcAggregateOperations.findAllById].
64+
*/
65+
inline fun <reified T> JdbcAggregateOperations.findAllById(ids: Iterable<*>): List<T> =
66+
findAllById(ids, T::class.java)
67+
68+
/**
69+
* Extension for [JdbcAggregateOperations.findAll].
70+
*/
71+
inline fun <reified T> JdbcAggregateOperations.findAll(): List<T> =
72+
findAll(T::class.java)
73+
74+
/**
75+
* Extension for [JdbcAggregateOperations.findAll] with sorting.
76+
*/
77+
inline fun <reified T> JdbcAggregateOperations.findAll(sort: Sort): List<T> =
78+
findAll(T::class.java, sort)
79+
80+
/**
81+
* Extension for [JdbcAggregateOperations.findAll] with pagination.
82+
*/
83+
inline fun <reified T> JdbcAggregateOperations.findAll(pageable: Pageable): Page<T> =
84+
findAll(T::class.java, pageable)
85+
86+
/**
87+
* Extension for [JdbcAggregateOperations.findOne] with a query.
88+
*/
89+
inline fun <reified T> JdbcAggregateOperations.findOne(query: Query): Optional<T> =
90+
findOne(query, T::class.java)
91+
92+
/**
93+
* Extension for [JdbcAggregateOperations.findAll] with a query.
94+
*/
95+
inline fun <reified T> JdbcAggregateOperations.findAll(query: Query): List<T> =
96+
findAll(query, T::class.java)
97+
98+
/**
99+
* Extension for [JdbcAggregateOperations.findAll] with query and pagination.
100+
*/
101+
inline fun <reified T> JdbcAggregateOperations.findAll(query: Query, pageable: Pageable): Page<T> =
102+
findAll(query, T::class.java, pageable)
103+
104+
/**
105+
* Extension for [JdbcAggregateOperations.deleteById].
106+
*/
107+
inline fun <reified T> JdbcAggregateOperations.deleteById(id: Any): Unit =
108+
deleteById(id, T::class.java)
109+
110+
/**
111+
* Extension for [JdbcAggregateOperations.deleteAllById].
112+
*/
113+
inline fun <reified T> JdbcAggregateOperations.deleteAllById(ids: Iterable<*>): Unit =
114+
deleteAllById(ids, T::class.java)
115+
116+
/**
117+
* Extension for [JdbcAggregateOperations.deleteAll].
118+
*/
119+
inline fun <reified T> JdbcAggregateOperations.deleteAll(): Unit =
120+
deleteAll(T::class.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
* Copyright 2020-2024 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.jdbc.core
17+
18+
import io.mockk.mockk
19+
import io.mockk.verify
20+
import org.junit.Test
21+
import org.springframework.data.domain.Pageable
22+
import org.springframework.data.domain.Sort
23+
import org.springframework.data.jdbc.testing.TestClass
24+
import org.springframework.data.relational.core.query.Query
25+
26+
/**
27+
* Unit tests for [JdbcAggregateOperations].
28+
*
29+
* @author Felix Desyatirikov
30+
*/
31+
32+
class JdbcAggregateOperationsExtensionsTests {
33+
34+
val operations = mockk<JdbcAggregateOperations>(relaxed = true)
35+
36+
@Test // gh-1961
37+
fun `count with reified type parameter extension should call its Java counterpart`() {
38+
operations.count<TestClass>()
39+
verify { operations.count(TestClass::class.java) }
40+
}
41+
42+
@Test // gh-1961
43+
fun `count(Query) with reified type parameter extension should call its Java counterpart`() {
44+
val query = mockk<Query>(relaxed = true)
45+
operations.count<TestClass>(query)
46+
verify {
47+
operations.count(query, TestClass::class.java)
48+
}
49+
}
50+
51+
@Test // gh-1961
52+
fun `exists(Query) with reified type parameter extension should call its Java counterpart`() {
53+
val query = mockk<Query>(relaxed = true)
54+
operations.exists<TestClass>(query)
55+
verify {
56+
operations.exists(query, TestClass::class.java)
57+
}
58+
}
59+
60+
@Test // gh-1961
61+
fun `existsById(id) with reified type parameter extension should call its Java counterpart`() {
62+
val id = 1L
63+
operations.existsById<TestClass>(id)
64+
verify {
65+
operations.existsById(id, TestClass::class.java)
66+
}
67+
}
68+
69+
@Test // gh-1961
70+
fun `findById(id) with reified type parameter extension should call its Java counterpart`() {
71+
val id = 1L
72+
operations.findById<TestClass>(id)
73+
verify {
74+
operations.findById(id, TestClass::class.java)
75+
}
76+
}
77+
78+
@Test // gh-1961
79+
fun `findAllById(ids) with reified type parameter extension should call its Java counterpart`() {
80+
val ids = listOf(1L, 2L)
81+
operations.findAllById<TestClass>(ids)
82+
verify {
83+
operations.findAllById(ids, TestClass::class.java)
84+
}
85+
}
86+
87+
@Test // gh-1961
88+
fun `findAll() with reified type parameter extension should call its Java counterpart`() {
89+
operations.findAll<TestClass>()
90+
verify {
91+
operations.findAll(TestClass::class.java)
92+
}
93+
}
94+
95+
@Test // gh-1961
96+
fun `findAll(Sort) with reified type parameter extension should call its Java counterpart`() {
97+
val sort = mockk<Sort>(relaxed = true)
98+
operations.findAll<TestClass>(sort)
99+
verify {
100+
operations.findAll(TestClass::class.java, sort)
101+
}
102+
}
103+
104+
@Test // gh-1961
105+
fun `findAll(Pageable) with reified type parameter extension should call its Java counterpart`() {
106+
val pageable = mockk<Pageable>(relaxed = true)
107+
operations.findAll<TestClass>(pageable)
108+
verify {
109+
operations.findAll(TestClass::class.java, pageable)
110+
}
111+
}
112+
113+
@Test // gh-1961
114+
fun `findOne(Query) with reified type parameter extension should call its Java counterpart`() {
115+
val query = mockk<Query>(relaxed = true)
116+
operations.findOne<TestClass>(query)
117+
verify {
118+
operations.findOne(query, TestClass::class.java)
119+
}
120+
}
121+
122+
@Test // gh-1961
123+
fun `findAll(Query) with reified type parameter extension should call its Java counterpart`() {
124+
val query = mockk<Query>(relaxed = true)
125+
operations.findAll<TestClass>(query)
126+
verify {
127+
operations.findAll(query, TestClass::class.java)
128+
}
129+
}
130+
131+
132+
@Test // gh-1961
133+
fun `findAll(Query, Pageable) with reified type parameter extension should call its Java counterpart`() {
134+
val query = mockk<Query>(relaxed = true)
135+
val pageable = mockk<Pageable>(relaxed = true)
136+
operations.findAll<TestClass>(query, pageable)
137+
verify {
138+
operations.findAll(query, TestClass::class.java, pageable)
139+
}
140+
}
141+
142+
@Test // gh-1961
143+
fun `deleteById(id) with reified type parameter extension should call its Java counterpart`() {
144+
val id = 1L
145+
operations.deleteById<TestClass>(id)
146+
verify {
147+
operations.deleteById(id, TestClass::class.java)
148+
}
149+
}
150+
151+
@Test // gh-1961
152+
fun `deleteAllById(ids) with reified type parameter extension should call its Java counterpart`() {
153+
val ids = listOf(1L, 2L)
154+
operations.deleteAllById<TestClass>(ids)
155+
verify {
156+
operations.deleteAllById(ids, TestClass::class.java)
157+
}
158+
}
159+
160+
@Test // gh-1961
161+
fun `deleteAll(ids) with reified type parameter extension should call its Java counterpart`() {
162+
operations.deleteAll<TestClass>()
163+
verify {
164+
operations.deleteAll(TestClass::class.java)
165+
}
166+
}
167+
}

0 commit comments

Comments
 (0)