Skip to content

Commit

Permalink
DATAMONGO-1689 - Add fluent extensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
sdeleuze committed Jun 14, 2017
1 parent 4c59d84 commit 781be77
Show file tree
Hide file tree
Showing 8 changed files with 308 additions and 0 deletions.
@@ -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 <T : Any> ExecutableAggregationOperation.aggregateAndReturn(entityClass: KClass<T>): ExecutableAggregationOperation.AggregationOperation<T> =
aggregateAndReturn(entityClass.java)

/**
* Extension for [ExecutableAggregationOperation.aggregateAndReturn] leveraging reified type parameters.
*
* @author Sebastien Deleuze
* @since 2.0
*/
inline fun <reified T : Any> ExecutableAggregationOperation.aggregateAndReturn(): ExecutableAggregationOperation.AggregationOperation<T> =
aggregateAndReturn(T::class.java)
@@ -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 <T : Any> ExecutableFindOperation.query(entityClass: KClass<T>): ExecutableFindOperation.FindOperation<T> =
query(entityClass.java)

/**
* Extension for [ExecutableFindOperation.query] leveraging reified type parameters.
*
* @author Sebastien Deleuze
* @since 2.0
*/
inline fun <reified T : Any> ExecutableFindOperation.query(): ExecutableFindOperation.FindOperation<T> =
query(T::class.java)


/**
* Extension for [ExecutableFindOperation.FindOperationWithProjection.as] providing a [KClass] based variant.
*
* @author Sebastien Deleuze
* @since 2.0
*/
fun <T : Any> ExecutableFindOperation.FindOperationWithProjection<T>.asType(resultType: KClass<T>): ExecutableFindOperation.FindOperationWithQuery<T> =
`as`(resultType.java)

/**
* Extension for [ExecutableFindOperation.FindOperationWithProjection.as] leveraging reified type parameters.
*
* @author Sebastien Deleuze
* @since 2.0
*/
inline fun <reified T : Any> ExecutableFindOperation.FindOperationWithProjection<T>.asType(): ExecutableFindOperation.FindOperationWithQuery<T> =
`as`(T::class.java)


@@ -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 <T : Any> ExecutableInsertOperation.insert(entityClass: KClass<T>): ExecutableInsertOperation.InsertOperation<T> =
insert(entityClass.java)

/**
* Extension for [ExecutableAggregationOperation.aggregateAndReturn] leveraging reified type parameters.
*
* @author Sebastien Deleuze
* @since 2.0
*/
inline fun <reified T : Any> ExecutableInsertOperation.insert(): ExecutableInsertOperation.InsertOperation<T> =
insert(T::class.java)
@@ -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 <T : Any> ExecutableRemoveOperation.remove(entityClass: KClass<T>): ExecutableRemoveOperation.RemoveOperation<T> =
remove(entityClass.java)

/**
* Extension for [ExecutableRemoveOperation.remove] leveraging reified type parameters.
*
* @author Sebastien Deleuze
* @since 2.0
*/
inline fun <reified T : Any> ExecutableRemoveOperation.remove(): ExecutableRemoveOperation.RemoveOperation<T> =
remove(T::class.java)
@@ -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<First>()
Mockito.verify(operation, Mockito.times(1)).aggregateAndReturn(First::class.java)
}

}
@@ -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<First>

@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<First>()
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)
}

}
@@ -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<First>()
Mockito.verify(operation, Mockito.times(1)).insert(First::class.java)
}

}
@@ -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<First>()
Mockito.verify(operation, Mockito.times(1)).remove(First::class.java)
}

}

0 comments on commit 781be77

Please sign in to comment.