-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend database factory to accept Kotlin sync/coroutine MongoDriver i…
…nstance.
- Loading branch information
1 parent
3b6b9cf
commit b0c86bb
Showing
4 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
...kotlin/org/springframework/data/mongodb/core/SimpleMongoClientDatabaseFactoryExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
) |
32 changes: 32 additions & 0 deletions
32
...tlin/org/springframework/data/mongodb/core/SimpleReactiveMongoDatabaseFactoryExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
) |
36 changes: 36 additions & 0 deletions
36
...n/org/springframework/data/mongodb/core/SimpleMongoClientDatabaseFactoryExtensionTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...org/springframework/data/mongodb/core/SimpleReactiveMongoDatabaseFactoryExtensionTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} | ||
} |