Skip to content

Commit 3d84e6b

Browse files
test(infrastructure): add some mongo tests
1 parent 27304a7 commit 3d84e6b

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed

gradle/libs.versions.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ kmongo = "4.8.0"
66

77
[libraries]
88
archunit = { module = "com.tngtech.archunit:archunit-junit5", version = "1.0.1" }
9+
embed-mongo = { module = "de.flapdoodle.embed:de.flapdoodle.embed.mongo", version = "4.6.1" }
910
azure-digital-twins = { module = "com.azure:azure-digitaltwins-core", version = "1.3.6" }
1011
azure-identity = { module = "com.azure:azure-identity", version = "1.8.0" }
1112
kmongo = { module = "org.litote.kmongo:kmongo", version.ref = "kmongo" }
@@ -20,10 +21,9 @@ ktor-content-negotiation = { module = "io.ktor:ktor-server-content-negotiation",
2021
ktor-serialization = { module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "ktor" }
2122
ktor-test = { module = "io.ktor:ktor-server-test-host", version.ref = "ktor" }
2223
logback = { module = "ch.qos.logback:logback-classic", version = "1.4.5" }
23-
testcontainer = { module = "org.testcontainers:mongodb", version = "1.17.6" }
2424

2525
[bundles]
26-
kotlin-testing = ["archunit", "testcontainer", "kotest-junit5", "kotest-assertions-core", "kotest-assertions-core-jvm", "ktor-test"]
26+
kotlin-testing = ["archunit", "embed-mongo", "kotest-junit5", "kotest-assertions-core", "kotest-assertions-core-jvm", "ktor-test"]
2727

2828
[plugins]
2929
dokka = { id ="org.jetbrains.dokka", version = "1.7.20" }
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2023. Smart Operating Block
3+
*
4+
* Use of this source code is governed by an MIT-style
5+
* license that can be found in the LICENSE file or at
6+
* https://opensource.org/licenses/MIT.
7+
*/
8+
9+
package infrastructure.database
10+
11+
import de.flapdoodle.embed.mongo.config.Net
12+
import de.flapdoodle.embed.mongo.distribution.Version
13+
import de.flapdoodle.embed.mongo.transitions.Mongod
14+
import de.flapdoodle.os.CommonOS
15+
import de.flapdoodle.os.ImmutablePlatform
16+
import de.flapdoodle.os.Platform
17+
import de.flapdoodle.os.linux.LinuxDistribution
18+
import de.flapdoodle.os.linux.UbuntuVersion
19+
import de.flapdoodle.reverse.StateID
20+
import de.flapdoodle.reverse.transitions.Start
21+
import de.flapdoodle.net.Net as Network
22+
23+
private inline fun <reified T : Any> T.toTransition() = Start.to(StateID.of(T::class.java)).initializedWith(this)
24+
25+
internal fun withMongo(operation: () -> Unit) {
26+
if (System.getProperty("os.name").lowercase().contains("win")) {
27+
println("Testing with MongoDB is disabled on Windows due to flaky behavior")
28+
} else {
29+
runCatching { Platform.detect(CommonOS.list()) }
30+
.onFailure { println("Failed to detect platform") }
31+
.onSuccess { detectedPlatform ->
32+
val platform: Platform = detectedPlatform
33+
.takeUnless { it.operatingSystem() == CommonOS.Linux && it.distribution().isEmpty }
34+
?: ImmutablePlatform.builder()
35+
.from(detectedPlatform)
36+
.distribution(LinuxDistribution.Ubuntu)
37+
.version(UbuntuVersion.Ubuntu_20_10)
38+
.build()
39+
val mongod = Mongod.instance()
40+
.withNet(Net.of("localhost", 27017, Network.localhostIsIPv6()).toTransition())
41+
.withPlatform(platform.toTransition())
42+
val runningState = mongod.start(Version.Main.V6_0)
43+
try {
44+
operation()
45+
} finally {
46+
runningState.current().stop()
47+
}
48+
}
49+
}
50+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2023. Smart Operating Block
3+
*
4+
* Use of this source code is governed by an MIT-style
5+
* license that can be found in the LICENSE file or at
6+
* https://opensource.org/licenses/MIT.
7+
*/
8+
9+
package infrastructure.database
10+
11+
import entity.user.User
12+
import io.kotest.core.spec.style.StringSpec
13+
import io.kotest.matchers.shouldBe
14+
import io.kotest.matchers.shouldNotBe
15+
16+
class TestMongo : StringSpec({
17+
18+
"Test user operations on mongo db" {
19+
withMongo {
20+
val mockUser = User("1234", "MockPassword!")
21+
22+
val mongoClient = MongoClient("mongodb://localhost:27017")
23+
24+
mongoClient.createUser(mockUser) shouldNotBe null
25+
26+
mongoClient.deleteUser(mockUser.userId) shouldBe true
27+
28+
mongoClient.createUser(mockUser) shouldNotBe null
29+
mongoClient.getUser(mockUser.userId).also {
30+
it shouldNotBe null
31+
it?.userId shouldBe mockUser.userId
32+
}
33+
}
34+
}
35+
})

0 commit comments

Comments
 (0)