Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Kotlin extensions to JdbcAggregateOperations #1962

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.mockk</groupId>
<artifactId>mockk-jvm</artifactId>
<version>${mockk}</version>
<scope>test</scope>
</dependency>

<!-- Testcontainers -->

<dependency>
Expand Down Expand Up @@ -239,6 +246,19 @@
<version>${hikari.version}</version>
<scope>test</scope>
</dependency>

<!-- Kotlin extension -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright 2017-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jdbc.core

import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Sort
import org.springframework.data.relational.core.query.Query

import java.util.Optional

/**
* Extensions for [JdbcAggregateOperations].
*
* @author Felix Desyatirikov
* @since 3.5
*/

/**
* Extension for [JdbcAggregateOperations.count].
*/
inline fun <reified T> JdbcAggregateOperations.count(): Long =
count(T::class.java)

/**
* Extension for [JdbcAggregateOperations.count] with a query.
*/
inline fun <reified T> JdbcAggregateOperations.count(query: Query): Long =
count(query, T::class.java)

/**
* Extension for [JdbcAggregateOperations.exists].
*/
inline fun <reified T> JdbcAggregateOperations.exists(query: Query): Boolean =
exists(query, T::class.java)

/**
* Extension for [JdbcAggregateOperations.existsById].
*/
inline fun <reified T> JdbcAggregateOperations.existsById(id: Any): Boolean =
existsById(id, T::class.java)

/**
* Extension for [JdbcAggregateOperations.findById].
*/
inline fun <reified T> JdbcAggregateOperations.findById(id: Any): T? =
findById(id, T::class.java)

/**
* Extension for [JdbcAggregateOperations.findAllById].
*/
inline fun <reified T> JdbcAggregateOperations.findAllById(ids: Iterable<*>): List<T> =
findAllById(ids, T::class.java)

/**
* Extension for [JdbcAggregateOperations.findAll].
*/
inline fun <reified T> JdbcAggregateOperations.findAll(): List<T> =
findAll(T::class.java)

/**
* Extension for [JdbcAggregateOperations.findAll] with sorting.
*/
inline fun <reified T> JdbcAggregateOperations.findAll(sort: Sort): List<T> =
findAll(T::class.java, sort)

/**
* Extension for [JdbcAggregateOperations.findAll] with pagination.
*/
inline fun <reified T> JdbcAggregateOperations.findAll(pageable: Pageable): Page<T> =
findAll(T::class.java, pageable)

/**
* Extension for [JdbcAggregateOperations.findOne] with a query.
*/
inline fun <reified T> JdbcAggregateOperations.findOne(query: Query): Optional<T> =
findOne(query, T::class.java)

/**
* Extension for [JdbcAggregateOperations.findAll] with a query.
*/
inline fun <reified T> JdbcAggregateOperations.findAll(query: Query): List<T> =
findAll(query, T::class.java)

/**
* Extension for [JdbcAggregateOperations.findAll] with query and pagination.
*/
inline fun <reified T> JdbcAggregateOperations.findAll(query: Query, pageable: Pageable): Page<T> =
findAll(query, T::class.java, pageable)

/**
* Extension for [JdbcAggregateOperations.deleteById].
*/
inline fun <reified T> JdbcAggregateOperations.deleteById(id: Any): Unit =
deleteById(id, T::class.java)

/**
* Extension for [JdbcAggregateOperations.deleteAllById].
*/
inline fun <reified T> JdbcAggregateOperations.deleteAllById(ids: Iterable<*>): Unit =
deleteAllById(ids, T::class.java)

/**
* Extension for [JdbcAggregateOperations.deleteAll].
*/
inline fun <reified T> JdbcAggregateOperations.deleteAll(): Unit =
deleteAll(T::class.java)
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
* Copyright 2020-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jdbc.core

import io.mockk.mockk
import io.mockk.verify
import org.junit.Test
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Sort
import org.springframework.data.jdbc.testing.TestClass
import org.springframework.data.relational.core.query.Query

