From 781be7732634c095e5857c67afedbd6a33b03157 Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Wed, 14 Jun 2017 15:54:22 +0200 Subject: [PATCH] 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..e08ebe6c0f --- /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 [ExecutableAggregationOperation.aggregateAndReturn] 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) + } + +}