From 449780573e86935e90759b9f9545991bc5a2b326 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Mon, 23 Apr 2018 08:28:53 +0200 Subject: [PATCH] DATAMONGO-1929 - Add Kotlin extensions for Executable and ReactiveMapReduceOperation. Original pull request: #551. --- .../ExecutableMapReduceOperationExtensions.kt | 54 +++++++++++++++ .../ReactiveMapReduceOperationExtensions.kt | 54 +++++++++++++++ ...utableMapReduceOperationExtensionsTests.kt | 66 +++++++++++++++++++ ...activeMapReduceOperationExtensionsTests.kt | 66 +++++++++++++++++++ 4 files changed, 240 insertions(+) create mode 100644 spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ExecutableMapReduceOperationExtensions.kt create mode 100644 spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ReactiveMapReduceOperationExtensions.kt create mode 100644 spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ExecutableMapReduceOperationExtensionsTests.kt create mode 100644 spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ReactiveMapReduceOperationExtensionsTests.kt diff --git a/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ExecutableMapReduceOperationExtensions.kt b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ExecutableMapReduceOperationExtensions.kt new file mode 100644 index 000000000..9e5b4706c --- /dev/null +++ b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ExecutableMapReduceOperationExtensions.kt @@ -0,0 +1,54 @@ +/* + * Copyright 2018 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 [ExecutableMapReduceOperation.mapReduce] providing a [KClass] based variant. + * + * @author Christoph Strobl + * @since 2.1 + */ +fun ExecutableMapReduceOperation.mapReduce(entityClass: KClass): ExecutableMapReduceOperation.MapReduceWithMapFunction = + mapReduce(entityClass.java) + +/** + * Extension for [ExecutableMapReduceOperation.mapReduce] leveraging reified type parameters. + * + * @author Christoph Strobl + * @since 2.1 + */ +inline fun ExecutableMapReduceOperation.mapReduce(): ExecutableMapReduceOperation.MapReduceWithMapFunction = + mapReduce(T::class.java) + +/** + * Extension for [ExecutableMapReduceOperation.MapReduceWithProjection.as] providing a [KClass] based variant. + * + * @author Christoph Strobl + * @since 2.1 + */ +fun ExecutableMapReduceOperation.MapReduceWithProjection.asType(resultType: KClass): ExecutableMapReduceOperation.MapReduceWithQuery = + `as`(resultType.java) + +/** + * Extension for [ExecutableMapReduceOperation.MapReduceWithProjection.as] leveraging reified type parameters. + * + * @author Christoph Strobl + * @since 2.1 + */ +inline fun ExecutableMapReduceOperation.MapReduceWithProjection.asType(): ExecutableMapReduceOperation.MapReduceWithQuery = + `as`(T::class.java) diff --git a/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ReactiveMapReduceOperationExtensions.kt b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ReactiveMapReduceOperationExtensions.kt new file mode 100644 index 000000000..6524ef266 --- /dev/null +++ b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/ReactiveMapReduceOperationExtensions.kt @@ -0,0 +1,54 @@ +/* + * Copyright 2018 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 [ReactiveMapReduceOperation.mapReduce] providing a [KClass] based variant. + * + * @author Christoph Strobl + * @since 2.1 + */ +fun ReactiveMapReduceOperation.mapReduce(entityClass: KClass): ReactiveMapReduceOperation.MapReduceWithMapFunction = + mapReduce(entityClass.java) + +/** + * Extension for [ReactiveMapReduceOperation.mapReduce] leveraging reified type parameters. + * + * @author Christoph Strobl + * @since 2.1 + */ +inline fun ReactiveMapReduceOperation.mapReduce(): ReactiveMapReduceOperation.MapReduceWithMapFunction = + mapReduce(T::class.java) + +/** + * Extension for [ReactiveMapReduceOperation.MapReduceWithProjection.as] providing a [KClass] based variant. + * + * @author Christoph Strobl + * @since 2.1 + */ +fun ReactiveMapReduceOperation.MapReduceWithProjection.asType(resultType: KClass): ReactiveMapReduceOperation.MapReduceWithQuery = + `as`(resultType.java) + +/** + * Extension for [ReactiveMapReduceOperation.MapReduceWithProjection.as] leveraging reified type parameters. + * + * @author Christoph Strobl + * @since 2.1 + */ +inline fun ReactiveMapReduceOperation.MapReduceWithProjection.asType(): ReactiveMapReduceOperation.MapReduceWithQuery = + `as`(T::class.java) diff --git a/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ExecutableMapReduceOperationExtensionsTests.kt b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ExecutableMapReduceOperationExtensionsTests.kt new file mode 100644 index 000000000..8b97e1007 --- /dev/null +++ b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ExecutableMapReduceOperationExtensionsTests.kt @@ -0,0 +1,66 @@ +/* + * Copyright 2018 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.verify +import example.first.First +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Answers +import org.mockito.Mock +import org.mockito.junit.MockitoJUnitRunner + +/** + * @author Christoph Strobl + */ +@RunWith(MockitoJUnitRunner::class) +class ExecutableMapReduceOperationExtensionsTests { + + @Mock(answer = Answers.RETURNS_MOCKS) + lateinit var operation: ExecutableMapReduceOperation + + @Mock(answer = Answers.RETURNS_MOCKS) + lateinit var operationWithProjection: ExecutableMapReduceOperation.MapReduceWithProjection + + + @Test // DATAMONGO-1929 + fun `ExecutableMapReduceOperation#mapReduce(KClass) extension should call its Java counterpart`() { + + operation.mapReduce(First::class) + verify(operation).mapReduce(First::class.java) + } + + @Test // DATAMONGO-1929 + fun `ExecutableMapReduceOperation#mapReduce() with reified type parameter extension should call its Java counterpart`() { + + operation.mapReduce() + verify(operation).mapReduce(First::class.java) + } + + @Test // DATAMONGO-1929 + fun `ExecutableMapReduceOperation#MapReduceWithProjection#asType(KClass) extension should call its Java counterpart`() { + + operationWithProjection.asType(First::class) + verify(operationWithProjection).`as`(First::class.java) + } + + @Test // DATAMONGO-1929 + fun `ExecutableMapReduceOperation#MapReduceWithProjection#asType() with reified type parameter extension should call its Java counterpart`() { + + operationWithProjection.asType() + verify(operationWithProjection).`as`(First::class.java) + } +} diff --git a/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ReactiveMapReduceOperationExtensionsTests.kt b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ReactiveMapReduceOperationExtensionsTests.kt new file mode 100644 index 000000000..c057b3034 --- /dev/null +++ b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/ReactiveMapReduceOperationExtensionsTests.kt @@ -0,0 +1,66 @@ +/* + * Copyright 2018 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.verify +import example.first.First +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Answers +import org.mockito.Mock +import org.mockito.junit.MockitoJUnitRunner + +/** + * @author Christoph Strobl + */ +@RunWith(MockitoJUnitRunner::class) +class ReactiveMapReduceOperationExtensionsTests { + + @Mock(answer = Answers.RETURNS_MOCKS) + lateinit var operation: ReactiveMapReduceOperation + + @Mock(answer = Answers.RETURNS_MOCKS) + lateinit var operationWithProjection: ReactiveMapReduceOperation.MapReduceWithProjection + + @Test // DATAMONGO-1929 + fun `ReactiveMapReduceOperation#mapReduce(KClass) extension should call its Java counterpart`() { + + operation.mapReduce(First::class) + verify(operation).mapReduce(First::class.java) + } + + @Test // DATAMONGO-1929 + fun `ReactiveMapReduceOperation#mapReduce() with reified type parameter extension should call its Java counterpart`() { + + operation.mapReduce() + verify(operation).mapReduce(First::class.java) + } + + @Test // DATAMONGO-1929 + fun `ReactiveMapReduceOperation#MapReduceWithProjection#asType(KClass) extension should call its Java counterpart`() { + + operationWithProjection.asType(First::class) + verify(operationWithProjection).`as`(First::class.java) + } + + @Test // DATAMONGO-1929 + fun `ReactiveMapReduceOperation#MapReduceWithProjection#asType() with reified type parameter extension should call its Java counterpart`() { + + operationWithProjection.asType() + verify(operationWithProjection).`as`(First::class.java) + } +}