/**
* Unit tests for [JdbcAggregateOperations].
*
* @author Felix Desyatirikov
*/

class JdbcAggregateOperationsExtensionsTests {

val operations = mockk<JdbcAggregateOperations>(relaxed = true)

@Test // gh-1961
fun `count with reified type parameter extension should call its Java counterpart`() {
operations.count<TestClass>()
verify { operations.count(TestClass::class.java) }
}

@Test // gh-1961
fun `count(Query) with reified type parameter extension should call its Java counterpart`() {
val query = mockk<Query>(relaxed = true)
operations.count<TestClass>(query)
verify {
operations.count(query, TestClass::class.java)
}
}

@Test // gh-1961
fun `exists(Query) with reified type parameter extension should call its Java counterpart`() {
val query = mockk<Query>(relaxed = true)
operations.exists<TestClass>(query)
verify {
operations.exists(query, TestClass::class.java)
}
}

@Test // gh-1961
fun `existsById(id) with reified type parameter extension should call its Java counterpart`() {
val id = 1L
operations.existsById<TestClass>(id)
verify {
operations.existsById(id, TestClass::class.java)
}
}

@Test // gh-1961
fun `findById(id) with reified type parameter extension should call its Java counterpart`() {
val id = 1L
operations.findById<TestClass>(id)
verify {
operations.findById(id, TestClass::class.java)
}
}

@Test // gh-1961
fun `findAllById(ids) with reified type parameter extension should call its Java counterpart`() {
val ids = listOf(1L, 2L)
operations.findAllById<TestClass>(ids)
verify {
operations.findAllById(ids, TestClass::class.java)
}
}

@Test // gh-1961
fun `findAll() with reified type parameter extension should call its Java counterpart`() {
operations.findAll<TestClass>()
verify {
operations.findAll(TestClass::class.java)
}
}

@Test // gh-1961
fun `findAll(Sort) with reified type parameter extension should call its Java counterpart`() {
val sort = mockk<Sort>(relaxed = true)
operations.findAll<TestClass>(sort)
verify {
operations.findAll(TestClass::class.java, sort)
}
}

@Test // gh-1961
fun `findAll(Pageable) with reified type parameter extension should call its Java counterpart`() {
val pageable = mockk<Pageable>(relaxed = true)
operations.findAll<TestClass>(pageable)
verify {
operations.findAll(TestClass::class.java, pageable)
}
}

@Test // gh-1961
fun `findOne(Query) with reified type parameter extension should call its Java counterpart`() {
val query = mockk<Query>(relaxed = true)
operations.findOne<TestClass>(query)
verify {
operations.findOne(query, TestClass::class.java)
}
}

@Test // gh-1961
fun `findAll(Query) with reified type parameter extension should call its Java counterpart`() {
val query = mockk<Query>(relaxed = true)
operations.findAll<TestClass>(query)
verify {
operations.findAll(query, TestClass::class.java)
}
}


@Test // gh-1961
fun `findAll(Query, Pageable) with reified type parameter extension should call its Java counterpart`() {
val query = mockk<Query>(relaxed = true)
val pageable = mockk<Pageable>(relaxed = true)
operations.findAll<TestClass>(query, pageable)
verify {
operations.findAll(query, TestClass::class.java, pageable)
}
}

@Test // gh-1961
fun `deleteById(id) with reified type parameter extension should call its Java counterpart`() {
val id = 1L
operations.deleteById<TestClass>(id)
verify {
operations.deleteById(id, TestClass::class.java)
}
}

@Test // gh-1961
fun `deleteAllById(ids) with reified type parameter extension should call its Java counterpart`() {
val ids = listOf(1L, 2L)
operations.deleteAllById<TestClass>(ids)
verify {
operations.deleteAllById(ids, TestClass::class.java)
}
}

@Test // gh-1961
fun `deleteAll(ids) with reified type parameter extension should call its Java counterpart`() {
operations.deleteAll<TestClass>()
verify {
operations.deleteAll(TestClass::class.java)
}
}
}
Loading