Skip to content

Commit b810721

Browse files
Extend database factory to accept Kotlin sync/coroutine MongoDriver instance.
1 parent 0ce912e commit b810721

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2023 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+
* https://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+
17+
package org.springframework.data.mongodb.core
18+
19+
import com.mongodb.kotlin.client.MongoClient
20+
import org.springframework.beans.DirectFieldAccessor
21+
22+
/**
23+
* Extension for [SimpleMongoClientDatabaseFactory] that accepts a [MongoClient].
24+
*
25+
* @author Christoph Strobl
26+
* @since 4.2
27+
*/
28+
fun SimpleMongoClientDatabaseFactory(client: MongoClient, database: String): SimpleMongoClientDatabaseFactory =
29+
SimpleMongoClientDatabaseFactory(
30+
DirectFieldAccessor(client).getPropertyValue("wrapped") as com.mongodb.client.MongoClient,
31+
database
32+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2023 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+
* https://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+
17+
package org.springframework.data.mongodb.core
18+
19+
import com.mongodb.kotlin.client.coroutine.MongoClient
20+
import org.springframework.beans.DirectFieldAccessor
21+
22+
/**
23+
* Extension for [SimpleReactiveMongoDatabaseFactory] that accepts a [MongoClient].
24+
*
25+
* @author Christoph Strobl
26+
* @since 4.2
27+
*/
28+
fun SimpleReactiveMongoDatabaseFactory(client: MongoClient, database: String): SimpleReactiveMongoDatabaseFactory =
29+
SimpleReactiveMongoDatabaseFactory(
30+
DirectFieldAccessor(client).getPropertyValue("wrapped") as com.mongodb.reactivestreams.client.MongoClient,
31+
database
32+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2023 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+
* https://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+
17+
package org.springframework.data.mongodb.core
18+
19+
import com.mongodb.kotlin.client.MongoClient
20+
import org.bson.Document
21+
import org.junit.jupiter.api.Test
22+
import org.springframework.data.mongodb.test.util.Assertions.assertThat
23+
24+
/**
25+
* @author Christoph Strobl
26+
*/
27+
class SimpleMongoClientDatabaseFactoryExtensionTests {
28+
29+
@Test // GH-4393
30+
fun `extension allows to create SimpleMongoClientDatabaseFactory with a Kotlin Driver instance`() {
31+
32+
val factory = SimpleMongoClientDatabaseFactory(MongoClient.create(), "test")
33+
34+
assertThat(factory.mongoDatabase.runCommand(Document("ping", 1))).containsKey("ok")
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2023 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+
* https://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+
17+
package org.springframework.data.mongodb.core
18+
19+
import com.mongodb.kotlin.client.coroutine.MongoClient
20+
import org.bson.Document
21+
import org.junit.jupiter.api.Test
22+
import reactor.core.publisher.Mono
23+
import reactor.test.StepVerifier
24+
25+
/**
26+
* @author Christoph Strobl
27+
*/
28+
class SimpleReactiveMongoDatabaseFactoryExtensionTests {
29+
30+
@Test // GH-4393
31+
fun `extension allows to create SimpleReactiveMongoDatabaseFactory with a Kotlin Coroutine Driver instance`() {
32+
33+
val factory = SimpleReactiveMongoDatabaseFactory(MongoClient.create(), "test")
34+
35+
factory.mongoDatabase.flatMap { Mono.from(it.runCommand(Document("ping", 1))) }
36+
.`as` { StepVerifier.create(it) }
37+
.expectNextCount(1)
38+
.verifyComplete()
39+
}
40+
}

0 commit comments

Comments
 (0)