Skip to content
This repository has been archived by the owner on Oct 14, 2021. It is now read-only.

Refactor mongo client connection string #245

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions adoptopenjdk-api-v3-persistance/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-core</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,43 @@ class MongoClient {
companion object {
@JvmStatic
private val LOGGER = LoggerFactory.getLogger(this::class.java)
}
private const val DEFAULT_DBNAME = "api-data"
private const val DEFAULT_HOST = "localhost"
private const val DEFAULT_PORT = "27017"
private const val DEFAULT_SERVER_SELECTION_TIMEOUT_MILLIS = "100"

init {
val dbName = System.getenv("MONGODB_DBNAME") ?: "api-data"
val username = System.getenv("MONGODB_USER")
val password = System.getenv("MONGODB_PASSWORD")
val host = System.getenv("MONGODB_HOST") ?: "localhost"
val port = System.getenv("MONGODB_PORT") ?: "27017"

LOGGER.info("Connecting to mongodb://$username:a-password@$host:$port/$dbName")
var uri = if (username != null && password != null) {
"mongodb://$username:$password@$host:$port/$dbName"
} else {
"mongodb://$host:$port"
fun createConnectionString(
dbName: String,
username: String? = null,
password: String? = null,
host: String? = DEFAULT_HOST,
port: String? = DEFAULT_PORT,
serverSelectionTimeoutMills: String? = DEFAULT_SERVER_SELECTION_TIMEOUT_MILLIS
): String {
return System.getProperty("MONGODB_TEST_CONNECTION_STRING")
?: if (username != null && password != null) {
LOGGER.info("Connecting to mongodb://$username:a-password@$host:$port/$dbName")
"mongodb://$username:$password@$host:$port/$dbName"
} else {
val developmentConnectionString = "mongodb://$host:$port/?serverSelectionTimeoutMS=$serverSelectionTimeoutMills"
LOGGER.info("Using development connection string - $developmentConnectionString")
developmentConnectionString
}
}
}

if (System.getProperty("MONGO_DB") != null) {
uri = System.getProperty("MONGO_DB")
}
init {
val dbName = System.getenv("MONGODB_DBNAME") ?: DEFAULT_DBNAME
val connectionString = createConnectionString(
dbName,
username = System.getenv("MONGODB_USER"),
password = System.getenv("MONGODB_PASSWORD"),
host = System.getenv("MONGODB_HOST"),
port = System.getenv("MONGODB_PORT"),
serverSelectionTimeoutMills = System.getenv("MONGODB_SERVER_SELECTION_TIMEOUT_MILLIS")
)

client = KMongo.createClient(uri).coroutine
client = KMongo.createClient(connectionString).coroutine
database = client.getDatabase(dbName)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package net.adoptopenjdk.api.v3.dataSources.persitence.mongo

import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test

class MongoClientTest {

@Test
fun `default connection string`() {
val connectionString = MongoClient.createConnectionString("api-data")
assertEquals(connectionString, "mongodb://localhost:27017/?serverSelectionTimeoutMS=100")
}

@Test
fun `default connection string with explicit server selection timeout`() {
val connectionString = MongoClient.createConnectionString("api-data", serverSelectionTimeoutMills = "999")
assertEquals(connectionString, "mongodb://localhost:27017/?serverSelectionTimeoutMS=999")
}

@Test
fun `override with test connection string`() {
System.setProperty("MONGODB_TEST_CONNECTION_STRING", "mongodb://some-host:99999")
val connectionString = MongoClient.createConnectionString("api-data")
assertEquals(connectionString, "mongodb://some-host:99999")
}

@Test
fun `fully specified connection string`() {
val connectionString = MongoClient.createConnectionString(
"api-data",
"some-user",
"some-password",
"some-host",
"12345"
)
assertEquals(connectionString, "mongodb://some-user:some-password@some-host:12345/api-data")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ abstract class BaseTest {
.net(Net(bindIp, port, Network.localhostIsIPv6()))
.build()

LOGGER.info("Mongo \"mongodb://localhost:${port}\"")
System.setProperty("MONGO_DB", "mongodb://localhost:$port")
val mongodbTestConnectionString = "mongodb://$bindIp:$port"
LOGGER.info("Mongo test connection string - $mongodbTestConnectionString")
System.setProperty("MONGODB_TEST_CONNECTION_STRING", mongodbTestConnectionString)

mongodExecutable = starter.prepare(mongodConfig)
mongodExecutable!!.start()
Expand Down