Skip to content

Commit e90c6b0

Browse files
sdeleuzeodrotbohm
authored andcommitted
DATAMONGO-1689 - Add Kotlin extensions for new fluent API.
Original pull request: #463.
1 parent d2e68cd commit e90c6b0

8 files changed

+308
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core
17+
18+
import kotlin.reflect.KClass
19+
20+
/**
21+
* Extension for [ExecutableAggregationOperation.aggregateAndReturn] providing a [KClass] based variant.
22+
*
23+
* @author Sebastien Deleuze
24+
* @since 2.0
25+
*/
26+
fun <T : Any> ExecutableAggregationOperation.aggregateAndReturn(entityClass: KClass<T>): ExecutableAggregationOperation.AggregationOperation<T> =
27+
aggregateAndReturn(entityClass.java)
28+
29+
/**
30+
* Extension for [ExecutableAggregationOperation.aggregateAndReturn] leveraging reified type parameters.
31+
*
32+
* @author Sebastien Deleuze
33+
* @since 2.0
34+
*/
35+
inline fun <reified T : Any> ExecutableAggregationOperation.aggregateAndReturn(): ExecutableAggregationOperation.AggregationOperation<T> =
36+
aggregateAndReturn(T::class.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core
17+
18+
import kotlin.reflect.KClass
19+
20+
/**
21+
* Extension for [ExecutableFindOperation.query] providing a [KClass] based variant.
22+
*
23+
* @author Sebastien Deleuze
24+
* @since 2.0
25+
*/
26+
fun <T : Any> ExecutableFindOperation.query(entityClass: KClass<T>): ExecutableFindOperation.FindOperation<T> =
27+
query(entityClass.java)
28+
29+
/**
30+
* Extension for [ExecutableFindOperation.query] leveraging reified type parameters.
31+
*
32+
* @author Sebastien Deleuze
33+
* @since 2.0
34+
*/
35+
inline fun <reified T : Any> ExecutableFindOperation.query(): ExecutableFindOperation.FindOperation<T> =
36+
query(T::class.java)
37+
38+
39+
/**
40+
* Extension for [ExecutableFindOperation.FindOperationWithProjection.as] providing a [KClass] based variant.
41+
*
42+
* @author Sebastien Deleuze
43+
* @since 2.0
44+
*/
45+
fun <T : Any> ExecutableFindOperation.FindOperationWithProjection<T>.asType(resultType: KClass<T>): ExecutableFindOperation.FindOperationWithQuery<T> =
46+
`as`(resultType.java)
47+
48+
/**
49+
* Extension for [ExecutableFindOperation.FindOperationWithProjection.as] leveraging reified type parameters.
50+
*
51+
* @author Sebastien Deleuze
52+
* @since 2.0
53+
*/
54+
inline fun <reified T : Any> ExecutableFindOperation.FindOperationWithProjection<T>.asType(): ExecutableFindOperation.FindOperationWithQuery<T> =
55+
`as`(T::class.java)
56+
57+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core
17+
18+
import kotlin.reflect.KClass
19+
20+
/**
21+
* Extension for [ExecutableInsertOperation.insert] providing a [KClass] based variant.
22+
*
23+
* @author Sebastien Deleuze
24+
* @since 2.0
25+
*/
26+
fun <T : Any> ExecutableInsertOperation.insert(entityClass: KClass<T>): ExecutableInsertOperation.InsertOperation<T> =
27+
insert(entityClass.java)
28+
29+
/**
30+
* Extension for [ExecutableInsertOperation.insert] leveraging reified type parameters.
31+
*
32+
* @author Sebastien Deleuze
33+
* @since 2.0
34+
*/
35+
inline fun <reified T : Any> ExecutableInsertOperation.insert(): ExecutableInsertOperation.InsertOperation<T> =
36+
insert(T::class.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core
17+
18+
import kotlin.reflect.KClass
19+
20+
/**
21+
* Extension for [ExecutableRemoveOperation.remove] providing a [KClass] based variant.
22+
*
23+
* @author Sebastien Deleuze
24+
* @since 2.0
25+
*/
26+
fun <T : Any> ExecutableRemoveOperation.remove(entityClass: KClass<T>): ExecutableRemoveOperation.RemoveOperation<T> =
27+
remove(entityClass.java)
28+
29+
/**
30+
* Extension for [ExecutableRemoveOperation.remove] leveraging reified type parameters.
31+
*
32+
* @author Sebastien Deleuze
33+
* @since 2.0
34+
*/
35+
inline fun <reified T : Any> ExecutableRemoveOperation.remove(): ExecutableRemoveOperation.RemoveOperation<T> =
36+
remove(T::class.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.springframework.data.mongodb.core
2+
3+
import example.first.First
4+
import org.junit.Test
5+
import org.junit.runner.RunWith
6+
import org.mockito.Answers
7+
import org.mockito.Mock
8+
import org.mockito.Mockito
9+
import org.mockito.junit.MockitoJUnitRunner
10+
11+
/**
12+
* @author Sebastien Deleuze
13+
*/
14+
@RunWith(MockitoJUnitRunner::class)
15+
class ExecutableAggregationOperationExtensionsTests {
16+
17+
@Mock(answer = Answers.RETURNS_MOCKS)
18+
lateinit var operation: ExecutableAggregationOperation
19+
20+
@Test // DATAMONGO-1689
21+
fun `aggregateAndReturn(KClass) extension should call its Java counterpart`() {
22+
operation.aggregateAndReturn(First::class)
23+
Mockito.verify(operation, Mockito.times(1)).aggregateAndReturn(First::class.java)
24+
}
25+
26+
@Test // DATAMONGO-1689
27+
fun `aggregateAndReturn() with reified type parameter extension should call its Java counterpart`() {
28+
operation.aggregateAndReturn<First>()
29+
Mockito.verify(operation, Mockito.times(1)).aggregateAndReturn(First::class.java)
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.springframework.data.mongodb.core
2+
3+
import example.first.First
4+
import org.junit.Test
5+
import org.junit.runner.RunWith
6+
import org.mockito.Answers
7+
import org.mockito.Mock
8+
import org.mockito.Mockito
9+
import org.mockito.junit.MockitoJUnitRunner
10+
11+
/**
12+
* @author Sebastien Deleuze
13+
*/
14+
@RunWith(MockitoJUnitRunner::class)
15+
class ExecutableFindOperationExtensionsTests {
16+
17+
@Mock(answer = Answers.RETURNS_MOCKS)
18+
lateinit var operation: ExecutableFindOperation
19+
20+
@Mock(answer = Answers.RETURNS_MOCKS)
21+
lateinit var operationWithProjection: ExecutableFindOperation.FindOperationWithProjection<First>
22+
23+
@Test // DATAMONGO-1689
24+
fun `ExecutableFindOperation#query(KClass) extension should call its Java counterpart`() {
25+
operation.query(First::class)
26+
Mockito.verify(operation, Mockito.times(1)).query(First::class.java)
27+
}
28+
29+
@Test // DATAMONGO-1689
30+
fun `ExecutableFindOperation#query() with reified type parameter extension should call its Java counterpart`() {
31+
operation.query<First>()
32+
Mockito.verify(operation, Mockito.times(1)).query(First::class.java)
33+
}
34+
35+
@Test // DATAMONGO-1689
36+
fun `ExecutableFindOperation#FindOperationWithProjection#asType(KClass) extension should call its Java counterpart`() {
37+
operationWithProjection.asType(First::class)
38+
Mockito.verify(operationWithProjection, Mockito.times(1)).`as`(First::class.java)
39+
}
40+
41+
@Test // DATAMONGO-1689
42+
fun `ExecutableFindOperation#FindOperationWithProjection#asType() with reified type parameter extension should call its Java counterpart`() {
43+
operationWithProjection.asType()
44+
Mockito.verify(operationWithProjection, Mockito.times(1)).`as`(First::class.java)
45+
}
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.springframework.data.mongodb.core
2+
3+
import example.first.First
4+
import org.junit.Test
5+
import org.junit.runner.RunWith
6+
import org.mockito.Answers
7+
import org.mockito.Mock
8+
import org.mockito.Mockito
9+
import org.mockito.junit.MockitoJUnitRunner
10+
11+
/**
12+
* @author Sebastien Deleuze
13+
*/
14+
@RunWith(MockitoJUnitRunner::class)
15+
class ExecutableInsertOperationExtensionsTests {
16+
17+
@Mock(answer = Answers.RETURNS_MOCKS)
18+
lateinit var operation: ExecutableInsertOperation
19+
20+
@Test // DATAMONGO-1689
21+
fun `insert(KClass) extension should call its Java counterpart`() {
22+
operation.insert(First::class)
23+
Mockito.verify(operation, Mockito.times(1)).insert(First::class.java)
24+
}
25+
26+
@Test // DATAMONGO-1689
27+
fun `insert() with reified type parameter extension should call its Java counterpart`() {
28+
operation.insert<First>()
29+
Mockito.verify(operation, Mockito.times(1)).insert(First::class.java)
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.springframework.data.mongodb.core
2+
3+
import example.first.First
4+
import org.junit.Test
5+
import org.junit.runner.RunWith
6+
import org.mockito.Answers
7+
import org.mockito.Mock
8+
import org.mockito.Mockito
9+
import org.mockito.junit.MockitoJUnitRunner
10+
11+
/**
12+
* @author Sebastien Deleuze
13+
*/
14+
@RunWith(MockitoJUnitRunner::class)
15+
class ExecutableRemoveOperationExtensionsTests {
16+
17+
@Mock(answer = Answers.RETURNS_MOCKS)
18+
lateinit var operation: ExecutableRemoveOperation
19+
20+
@Test // DATAMONGO-1689
21+
fun `remove(KClass) extension should call its Java counterpart`() {
22+
operation.remove(First::class)
23+
Mockito.verify(operation, Mockito.times(1)).remove(First::class.java)
24+
}
25+
26+
@Test // DATAMONGO-1689
27+
fun `remove() with reified type parameter extension should call its Java counterpart`() {
28+
operation.remove<First>()
29+
Mockito.verify(operation, Mockito.times(1)).remove(First::class.java)
30+
}
31+
32+
}

0 commit comments

Comments
 (0)