From e15dc3d2fdfb7e0cfedeaf3765657f5ee42ae4bb Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Wed, 7 Jun 2017 14:00:07 +0200 Subject: [PATCH 1/5] DATAMONGO-1689 - Prepare issue branch. --- pom.xml | 4 ++-- spring-data-mongodb-cross-store/pom.xml | 4 ++-- spring-data-mongodb-distribution/pom.xml | 2 +- spring-data-mongodb-log4j/pom.xml | 2 +- spring-data-mongodb/pom.xml | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index 23d6477249..ada5446016 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-mongodb-parent - 2.0.0.BUILD-SNAPSHOT + 2.0.0.DATAMONGO-1689-SNAPSHOT pom Spring Data MongoDB @@ -15,7 +15,7 @@ org.springframework.data.build spring-data-parent - 2.0.0.BUILD-SNAPSHOT + 2.0.0.DATABUILD-369-SNAPSHOT diff --git a/spring-data-mongodb-cross-store/pom.xml b/spring-data-mongodb-cross-store/pom.xml index 4a49168713..72cf07e714 100644 --- a/spring-data-mongodb-cross-store/pom.xml +++ b/spring-data-mongodb-cross-store/pom.xml @@ -6,7 +6,7 @@ org.springframework.data spring-data-mongodb-parent - 2.0.0.BUILD-SNAPSHOT + 2.0.0.DATAMONGO-1689-SNAPSHOT ../pom.xml @@ -48,7 +48,7 @@ org.springframework.data spring-data-mongodb - 2.0.0.BUILD-SNAPSHOT + 2.0.0.DATAMONGO-1689-SNAPSHOT diff --git a/spring-data-mongodb-distribution/pom.xml b/spring-data-mongodb-distribution/pom.xml index 750ed23aa8..54b1c2bd59 100644 --- a/spring-data-mongodb-distribution/pom.xml +++ b/spring-data-mongodb-distribution/pom.xml @@ -13,7 +13,7 @@ org.springframework.data spring-data-mongodb-parent - 2.0.0.BUILD-SNAPSHOT + 2.0.0.DATAMONGO-1689-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb-log4j/pom.xml b/spring-data-mongodb-log4j/pom.xml index 50d0a6454a..b4692cd9a5 100644 --- a/spring-data-mongodb-log4j/pom.xml +++ b/spring-data-mongodb-log4j/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-mongodb-parent - 2.0.0.BUILD-SNAPSHOT + 2.0.0.DATAMONGO-1689-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index 99ef83a54b..2ad13f19a7 100644 --- a/spring-data-mongodb/pom.xml +++ b/spring-data-mongodb/pom.xml @@ -11,7 +11,7 @@ org.springframework.data spring-data-mongodb-parent - 2.0.0.BUILD-SNAPSHOT + 2.0.0.DATAMONGO-1689-SNAPSHOT ../pom.xml From bbd2da8933cf21e27df1a021acae19d7b3f79e71 Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Mon, 15 May 2017 15:55:14 +0200 Subject: [PATCH 2/5] DATAMONGO-1689 - Add Kotlin extensions for [Reactive]MongoOperations. We now offer dedicated Kotlin extensions for MongoOperations and ReactiveMongoOperations. --- spring-data-mongodb/pom.xml | 83 +++ .../mongodb/core/MongoOperationsExtensions.kt | 520 ++++++++++++++++ .../core/ReactiveMongoOperationsExtensions.kt | 374 ++++++++++++ .../core/MongoOperationsExtensionsTests.kt | 562 ++++++++++++++++++ .../ReactiveMongoOperationsExtensionsTests.kt | 434 ++++++++++++++ 5 files changed, 1973 insertions(+) create mode 100644 spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/MongoOperationsExtensions.kt create mode 100644 spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ReactiveMongoOperationsExtensions.kt create mode 100644 spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/MongoOperationsExtensionsTests.kt create mode 100644 spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ReactiveMongoOperationsExtensionsTests.kt diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index 2ad13f19a7..9e0c0f12f2 100644 --- a/spring-data-mongodb/pom.xml +++ b/spring-data-mongodb/pom.xml @@ -18,6 +18,7 @@ 1.3 1.5 + 1.1.2-3 @@ -230,12 +231,94 @@ spring-webmvc test + + org.jetbrains.kotlin + kotlin-stdlib-jre8 + ${kotlin} + true + + + org.jetbrains.kotlin + kotlin-reflect + ${kotlin} + true + + + org.jetbrains.kotlin + kotlin-test + ${kotlin} + test + + + com.nhaarman + mockito-kotlin + 1.4.0 + test + + + kotlin-maven-plugin + org.jetbrains.kotlin + ${kotlin} + + 1.8 + + + + compile + compile + compile + + + ${project.basedir}/src/main/kotlin + ${project.basedir}/src/main/java + + + + + test-compile + test-compile + test-compile + + + ${project.basedir}/src/test/kotlin + ${project.basedir}/src/test/java + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + default-compile + none + + + default-testCompile + none + + + java-compile + compile + compile + + + java-test-compile + test-compile + testCompile + + + + com.mysema.maven apt-maven-plugin diff --git a/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/MongoOperationsExtensions.kt b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/MongoOperationsExtensions.kt new file mode 100644 index 0000000000..27afc6cc02 --- /dev/null +++ b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/MongoOperationsExtensions.kt @@ -0,0 +1,520 @@ +/* + * Copyright 2017 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 + * + * http://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.mongodb.core + +import com.mongodb.client.MongoCollection +import com.mongodb.client.result.DeleteResult +import com.mongodb.client.result.UpdateResult +import org.bson.Document +import org.springframework.data.geo.GeoResults +import org.springframework.data.mongodb.core.BulkOperations.BulkMode +import org.springframework.data.mongodb.core.aggregation.Aggregation +import org.springframework.data.mongodb.core.aggregation.AggregationResults +import org.springframework.data.mongodb.core.mapreduce.GroupBy +import org.springframework.data.mongodb.core.mapreduce.GroupByResults +import org.springframework.data.mongodb.core.mapreduce.MapReduceOptions +import org.springframework.data.mongodb.core.mapreduce.MapReduceResults +import org.springframework.data.mongodb.core.query.Criteria +import org.springframework.data.mongodb.core.query.NearQuery +import org.springframework.data.mongodb.core.query.Query +import org.springframework.data.mongodb.core.query.Update +import org.springframework.data.util.CloseableIterator +import kotlin.reflect.KClass + +/** + * Extension for [MongoOperations.getCollectionName] providing a [KClass] based variant. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +fun MongoOperations.getCollectionName(entityClass: KClass): String = + getCollectionName(entityClass.java) + +/** + * Extension for [MongoOperations.getCollectionName] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun MongoOperations.getCollectionName(): String = + getCollectionName(T::class.java) + +/** + * Extension for [MongoOperations.execute] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun MongoOperations.execute(action: CollectionCallback): T = + execute(T::class.java, action) + +/** + * Extension for [MongoOperations.stream] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun MongoOperations.stream(query: Query): CloseableIterator = + stream(query, T::class.java) + +/** + * Extension for [MongoOperations.stream] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun MongoOperations.stream(query: Query, + collectionName: String? = null): CloseableIterator = + if (collectionName != null) stream(query, T::class.java, collectionName) + else stream(query, T::class.java) + +/** + * Extension for [MongoOperations.createCollection] providing a [KClass] based variant. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +fun MongoOperations.createCollection(entityClass: KClass, + collectionOptions: CollectionOptions? = null): MongoCollection = + if (collectionOptions != null) createCollection(entityClass.java, collectionOptions) + else createCollection(entityClass.java) + +/** + * Extension for [MongoOperations.createCollection] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun MongoOperations.createCollection( + collectionOptions: CollectionOptions? = null): MongoCollection = + if (collectionOptions != null) createCollection(T::class.java, collectionOptions) + else createCollection(T::class.java) + +/** + * Extension for [MongoOperations.collectionExists] providing a [KClass] based variant. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +fun MongoOperations.collectionExists(entityClass: KClass): Boolean = + collectionExists(entityClass.java) + +/** + * Extension for [MongoOperations.collectionExists] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun MongoOperations.collectionExists(): Boolean = + collectionExists(T::class.java) + +/** + * Extension for [MongoOperations.dropCollection] providing a [KClass] based variant. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +fun MongoOperations.dropCollection(entityClass: KClass) { + dropCollection(entityClass.java) +} + +/** + * Extension for [MongoOperations.dropCollection] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun MongoOperations.dropCollection() { + dropCollection(T::class.java) +} + +/** + * Extension for [MongoOperations.indexOps] providing a [KClass] based variant. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +fun MongoOperations.indexOps(entityClass: KClass): IndexOperations = + indexOps(entityClass.java) + +/** + * Extension for [MongoOperations.indexOps] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun MongoOperations.indexOps(): IndexOperations = + indexOps(T::class.java) + +/** + * Extension for [MongoOperations.bulkOps] providing a [KClass] based variant. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +fun MongoOperations.bulkOps(bulkMode: BulkMode, entityClass: KClass, + collectionName: String? = null): BulkOperations = + if (collectionName != null) bulkOps(bulkMode, entityClass.java, collectionName) + else bulkOps(bulkMode, entityClass.java) + +/** + * Extension for [MongoOperations.bulkOps] providing a [KClass] based variant. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +inline fun MongoOperations.bulkOps(bulkMode: BulkMode, + collectionName: String? = null): BulkOperations = + if (collectionName != null) bulkOps(bulkMode, T::class.java, collectionName) + else bulkOps(bulkMode, T::class.java) + +/** + * Extension for [MongoOperations.findAll] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun MongoOperations.findAll(collectionName: String? = null): List = + if (collectionName != null) findAll(T::class.java, collectionName) else findAll(T::class.java) + +/** + * Extension for [MongoOperations.group] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun MongoOperations.group(inputCollectionName: String, groupBy: GroupBy): GroupByResults = + group(inputCollectionName, groupBy, T::class.java) + +/** + * Extension for [MongoOperations.group] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun MongoOperations.group(criteria: Criteria, + inputCollectionName: String, groupBy: GroupBy): GroupByResults = + group(criteria, inputCollectionName, groupBy, T::class.java) + +/** + * Extension for [MongoOperations.aggregate] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun MongoOperations.aggregate(aggregation: Aggregation, + inputType: KClass<*>): AggregationResults = + aggregate(aggregation, inputType.java, O::class.java) + +/** + * Extension for [MongoOperations.aggregate] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun MongoOperations.aggregate(aggregation: Aggregation, + collectionName: String): AggregationResults = + aggregate(aggregation, collectionName, O::class.java) + +/** + * Extension for [MongoOperations.aggregateStream] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun MongoOperations.aggregateStream(aggregation: Aggregation, + inputType: KClass<*>): CloseableIterator = + aggregateStream(aggregation, inputType.java, O::class.java) + +/** + * Extension for [MongoOperations.aggregateStream] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun MongoOperations.aggregateStream(aggregation: Aggregation, + collectionName: String): CloseableIterator = + aggregateStream(aggregation, collectionName, O::class.java) + +/** + * Extension for [MongoOperations.mapReduce] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun MongoOperations.mapReduce(collectionName: String, + mapFunction: String, reduceFunction: String, options: MapReduceOptions? = null): MapReduceResults = + if (options != null) mapReduce(collectionName, mapFunction, reduceFunction, options, T::class.java) + else mapReduce(collectionName, mapFunction, reduceFunction, T::class.java) + +/** + * Extension for [MongoOperations.mapReduce] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun MongoOperations.mapReduce(query: Query, collectionName: String, + mapFunction: String, reduceFunction: String, options: MapReduceOptions? = null): MapReduceResults = + if (options != null) mapReduce(query, collectionName, mapFunction, reduceFunction, options, T::class.java) + else mapReduce(query, collectionName, mapFunction, reduceFunction, T::class.java) + +/** + * Extension for [MongoOperations.geoNear] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun MongoOperations.geoNear(near: NearQuery, + collectionName: String? = null): GeoResults = + if (collectionName != null) geoNear(near, T::class.java, collectionName) + else geoNear(near, T::class.java) + +/** + * Extension for [MongoOperations.findOne] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun MongoOperations.findOne(query: Query, collectionName: String? = null): T = + if (collectionName != null) findOne(query, T::class.java, collectionName) else findOne(query, T::class.java) + +/** + * Extension for [MongoOperations.exists] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +fun MongoOperations.exists(query: Query, entityClass: KClass, + collectionName: String? = null): Boolean = + if (collectionName != null) exists(query, entityClass.java, collectionName) + else exists(query, entityClass.java) + +/** + * Extension for [MongoOperations.exists] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +inline fun MongoOperations.exists(query: Query, collectionName: String? = null): Boolean = + if (collectionName != null) exists(query, T::class.java, collectionName) + else exists(query, T::class.java) + +/** + * Extension for [MongoOperations.find] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun MongoOperations.find(query: Query, collectionName: String? = null): List = + if (collectionName != null) find(query, T::class.java, collectionName) + else find(query, T::class.java) + +/** + * Extension for [MongoOperations.findById] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun MongoOperations.findById(id: Any, collectionName: String? = null): T = + if (collectionName != null) findById(id, T::class.java, collectionName) + else findById(id, T::class.java) + +/** + * Extension for [MongoOperations.findAndModify] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun MongoOperations.findAndModify(query: Query, update: Update, + options: FindAndModifyOptions, collectionName: String? = null): T = + if (collectionName != null) findAndModify(query, update, options, T::class.java, collectionName) + else findAndModify(query, update, options, T::class.java) + +/** + * Extension for [MongoOperations.findAndRemove] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun MongoOperations.findAndRemove(query: Query, collectionName: String? = null): T = + if (collectionName != null) findAndRemove(query, T::class.java, collectionName) + else findAndRemove(query, T::class.java) + +/** + * Extension for [MongoOperations.count] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +fun MongoOperations.count(query: Query = Query(), entityClass: KClass, + collectionName: String? = null): Long = + if (collectionName != null) count(query, entityClass.java, collectionName) + else count(query, entityClass.java) + +/** + * Extension for [MongoOperations.count] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +inline fun MongoOperations.count(query: Query = Query(), collectionName: String? = null): Long = + if (collectionName != null) count(query, T::class.java, collectionName) else count(query, T::class.java) + +/** + * Extension for [MongoOperations.insert] providing a [KClass] based variant. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +fun MongoOperations.insert(batchToSave: Collection, entityClass: KClass) { + insert(batchToSave, entityClass.java) +} + +/** + * Extension for [MongoOperations.upsert] providing a [KClass] based variant. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +fun MongoOperations.upsert(query: Query, update: Update, entityClass: KClass, + collectionName: String? = null): UpdateResult = + if (collectionName != null) upsert(query, update, entityClass.java, collectionName) + else upsert(query, update, entityClass.java) + +/** + * Extension for [MongoOperations.upsert] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +inline fun MongoOperations.upsert(query: Query, update: Update, + collectionName: String? = null): UpdateResult = + if (collectionName != null) upsert(query, update, T::class.java, collectionName) + else upsert(query, update, T::class.java) + +/** + * Extension for [MongoOperations.updateFirst] providing a [KClass] based variant. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +fun MongoOperations.updateFirst(query: Query, update: Update, entityClass: KClass, + collectionName: String? = null): UpdateResult = + if (collectionName != null) updateFirst(query, update, entityClass.java, collectionName) + else updateFirst(query, update, entityClass.java) + +/** + * Extension for [MongoOperations.updateFirst] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +inline fun MongoOperations.updateFirst(query: Query, update: Update, + collectionName: String? = null): UpdateResult = + if (collectionName != null) updateFirst(query, update, T::class.java, collectionName) + else updateFirst(query, update, T::class.java) + + +/** + * Extension for [MongoOperations.updateMulti] providing a [KClass] based variant. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +fun MongoOperations.updateMulti(query: Query, update: Update, entityClass: KClass, + collectionName: String? = null): UpdateResult = + if (collectionName != null) updateMulti(query, update, entityClass.java, collectionName) + else updateMulti(query, update, entityClass.java) + +/** + * Extension for [MongoOperations.updateMulti] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +inline fun MongoOperations.updateMulti(query: Query, update: Update, + collectionName: String? = null): UpdateResult = + if (collectionName != null) updateMulti(query, update, T::class.java, collectionName) + else updateMulti(query, update, T::class.java) + +/** + * Extension for [MongoOperations.remove] providing a [KClass] based variant. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +fun MongoOperations.remove(query: Query, entityClass: KClass, + collectionName: String? = null): DeleteResult = + if (collectionName != null) remove(query, entityClass.java, collectionName) + else remove(query, entityClass.java) + +/** + * Extension for [MongoOperations.remove] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +inline fun MongoOperations.remove(query: Query, collectionName: String? = null): DeleteResult = + if (collectionName != null) remove(query, T::class.java, collectionName) + else remove(query, T::class.java) + +/** + * Extension for [MongoOperations.findAllAndRemove] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun MongoOperations.findAllAndRemove(query: Query): List = + findAllAndRemove(query, T::class.java) diff --git a/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ReactiveMongoOperationsExtensions.kt b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ReactiveMongoOperationsExtensions.kt new file mode 100644 index 0000000000..bc00f47072 --- /dev/null +++ b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ReactiveMongoOperationsExtensions.kt @@ -0,0 +1,374 @@ +/* + * Copyright 2017 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 + * + * http://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.mongodb.core + +import com.mongodb.client.result.DeleteResult +import com.mongodb.client.result.UpdateResult +import com.mongodb.reactivestreams.client.MongoCollection +import org.bson.Document +import org.springframework.data.geo.GeoResult +import org.springframework.data.mongodb.core.query.NearQuery +import org.springframework.data.mongodb.core.query.Query +import org.springframework.data.mongodb.core.query.Update +import reactor.core.publisher.Flux +import reactor.core.publisher.Mono +import kotlin.reflect.KClass + +/** + * Extension for [ReactiveMongoOperations.indexOps] providing a [KClass] based variant. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +fun ReactiveMongoOperations.indexOps(entityClass: KClass): ReactiveIndexOperations = + indexOps(entityClass.java) + +/** + * Extension for [ReactiveMongoOperations.indexOps] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun ReactiveMongoOperations.indexOps(): ReactiveIndexOperations = + indexOps(T::class.java) + +/** + * Extension for [ReactiveMongoOperations.execute] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun ReactiveMongoOperations.execute(action: ReactiveCollectionCallback): Flux = + execute(T::class.java, action) + +/** + * Extension for [ReactiveMongoOperations.createCollection] providing a [KClass] based variant. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +fun ReactiveMongoOperations.createCollection(entityClass: KClass, + collectionOptions: CollectionOptions? = null): Mono> = + if (collectionOptions != null) createCollection(entityClass.java, collectionOptions) else createCollection(entityClass.java) + +/** + * Extension for [ReactiveMongoOperations.createCollection] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun ReactiveMongoOperations.createCollection( + collectionOptions: CollectionOptions? = null): Mono> = + if (collectionOptions != null) createCollection(T::class.java, collectionOptions) else createCollection(T::class.java) + +/** + * Extension for [ReactiveMongoOperations.collectionExists] providing a [KClass] based variant. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +fun ReactiveMongoOperations.collectionExists(entityClass: KClass): Mono = + collectionExists(entityClass.java) + +/** + * Extension for [ReactiveMongoOperations.collectionExists] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun ReactiveMongoOperations.collectionExists(): Mono = + collectionExists(T::class.java) + +/** + * Extension for [ReactiveMongoOperations.dropCollection] providing a [KClass] based variant. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +fun ReactiveMongoOperations.dropCollection(entityClass: KClass): Mono = + dropCollection(entityClass.java) + +/** + * Extension for [ReactiveMongoOperations.dropCollection] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun ReactiveMongoOperations.dropCollection(): Mono = + dropCollection(T::class.java) + + +/** + * Extension for [ReactiveMongoOperations.findAll] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun ReactiveMongoOperations.findAll(collectionName: String? = null): Flux = + if (collectionName != null) findAll(T::class.java, collectionName) else findAll(T::class.java) + +/** + * Extension for [ReactiveMongoOperations.findOne] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun ReactiveMongoOperations.findOne(query: Query, collectionName: String? = null): Mono = + if (collectionName != null) findOne(query, T::class.java, collectionName) else findOne(query, T::class.java) + +/** + * Extension for [ReactiveMongoOperations.exists] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +fun ReactiveMongoOperations.exists(query: Query, entityClass: KClass, + collectionName: String? = null): Mono = + if (collectionName != null) exists(query, entityClass.java, collectionName) else exists(query, entityClass.java) + +/** + * Extension for [ReactiveMongoOperations.exists] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +inline fun ReactiveMongoOperations.exists(query: Query, + collectionName: String? = null): Mono = + if (collectionName != null) exists(query, T::class.java, collectionName) else exists(query, T::class.java) + +/** + * Extension for [ReactiveMongoOperations.find] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun ReactiveMongoOperations.find(query: Query, collectionName: String? = null): Flux = + if (collectionName != null) find(query, T::class.java, collectionName) else find(query, T::class.java) + +/** + * Extension for [ReactiveMongoOperations.findById] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun ReactiveMongoOperations.findById(id: Any, collectionName: String? = null): Mono = + if (collectionName != null) findById(id, T::class.java, collectionName) else findById(id, T::class.java) + +/** + * Extension for [ReactiveMongoOperations.geoNear] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun ReactiveMongoOperations.geoNear(near: NearQuery, + collectionName: String? = null): Flux> = + if (collectionName != null) geoNear(near, T::class.java, collectionName) else geoNear(near, T::class.java) + +/** + * Extension for [ReactiveMongoOperations.findAndModify] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun ReactiveMongoOperations.findAndModify(query: Query, + update: Update, options: FindAndModifyOptions, collectionName: String? = null): Mono = + if (collectionName != null) findAndModify(query, update, options, T::class.java, collectionName) else findAndModify(query, update, options, T::class.java) + +/** + * Extension for [ReactiveMongoOperations.findAndRemove] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun ReactiveMongoOperations.findAndRemove(query: Query, + collectionName: String? = null): Mono = + if (collectionName != null) findAndRemove(query, T::class.java, collectionName) + else findAndRemove(query, T::class.java) + +/** + * Extension for [ReactiveMongoOperations.count] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +fun ReactiveMongoOperations.count(query: Query = Query(), entityClass: KClass, + collectionName: String? = null): Mono = + if (collectionName != null) count(query, entityClass.java, collectionName) + else count(query, entityClass.java) + +/** + * Extension for [ReactiveMongoOperations.count] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters + * + * @author Sebastien Deleuze + * @since 5.0 + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +inline fun ReactiveMongoOperations.count(query: Query = Query(), + collectionName: String? = null): Mono = + if (collectionName != null) count(query, T::class.java, collectionName) + else count(query, T::class.java) + +/** + * Extension for [ReactiveMongoOperations.insert] providing a [KClass] based variant. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +fun ReactiveMongoOperations.insert(batchToSave: Collection, entityClass: KClass): Flux = + insert(batchToSave, entityClass.java) + +/** + * Extension for [ReactiveMongoOperations.insertAll] providing a [KClass] based variant. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +fun ReactiveMongoOperations.insertAll(batchToSave: Mono>, + entityClass: KClass): Flux = + insertAll(batchToSave, entityClass.java) + +/** + * Extension for [ReactiveMongoOperations.upsert] providing a [KClass] based variant. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +fun ReactiveMongoOperations.upsert(query: Query, update: Update, entityClass: KClass, + collectionName: String? = null): Mono = + if (collectionName != null) upsert(query, update, entityClass.java, collectionName) else upsert(query, update, entityClass.java) + +/** + * Extension for [ReactiveMongoOperations.upsert] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +inline fun ReactiveMongoOperations.upsert(query: Query, update: Update, + collectionName: String? = null): Mono = + if (collectionName != null) upsert(query, update, T::class.java, collectionName) + else upsert(query, update, T::class.java) + +/** + * Extension for [ReactiveMongoOperations.updateFirst] providing a [KClass] based variant. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +fun ReactiveMongoOperations.updateFirst(query: Query, update: Update, + entityClass: KClass, collectionName: String? = null): Mono = + if (collectionName != null) updateFirst(query, update, entityClass.java, collectionName) + else updateFirst(query, update, entityClass.java) + +/** + * Extension for [ReactiveMongoOperations.updateFirst] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +inline fun ReactiveMongoOperations.updateFirst(query: Query, update: Update, + collectionName: String? = null): Mono = + if (collectionName != null) updateFirst(query, update, T::class.java, collectionName) + else updateFirst(query, update, T::class.java) + +/** + * Extension for [ReactiveMongoOperations.updateMulti] providing a [KClass] based variant. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +fun ReactiveMongoOperations.updateMulti(query: Query, update: Update, + entityClass: KClass, collectionName: String? = null): Mono = + if (collectionName != null) updateMulti(query, update, entityClass.java, collectionName) + else updateMulti(query, update, entityClass.java) + +/** + * Extension for [ReactiveMongoOperations.updateMulti] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +inline fun ReactiveMongoOperations.updateMulti(query: Query, update: Update, + collectionName: String? = null): Mono = + if (collectionName != null) updateMulti(query, update, T::class.java, collectionName) + else updateMulti(query, update, T::class.java) + +/** + * Extension for [ReactiveMongoOperations.remove] providing a [KClass] based variant. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +fun ReactiveMongoOperations.remove(query: Query, entityClass: KClass, + collectionName: String? = null): Mono = + if (collectionName != null) remove(query, entityClass.java, collectionName) + else remove(query, entityClass.java) + +/** + * Extension for [ReactiveMongoOperations.remove] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +inline fun ReactiveMongoOperations.remove(query: Query, + collectionName: String? = null): Mono = + if (collectionName != null) remove(query, T::class.java, collectionName) + else remove(query, T::class.java) + +/** + * Extension for [ReactiveMongoOperations.findAllAndRemove] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +inline fun ReactiveMongoOperations.findAllAndRemove(query: Query): Flux = + findAllAndRemove(query, T::class.java) + +/** + * Extension for [ReactiveMongoOperations.tail] avoiding requiring the type parameter + * thanks to Kotlin reified type parameters. + * + * @author Sebastien Deleuze + * @since 5.0 + */ +inline fun ReactiveMongoOperations.tail(query: Query, collectionName: String? = null): Flux = + if (collectionName != null) tail(query, T::class.java, collectionName) else tail(query, T::class.java) diff --git a/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/MongoOperationsExtensionsTests.kt b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/MongoOperationsExtensionsTests.kt new file mode 100644 index 0000000000..97b8950604 --- /dev/null +++ b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/MongoOperationsExtensionsTests.kt @@ -0,0 +1,562 @@ +/* + * Copyright 2016-2017 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 + * + * http://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.mongodb.core + +import com.nhaarman.mockito_kotlin.mock +import example.first.First +import example.second.Second +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Answers +import org.mockito.Mock +import org.mockito.Mockito.* +import org.mockito.junit.MockitoJUnitRunner +import org.springframework.data.mongodb.core.BulkOperations.BulkMode +import org.springframework.data.mongodb.core.aggregation.Aggregation +import org.springframework.data.mongodb.core.mapreduce.GroupBy +import org.springframework.data.mongodb.core.mapreduce.MapReduceOptions +import org.springframework.data.mongodb.core.query.Criteria +import org.springframework.data.mongodb.core.query.NearQuery +import org.springframework.data.mongodb.core.query.Query +import org.springframework.data.mongodb.core.query.Update + +/** + * @author Sebastien Deleuze + */ +@RunWith(MockitoJUnitRunner::class) +class MongoOperationsExtensionsTests { + + @Mock(answer = Answers.RETURNS_MOCKS) + lateinit var operations: MongoOperations + + @Test + fun `getCollectionName(KClass) extension should call its Java counterpart`() { + operations.getCollectionName(First::class) + verify(operations, times(1)).getCollectionName(First::class.java) + } + + @Test + fun `getCollectionName() with reified type parameter extension should call its Java counterpart`() { + operations.getCollectionName() + verify(operations, times(1)).getCollectionName(First::class.java) + } + + @Test + fun `execute(CollectionCallback) with reified type parameter extension should call its Java counterpart`() { + val collectionCallback = mock>() + operations.execute(collectionCallback) + verify(operations, times(1)).execute(First::class.java, collectionCallback) + } + + @Test + fun `stream(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.stream(query) + verify(operations, times(1)).stream(query, First::class.java) + } + + @Test + fun `stream(Query, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + val collectionName = "foo" + operations.stream(query, collectionName) + verify(operations, times(1)).stream(query, First::class.java, collectionName) + } + + @Test + fun `createCollection(KClass) extension should call its Java counterpart`() { + operations.createCollection(First::class) + verify(operations, times(1)).createCollection(First::class.java) + } + + @Test + fun `createCollection(KClass, CollectionOptions) extension should call its Java counterpart`() { + val collectionOptions = mock() + operations.createCollection(First::class, collectionOptions) + verify(operations, times(1)).createCollection(First::class.java,collectionOptions) + } + + @Test + fun `createCollection() with reified type parameter extension should call its Java counterpart`() { + operations.createCollection() + verify(operations, times(1)).createCollection(First::class.java) + } + + @Test + fun `createCollection(CollectionOptions) with reified type parameter extension should call its Java counterpart`() { + val collectionOptions = mock() + operations.createCollection(collectionOptions) + verify(operations, times(1)).createCollection(First::class.java, collectionOptions) + } + + + @Test + fun `collectionExists(KClass) extension should call its Java counterpart`() { + operations.collectionExists(First::class) + verify(operations, times(1)).collectionExists(First::class.java) + } + + @Test + fun `collectionExists() with reified type parameter extension should call its Java counterpart`() { + operations.collectionExists() + verify(operations, times(1)).collectionExists(First::class.java) + } + + @Test + fun `dropCollection(KClass) extension should call its Java counterpart`() { + operations.dropCollection(First::class) + verify(operations, times(1)).dropCollection(First::class.java) + } + + @Test + fun `dropCollection() with reified type parameter extension should call its Java counterpart`() { + operations.dropCollection() + verify(operations, times(1)).dropCollection(First::class.java) + } + + @Test + fun `indexOps(KClass) extension should call its Java counterpart`() { + operations.indexOps(First::class) + verify(operations, times(1)).indexOps(First::class.java) + } + + @Test + fun `indexOps() with reified type parameter extension should call its Java counterpart`() { + operations.indexOps() + verify(operations, times(1)).indexOps(First::class.java) + } + + @Test + fun `bulkOps(BulkMode, KClass) extension should call its Java counterpart`() { + val bulkMode = BulkMode.ORDERED + operations.bulkOps(bulkMode, First::class) + verify(operations, times(1)).bulkOps(bulkMode, First::class.java) + } + + @Test + fun `bulkOps(BulkMode, KClass, String) extension should call its Java counterpart`() { + val bulkMode = BulkMode.ORDERED + val collectionName = "foo" + operations.bulkOps(bulkMode, First::class, collectionName) + verify(operations, times(1)).bulkOps(bulkMode, First::class.java, collectionName) + } + + @Test + fun `bulkOps(BulkMode) with reified type parameter extension should call its Java counterpart`() { + val bulkMode = BulkMode.ORDERED + operations.bulkOps(bulkMode) + verify(operations, times(1)).bulkOps(bulkMode, First::class.java) + } + + @Test + fun `bulkOps(BulkMode, String) with reified type parameter extension should call its Java counterpart`() { + val bulkMode = BulkMode.ORDERED + val collectionName = "foo" + operations.bulkOps(bulkMode, collectionName) + verify(operations, times(1)).bulkOps(bulkMode, First::class.java, collectionName) + } + + @Test + fun `findAll() with reified type parameter extension should call its Java counterpart`() { + operations.findAll() + verify(operations, times(1)).findAll(First::class.java) + } + + @Test + fun `findAll(String) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" + operations.findAll(collectionName) + verify(operations, times(1)).findAll(First::class.java, collectionName) + } + + @Test + fun `group(String, GroupBy) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" + val groupBy = mock() + operations.group(collectionName, groupBy) + verify(operations, times(1)).group(collectionName, groupBy, First::class.java) + } + + @Test + fun `group(Criteria, String, GroupBy) with reified type parameter extension should call its Java counterpart`() { + val criteria = mock() + val collectionName = "foo" + val groupBy = mock() + operations.group(criteria, collectionName, groupBy) + verify(operations, times(1)).group(criteria, collectionName, groupBy, First::class.java) + } + + @Test + fun `aggregate(Aggregation, KClass) with reified type parameter extension should call its Java counterpart`() { + val aggregation = mock() + operations.aggregate(aggregation, Second::class) + verify(operations, times(1)).aggregate(aggregation, Second::class.java, First::class.java) + } + + @Test + fun `aggregate(Aggregation, String) with reified type parameter extension should call its Java counterpart`() { + val aggregation = mock() + val collectionName = "foo" + operations.aggregate(aggregation, collectionName) + verify(operations, times(1)).aggregate(aggregation, collectionName, First::class.java) + } + + @Test + fun `aggregateStream(Aggregation, KClass) with reified type parameter extension should call its Java counterpart`() { + val aggregation = mock() + operations.aggregateStream(aggregation, Second::class) + verify(operations, times(1)).aggregateStream(aggregation, Second::class.java, First::class.java) + } + + @Test + fun `aggregateStream(Aggregation, String) with reified type parameter extension should call its Java counterpart`() { + val aggregation = mock() + val collectionName = "foo" + operations.aggregateStream(aggregation, collectionName) + verify(operations, times(1)).aggregateStream(aggregation, collectionName, First::class.java) + } + + @Test + fun `mapReduce(String, String, String) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" + val mapFunction = "bar" + val reduceFunction = "baz" + operations.mapReduce(collectionName, mapFunction, reduceFunction) + verify(operations, times(1)).mapReduce(collectionName, mapFunction, reduceFunction, First::class.java) + } + + @Test + fun `mapReduce(String, String, String, MapReduceOptions) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" + val mapFunction = "bar" + val reduceFunction = "baz" + val options = mock() + operations.mapReduce(collectionName, mapFunction, reduceFunction, options) + verify(operations, times(1)).mapReduce(collectionName, mapFunction, reduceFunction, options, First::class.java) + } + + @Test + fun `mapReduce(Query, String, String, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + val collectionName = "foo" + val mapFunction = "bar" + val reduceFunction = "baz" + operations.mapReduce(query, collectionName, mapFunction, reduceFunction) + verify(operations, times(1)).mapReduce(query, collectionName, mapFunction, reduceFunction, First::class.java) + } + + @Test + fun `mapReduce(Query, String, String, String, MapReduceOptions) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + val collectionName = "foo" + val mapFunction = "bar" + val reduceFunction = "baz" + val options = mock() + operations.mapReduce(query, collectionName, mapFunction, reduceFunction, options) + verify(operations, times(1)).mapReduce(query, collectionName, mapFunction, reduceFunction, options, First::class.java) + } + + @Test + fun `geoNear(Query) with reified type parameter extension should call its Java counterpart`() { + val query = NearQuery.near(0.0, 0.0) + operations.geoNear(query) + verify(operations, times(1)).geoNear(query, First::class.java) + } + + @Test + fun `geoNear(Query, String) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" + val query = NearQuery.near(0.0, 0.0) + operations.geoNear(query, collectionName) + verify(operations, times(1)).geoNear(query, First::class.java, collectionName) + } + + @Test + fun `findOne(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.findOne(query) + verify(operations, times(1)).findOne(query, First::class.java) + } + + @Test + fun `findOne(Query, String) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" + val query = mock() + operations.findOne(query, collectionName) + verify(operations, times(1)).findOne(query, First::class.java, collectionName) + } + + @Test + fun `exists(Query, KClass) extension should call its Java counterpart`() { + val query = mock() + operations.exists(query, First::class) + verify(operations, times(1)).exists(query, First::class.java) + } + + @Test + fun `exists(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.exists(query) + verify(operations, times(1)).exists(query, First::class.java) + } + + @Test + fun `find(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.find(query) + verify(operations, times(1)).find(query, First::class.java) + } + + @Test + fun `find(Query, String) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" + val query = mock() + operations.find(query, collectionName) + verify(operations, times(1)).find(query, First::class.java, collectionName) + } + + @Test + fun `findById(Any) with reified type parameter extension should call its Java counterpart`() { + val id = 1L + operations.findById(id) + verify(operations, times(1)).findById(id, First::class.java) + } + + @Test + fun `findById(Any, String) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" + val id = 1L + operations.findById(id, collectionName) + verify(operations, times(1)).findById(id, First::class.java, collectionName) + } + + @Test + fun `findAndModify(Query, Update, FindAndModifyOptions) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + val update = mock() + val options = mock() + operations.findAndModify(query, update, options) + verify(operations, times(1)).findAndModify(query, update, options, First::class.java) + } + + @Test + fun `findAndModify(Query, Update, FindAndModifyOptions, String) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" + val query = mock() + val update = mock() + val options = mock() + operations.findAndModify(query, update, options, collectionName) + verify(operations, times(1)).findAndModify(query, update, options, First::class.java, collectionName) + } + + @Test + fun `findAndRemove(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.findAndRemove(query) + verify(operations, times(1)).findAndRemove(query, First::class.java) + } + + @Test + fun `findAndRemove(Query, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + val collectionName = "foo" + operations.findAndRemove(query, collectionName) + verify(operations, times(1)).findAndRemove(query, First::class.java, collectionName) + } + + @Test + fun `count() with reified type parameter extension should call its Java counterpart`() { + operations.count() + verify(operations, times(1)).count(any(), eq(First::class.java)) + } + + @Test + fun `count(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.count(query) + verify(operations, times(1)).count(query, First::class.java) + } + + @Test + fun `count(Query, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + val collectionName = "foo" + operations.count(query, collectionName) + verify(operations, times(1)).count(query, First::class.java, collectionName) + } + + @Test + fun `count(Query, KClass) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.count(query, First::class) + verify(operations, times(1)).count(query, First::class.java) + } + + @Test + fun `count(Query, KClass, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + val collectionName = "foo" + operations.count(query, First::class, collectionName) + verify(operations, times(1)).count(query, First::class.java, collectionName) + } + + @Test + fun `insert(Collection, KClass) extension should call its Java counterpart`() { + val collection = listOf(First(), First()) + operations.insert(collection, First::class) + verify(operations, times(1)).insert(collection, First::class.java) + } + + @Test + fun `upsert(Query, Update, KClass) extension should call its Java counterpart`() { + val query = mock() + val update = mock() + operations.upsert(query, update, First::class) + verify(operations, times(1)).upsert(query, update, First::class.java) + } + + @Test + fun `upsert(Query, Update, KClass, String) extension should call its Java counterpart`() { + val query = mock() + val update = mock() + val collectionName = "foo" + operations.upsert(query, update, First::class, collectionName) + verify(operations, times(1)).upsert(query, update, First::class.java, collectionName) + } + + @Test + fun `upsert(Query, Update) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + val update = mock() + operations.upsert(query, update) + verify(operations, times(1)).upsert(query, update, First::class.java) + } + + @Test + fun `upsert(Query, Update, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + val update = mock() + val collectionName = "foo" + operations.upsert(query, update, collectionName) + verify(operations, times(1)).upsert(query, update, First::class.java, collectionName) + } + + @Test + fun `updateFirst(Query, Update, KClass) extension should call its Java counterpart`() { + val query = mock() + val update = mock() + operations.updateFirst(query, update, First::class) + verify(operations, times(1)).updateFirst(query, update, First::class.java) + } + + @Test + fun `updateFirst(Query, Update, KClass, String) extension should call its Java counterpart`() { + val query = mock() + val update = mock() + val collectionName = "foo" + operations.updateFirst(query, update, First::class, collectionName) + verify(operations, times(1)).updateFirst(query, update, First::class.java, collectionName) + } + + @Test + fun `updateFirst(Query, Update) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + val update = mock() + operations.updateFirst(query, update) + verify(operations, times(1)).updateFirst(query, update, First::class.java) + } + + @Test + fun `updateFirst(Query, Update, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + val update = mock() + val collectionName = "foo" + operations.updateFirst(query, update, collectionName) + verify(operations, times(1)).updateFirst(query, update, First::class.java, collectionName) + } + + @Test + fun `updateMulti(Query, Update, KClass) extension should call its Java counterpart`() { + val query = mock() + val update = mock() + operations.updateMulti(query, update, First::class) + verify(operations, times(1)).updateMulti(query, update, First::class.java) + } + + @Test + fun `updateMulti(Query, Update, KClass, String) extension should call its Java counterpart`() { + val query = mock() + val update = mock() + val collectionName = "foo" + operations.updateMulti(query, update, First::class, collectionName) + verify(operations, times(1)).updateMulti(query, update, First::class.java, collectionName) + } + + @Test + fun `updateMulti(Query, Update) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + val update = mock() + operations.updateMulti(query, update) + verify(operations, times(1)).updateMulti(query, update, First::class.java) + } + + @Test + fun `updateMulti(Query, Update, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + val update = mock() + val collectionName = "foo" + operations.updateMulti(query, update, collectionName) + verify(operations, times(1)).updateMulti(query, update, First::class.java, collectionName) + } + + @Test + fun `remove(Query, KClass) extension should call its Java counterpart`() { + val query = mock() + operations.remove(query, First::class) + verify(operations, times(1)).remove(query, First::class.java) + } + + @Test + fun `remove(Query, KClass, String) extension should call its Java counterpart`() { + val query = mock() + val collectionName = "foo" + operations.remove(query, First::class, collectionName) + verify(operations, times(1)).remove(query, First::class.java, collectionName) + } + + @Test + fun `remove(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.remove(query) + verify(operations, times(1)).remove(query, First::class.java) + } + + @Test + fun `remove(Query, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + val collectionName = "foo" + operations.remove(query, collectionName) + verify(operations, times(1)).remove(query, First::class.java, collectionName) + } + + @Test + fun `findAllAndRemove(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.findAllAndRemove(query) + verify(operations, times(1)).findAllAndRemove(query, First::class.java) + } + +} diff --git a/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ReactiveMongoOperationsExtensionsTests.kt b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ReactiveMongoOperationsExtensionsTests.kt new file mode 100644 index 0000000000..b45c563dc9 --- /dev/null +++ b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ReactiveMongoOperationsExtensionsTests.kt @@ -0,0 +1,434 @@ +/* + * Copyright 2016-2017 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 + * + * http://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.mongodb.core + +import com.nhaarman.mockito_kotlin.mock +import example.first.First +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Answers +import org.mockito.Mock +import org.mockito.Mockito.* +import org.mockito.junit.MockitoJUnitRunner +import org.springframework.data.mongodb.core.query.NearQuery +import org.springframework.data.mongodb.core.query.Query +import org.springframework.data.mongodb.core.query.Update +import reactor.core.publisher.Mono + +/** + * @author Sebastien Deleuze + */ +@RunWith(MockitoJUnitRunner::class) +class ReactiveMongoOperationsExtensionsTests { + + @Mock(answer = Answers.RETURNS_MOCKS) + lateinit var operations: ReactiveMongoOperations + + @Test + fun `indexOps(KClass) extension should call its Java counterpart`() { + operations.indexOps(First::class) + verify(operations, times(1)).indexOps(First::class.java) + } + + @Test + fun `indexOps() with reified type parameter extension should call its Java counterpart`() { + operations.indexOps() + verify(operations, times(1)).indexOps(First::class.java) + } + + @Test + fun `execute(ReactiveCollectionCallback) with reified type parameter extension should call its Java counterpart`() { + val collectionCallback = mock>() + operations.execute(collectionCallback) + verify(operations, times(1)).execute(First::class.java, collectionCallback) + } + + @Test + fun `createCollection(KClass) extension should call its Java counterpart`() { + operations.createCollection(First::class) + verify(operations, times(1)).createCollection(First::class.java) + } + + @Test + fun `createCollection(KClass, CollectionOptions) extension should call its Java counterpart`() { + val collectionOptions = mock() + operations.createCollection(First::class, collectionOptions) + verify(operations, times(1)).createCollection(First::class.java, collectionOptions) + } + + @Test + fun `createCollection() with reified type parameter extension should call its Java counterpart`() { + operations.createCollection() + verify(operations, times(1)).createCollection(First::class.java) + } + + @Test + fun `createCollection(CollectionOptions) with reified type parameter extension should call its Java counterpart`() { + val collectionOptions = mock() + operations.createCollection(collectionOptions) + verify(operations, times(1)).createCollection(First::class.java, collectionOptions) + } + + @Test + fun `collectionExists(KClass) extension should call its Java counterpart`() { + operations.collectionExists(First::class) + verify(operations, times(1)).collectionExists(First::class.java) + } + + @Test + fun `collectionExists() with reified type parameter extension should call its Java counterpart`() { + operations.collectionExists() + verify(operations, times(1)).collectionExists(First::class.java) + } + + @Test + fun `dropCollection(KClass) extension should call its Java counterpart`() { + operations.dropCollection(First::class) + verify(operations, times(1)).dropCollection(First::class.java) + } + + @Test + fun `dropCollection() with reified type parameter extension should call its Java counterpart`() { + operations.dropCollection() + verify(operations, times(1)).dropCollection(First::class.java) + } + + @Test + fun `findAll() with reified type parameter extension should call its Java counterpart`() { + operations.findAll() + verify(operations, times(1)).findAll(First::class.java) + } + + @Test + fun `findAll(String) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" + operations.findAll(collectionName) + verify(operations, times(1)).findAll(First::class.java, collectionName) + } + + @Test + fun `findOne(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.findOne(query) + verify(operations, times(1)).findOne(query, First::class.java) + } + + @Test + fun `findOne(Query, String) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" + val query = mock() + operations.findOne(query, collectionName) + verify(operations, times(1)).findOne(query, First::class.java, collectionName) + } + + @Test + fun `exists(Query, KClass) extension should call its Java counterpart`() { + val query = mock() + operations.exists(query, First::class) + verify(operations, times(1)).exists(query, First::class.java) + } + + @Test + fun `exists(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.exists(query) + verify(operations, times(1)).exists(query, First::class.java) + } + + @Test + fun `find(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.find(query) + verify(operations, times(1)).find(query, First::class.java) + } + + @Test + fun `find(Query, String) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" + val query = mock() + operations.find(query, collectionName) + verify(operations, times(1)).find(query, First::class.java, collectionName) + } + + @Test + fun `findById(Any) with reified type parameter extension should call its Java counterpart`() { + val id = 1L + operations.findById(id) + verify(operations, times(1)).findById(id, First::class.java) + } + + @Test + fun `findById(Any, String) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" + val id = 1L + operations.findById(id, collectionName) + verify(operations, times(1)).findById(id, First::class.java, collectionName) + } + + @Test + fun `geoNear(Query) with reified type parameter extension should call its Java counterpart`() { + val query = NearQuery.near(0.0, 0.0) + operations.geoNear(query) + verify(operations, times(1)).geoNear(query, First::class.java) + } + + @Test + fun `geoNear(Query, String) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" + val query = NearQuery.near(0.0, 0.0) + operations.geoNear(query, collectionName) + verify(operations, times(1)).geoNear(query, First::class.java, collectionName) + } + + @Test + fun `findAndModify(Query, Update, FindAndModifyOptions) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + val update = mock() + val options = mock() + operations.findAndModify(query, update, options) + verify(operations, times(1)).findAndModify(query, update, options, First::class.java) + } + + @Test + fun `findAndModify(Query, Update, FindAndModifyOptions, String) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" + val query = mock() + val update = mock() + val options = mock() + operations.findAndModify(query, update, options, collectionName) + verify(operations, times(1)).findAndModify(query, update, options, First::class.java, collectionName) + } + + @Test + fun `findAndRemove(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.findAndRemove(query) + verify(operations, times(1)).findAndRemove(query, First::class.java) + } + + @Test + fun `findAndRemove(Query, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + val collectionName = "foo" + operations.findAndRemove(query, collectionName) + verify(operations, times(1)).findAndRemove(query, First::class.java, collectionName) + } + + @Test + fun `count() with reified type parameter extension should call its Java counterpart`() { + operations.count() + verify(operations, times(1)).count(any(), eq(First::class.java)) + } + + @Test + fun `count(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.count(query) + verify(operations, times(1)).count(query, First::class.java) + } + + @Test + fun `count(Query, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + val collectionName = "foo" + operations.count(query, collectionName) + verify(operations, times(1)).count(query, First::class.java, collectionName) + } + + @Test + fun `count(Query, KClass) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.count(query, First::class) + verify(operations, times(1)).count(query, First::class.java) + } + + @Test + fun `count(Query, KClass, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + val collectionName = "foo" + operations.count(query, First::class, collectionName) + verify(operations, times(1)).count(query, First::class.java, collectionName) + } + + @Test + fun `insert(Collection, KClass) extension should call its Java counterpart`() { + val collection = listOf(First(), First()) + operations.insert(collection, First::class) + verify(operations, times(1)).insert(collection, First::class.java) + } + + @Test + fun `insertAll(Mono, KClass) extension should call its Java counterpart`() { + val collection = Mono.just(listOf(First(), First())) + operations.insertAll(collection, First::class) + verify(operations, times(1)).insertAll(collection, First::class.java) + } + + @Test + fun `upsert(Query, Update, KClass) extension should call its Java counterpart`() { + val query = mock() + val update = mock() + operations.upsert(query, update, First::class) + verify(operations, times(1)).upsert(query, update, First::class.java) + } + + @Test + fun `upsert(Query, Update, KClass, String) extension should call its Java counterpart`() { + val query = mock() + val update = mock() + val collectionName = "foo" + operations.upsert(query, update, First::class, collectionName) + verify(operations, times(1)).upsert(query, update, First::class.java, collectionName) + } + + @Test + fun `upsert(Query, Update) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + val update = mock() + operations.upsert(query, update) + verify(operations, times(1)).upsert(query, update, First::class.java) + } + + @Test + fun `upsert(Query, Update, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + val update = mock() + val collectionName = "foo" + operations.upsert(query, update, collectionName) + verify(operations, times(1)).upsert(query, update, First::class.java, collectionName) + } + + @Test + fun `updateFirst(Query, Update, KClass) extension should call its Java counterpart`() { + val query = mock() + val update = mock() + operations.updateFirst(query, update, First::class) + verify(operations, times(1)).updateFirst(query, update, First::class.java) + } + + @Test + fun `updateFirst(Query, Update, KClass, String) extension should call its Java counterpart`() { + val query = mock() + val update = mock() + val collectionName = "foo" + operations.updateFirst(query, update, First::class, collectionName) + verify(operations, times(1)).updateFirst(query, update, First::class.java, collectionName) + } + + @Test + fun `updateFirst(Query, Update) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + val update = mock() + operations.updateFirst(query, update) + verify(operations, times(1)).updateFirst(query, update, First::class.java) + } + + @Test + fun `updateFirst(Query, Update, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + val update = mock() + val collectionName = "foo" + operations.updateFirst(query, update, collectionName) + verify(operations, times(1)).updateFirst(query, update, First::class.java, collectionName) + } + + @Test + fun `updateMulti(Query, Update, KClass) extension should call its Java counterpart`() { + val query = mock() + val update = mock() + operations.updateMulti(query, update, First::class) + verify(operations, times(1)).updateMulti(query, update, First::class.java) + } + + @Test + fun `updateMulti(Query, Update, KClass, String) extension should call its Java counterpart`() { + val query = mock() + val update = mock() + val collectionName = "foo" + operations.updateMulti(query, update, First::class, collectionName) + verify(operations, times(1)).updateMulti(query, update, First::class.java, collectionName) + } + + @Test + fun `updateMulti(Query, Update) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + val update = mock() + operations.updateMulti(query, update) + verify(operations, times(1)).updateMulti(query, update, First::class.java) + } + + @Test + fun `updateMulti(Query, Update, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + val update = mock() + val collectionName = "foo" + operations.updateMulti(query, update, collectionName) + verify(operations, times(1)).updateMulti(query, update, First::class.java, collectionName) + } + + @Test + fun `remove(Query, KClass) extension should call its Java counterpart`() { + val query = mock() + operations.remove(query, First::class) + verify(operations, times(1)).remove(query, First::class.java) + } + + @Test + fun `remove(Query, KClass, String) extension should call its Java counterpart`() { + val query = mock() + val collectionName = "foo" + operations.remove(query, First::class, collectionName) + verify(operations, times(1)).remove(query, First::class.java, collectionName) + } + + @Test + fun `remove(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.remove(query) + verify(operations, times(1)).remove(query, First::class.java) + } + + @Test + fun `remove(Query, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + val collectionName = "foo" + operations.remove(query, collectionName) + verify(operations, times(1)).remove(query, First::class.java, collectionName) + } + + @Test + fun `findAllAndRemove(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.findAllAndRemove(query) + verify(operations, times(1)).findAllAndRemove(query, First::class.java) + } + + @Test + fun `tail(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.tail(query) + verify(operations, times(1)).tail(query, First::class.java) + } + + @Test + fun `tail(Query, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + val collectionName = "foo" + operations.tail(query, collectionName) + verify(operations, times(1)).tail(query, First::class.java, collectionName) + } + +} From a19287cf34fb04364c815c1566f2959218a61682 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Wed, 7 Jun 2017 14:15:18 +0200 Subject: [PATCH 3/5] DATAMONGO-1689 - Polishing. Remove Kotlin specifics from pom.xml as those are managed in spring-data-build. Additionally format code, update license header, update JavaDoc and add issue reference to tests. --- spring-data-mongodb/pom.xml | 59 ---- .../mongodb/core/MongoOperationsExtensions.kt | 160 +++++------ .../core/ReactiveMongoOperationsExtensions.kt | 120 ++++---- .../core/MongoOperationsExtensionsTests.kt | 260 +++++++++++++----- .../ReactiveMongoOperationsExtensionsTests.kt | 205 ++++++++++---- 5 files changed, 459 insertions(+), 345 deletions(-) diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index 9e0c0f12f2..6ce66c4cb9 100644 --- a/spring-data-mongodb/pom.xml +++ b/spring-data-mongodb/pom.xml @@ -18,7 +18,6 @@ 1.3 1.5 - 1.1.2-3 @@ -261,64 +260,6 @@ - - kotlin-maven-plugin - org.jetbrains.kotlin - ${kotlin} - - 1.8 - - - - compile - compile - compile - - - ${project.basedir}/src/main/kotlin - ${project.basedir}/src/main/java - - - - - test-compile - test-compile - test-compile - - - ${project.basedir}/src/test/kotlin - ${project.basedir}/src/test/java - - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - - default-compile - none - - - default-testCompile - none - - - java-compile - compile - compile - - - java-test-compile - test-compile - testCompile - - - - com.mysema.maven apt-maven-plugin diff --git a/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/MongoOperationsExtensions.kt b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/MongoOperationsExtensions.kt index 27afc6cc02..62abe677f1 100644 --- a/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/MongoOperationsExtensions.kt +++ b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/MongoOperationsExtensions.kt @@ -38,7 +38,7 @@ import kotlin.reflect.KClass * Extension for [MongoOperations.getCollectionName] providing a [KClass] based variant. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ fun MongoOperations.getCollectionName(entityClass: KClass): String = getCollectionName(entityClass.java) @@ -48,7 +48,7 @@ fun MongoOperations.getCollectionName(entityClass: KClass): String * thanks to Kotlin reified type parameters. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ inline fun MongoOperations.getCollectionName(): String = getCollectionName(T::class.java) @@ -58,7 +58,7 @@ inline fun MongoOperations.getCollectionName(): String = * thanks to Kotlin reified type parameters. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ inline fun MongoOperations.execute(action: CollectionCallback): T = execute(T::class.java, action) @@ -68,20 +68,19 @@ inline fun MongoOperations.execute(action: CollectionCallback< * thanks to Kotlin reified type parameters. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ inline fun MongoOperations.stream(query: Query): CloseableIterator = - stream(query, T::class.java) + stream(query, T::class.java) /** * Extension for [MongoOperations.stream] avoiding requiring the type parameter * thanks to Kotlin reified type parameters. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -inline fun MongoOperations.stream(query: Query, - collectionName: String? = null): CloseableIterator = +inline fun MongoOperations.stream(query: Query, collectionName: String? = null): CloseableIterator = if (collectionName != null) stream(query, T::class.java, collectionName) else stream(query, T::class.java) @@ -89,10 +88,9 @@ inline fun MongoOperations.stream(query: Query, * Extension for [MongoOperations.createCollection] providing a [KClass] based variant. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -fun MongoOperations.createCollection(entityClass: KClass, - collectionOptions: CollectionOptions? = null): MongoCollection = +fun MongoOperations.createCollection(entityClass: KClass, collectionOptions: CollectionOptions? = null): MongoCollection = if (collectionOptions != null) createCollection(entityClass.java, collectionOptions) else createCollection(entityClass.java) @@ -101,7 +99,7 @@ fun MongoOperations.createCollection(entityClass: KClass, * thanks to Kotlin reified type parameters. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ inline fun MongoOperations.createCollection( collectionOptions: CollectionOptions? = null): MongoCollection = @@ -112,7 +110,7 @@ inline fun MongoOperations.createCollection( * Extension for [MongoOperations.collectionExists] providing a [KClass] based variant. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ fun MongoOperations.collectionExists(entityClass: KClass): Boolean = collectionExists(entityClass.java) @@ -122,7 +120,7 @@ fun MongoOperations.collectionExists(entityClass: KClass): Boolean * thanks to Kotlin reified type parameters. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ inline fun MongoOperations.collectionExists(): Boolean = collectionExists(T::class.java) @@ -131,10 +129,10 @@ inline fun MongoOperations.collectionExists(): Boolean = * Extension for [MongoOperations.dropCollection] providing a [KClass] based variant. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ fun MongoOperations.dropCollection(entityClass: KClass) { - dropCollection(entityClass.java) + dropCollection(entityClass.java) } /** @@ -142,17 +140,17 @@ fun MongoOperations.dropCollection(entityClass: KClass) { * thanks to Kotlin reified type parameters. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ inline fun MongoOperations.dropCollection() { - dropCollection(T::class.java) + dropCollection(T::class.java) } /** * Extension for [MongoOperations.indexOps] providing a [KClass] based variant. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ fun MongoOperations.indexOps(entityClass: KClass): IndexOperations = indexOps(entityClass.java) @@ -162,7 +160,7 @@ fun MongoOperations.indexOps(entityClass: KClass): IndexOperations * thanks to Kotlin reified type parameters. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ inline fun MongoOperations.indexOps(): IndexOperations = indexOps(T::class.java) @@ -171,10 +169,9 @@ inline fun MongoOperations.indexOps(): IndexOperations = * Extension for [MongoOperations.bulkOps] providing a [KClass] based variant. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -fun MongoOperations.bulkOps(bulkMode: BulkMode, entityClass: KClass, - collectionName: String? = null): BulkOperations = +fun MongoOperations.bulkOps(bulkMode: BulkMode, entityClass: KClass, collectionName: String? = null): BulkOperations = if (collectionName != null) bulkOps(bulkMode, entityClass.java, collectionName) else bulkOps(bulkMode, entityClass.java) @@ -182,11 +179,10 @@ fun MongoOperations.bulkOps(bulkMode: BulkMode, entityClass: KClass * Extension for [MongoOperations.bulkOps] providing a [KClass] based variant. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER") -inline fun MongoOperations.bulkOps(bulkMode: BulkMode, - collectionName: String? = null): BulkOperations = +inline fun MongoOperations.bulkOps(bulkMode: BulkMode, collectionName: String? = null): BulkOperations = if (collectionName != null) bulkOps(bulkMode, T::class.java, collectionName) else bulkOps(bulkMode, T::class.java) @@ -195,7 +191,7 @@ inline fun MongoOperations.bulkOps(bulkMode: BulkMode, * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ inline fun MongoOperations.findAll(collectionName: String? = null): List = if (collectionName != null) findAll(T::class.java, collectionName) else findAll(T::class.java) @@ -205,7 +201,7 @@ inline fun MongoOperations.findAll(collectionName: String? = n * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ inline fun MongoOperations.group(inputCollectionName: String, groupBy: GroupBy): GroupByResults = group(inputCollectionName, groupBy, T::class.java) @@ -215,10 +211,9 @@ inline fun MongoOperations.group(inputCollectionName: String, * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -inline fun MongoOperations.group(criteria: Criteria, - inputCollectionName: String, groupBy: GroupBy): GroupByResults = +inline fun MongoOperations.group(criteria: Criteria, inputCollectionName: String, groupBy: GroupBy): GroupByResults = group(criteria, inputCollectionName, groupBy, T::class.java) /** @@ -226,10 +221,9 @@ inline fun MongoOperations.group(criteria: Criteria, * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -inline fun MongoOperations.aggregate(aggregation: Aggregation, - inputType: KClass<*>): AggregationResults = +inline fun MongoOperations.aggregate(aggregation: Aggregation, inputType: KClass<*>): AggregationResults = aggregate(aggregation, inputType.java, O::class.java) /** @@ -237,10 +231,9 @@ inline fun MongoOperations.aggregate(aggregation: Aggregation, * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -inline fun MongoOperations.aggregate(aggregation: Aggregation, - collectionName: String): AggregationResults = +inline fun MongoOperations.aggregate(aggregation: Aggregation, collectionName: String): AggregationResults = aggregate(aggregation, collectionName, O::class.java) /** @@ -248,10 +241,9 @@ inline fun MongoOperations.aggregate(aggregation: Aggregation, * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -inline fun MongoOperations.aggregateStream(aggregation: Aggregation, - inputType: KClass<*>): CloseableIterator = +inline fun MongoOperations.aggregateStream(aggregation: Aggregation, inputType: KClass<*>): CloseableIterator = aggregateStream(aggregation, inputType.java, O::class.java) /** @@ -259,10 +251,9 @@ inline fun MongoOperations.aggregateStream(aggregation: Aggreg * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -inline fun MongoOperations.aggregateStream(aggregation: Aggregation, - collectionName: String): CloseableIterator = +inline fun MongoOperations.aggregateStream(aggregation: Aggregation, collectionName: String): CloseableIterator = aggregateStream(aggregation, collectionName, O::class.java) /** @@ -270,10 +261,9 @@ inline fun MongoOperations.aggregateStream(aggregation: Aggreg * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -inline fun MongoOperations.mapReduce(collectionName: String, - mapFunction: String, reduceFunction: String, options: MapReduceOptions? = null): MapReduceResults = +inline fun MongoOperations.mapReduce(collectionName: String, mapFunction: String, reduceFunction: String, options: MapReduceOptions? = null): MapReduceResults = if (options != null) mapReduce(collectionName, mapFunction, reduceFunction, options, T::class.java) else mapReduce(collectionName, mapFunction, reduceFunction, T::class.java) @@ -282,10 +272,9 @@ inline fun MongoOperations.mapReduce(collectionName: String, * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 52.0 */ -inline fun MongoOperations.mapReduce(query: Query, collectionName: String, - mapFunction: String, reduceFunction: String, options: MapReduceOptions? = null): MapReduceResults = +inline fun MongoOperations.mapReduce(query: Query, collectionName: String, mapFunction: String, reduceFunction: String, options: MapReduceOptions? = null): MapReduceResults = if (options != null) mapReduce(query, collectionName, mapFunction, reduceFunction, options, T::class.java) else mapReduce(query, collectionName, mapFunction, reduceFunction, T::class.java) @@ -294,10 +283,9 @@ inline fun MongoOperations.mapReduce(query: Query, collectionN * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -inline fun MongoOperations.geoNear(near: NearQuery, - collectionName: String? = null): GeoResults = +inline fun MongoOperations.geoNear(near: NearQuery, collectionName: String? = null): GeoResults = if (collectionName != null) geoNear(near, T::class.java, collectionName) else geoNear(near, T::class.java) @@ -306,7 +294,7 @@ inline fun MongoOperations.geoNear(near: NearQuery, * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ inline fun MongoOperations.findOne(query: Query, collectionName: String? = null): T = if (collectionName != null) findOne(query, T::class.java, collectionName) else findOne(query, T::class.java) @@ -316,10 +304,9 @@ inline fun MongoOperations.findOne(query: Query, collectionNam * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -fun MongoOperations.exists(query: Query, entityClass: KClass, - collectionName: String? = null): Boolean = +fun MongoOperations.exists(query: Query, entityClass: KClass, collectionName: String? = null): Boolean = if (collectionName != null) exists(query, entityClass.java, collectionName) else exists(query, entityClass.java) @@ -328,7 +315,7 @@ fun MongoOperations.exists(query: Query, entityClass: KClass, * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER") inline fun MongoOperations.exists(query: Query, collectionName: String? = null): Boolean = @@ -340,7 +327,7 @@ inline fun MongoOperations.exists(query: Query, collectionName * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ inline fun MongoOperations.find(query: Query, collectionName: String? = null): List = if (collectionName != null) find(query, T::class.java, collectionName) @@ -351,7 +338,7 @@ inline fun MongoOperations.find(query: Query, collectionName: * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ inline fun MongoOperations.findById(id: Any, collectionName: String? = null): T = if (collectionName != null) findById(id, T::class.java, collectionName) @@ -362,10 +349,9 @@ inline fun MongoOperations.findById(id: Any, collectionName: S * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -inline fun MongoOperations.findAndModify(query: Query, update: Update, - options: FindAndModifyOptions, collectionName: String? = null): T = +inline fun MongoOperations.findAndModify(query: Query, update: Update, options: FindAndModifyOptions, collectionName: String? = null): T = if (collectionName != null) findAndModify(query, update, options, T::class.java, collectionName) else findAndModify(query, update, options, T::class.java) @@ -374,7 +360,7 @@ inline fun MongoOperations.findAndModify(query: Query, update: * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ inline fun MongoOperations.findAndRemove(query: Query, collectionName: String? = null): T = if (collectionName != null) findAndRemove(query, T::class.java, collectionName) @@ -385,10 +371,9 @@ inline fun MongoOperations.findAndRemove(query: Query, collect * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -fun MongoOperations.count(query: Query = Query(), entityClass: KClass, - collectionName: String? = null): Long = +fun MongoOperations.count(query: Query = Query(), entityClass: KClass, collectionName: String? = null): Long = if (collectionName != null) count(query, entityClass.java, collectionName) else count(query, entityClass.java) @@ -397,7 +382,7 @@ fun MongoOperations.count(query: Query = Query(), entityClass: KClass< * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER") inline fun MongoOperations.count(query: Query = Query(), collectionName: String? = null): Long = @@ -407,7 +392,7 @@ inline fun MongoOperations.count(query: Query = Query(), colle * Extension for [MongoOperations.insert] providing a [KClass] based variant. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ fun MongoOperations.insert(batchToSave: Collection, entityClass: KClass) { insert(batchToSave, entityClass.java) @@ -417,10 +402,9 @@ fun MongoOperations.insert(batchToSave: Collection, entityClass: KC * Extension for [MongoOperations.upsert] providing a [KClass] based variant. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -fun MongoOperations.upsert(query: Query, update: Update, entityClass: KClass, - collectionName: String? = null): UpdateResult = +fun MongoOperations.upsert(query: Query, update: Update, entityClass: KClass, collectionName: String? = null): UpdateResult = if (collectionName != null) upsert(query, update, entityClass.java, collectionName) else upsert(query, update, entityClass.java) @@ -429,11 +413,10 @@ fun MongoOperations.upsert(query: Query, update: Update, entityClass: * thanks to Kotlin reified type parameters. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER") -inline fun MongoOperations.upsert(query: Query, update: Update, - collectionName: String? = null): UpdateResult = +inline fun MongoOperations.upsert(query: Query, update: Update, collectionName: String? = null): UpdateResult = if (collectionName != null) upsert(query, update, T::class.java, collectionName) else upsert(query, update, T::class.java) @@ -441,10 +424,9 @@ inline fun MongoOperations.upsert(query: Query, update: Update * Extension for [MongoOperations.updateFirst] providing a [KClass] based variant. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -fun MongoOperations.updateFirst(query: Query, update: Update, entityClass: KClass, - collectionName: String? = null): UpdateResult = +fun MongoOperations.updateFirst(query: Query, update: Update, entityClass: KClass, collectionName: String? = null): UpdateResult = if (collectionName != null) updateFirst(query, update, entityClass.java, collectionName) else updateFirst(query, update, entityClass.java) @@ -453,11 +435,10 @@ fun MongoOperations.updateFirst(query: Query, update: Update, entityCl * thanks to Kotlin reified type parameters. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER") -inline fun MongoOperations.updateFirst(query: Query, update: Update, - collectionName: String? = null): UpdateResult = +inline fun MongoOperations.updateFirst(query: Query, update: Update, collectionName: String? = null): UpdateResult = if (collectionName != null) updateFirst(query, update, T::class.java, collectionName) else updateFirst(query, update, T::class.java) @@ -466,10 +447,9 @@ inline fun MongoOperations.updateFirst(query: Query, update: U * Extension for [MongoOperations.updateMulti] providing a [KClass] based variant. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -fun MongoOperations.updateMulti(query: Query, update: Update, entityClass: KClass, - collectionName: String? = null): UpdateResult = +fun MongoOperations.updateMulti(query: Query, update: Update, entityClass: KClass, collectionName: String? = null): UpdateResult = if (collectionName != null) updateMulti(query, update, entityClass.java, collectionName) else updateMulti(query, update, entityClass.java) @@ -478,11 +458,10 @@ fun MongoOperations.updateMulti(query: Query, update: Update, entityCl * thanks to Kotlin reified type parameters. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER") -inline fun MongoOperations.updateMulti(query: Query, update: Update, - collectionName: String? = null): UpdateResult = +inline fun MongoOperations.updateMulti(query: Query, update: Update, collectionName: String? = null): UpdateResult = if (collectionName != null) updateMulti(query, update, T::class.java, collectionName) else updateMulti(query, update, T::class.java) @@ -490,10 +469,9 @@ inline fun MongoOperations.updateMulti(query: Query, update: U * Extension for [MongoOperations.remove] providing a [KClass] based variant. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -fun MongoOperations.remove(query: Query, entityClass: KClass, - collectionName: String? = null): DeleteResult = +fun MongoOperations.remove(query: Query, entityClass: KClass, collectionName: String? = null): DeleteResult = if (collectionName != null) remove(query, entityClass.java, collectionName) else remove(query, entityClass.java) @@ -502,7 +480,7 @@ fun MongoOperations.remove(query: Query, entityClass: KClass, * thanks to Kotlin reified type parameters. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER") inline fun MongoOperations.remove(query: Query, collectionName: String? = null): DeleteResult = @@ -514,7 +492,7 @@ inline fun MongoOperations.remove(query: Query, collectionName * thanks to Kotlin reified type parameters. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ inline fun MongoOperations.findAllAndRemove(query: Query): List = findAllAndRemove(query, T::class.java) diff --git a/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ReactiveMongoOperationsExtensions.kt b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ReactiveMongoOperationsExtensions.kt index bc00f47072..a3f6911d60 100644 --- a/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ReactiveMongoOperationsExtensions.kt +++ b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ReactiveMongoOperationsExtensions.kt @@ -31,7 +31,7 @@ import kotlin.reflect.KClass * Extension for [ReactiveMongoOperations.indexOps] providing a [KClass] based variant. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ fun ReactiveMongoOperations.indexOps(entityClass: KClass): ReactiveIndexOperations = indexOps(entityClass.java) @@ -41,7 +41,7 @@ fun ReactiveMongoOperations.indexOps(entityClass: KClass): Reactive * thanks to Kotlin reified type parameters. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ inline fun ReactiveMongoOperations.indexOps(): ReactiveIndexOperations = indexOps(T::class.java) @@ -51,7 +51,7 @@ inline fun ReactiveMongoOperations.indexOps(): ReactiveIndexOp * thanks to Kotlin reified type parameters. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ inline fun ReactiveMongoOperations.execute(action: ReactiveCollectionCallback): Flux = execute(T::class.java, action) @@ -60,10 +60,9 @@ inline fun ReactiveMongoOperations.execute(action: ReactiveCol * Extension for [ReactiveMongoOperations.createCollection] providing a [KClass] based variant. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -fun ReactiveMongoOperations.createCollection(entityClass: KClass, - collectionOptions: CollectionOptions? = null): Mono> = +fun ReactiveMongoOperations.createCollection(entityClass: KClass, collectionOptions: CollectionOptions? = null): Mono> = if (collectionOptions != null) createCollection(entityClass.java, collectionOptions) else createCollection(entityClass.java) /** @@ -71,17 +70,16 @@ fun ReactiveMongoOperations.createCollection(entityClass: KClass, * thanks to Kotlin reified type parameters. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -inline fun ReactiveMongoOperations.createCollection( - collectionOptions: CollectionOptions? = null): Mono> = +inline fun ReactiveMongoOperations.createCollection(collectionOptions: CollectionOptions? = null): Mono> = if (collectionOptions != null) createCollection(T::class.java, collectionOptions) else createCollection(T::class.java) /** * Extension for [ReactiveMongoOperations.collectionExists] providing a [KClass] based variant. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ fun ReactiveMongoOperations.collectionExists(entityClass: KClass): Mono = collectionExists(entityClass.java) @@ -91,7 +89,7 @@ fun ReactiveMongoOperations.collectionExists(entityClass: KClass): * thanks to Kotlin reified type parameters. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ inline fun ReactiveMongoOperations.collectionExists(): Mono = collectionExists(T::class.java) @@ -100,7 +98,7 @@ inline fun ReactiveMongoOperations.collectionExists(): Mono ReactiveMongoOperations.dropCollection(entityClass: KClass): Mono = dropCollection(entityClass.java) @@ -110,7 +108,7 @@ fun ReactiveMongoOperations.dropCollection(entityClass: KClass): Mo * thanks to Kotlin reified type parameters. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ inline fun ReactiveMongoOperations.dropCollection(): Mono = dropCollection(T::class.java) @@ -121,7 +119,7 @@ inline fun ReactiveMongoOperations.dropCollection(): Mono ReactiveMongoOperations.findAll(collectionName: String? = null): Flux = if (collectionName != null) findAll(T::class.java, collectionName) else findAll(T::class.java) @@ -131,7 +129,7 @@ inline fun ReactiveMongoOperations.findAll(collectionName: Str * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ inline fun ReactiveMongoOperations.findOne(query: Query, collectionName: String? = null): Mono = if (collectionName != null) findOne(query, T::class.java, collectionName) else findOne(query, T::class.java) @@ -141,10 +139,9 @@ inline fun ReactiveMongoOperations.findOne(query: Query, colle * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -fun ReactiveMongoOperations.exists(query: Query, entityClass: KClass, - collectionName: String? = null): Mono = +fun ReactiveMongoOperations.exists(query: Query, entityClass: KClass, collectionName: String? = null): Mono = if (collectionName != null) exists(query, entityClass.java, collectionName) else exists(query, entityClass.java) /** @@ -152,11 +149,10 @@ fun ReactiveMongoOperations.exists(query: Query, entityClass: KClass ReactiveMongoOperations.exists(query: Query, - collectionName: String? = null): Mono = +inline fun ReactiveMongoOperations.exists(query: Query, collectionName: String? = null): Mono = if (collectionName != null) exists(query, T::class.java, collectionName) else exists(query, T::class.java) /** @@ -164,7 +160,7 @@ inline fun ReactiveMongoOperations.exists(query: Query, * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ inline fun ReactiveMongoOperations.find(query: Query, collectionName: String? = null): Flux = if (collectionName != null) find(query, T::class.java, collectionName) else find(query, T::class.java) @@ -174,7 +170,7 @@ inline fun ReactiveMongoOperations.find(query: Query, collecti * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ inline fun ReactiveMongoOperations.findById(id: Any, collectionName: String? = null): Mono = if (collectionName != null) findById(id, T::class.java, collectionName) else findById(id, T::class.java) @@ -184,10 +180,9 @@ inline fun ReactiveMongoOperations.findById(id: Any, collectio * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -inline fun ReactiveMongoOperations.geoNear(near: NearQuery, - collectionName: String? = null): Flux> = +inline fun ReactiveMongoOperations.geoNear(near: NearQuery, collectionName: String? = null): Flux> = if (collectionName != null) geoNear(near, T::class.java, collectionName) else geoNear(near, T::class.java) /** @@ -195,10 +190,9 @@ inline fun ReactiveMongoOperations.geoNear(near: NearQuery, * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -inline fun ReactiveMongoOperations.findAndModify(query: Query, - update: Update, options: FindAndModifyOptions, collectionName: String? = null): Mono = +inline fun ReactiveMongoOperations.findAndModify(query: Query, update: Update, options: FindAndModifyOptions, collectionName: String? = null): Mono = if (collectionName != null) findAndModify(query, update, options, T::class.java, collectionName) else findAndModify(query, update, options, T::class.java) /** @@ -206,10 +200,9 @@ inline fun ReactiveMongoOperations.findAndModify(query: Query, * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -inline fun ReactiveMongoOperations.findAndRemove(query: Query, - collectionName: String? = null): Mono = +inline fun ReactiveMongoOperations.findAndRemove(query: Query, collectionName: String? = null): Mono = if (collectionName != null) findAndRemove(query, T::class.java, collectionName) else findAndRemove(query, T::class.java) @@ -218,10 +211,9 @@ inline fun ReactiveMongoOperations.findAndRemove(query: Query, * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -fun ReactiveMongoOperations.count(query: Query = Query(), entityClass: KClass, - collectionName: String? = null): Mono = +fun ReactiveMongoOperations.count(query: Query = Query(), entityClass: KClass, collectionName: String? = null): Mono = if (collectionName != null) count(query, entityClass.java, collectionName) else count(query, entityClass.java) @@ -230,11 +222,10 @@ fun ReactiveMongoOperations.count(query: Query = Query(), entityClass: * thanks to Kotlin reified type parameters * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER") -inline fun ReactiveMongoOperations.count(query: Query = Query(), - collectionName: String? = null): Mono = +inline fun ReactiveMongoOperations.count(query: Query = Query(), collectionName: String? = null): Mono = if (collectionName != null) count(query, T::class.java, collectionName) else count(query, T::class.java) @@ -242,29 +233,27 @@ inline fun ReactiveMongoOperations.count(query: Query = Query( * Extension for [ReactiveMongoOperations.insert] providing a [KClass] based variant. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ fun ReactiveMongoOperations.insert(batchToSave: Collection, entityClass: KClass): Flux = - insert(batchToSave, entityClass.java) + insert(batchToSave, entityClass.java) /** * Extension for [ReactiveMongoOperations.insertAll] providing a [KClass] based variant. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -fun ReactiveMongoOperations.insertAll(batchToSave: Mono>, - entityClass: KClass): Flux = +fun ReactiveMongoOperations.insertAll(batchToSave: Mono>, entityClass: KClass): Flux = insertAll(batchToSave, entityClass.java) /** * Extension for [ReactiveMongoOperations.upsert] providing a [KClass] based variant. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -fun ReactiveMongoOperations.upsert(query: Query, update: Update, entityClass: KClass, - collectionName: String? = null): Mono = +fun ReactiveMongoOperations.upsert(query: Query, update: Update, entityClass: KClass, collectionName: String? = null): Mono = if (collectionName != null) upsert(query, update, entityClass.java, collectionName) else upsert(query, update, entityClass.java) /** @@ -272,11 +261,10 @@ fun ReactiveMongoOperations.upsert(query: Query, update: Update, entit * thanks to Kotlin reified type parameters. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER") -inline fun ReactiveMongoOperations.upsert(query: Query, update: Update, - collectionName: String? = null): Mono = +inline fun ReactiveMongoOperations.upsert(query: Query, update: Update, collectionName: String? = null): Mono = if (collectionName != null) upsert(query, update, T::class.java, collectionName) else upsert(query, update, T::class.java) @@ -284,10 +272,9 @@ inline fun ReactiveMongoOperations.upsert(query: Query, update * Extension for [ReactiveMongoOperations.updateFirst] providing a [KClass] based variant. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -fun ReactiveMongoOperations.updateFirst(query: Query, update: Update, - entityClass: KClass, collectionName: String? = null): Mono = +fun ReactiveMongoOperations.updateFirst(query: Query, update: Update, entityClass: KClass, collectionName: String? = null): Mono = if (collectionName != null) updateFirst(query, update, entityClass.java, collectionName) else updateFirst(query, update, entityClass.java) @@ -296,11 +283,10 @@ fun ReactiveMongoOperations.updateFirst(query: Query, update: Update, * thanks to Kotlin reified type parameters. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER") -inline fun ReactiveMongoOperations.updateFirst(query: Query, update: Update, - collectionName: String? = null): Mono = +inline fun ReactiveMongoOperations.updateFirst(query: Query, update: Update, collectionName: String? = null): Mono = if (collectionName != null) updateFirst(query, update, T::class.java, collectionName) else updateFirst(query, update, T::class.java) @@ -308,10 +294,9 @@ inline fun ReactiveMongoOperations.updateFirst(query: Query, u * Extension for [ReactiveMongoOperations.updateMulti] providing a [KClass] based variant. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -fun ReactiveMongoOperations.updateMulti(query: Query, update: Update, - entityClass: KClass, collectionName: String? = null): Mono = +fun ReactiveMongoOperations.updateMulti(query: Query, update: Update, entityClass: KClass, collectionName: String? = null): Mono = if (collectionName != null) updateMulti(query, update, entityClass.java, collectionName) else updateMulti(query, update, entityClass.java) @@ -320,11 +305,10 @@ fun ReactiveMongoOperations.updateMulti(query: Query, update: Update, * thanks to Kotlin reified type parameters. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER") -inline fun ReactiveMongoOperations.updateMulti(query: Query, update: Update, - collectionName: String? = null): Mono = +inline fun ReactiveMongoOperations.updateMulti(query: Query, update: Update, collectionName: String? = null): Mono = if (collectionName != null) updateMulti(query, update, T::class.java, collectionName) else updateMulti(query, update, T::class.java) @@ -332,10 +316,9 @@ inline fun ReactiveMongoOperations.updateMulti(query: Query, u * Extension for [ReactiveMongoOperations.remove] providing a [KClass] based variant. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ -fun ReactiveMongoOperations.remove(query: Query, entityClass: KClass, - collectionName: String? = null): Mono = +fun ReactiveMongoOperations.remove(query: Query, entityClass: KClass, collectionName: String? = null): Mono = if (collectionName != null) remove(query, entityClass.java, collectionName) else remove(query, entityClass.java) @@ -344,11 +327,10 @@ fun ReactiveMongoOperations.remove(query: Query, entityClass: KClass ReactiveMongoOperations.remove(query: Query, - collectionName: String? = null): Mono = +inline fun ReactiveMongoOperations.remove(query: Query, collectionName: String? = null): Mono = if (collectionName != null) remove(query, T::class.java, collectionName) else remove(query, T::class.java) @@ -357,7 +339,7 @@ inline fun ReactiveMongoOperations.remove(query: Query, * thanks to Kotlin reified type parameters. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER") inline fun ReactiveMongoOperations.findAllAndRemove(query: Query): Flux = @@ -368,7 +350,7 @@ inline fun ReactiveMongoOperations.findAllAndRemove(query: Que * thanks to Kotlin reified type parameters. * * @author Sebastien Deleuze - * @since 5.0 + * @since 2.0 */ inline fun ReactiveMongoOperations.tail(query: Query, collectionName: String? = null): Flux = if (collectionName != null) tail(query, T::class.java, collectionName) else tail(query, T::class.java) diff --git a/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/MongoOperationsExtensionsTests.kt b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/MongoOperationsExtensionsTests.kt index 97b8950604..ec3ee3ac4b 100644 --- a/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/MongoOperationsExtensionsTests.kt +++ b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/MongoOperationsExtensionsTests.kt @@ -1,5 +1,5 @@ /* - * Copyright 2016-2017 the original author or authors. + * Copyright 2017 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. @@ -42,521 +42,639 @@ class MongoOperationsExtensionsTests { @Mock(answer = Answers.RETURNS_MOCKS) lateinit var operations: MongoOperations - @Test + @Test // DATAMONGO-1689 fun `getCollectionName(KClass) extension should call its Java counterpart`() { + operations.getCollectionName(First::class) verify(operations, times(1)).getCollectionName(First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `getCollectionName() with reified type parameter extension should call its Java counterpart`() { + operations.getCollectionName() verify(operations, times(1)).getCollectionName(First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `execute(CollectionCallback) with reified type parameter extension should call its Java counterpart`() { + val collectionCallback = mock>() operations.execute(collectionCallback) verify(operations, times(1)).execute(First::class.java, collectionCallback) } - @Test + @Test // DATAMONGO-1689 fun `stream(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() operations.stream(query) verify(operations, times(1)).stream(query, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `stream(Query, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() val collectionName = "foo" operations.stream(query, collectionName) verify(operations, times(1)).stream(query, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `createCollection(KClass) extension should call its Java counterpart`() { + operations.createCollection(First::class) verify(operations, times(1)).createCollection(First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `createCollection(KClass, CollectionOptions) extension should call its Java counterpart`() { + val collectionOptions = mock() operations.createCollection(First::class, collectionOptions) - verify(operations, times(1)).createCollection(First::class.java,collectionOptions) + verify(operations, times(1)).createCollection(First::class.java, collectionOptions) } - @Test + @Test // DATAMONGO-1689 fun `createCollection() with reified type parameter extension should call its Java counterpart`() { + operations.createCollection() verify(operations, times(1)).createCollection(First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `createCollection(CollectionOptions) with reified type parameter extension should call its Java counterpart`() { + val collectionOptions = mock() operations.createCollection(collectionOptions) verify(operations, times(1)).createCollection(First::class.java, collectionOptions) } - @Test + @Test // DATAMONGO-1689 fun `collectionExists(KClass) extension should call its Java counterpart`() { + operations.collectionExists(First::class) verify(operations, times(1)).collectionExists(First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `collectionExists() with reified type parameter extension should call its Java counterpart`() { + operations.collectionExists() verify(operations, times(1)).collectionExists(First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `dropCollection(KClass) extension should call its Java counterpart`() { + operations.dropCollection(First::class) verify(operations, times(1)).dropCollection(First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `dropCollection() with reified type parameter extension should call its Java counterpart`() { + operations.dropCollection() verify(operations, times(1)).dropCollection(First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `indexOps(KClass) extension should call its Java counterpart`() { + operations.indexOps(First::class) verify(operations, times(1)).indexOps(First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `indexOps() with reified type parameter extension should call its Java counterpart`() { + operations.indexOps() verify(operations, times(1)).indexOps(First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `bulkOps(BulkMode, KClass) extension should call its Java counterpart`() { + val bulkMode = BulkMode.ORDERED + operations.bulkOps(bulkMode, First::class) verify(operations, times(1)).bulkOps(bulkMode, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `bulkOps(BulkMode, KClass, String) extension should call its Java counterpart`() { + val bulkMode = BulkMode.ORDERED val collectionName = "foo" + operations.bulkOps(bulkMode, First::class, collectionName) verify(operations, times(1)).bulkOps(bulkMode, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `bulkOps(BulkMode) with reified type parameter extension should call its Java counterpart`() { + val bulkMode = BulkMode.ORDERED + operations.bulkOps(bulkMode) verify(operations, times(1)).bulkOps(bulkMode, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `bulkOps(BulkMode, String) with reified type parameter extension should call its Java counterpart`() { + val bulkMode = BulkMode.ORDERED val collectionName = "foo" + operations.bulkOps(bulkMode, collectionName) verify(operations, times(1)).bulkOps(bulkMode, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `findAll() with reified type parameter extension should call its Java counterpart`() { + operations.findAll() verify(operations, times(1)).findAll(First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `findAll(String) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" + operations.findAll(collectionName) verify(operations, times(1)).findAll(First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `group(String, GroupBy) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" val groupBy = mock() + operations.group(collectionName, groupBy) verify(operations, times(1)).group(collectionName, groupBy, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `group(Criteria, String, GroupBy) with reified type parameter extension should call its Java counterpart`() { + val criteria = mock() val collectionName = "foo" val groupBy = mock() + operations.group(criteria, collectionName, groupBy) verify(operations, times(1)).group(criteria, collectionName, groupBy, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `aggregate(Aggregation, KClass) with reified type parameter extension should call its Java counterpart`() { + val aggregation = mock() + operations.aggregate(aggregation, Second::class) verify(operations, times(1)).aggregate(aggregation, Second::class.java, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `aggregate(Aggregation, String) with reified type parameter extension should call its Java counterpart`() { + val aggregation = mock() val collectionName = "foo" + operations.aggregate(aggregation, collectionName) verify(operations, times(1)).aggregate(aggregation, collectionName, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `aggregateStream(Aggregation, KClass) with reified type parameter extension should call its Java counterpart`() { + val aggregation = mock() + operations.aggregateStream(aggregation, Second::class) verify(operations, times(1)).aggregateStream(aggregation, Second::class.java, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `aggregateStream(Aggregation, String) with reified type parameter extension should call its Java counterpart`() { + val aggregation = mock() val collectionName = "foo" + operations.aggregateStream(aggregation, collectionName) verify(operations, times(1)).aggregateStream(aggregation, collectionName, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `mapReduce(String, String, String) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" val mapFunction = "bar" val reduceFunction = "baz" + operations.mapReduce(collectionName, mapFunction, reduceFunction) verify(operations, times(1)).mapReduce(collectionName, mapFunction, reduceFunction, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `mapReduce(String, String, String, MapReduceOptions) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" val mapFunction = "bar" val reduceFunction = "baz" val options = mock() + operations.mapReduce(collectionName, mapFunction, reduceFunction, options) verify(operations, times(1)).mapReduce(collectionName, mapFunction, reduceFunction, options, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `mapReduce(Query, String, String, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() val collectionName = "foo" val mapFunction = "bar" val reduceFunction = "baz" + operations.mapReduce(query, collectionName, mapFunction, reduceFunction) verify(operations, times(1)).mapReduce(query, collectionName, mapFunction, reduceFunction, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `mapReduce(Query, String, String, String, MapReduceOptions) with reified type parameter extension should call its Java counterpart`() { + val query = mock() val collectionName = "foo" val mapFunction = "bar" val reduceFunction = "baz" val options = mock() + operations.mapReduce(query, collectionName, mapFunction, reduceFunction, options) verify(operations, times(1)).mapReduce(query, collectionName, mapFunction, reduceFunction, options, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `geoNear(Query) with reified type parameter extension should call its Java counterpart`() { + val query = NearQuery.near(0.0, 0.0) + operations.geoNear(query) verify(operations, times(1)).geoNear(query, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `geoNear(Query, String) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" val query = NearQuery.near(0.0, 0.0) + operations.geoNear(query, collectionName) verify(operations, times(1)).geoNear(query, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `findOne(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.findOne(query) verify(operations, times(1)).findOne(query, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `findOne(Query, String) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" val query = mock() + operations.findOne(query, collectionName) verify(operations, times(1)).findOne(query, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `exists(Query, KClass) extension should call its Java counterpart`() { + val query = mock() + operations.exists(query, First::class) verify(operations, times(1)).exists(query, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `exists(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.exists(query) verify(operations, times(1)).exists(query, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `find(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.find(query) verify(operations, times(1)).find(query, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `find(Query, String) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" val query = mock() + operations.find(query, collectionName) verify(operations, times(1)).find(query, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `findById(Any) with reified type parameter extension should call its Java counterpart`() { + val id = 1L + operations.findById(id) verify(operations, times(1)).findById(id, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `findById(Any, String) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" val id = 1L + operations.findById(id, collectionName) verify(operations, times(1)).findById(id, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `findAndModify(Query, Update, FindAndModifyOptions) with reified type parameter extension should call its Java counterpart`() { + val query = mock() val update = mock() val options = mock() + operations.findAndModify(query, update, options) verify(operations, times(1)).findAndModify(query, update, options, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `findAndModify(Query, Update, FindAndModifyOptions, String) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" val query = mock() val update = mock() val options = mock() + operations.findAndModify(query, update, options, collectionName) verify(operations, times(1)).findAndModify(query, update, options, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `findAndRemove(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.findAndRemove(query) verify(operations, times(1)).findAndRemove(query, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `findAndRemove(Query, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() val collectionName = "foo" + operations.findAndRemove(query, collectionName) verify(operations, times(1)).findAndRemove(query, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `count() with reified type parameter extension should call its Java counterpart`() { + operations.count() verify(operations, times(1)).count(any(), eq(First::class.java)) } - @Test + @Test // DATAMONGO-1689 fun `count(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.count(query) verify(operations, times(1)).count(query, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `count(Query, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() val collectionName = "foo" + operations.count(query, collectionName) verify(operations, times(1)).count(query, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `count(Query, KClass) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.count(query, First::class) verify(operations, times(1)).count(query, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `count(Query, KClass, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() val collectionName = "foo" + operations.count(query, First::class, collectionName) verify(operations, times(1)).count(query, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `insert(Collection, KClass) extension should call its Java counterpart`() { + val collection = listOf(First(), First()) + operations.insert(collection, First::class) verify(operations, times(1)).insert(collection, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `upsert(Query, Update, KClass) extension should call its Java counterpart`() { + val query = mock() val update = mock() + operations.upsert(query, update, First::class) verify(operations, times(1)).upsert(query, update, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `upsert(Query, Update, KClass, String) extension should call its Java counterpart`() { + val query = mock() val update = mock() val collectionName = "foo" + operations.upsert(query, update, First::class, collectionName) verify(operations, times(1)).upsert(query, update, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `upsert(Query, Update) with reified type parameter extension should call its Java counterpart`() { + val query = mock() val update = mock() + operations.upsert(query, update) verify(operations, times(1)).upsert(query, update, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `upsert(Query, Update, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() val update = mock() val collectionName = "foo" + operations.upsert(query, update, collectionName) verify(operations, times(1)).upsert(query, update, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `updateFirst(Query, Update, KClass) extension should call its Java counterpart`() { + val query = mock() val update = mock() + operations.updateFirst(query, update, First::class) verify(operations, times(1)).updateFirst(query, update, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `updateFirst(Query, Update, KClass, String) extension should call its Java counterpart`() { + val query = mock() val update = mock() val collectionName = "foo" + operations.updateFirst(query, update, First::class, collectionName) verify(operations, times(1)).updateFirst(query, update, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `updateFirst(Query, Update) with reified type parameter extension should call its Java counterpart`() { + val query = mock() val update = mock() + operations.updateFirst(query, update) verify(operations, times(1)).updateFirst(query, update, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `updateFirst(Query, Update, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() val update = mock() val collectionName = "foo" + operations.updateFirst(query, update, collectionName) verify(operations, times(1)).updateFirst(query, update, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `updateMulti(Query, Update, KClass) extension should call its Java counterpart`() { + val query = mock() val update = mock() + operations.updateMulti(query, update, First::class) verify(operations, times(1)).updateMulti(query, update, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `updateMulti(Query, Update, KClass, String) extension should call its Java counterpart`() { + val query = mock() val update = mock() val collectionName = "foo" + operations.updateMulti(query, update, First::class, collectionName) verify(operations, times(1)).updateMulti(query, update, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `updateMulti(Query, Update) with reified type parameter extension should call its Java counterpart`() { + val query = mock() val update = mock() + operations.updateMulti(query, update) verify(operations, times(1)).updateMulti(query, update, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `updateMulti(Query, Update, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() val update = mock() val collectionName = "foo" + operations.updateMulti(query, update, collectionName) verify(operations, times(1)).updateMulti(query, update, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `remove(Query, KClass) extension should call its Java counterpart`() { + val query = mock() + operations.remove(query, First::class) verify(operations, times(1)).remove(query, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `remove(Query, KClass, String) extension should call its Java counterpart`() { + val query = mock() val collectionName = "foo" + operations.remove(query, First::class, collectionName) verify(operations, times(1)).remove(query, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `remove(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.remove(query) verify(operations, times(1)).remove(query, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `remove(Query, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() val collectionName = "foo" + operations.remove(query, collectionName) verify(operations, times(1)).remove(query, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `findAllAndRemove(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.findAllAndRemove(query) verify(operations, times(1)).findAllAndRemove(query, First::class.java) } - } diff --git a/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ReactiveMongoOperationsExtensionsTests.kt b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ReactiveMongoOperationsExtensionsTests.kt index b45c563dc9..e41af8fc0b 100644 --- a/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ReactiveMongoOperationsExtensionsTests.kt +++ b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ReactiveMongoOperationsExtensionsTests.kt @@ -1,5 +1,5 @@ /* - * Copyright 2016-2017 the original author or authors. + * Copyright 2017 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. @@ -37,398 +37,493 @@ class ReactiveMongoOperationsExtensionsTests { @Mock(answer = Answers.RETURNS_MOCKS) lateinit var operations: ReactiveMongoOperations - @Test + @Test // DATAMONGO-1689 fun `indexOps(KClass) extension should call its Java counterpart`() { + operations.indexOps(First::class) verify(operations, times(1)).indexOps(First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `indexOps() with reified type parameter extension should call its Java counterpart`() { + operations.indexOps() verify(operations, times(1)).indexOps(First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `execute(ReactiveCollectionCallback) with reified type parameter extension should call its Java counterpart`() { + val collectionCallback = mock>() + operations.execute(collectionCallback) verify(operations, times(1)).execute(First::class.java, collectionCallback) } - @Test + @Test // DATAMONGO-1689 fun `createCollection(KClass) extension should call its Java counterpart`() { + operations.createCollection(First::class) verify(operations, times(1)).createCollection(First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `createCollection(KClass, CollectionOptions) extension should call its Java counterpart`() { + val collectionOptions = mock() + operations.createCollection(First::class, collectionOptions) verify(operations, times(1)).createCollection(First::class.java, collectionOptions) } - @Test + @Test // DATAMONGO-1689 fun `createCollection() with reified type parameter extension should call its Java counterpart`() { + operations.createCollection() verify(operations, times(1)).createCollection(First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `createCollection(CollectionOptions) with reified type parameter extension should call its Java counterpart`() { + val collectionOptions = mock() + operations.createCollection(collectionOptions) verify(operations, times(1)).createCollection(First::class.java, collectionOptions) } - @Test + @Test // DATAMONGO-1689 fun `collectionExists(KClass) extension should call its Java counterpart`() { + operations.collectionExists(First::class) verify(operations, times(1)).collectionExists(First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `collectionExists() with reified type parameter extension should call its Java counterpart`() { + operations.collectionExists() verify(operations, times(1)).collectionExists(First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `dropCollection(KClass) extension should call its Java counterpart`() { + operations.dropCollection(First::class) verify(operations, times(1)).dropCollection(First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `dropCollection() with reified type parameter extension should call its Java counterpart`() { + operations.dropCollection() verify(operations, times(1)).dropCollection(First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `findAll() with reified type parameter extension should call its Java counterpart`() { + operations.findAll() verify(operations, times(1)).findAll(First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `findAll(String) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" + operations.findAll(collectionName) verify(operations, times(1)).findAll(First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `findOne(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.findOne(query) verify(operations, times(1)).findOne(query, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `findOne(Query, String) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" val query = mock() + operations.findOne(query, collectionName) verify(operations, times(1)).findOne(query, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `exists(Query, KClass) extension should call its Java counterpart`() { + val query = mock() + operations.exists(query, First::class) verify(operations, times(1)).exists(query, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `exists(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.exists(query) verify(operations, times(1)).exists(query, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `find(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.find(query) verify(operations, times(1)).find(query, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `find(Query, String) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" val query = mock() + operations.find(query, collectionName) verify(operations, times(1)).find(query, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `findById(Any) with reified type parameter extension should call its Java counterpart`() { + val id = 1L + operations.findById(id) verify(operations, times(1)).findById(id, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `findById(Any, String) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" val id = 1L + operations.findById(id, collectionName) verify(operations, times(1)).findById(id, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `geoNear(Query) with reified type parameter extension should call its Java counterpart`() { + val query = NearQuery.near(0.0, 0.0) + operations.geoNear(query) verify(operations, times(1)).geoNear(query, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `geoNear(Query, String) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" val query = NearQuery.near(0.0, 0.0) + operations.geoNear(query, collectionName) verify(operations, times(1)).geoNear(query, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `findAndModify(Query, Update, FindAndModifyOptions) with reified type parameter extension should call its Java counterpart`() { + val query = mock() val update = mock() val options = mock() + operations.findAndModify(query, update, options) verify(operations, times(1)).findAndModify(query, update, options, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `findAndModify(Query, Update, FindAndModifyOptions, String) with reified type parameter extension should call its Java counterpart`() { + val collectionName = "foo" val query = mock() val update = mock() val options = mock() + operations.findAndModify(query, update, options, collectionName) verify(operations, times(1)).findAndModify(query, update, options, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `findAndRemove(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.findAndRemove(query) verify(operations, times(1)).findAndRemove(query, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `findAndRemove(Query, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() val collectionName = "foo" + operations.findAndRemove(query, collectionName) verify(operations, times(1)).findAndRemove(query, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `count() with reified type parameter extension should call its Java counterpart`() { + operations.count() verify(operations, times(1)).count(any(), eq(First::class.java)) } - @Test + @Test // DATAMONGO-1689 fun `count(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.count(query) verify(operations, times(1)).count(query, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `count(Query, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() val collectionName = "foo" + operations.count(query, collectionName) verify(operations, times(1)).count(query, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `count(Query, KClass) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.count(query, First::class) verify(operations, times(1)).count(query, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `count(Query, KClass, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() val collectionName = "foo" + operations.count(query, First::class, collectionName) verify(operations, times(1)).count(query, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `insert(Collection, KClass) extension should call its Java counterpart`() { + val collection = listOf(First(), First()) + operations.insert(collection, First::class) verify(operations, times(1)).insert(collection, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `insertAll(Mono, KClass) extension should call its Java counterpart`() { + val collection = Mono.just(listOf(First(), First())) + operations.insertAll(collection, First::class) verify(operations, times(1)).insertAll(collection, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `upsert(Query, Update, KClass) extension should call its Java counterpart`() { + val query = mock() val update = mock() + operations.upsert(query, update, First::class) verify(operations, times(1)).upsert(query, update, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `upsert(Query, Update, KClass, String) extension should call its Java counterpart`() { + val query = mock() val update = mock() val collectionName = "foo" + operations.upsert(query, update, First::class, collectionName) verify(operations, times(1)).upsert(query, update, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `upsert(Query, Update) with reified type parameter extension should call its Java counterpart`() { + val query = mock() val update = mock() + operations.upsert(query, update) verify(operations, times(1)).upsert(query, update, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `upsert(Query, Update, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() val update = mock() val collectionName = "foo" + operations.upsert(query, update, collectionName) verify(operations, times(1)).upsert(query, update, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `updateFirst(Query, Update, KClass) extension should call its Java counterpart`() { + val query = mock() val update = mock() + operations.updateFirst(query, update, First::class) verify(operations, times(1)).updateFirst(query, update, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `updateFirst(Query, Update, KClass, String) extension should call its Java counterpart`() { + val query = mock() val update = mock() val collectionName = "foo" + operations.updateFirst(query, update, First::class, collectionName) verify(operations, times(1)).updateFirst(query, update, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `updateFirst(Query, Update) with reified type parameter extension should call its Java counterpart`() { + val query = mock() val update = mock() + operations.updateFirst(query, update) verify(operations, times(1)).updateFirst(query, update, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `updateFirst(Query, Update, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() val update = mock() val collectionName = "foo" + operations.updateFirst(query, update, collectionName) verify(operations, times(1)).updateFirst(query, update, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `updateMulti(Query, Update, KClass) extension should call its Java counterpart`() { + val query = mock() val update = mock() + operations.updateMulti(query, update, First::class) verify(operations, times(1)).updateMulti(query, update, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `updateMulti(Query, Update, KClass, String) extension should call its Java counterpart`() { + val query = mock() val update = mock() val collectionName = "foo" + operations.updateMulti(query, update, First::class, collectionName) verify(operations, times(1)).updateMulti(query, update, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `updateMulti(Query, Update) with reified type parameter extension should call its Java counterpart`() { + val query = mock() val update = mock() + operations.updateMulti(query, update) verify(operations, times(1)).updateMulti(query, update, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `updateMulti(Query, Update, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() val update = mock() val collectionName = "foo" + operations.updateMulti(query, update, collectionName) verify(operations, times(1)).updateMulti(query, update, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `remove(Query, KClass) extension should call its Java counterpart`() { + val query = mock() + operations.remove(query, First::class) verify(operations, times(1)).remove(query, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `remove(Query, KClass, String) extension should call its Java counterpart`() { + val query = mock() val collectionName = "foo" + operations.remove(query, First::class, collectionName) verify(operations, times(1)).remove(query, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `remove(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.remove(query) verify(operations, times(1)).remove(query, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `remove(Query, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() val collectionName = "foo" + operations.remove(query, collectionName) verify(operations, times(1)).remove(query, First::class.java, collectionName) } - @Test + @Test // DATAMONGO-1689 fun `findAllAndRemove(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.findAllAndRemove(query) verify(operations, times(1)).findAllAndRemove(query, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `tail(Query) with reified type parameter extension should call its Java counterpart`() { + val query = mock() + operations.tail(query) verify(operations, times(1)).tail(query, First::class.java) } - @Test + @Test // DATAMONGO-1689 fun `tail(Query, String) with reified type parameter extension should call its Java counterpart`() { + val query = mock() val collectionName = "foo" + operations.tail(query, collectionName) verify(operations, times(1)).tail(query, First::class.java, collectionName) } - } From 4c59d84d0030de0cb90dfd228c02737626423cf8 Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Wed, 14 Jun 2017 15:54:09 +0200 Subject: [PATCH 4/5] DATAMONGO-1689 - Polishing. Bring back Maven Kotlin configuration for now + documentation fixes. --- spring-data-mongodb/pom.xml | 77 ++++++++++++++- .../mongodb/core/MongoOperationsExtensions.kt | 98 +++++++------------ .../core/ReactiveMongoOperationsExtensions.kt | 66 +++++-------- 3 files changed, 130 insertions(+), 111 deletions(-) diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index 6ce66c4cb9..d77efc652c 100644 --- a/spring-data-mongodb/pom.xml +++ b/spring-data-mongodb/pom.xml @@ -18,6 +18,7 @@ 1.3 1.5 + 1.1.2-5 @@ -232,7 +233,7 @@ org.jetbrains.kotlin - kotlin-stdlib-jre8 + kotlin-stdlib ${kotlin} true @@ -251,8 +252,22 @@ com.nhaarman mockito-kotlin - 1.4.0 + 1.5.0 test + + + org.jetbrains.kotlin + kotlin-stdlib + + + org.jetbrains.kotlin + kotlin-reflect + + + org.mockito + mockito-core + + @@ -260,6 +275,64 @@ + + kotlin-maven-plugin + org.jetbrains.kotlin + ${kotlin} + + 1.8 + + + + compile + compile + compile + + + ${project.basedir}/src/main/kotlin + ${project.basedir}/src/main/java + + + + + test-compile + test-compile + test-compile + + + ${project.basedir}/src/test/kotlin + ${project.basedir}/src/test/java + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + default-compile + none + + + default-testCompile + none + + + java-compile + compile + compile + + + java-test-compile + test-compile + testCompile + + + + com.mysema.maven apt-maven-plugin diff --git a/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/MongoOperationsExtensions.kt b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/MongoOperationsExtensions.kt index 62abe677f1..7956df812b 100644 --- a/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/MongoOperationsExtensions.kt +++ b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/MongoOperationsExtensions.kt @@ -44,8 +44,7 @@ fun MongoOperations.getCollectionName(entityClass: KClass): String getCollectionName(entityClass.java) /** - * Extension for [MongoOperations.getCollectionName] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters. + * Extension for [MongoOperations.getCollectionName] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -54,8 +53,7 @@ inline fun MongoOperations.getCollectionName(): String = getCollectionName(T::class.java) /** - * Extension for [MongoOperations.execute] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters. + * Extension for [MongoOperations.execute] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -64,8 +62,7 @@ inline fun MongoOperations.execute(action: CollectionCallback< execute(T::class.java, action) /** - * Extension for [MongoOperations.stream] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters. + * Extension for [MongoOperations.stream] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -74,8 +71,7 @@ inline fun MongoOperations.stream(query: Query): CloseableIter stream(query, T::class.java) /** - * Extension for [MongoOperations.stream] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters. + * Extension for [MongoOperations.stream] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -95,8 +91,7 @@ fun MongoOperations.createCollection(entityClass: KClass, collectio else createCollection(entityClass.java) /** - * Extension for [MongoOperations.createCollection] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters. + * Extension for [MongoOperations.createCollection] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -116,8 +111,7 @@ fun MongoOperations.collectionExists(entityClass: KClass): Boolean collectionExists(entityClass.java) /** - * Extension for [MongoOperations.collectionExists] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters. + * Extension for [MongoOperations.collectionExists] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -136,8 +130,7 @@ fun MongoOperations.dropCollection(entityClass: KClass) { } /** - * Extension for [MongoOperations.dropCollection] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters. + * Extension for [MongoOperations.dropCollection] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -156,8 +149,7 @@ fun MongoOperations.indexOps(entityClass: KClass): IndexOperations indexOps(entityClass.java) /** - * Extension for [MongoOperations.indexOps] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters. + * Extension for [MongoOperations.indexOps] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -176,7 +168,7 @@ fun MongoOperations.bulkOps(bulkMode: BulkMode, entityClass: KClass else bulkOps(bulkMode, entityClass.java) /** - * Extension for [MongoOperations.bulkOps] providing a [KClass] based variant. + * Extension for [MongoOperations.bulkOps] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -187,8 +179,7 @@ inline fun MongoOperations.bulkOps(bulkMode: BulkMode, collect else bulkOps(bulkMode, T::class.java) /** - * Extension for [MongoOperations.findAll] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [MongoOperations.findAll] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -197,8 +188,7 @@ inline fun MongoOperations.findAll(collectionName: String? = n if (collectionName != null) findAll(T::class.java, collectionName) else findAll(T::class.java) /** - * Extension for [MongoOperations.group] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [MongoOperations.group] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -207,8 +197,7 @@ inline fun MongoOperations.group(inputCollectionName: String, group(inputCollectionName, groupBy, T::class.java) /** - * Extension for [MongoOperations.group] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [MongoOperations.group] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -217,8 +206,7 @@ inline fun MongoOperations.group(criteria: Criteria, inputColl group(criteria, inputCollectionName, groupBy, T::class.java) /** - * Extension for [MongoOperations.aggregate] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [MongoOperations.aggregate] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -227,8 +215,7 @@ inline fun MongoOperations.aggregate(aggregation: Aggregation, aggregate(aggregation, inputType.java, O::class.java) /** - * Extension for [MongoOperations.aggregate] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [MongoOperations.aggregate] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -237,8 +224,7 @@ inline fun MongoOperations.aggregate(aggregation: Aggregation, aggregate(aggregation, collectionName, O::class.java) /** - * Extension for [MongoOperations.aggregateStream] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [MongoOperations.aggregateStream] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -247,8 +233,7 @@ inline fun MongoOperations.aggregateStream(aggregation: Aggreg aggregateStream(aggregation, inputType.java, O::class.java) /** - * Extension for [MongoOperations.aggregateStream] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [MongoOperations.aggregateStream] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -257,8 +242,7 @@ inline fun MongoOperations.aggregateStream(aggregation: Aggreg aggregateStream(aggregation, collectionName, O::class.java) /** - * Extension for [MongoOperations.mapReduce] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [MongoOperations.mapReduce] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -268,8 +252,7 @@ inline fun MongoOperations.mapReduce(collectionName: String, m else mapReduce(collectionName, mapFunction, reduceFunction, T::class.java) /** - * Extension for [MongoOperations.mapReduce] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [MongoOperations.mapReduce] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 52.0 @@ -279,8 +262,7 @@ inline fun MongoOperations.mapReduce(query: Query, collectionN else mapReduce(query, collectionName, mapFunction, reduceFunction, T::class.java) /** - * Extension for [MongoOperations.geoNear] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [MongoOperations.geoNear] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -290,8 +272,7 @@ inline fun MongoOperations.geoNear(near: NearQuery, collection else geoNear(near, T::class.java) /** - * Extension for [MongoOperations.findOne] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [MongoOperations.findOne] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -300,8 +281,7 @@ inline fun MongoOperations.findOne(query: Query, collectionNam if (collectionName != null) findOne(query, T::class.java, collectionName) else findOne(query, T::class.java) /** - * Extension for [MongoOperations.exists] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [MongoOperations.exists] providing a [KClass] based variant. * * @author Sebastien Deleuze * @since 2.0 @@ -311,8 +291,7 @@ fun MongoOperations.exists(query: Query, entityClass: KClass, colle else exists(query, entityClass.java) /** - * Extension for [MongoOperations.exists] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [MongoOperations.exists] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -323,8 +302,7 @@ inline fun MongoOperations.exists(query: Query, collectionName else exists(query, T::class.java) /** - * Extension for [MongoOperations.find] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [MongoOperations.find] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -334,8 +312,7 @@ inline fun MongoOperations.find(query: Query, collectionName: else find(query, T::class.java) /** - * Extension for [MongoOperations.findById] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [MongoOperations.findById] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -345,8 +322,7 @@ inline fun MongoOperations.findById(id: Any, collectionName: S else findById(id, T::class.java) /** - * Extension for [MongoOperations.findAndModify] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [MongoOperations.findAndModify] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -356,8 +332,7 @@ inline fun MongoOperations.findAndModify(query: Query, update: else findAndModify(query, update, options, T::class.java) /** - * Extension for [MongoOperations.findAndRemove] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [MongoOperations.findAndRemove] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -367,8 +342,7 @@ inline fun MongoOperations.findAndRemove(query: Query, collect else findAndRemove(query, T::class.java) /** - * Extension for [MongoOperations.count] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [MongoOperations.count] providing a [KClass] based variant. * * @author Sebastien Deleuze * @since 2.0 @@ -378,8 +352,7 @@ fun MongoOperations.count(query: Query = Query(), entityClass: KClass< else count(query, entityClass.java) /** - * Extension for [MongoOperations.count] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [MongoOperations.count] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -409,8 +382,7 @@ fun MongoOperations.upsert(query: Query, update: Update, entityClass: else upsert(query, update, entityClass.java) /** - * Extension for [MongoOperations.upsert] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters. + * Extension for [MongoOperations.upsert] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -431,8 +403,7 @@ fun MongoOperations.updateFirst(query: Query, update: Update, entityCl else updateFirst(query, update, entityClass.java) /** - * Extension for [MongoOperations.updateFirst] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters. + * Extension for [MongoOperations.updateFirst] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -454,8 +425,7 @@ fun MongoOperations.updateMulti(query: Query, update: Update, entityCl else updateMulti(query, update, entityClass.java) /** - * Extension for [MongoOperations.updateMulti] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters. + * Extension for [MongoOperations.updateMulti] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -476,8 +446,7 @@ fun MongoOperations.remove(query: Query, entityClass: KClass, colle else remove(query, entityClass.java) /** - * Extension for [MongoOperations.remove] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters. + * Extension for [MongoOperations.remove] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -488,8 +457,7 @@ inline fun MongoOperations.remove(query: Query, collectionName else remove(query, T::class.java) /** - * Extension for [MongoOperations.findAllAndRemove] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters. + * Extension for [MongoOperations.findAllAndRemove] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 diff --git a/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ReactiveMongoOperationsExtensions.kt b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ReactiveMongoOperationsExtensions.kt index a3f6911d60..5e92c8db49 100644 --- a/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ReactiveMongoOperationsExtensions.kt +++ b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ReactiveMongoOperationsExtensions.kt @@ -37,8 +37,7 @@ fun ReactiveMongoOperations.indexOps(entityClass: KClass): Reactive indexOps(entityClass.java) /** - * Extension for [ReactiveMongoOperations.indexOps] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters. + * Extension for [ReactiveMongoOperations.indexOps] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -47,8 +46,7 @@ inline fun ReactiveMongoOperations.indexOps(): ReactiveIndexOp indexOps(T::class.java) /** - * Extension for [ReactiveMongoOperations.execute] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters. + * Extension for [ReactiveMongoOperations.execute] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -66,8 +64,7 @@ fun ReactiveMongoOperations.createCollection(entityClass: KClass, c if (collectionOptions != null) createCollection(entityClass.java, collectionOptions) else createCollection(entityClass.java) /** - * Extension for [ReactiveMongoOperations.createCollection] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters. + * Extension for [ReactiveMongoOperations.createCollection] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -85,8 +82,7 @@ fun ReactiveMongoOperations.collectionExists(entityClass: KClass): collectionExists(entityClass.java) /** - * Extension for [ReactiveMongoOperations.collectionExists] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters. + * Extension for [ReactiveMongoOperations.collectionExists] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -104,8 +100,7 @@ fun ReactiveMongoOperations.dropCollection(entityClass: KClass): Mo dropCollection(entityClass.java) /** - * Extension for [ReactiveMongoOperations.dropCollection] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters. + * Extension for [ReactiveMongoOperations.dropCollection] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -115,8 +110,7 @@ inline fun ReactiveMongoOperations.dropCollection(): Mono ReactiveMongoOperations.findAll(collectionName: Str if (collectionName != null) findAll(T::class.java, collectionName) else findAll(T::class.java) /** - * Extension for [ReactiveMongoOperations.findOne] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [ReactiveMongoOperations.findOne] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -135,8 +128,7 @@ inline fun ReactiveMongoOperations.findOne(query: Query, colle if (collectionName != null) findOne(query, T::class.java, collectionName) else findOne(query, T::class.java) /** - * Extension for [ReactiveMongoOperations.exists] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [ReactiveMongoOperations.exists] providing a [KClass] based variant. * * @author Sebastien Deleuze * @since 2.0 @@ -145,8 +137,7 @@ fun ReactiveMongoOperations.exists(query: Query, entityClass: KClass ReactiveMongoOperations.exists(query: Query, collec if (collectionName != null) exists(query, T::class.java, collectionName) else exists(query, T::class.java) /** - * Extension for [ReactiveMongoOperations.find] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [ReactiveMongoOperations.find] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -166,8 +156,7 @@ inline fun ReactiveMongoOperations.find(query: Query, collecti if (collectionName != null) find(query, T::class.java, collectionName) else find(query, T::class.java) /** - * Extension for [ReactiveMongoOperations.findById] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [ReactiveMongoOperations.findById] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -176,8 +165,7 @@ inline fun ReactiveMongoOperations.findById(id: Any, collectio if (collectionName != null) findById(id, T::class.java, collectionName) else findById(id, T::class.java) /** - * Extension for [ReactiveMongoOperations.geoNear] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [ReactiveMongoOperations.geoNear] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -186,8 +174,7 @@ inline fun ReactiveMongoOperations.geoNear(near: NearQuery, co if (collectionName != null) geoNear(near, T::class.java, collectionName) else geoNear(near, T::class.java) /** - * Extension for [ReactiveMongoOperations.findAndModify] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [ReactiveMongoOperations.findAndModify] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -196,8 +183,7 @@ inline fun ReactiveMongoOperations.findAndModify(query: Query, if (collectionName != null) findAndModify(query, update, options, T::class.java, collectionName) else findAndModify(query, update, options, T::class.java) /** - * Extension for [ReactiveMongoOperations.findAndRemove] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [ReactiveMongoOperations.findAndRemove] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -207,8 +193,7 @@ inline fun ReactiveMongoOperations.findAndRemove(query: Query, else findAndRemove(query, T::class.java) /** - * Extension for [ReactiveMongoOperations.count] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [ReactiveMongoOperations.count] providing a [KClass] based variant. * * @author Sebastien Deleuze * @since 2.0 @@ -218,8 +203,7 @@ fun ReactiveMongoOperations.count(query: Query = Query(), entityClass: else count(query, entityClass.java) /** - * Extension for [ReactiveMongoOperations.count] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters + * Extension for [ReactiveMongoOperations.count] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -257,8 +241,7 @@ fun ReactiveMongoOperations.upsert(query: Query, update: Update, entit if (collectionName != null) upsert(query, update, entityClass.java, collectionName) else upsert(query, update, entityClass.java) /** - * Extension for [ReactiveMongoOperations.upsert] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters. + * Extension for [ReactiveMongoOperations.upsert] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -279,8 +262,7 @@ fun ReactiveMongoOperations.updateFirst(query: Query, update: Update, else updateFirst(query, update, entityClass.java) /** - * Extension for [ReactiveMongoOperations.updateFirst] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters. + * Extension for [ReactiveMongoOperations.updateFirst] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -301,8 +283,7 @@ fun ReactiveMongoOperations.updateMulti(query: Query, update: Update, else updateMulti(query, update, entityClass.java) /** - * Extension for [ReactiveMongoOperations.updateMulti] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters. + * Extension for [ReactiveMongoOperations.updateMulti] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -323,8 +304,7 @@ fun ReactiveMongoOperations.remove(query: Query, entityClass: KClass ReactiveMongoOperations.remove(query: Query, collec else remove(query, T::class.java) /** - * Extension for [ReactiveMongoOperations.findAllAndRemove] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters. + * Extension for [ReactiveMongoOperations.findAllAndRemove] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 @@ -346,8 +325,7 @@ inline fun ReactiveMongoOperations.findAllAndRemove(query: Que findAllAndRemove(query, T::class.java) /** - * Extension for [ReactiveMongoOperations.tail] avoiding requiring the type parameter - * thanks to Kotlin reified type parameters. + * Extension for [ReactiveMongoOperations.tail] leveraging reified type parameters. * * @author Sebastien Deleuze * @since 2.0 From 969b53cb4713d75076d3bdf1d8e268a7c58643d9 Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Wed, 14 Jun 2017 15:54:22 +0200 Subject: [PATCH 5/5] DATAMONGO-1689 - Add fluent extensions. --- ...xecutableAggregationOperationExtensions.kt | 36 ++++++++++++ .../core/ExecutableFindOperationExtensions.kt | 57 +++++++++++++++++++ .../ExecutableInsertOperationExtensions.kt | 36 ++++++++++++ .../ExecutableRemoveOperationExtensions.kt | 36 ++++++++++++ ...ableAggregationOperationExtensionsTests.kt | 32 +++++++++++ .../ExecutableFindOperationExtensionsTests.kt | 47 +++++++++++++++ ...xecutableInsertOperationExtensionsTests.kt | 32 +++++++++++ ...xecutableRemoveOperationExtensionsTests.kt | 32 +++++++++++ 8 files changed, 308 insertions(+) create mode 100644 spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ExecutableAggregationOperationExtensions.kt create mode 100644 spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ExecutableFindOperationExtensions.kt create mode 100644 spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ExecutableInsertOperationExtensions.kt create mode 100644 spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ExecutableRemoveOperationExtensions.kt create mode 100644 spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ExecutableAggregationOperationExtensionsTests.kt create mode 100644 spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ExecutableFindOperationExtensionsTests.kt create mode 100644 spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ExecutableInsertOperationExtensionsTests.kt create mode 100644 spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ExecutableRemoveOperationExtensionsTests.kt diff --git a/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ExecutableAggregationOperationExtensions.kt b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ExecutableAggregationOperationExtensions.kt new file mode 100644 index 0000000000..5b43222c42 --- /dev/null +++ b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ExecutableAggregationOperationExtensions.kt @@ -0,0 +1,36 @@ +/* + * Copyright 2017 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 + * + * http://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.mongodb.core + +import kotlin.reflect.KClass + +/** + * Extension for [ExecutableAggregationOperation.aggregateAndReturn] providing a [KClass] based variant. + * + * @author Sebastien Deleuze + * @since 2.0 + */ +fun ExecutableAggregationOperation.aggregateAndReturn(entityClass: KClass): ExecutableAggregationOperation.AggregationOperation = + aggregateAndReturn(entityClass.java) + +/** + * Extension for [ExecutableAggregationOperation.aggregateAndReturn] leveraging reified type parameters. + * + * @author Sebastien Deleuze + * @since 2.0 + */ +inline fun ExecutableAggregationOperation.aggregateAndReturn(): ExecutableAggregationOperation.AggregationOperation = + aggregateAndReturn(T::class.java) \ No newline at end of file diff --git a/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ExecutableFindOperationExtensions.kt b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ExecutableFindOperationExtensions.kt new file mode 100644 index 0000000000..be445235e1 --- /dev/null +++ b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ExecutableFindOperationExtensions.kt @@ -0,0 +1,57 @@ +/* + * Copyright 2017 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 + * + * http://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.mongodb.core + +import kotlin.reflect.KClass + +/** + * Extension for [ExecutableFindOperation.query] providing a [KClass] based variant. + * + * @author Sebastien Deleuze + * @since 2.0 + */ +fun ExecutableFindOperation.query(entityClass: KClass): ExecutableFindOperation.FindOperation = + query(entityClass.java) + +/** + * Extension for [ExecutableFindOperation.query] leveraging reified type parameters. + * + * @author Sebastien Deleuze + * @since 2.0 + */ +inline fun ExecutableFindOperation.query(): ExecutableFindOperation.FindOperation = + query(T::class.java) + + +/** + * Extension for [ExecutableFindOperation.FindOperationWithProjection.as] providing a [KClass] based variant. + * + * @author Sebastien Deleuze + * @since 2.0 + */ +fun ExecutableFindOperation.FindOperationWithProjection.asType(resultType: KClass): ExecutableFindOperation.FindOperationWithQuery = + `as`(resultType.java) + +/** + * Extension for [ExecutableFindOperation.FindOperationWithProjection.as] leveraging reified type parameters. + * + * @author Sebastien Deleuze + * @since 2.0 + */ +inline fun ExecutableFindOperation.FindOperationWithProjection.asType(): ExecutableFindOperation.FindOperationWithQuery = + `as`(T::class.java) + + diff --git a/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ExecutableInsertOperationExtensions.kt b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ExecutableInsertOperationExtensions.kt new file mode 100644 index 0000000000..1b0f80fcc0 --- /dev/null +++ b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ExecutableInsertOperationExtensions.kt @@ -0,0 +1,36 @@ +/* + * Copyright 2017 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 + * + * http://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.mongodb.core + +import kotlin.reflect.KClass + +/** + * Extension for [ExecutableInsertOperation.insert] providing a [KClass] based variant. + * + * @author Sebastien Deleuze + * @since 2.0 + */ +fun ExecutableInsertOperation.insert(entityClass: KClass): ExecutableInsertOperation.InsertOperation = + insert(entityClass.java) + +/** + * Extension for [ExecutableInsertOperation.insert] leveraging reified type parameters. + * + * @author Sebastien Deleuze + * @since 2.0 + */ +inline fun ExecutableInsertOperation.insert(): ExecutableInsertOperation.InsertOperation = + insert(T::class.java) \ No newline at end of file diff --git a/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ExecutableRemoveOperationExtensions.kt b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ExecutableRemoveOperationExtensions.kt new file mode 100644 index 0000000000..5c84e67d64 --- /dev/null +++ b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ExecutableRemoveOperationExtensions.kt @@ -0,0 +1,36 @@ +/* + * Copyright 2017 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 + * + * http://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.mongodb.core + +import kotlin.reflect.KClass + +/** + * Extension for [ExecutableRemoveOperation.remove] providing a [KClass] based variant. + * + * @author Sebastien Deleuze + * @since 2.0 + */ +fun ExecutableRemoveOperation.remove(entityClass: KClass): ExecutableRemoveOperation.RemoveOperation = + remove(entityClass.java) + +/** + * Extension for [ExecutableRemoveOperation.remove] leveraging reified type parameters. + * + * @author Sebastien Deleuze + * @since 2.0 + */ +inline fun ExecutableRemoveOperation.remove(): ExecutableRemoveOperation.RemoveOperation = + remove(T::class.java) \ No newline at end of file diff --git a/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ExecutableAggregationOperationExtensionsTests.kt b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ExecutableAggregationOperationExtensionsTests.kt new file mode 100644 index 0000000000..2f49a75998 --- /dev/null +++ b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ExecutableAggregationOperationExtensionsTests.kt @@ -0,0 +1,32 @@ +package org.springframework.data.mongodb.core + +import example.first.First +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Answers +import org.mockito.Mock +import org.mockito.Mockito +import org.mockito.junit.MockitoJUnitRunner + +/** + * @author Sebastien Deleuze + */ +@RunWith(MockitoJUnitRunner::class) +class ExecutableAggregationOperationExtensionsTests { + + @Mock(answer = Answers.RETURNS_MOCKS) + lateinit var operation: ExecutableAggregationOperation + + @Test // DATAMONGO-1689 + fun `aggregateAndReturn(KClass) extension should call its Java counterpart`() { + operation.aggregateAndReturn(First::class) + Mockito.verify(operation, Mockito.times(1)).aggregateAndReturn(First::class.java) + } + + @Test // DATAMONGO-1689 + fun `aggregateAndReturn() with reified type parameter extension should call its Java counterpart`() { + operation.aggregateAndReturn() + Mockito.verify(operation, Mockito.times(1)).aggregateAndReturn(First::class.java) + } + +} diff --git a/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ExecutableFindOperationExtensionsTests.kt b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ExecutableFindOperationExtensionsTests.kt new file mode 100644 index 0000000000..20aa74484d --- /dev/null +++ b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ExecutableFindOperationExtensionsTests.kt @@ -0,0 +1,47 @@ +package org.springframework.data.mongodb.core + +import example.first.First +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Answers +import org.mockito.Mock +import org.mockito.Mockito +import org.mockito.junit.MockitoJUnitRunner + +/** + * @author Sebastien Deleuze + */ +@RunWith(MockitoJUnitRunner::class) +class ExecutableFindOperationExtensionsTests { + + @Mock(answer = Answers.RETURNS_MOCKS) + lateinit var operation: ExecutableFindOperation + + @Mock(answer = Answers.RETURNS_MOCKS) + lateinit var operationWithProjection: ExecutableFindOperation.FindOperationWithProjection + + @Test // DATAMONGO-1689 + fun `ExecutableFindOperation#query(KClass) extension should call its Java counterpart`() { + operation.query(First::class) + Mockito.verify(operation, Mockito.times(1)).query(First::class.java) + } + + @Test // DATAMONGO-1689 + fun `ExecutableFindOperation#query() with reified type parameter extension should call its Java counterpart`() { + operation.query() + Mockito.verify(operation, Mockito.times(1)).query(First::class.java) + } + + @Test // DATAMONGO-1689 + fun `ExecutableFindOperation#FindOperationWithProjection#asType(KClass) extension should call its Java counterpart`() { + operationWithProjection.asType(First::class) + Mockito.verify(operationWithProjection, Mockito.times(1)).`as`(First::class.java) + } + + @Test // DATAMONGO-1689 + fun `ExecutableFindOperation#FindOperationWithProjection#asType() with reified type parameter extension should call its Java counterpart`() { + operationWithProjection.asType() + Mockito.verify(operationWithProjection, Mockito.times(1)).`as`(First::class.java) + } + +} diff --git a/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ExecutableInsertOperationExtensionsTests.kt b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ExecutableInsertOperationExtensionsTests.kt new file mode 100644 index 0000000000..800c8dd82f --- /dev/null +++ b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ExecutableInsertOperationExtensionsTests.kt @@ -0,0 +1,32 @@ +package org.springframework.data.mongodb.core + +import example.first.First +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Answers +import org.mockito.Mock +import org.mockito.Mockito +import org.mockito.junit.MockitoJUnitRunner + +/** + * @author Sebastien Deleuze + */ +@RunWith(MockitoJUnitRunner::class) +class ExecutableInsertOperationExtensionsTests { + + @Mock(answer = Answers.RETURNS_MOCKS) + lateinit var operation: ExecutableInsertOperation + + @Test // DATAMONGO-1689 + fun `insert(KClass) extension should call its Java counterpart`() { + operation.insert(First::class) + Mockito.verify(operation, Mockito.times(1)).insert(First::class.java) + } + + @Test // DATAMONGO-1689 + fun `insert() with reified type parameter extension should call its Java counterpart`() { + operation.insert() + Mockito.verify(operation, Mockito.times(1)).insert(First::class.java) + } + +} diff --git a/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ExecutableRemoveOperationExtensionsTests.kt b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ExecutableRemoveOperationExtensionsTests.kt new file mode 100644 index 0000000000..126ff42e8b --- /dev/null +++ b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ExecutableRemoveOperationExtensionsTests.kt @@ -0,0 +1,32 @@ +package org.springframework.data.mongodb.core + +import example.first.First +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Answers +import org.mockito.Mock +import org.mockito.Mockito +import org.mockito.junit.MockitoJUnitRunner + +/** + * @author Sebastien Deleuze + */ +@RunWith(MockitoJUnitRunner::class) +class ExecutableRemoveOperationExtensionsTests { + + @Mock(answer = Answers.RETURNS_MOCKS) + lateinit var operation: ExecutableRemoveOperation + + @Test // DATAMONGO-1689 + fun `remove(KClass) extension should call its Java counterpart`() { + operation.remove(First::class) + Mockito.verify(operation, Mockito.times(1)).remove(First::class.java) + } + + @Test // DATAMONGO-1689 + fun `remove() with reified type parameter extension should call its Java counterpart`() { + operation.remove() + Mockito.verify(operation, Mockito.times(1)).remove(First::class.java) + } + +}