From afab98ccb2a09573141ff7fc78434713ef2282d7 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Tue, 23 May 2023 11:33:16 +0200 Subject: [PATCH 1/3] Prepare issue branch. --- pom.xml | 2 +- spring-data-mongodb-benchmarks/pom.xml | 2 +- spring-data-mongodb-distribution/pom.xml | 2 +- spring-data-mongodb/pom.xml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index 366786fc6d..ff933b04a7 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-mongodb-parent - 4.2.0-SNAPSHOT + 4.2.x-4393-SNAPSHOT pom Spring Data MongoDB diff --git a/spring-data-mongodb-benchmarks/pom.xml b/spring-data-mongodb-benchmarks/pom.xml index 2de4b6b635..d569ebe5c7 100644 --- a/spring-data-mongodb-benchmarks/pom.xml +++ b/spring-data-mongodb-benchmarks/pom.xml @@ -7,7 +7,7 @@ org.springframework.data spring-data-mongodb-parent - 4.2.0-SNAPSHOT + 4.2.x-4393-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb-distribution/pom.xml b/spring-data-mongodb-distribution/pom.xml index 41b81f9aa6..c4a6f00b8f 100644 --- a/spring-data-mongodb-distribution/pom.xml +++ b/spring-data-mongodb-distribution/pom.xml @@ -15,7 +15,7 @@ org.springframework.data spring-data-mongodb-parent - 4.2.0-SNAPSHOT + 4.2.x-4393-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index dc07f13ccc..8dac681674 100644 --- a/spring-data-mongodb/pom.xml +++ b/spring-data-mongodb/pom.xml @@ -13,7 +13,7 @@ org.springframework.data spring-data-mongodb-parent - 4.2.0-SNAPSHOT + 4.2.x-4393-SNAPSHOT ../pom.xml From 3b6b9cf99ec21a7943f870ceef11ed7e5de07d9c Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Tue, 23 May 2023 11:36:51 +0200 Subject: [PATCH 2/3] Add Kotlin driver dependency. --- spring-data-mongodb/pom.xml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index 8dac681674..105d094c65 100644 --- a/spring-data-mongodb/pom.xml +++ b/spring-data-mongodb/pom.xml @@ -104,6 +104,12 @@ ${mongo} true + + org.mongodb + mongodb-driver-kotlin-sync + ${mongo} + true + org.mongodb @@ -111,6 +117,12 @@ ${mongo.reactivestreams} true + + org.mongodb + mongodb-driver-kotlin-coroutine + ${mongo} + true + org.mongodb From b0c86bb07ce1a93fccb27a615a2e304b158e17d8 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Tue, 23 May 2023 11:46:27 +0200 Subject: [PATCH 3/3] Extend database factory to accept Kotlin sync/coroutine MongoDriver instance. --- ...mpleMongoClientDatabaseFactoryExtension.kt | 32 +++++++++++++++ ...leReactiveMongoDatabaseFactoryExtension.kt | 32 +++++++++++++++ ...ongoClientDatabaseFactoryExtensionTests.kt | 36 +++++++++++++++++ ...ctiveMongoDatabaseFactoryExtensionTests.kt | 40 +++++++++++++++++++ 4 files changed, 140 insertions(+) create mode 100644 spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/SimpleMongoClientDatabaseFactoryExtension.kt create mode 100644 spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/SimpleReactiveMongoDatabaseFactoryExtension.kt create mode 100644 spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/SimpleMongoClientDatabaseFactoryExtensionTests.kt create mode 100644 spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/SimpleReactiveMongoDatabaseFactoryExtensionTests.kt diff --git a/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/SimpleMongoClientDatabaseFactoryExtension.kt b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/SimpleMongoClientDatabaseFactoryExtension.kt new file mode 100644 index 0000000000..c423cbe9d9 --- /dev/null +++ b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/SimpleMongoClientDatabaseFactoryExtension.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2023 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 + * + * https://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.mongodb.kotlin.client.MongoClient +import org.springframework.beans.DirectFieldAccessor + +/** + * Extension for [SimpleMongoClientDatabaseFactory] that accepts a [MongoClient]. + * + * @author Christoph Strobl + * @since 4.2 + */ +fun SimpleMongoClientDatabaseFactory(client: MongoClient, database: String): SimpleMongoClientDatabaseFactory = + SimpleMongoClientDatabaseFactory( + DirectFieldAccessor(client).getPropertyValue("wrapped") as com.mongodb.client.MongoClient, + database + ) diff --git a/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/SimpleReactiveMongoDatabaseFactoryExtension.kt b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/SimpleReactiveMongoDatabaseFactoryExtension.kt new file mode 100644 index 0000000000..82fe0cfca0 --- /dev/null +++ b/spring-data-mongodb/src/main/kotlin/org/springframework/data/mongodb/core/SimpleReactiveMongoDatabaseFactoryExtension.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2023 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 + * + * https://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.mongodb.kotlin.client.coroutine.MongoClient +import org.springframework.beans.DirectFieldAccessor + +/** + * Extension for [SimpleReactiveMongoDatabaseFactory] that accepts a [MongoClient]. + * + * @author Christoph Strobl + * @since 4.2 + */ +fun SimpleReactiveMongoDatabaseFactory(client: MongoClient, database: String): SimpleReactiveMongoDatabaseFactory = + SimpleReactiveMongoDatabaseFactory( + DirectFieldAccessor(client).getPropertyValue("wrapped") as com.mongodb.reactivestreams.client.MongoClient, + database + ) diff --git a/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/SimpleMongoClientDatabaseFactoryExtensionTests.kt b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/SimpleMongoClientDatabaseFactoryExtensionTests.kt new file mode 100644 index 0000000000..62ca03ef3d --- /dev/null +++ b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/SimpleMongoClientDatabaseFactoryExtensionTests.kt @@ -0,0 +1,36 @@ +/* + * Copyright 2023 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 + * + * https://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.mongodb.kotlin.client.MongoClient +import org.bson.Document +import org.junit.jupiter.api.Test +import org.springframework.data.mongodb.test.util.Assertions.assertThat + +/** + * @author Christoph Strobl + */ +class SimpleMongoClientDatabaseFactoryExtensionTests { + + @Test // GH-4393 + fun `extension allows to create SimpleMongoClientDatabaseFactory with a Kotlin Driver instance`() { + + val factory = SimpleMongoClientDatabaseFactory(MongoClient.create(), "test") + + assertThat(factory.mongoDatabase.runCommand(Document("ping", 1))).containsKey("ok") + } +} diff --git a/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/SimpleReactiveMongoDatabaseFactoryExtensionTests.kt b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/SimpleReactiveMongoDatabaseFactoryExtensionTests.kt new file mode 100644 index 0000000000..9d3c4c3ea6 --- /dev/null +++ b/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/SimpleReactiveMongoDatabaseFactoryExtensionTests.kt @@ -0,0 +1,40 @@ +/* + * Copyright 2023 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 + * + * https://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.mongodb.kotlin.client.coroutine.MongoClient +import org.bson.Document +import org.junit.jupiter.api.Test +import reactor.core.publisher.Mono +import reactor.test.StepVerifier + +/** + * @author Christoph Strobl + */ +class SimpleReactiveMongoDatabaseFactoryExtensionTests { + + @Test // GH-4393 + fun `extension allows to create SimpleReactiveMongoDatabaseFactory with a Kotlin Coroutine Driver instance`() { + + val factory = SimpleReactiveMongoDatabaseFactory(MongoClient.create(), "test") + + factory.mongoDatabase.flatMap { Mono.from(it.runCommand(Document("ping", 1))) } + .`as` { StepVerifier.create(it) } + .expectNextCount(1) + .verifyComplete() + } +}