diff --git a/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoServiceBindingConverter.java b/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoServiceBindingConverter.java
index 7931f9a686d8b..e0cb6f95549d4 100644
--- a/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoServiceBindingConverter.java
+++ b/extensions/mongodb-client/runtime/src/main/java/io/quarkus/mongodb/runtime/MongoServiceBindingConverter.java
@@ -1,5 +1,6 @@
package io.quarkus.mongodb.runtime;
+import static java.lang.Boolean.parseBoolean;
import static java.lang.String.format;
import java.util.HashMap;
@@ -17,27 +18,30 @@
* ServiceBindingConverter for MongoDB to support SBO (Service Binding Operator) in Quarkus.
*
* This class supports both the Standard and SRV connection string format for
- * MongoDB depending on whether port is provided or not.
+ * MongoDB depending on whether srv property is true or false. If the srv property is
+ * missing then it is same as having a value of false.
*
*
+ *
* Following individual properties are supported to make the connection string:
*
* - username
* - password
- * - host
- * - port
+ * - host (port information can be provided in this property separated by : sign, e.g. localhost:27010
* - database
+ * - srv
+ * - options
*
- * Other than host all other properties are optinoal
+ * Other than host all other properties are optional
*
*
*
- * Only following options are supported by default:
+ * The Quarkus properties set by this class are:
*
- * - retryWrites=true
- * - w=majority
+ * - quarkus.mongodb.connection-string
+ * - quarkus.mongodb.credentials.username (if username is provided)
+ * - quarkus.mongodb.credentials.password (if password is provided)
*
- *
*/
public class MongoServiceBindingConverter implements ServiceBindingConverter {
private static final Logger LOGGER = Logger.getLogger(MongoServiceBindingConverter.class);
@@ -45,21 +49,22 @@ public class MongoServiceBindingConverter implements ServiceBindingConverter {
private static final String BINDING_TYPE = "mongodb";
public static final String BINDING_CONFIG_SOURCE_NAME = BINDING_TYPE + "-k8s-service-binding-source";
public static final String MONGO_DB_CONNECTION_STRING = "quarkus.mongodb.connection-string";
+ public static final String MONGO_DB_USERNAME = "quarkus.mongodb.credentials.username";
+ public static final String MONGO_DB_PASSWORD = "quarkus.mongodb.credentials.password";
// DB properties
public static final String DB_USER = "username";
public static final String DB_PASSWORD = "password";
public static final String DB_HOST = "host";
- public static final String DB_PORT = "port";
public static final String DB_DATABASE = "database";
+ public static final String DB_OPTIONS = "options";
public static final String DB_PREFIX_STANDARD = "mongodb://";
public static final String DB_PREFIX_SRV = "mongodb+srv://";
- public static final String DB_DEFAULT_OPTIONS = "?retryWrites=true&w=majority";
+ public static final String DB_SRV = "srv";
- private static final String CONNECTION_STRING_WITH_USER = "%s%s:%s@%s/%s" + DB_DEFAULT_OPTIONS; // user:pass@hostPort/database
- private static final String CONNECTION_STRING_WITH_USER_NO_DB = "%s%s:%s@%s" + DB_DEFAULT_OPTIONS; // user:pass@hostPort
- private static final String CONNECTION_STRING_WITHOUT_USER = "%s%s/%s" + DB_DEFAULT_OPTIONS; // hostPort/database
- private static final String CONNECTION_STRING_WITHOUT_USER_NO_DB = "%s%s" + DB_DEFAULT_OPTIONS; // hostPort
+ // 1st format specifier is for the prefix in each of the following ConnectionString
+ private static final String CONNECTION_STRING_WITH_HOST_N_DB = "%s%s/%s"; // hostPort/database
+ private static final String CONNECTION_STRING_WITH_ONLY_HOST = "%s%s"; // hostPort
@Override
public Optional convert(List serviceBindings) {
@@ -68,37 +73,54 @@ public Optional convert(List service
return Optional.empty();
}
+ ServiceBinding binding = matchingByType.get();
Map properties = new HashMap<>();
- properties.put(MONGO_DB_CONNECTION_STRING, getConnectionString(matchingByType.get()));
+ setConnectionString(binding, properties);
+ setUsername(binding, properties);
+ setPassword(binding, properties);
return Optional.of(new ServiceBindingConfigSource(BINDING_CONFIG_SOURCE_NAME, properties));
}
- private String getConnectionString(ServiceBinding binding) {
- String user = getDbProperty(binding, DB_USER);
- String password = getDbProperty(binding, DB_PASSWORD);
- String hostPort = getHostPort(binding);
+ private void setConnectionString(ServiceBinding binding, Map properties) {
+ String hostPort = getHost(binding);
+ String prefix = isSrv(binding) ? DB_PREFIX_SRV : DB_PREFIX_STANDARD;
String database = getDbProperty(binding, DB_DATABASE);
- String prefix = isPortProvided(hostPort) ? DB_PREFIX_STANDARD : DB_PREFIX_SRV;
-
+ String options = getDbProperty(binding, DB_OPTIONS);
+ boolean isOptionsNotBlank = isNotBlank(options);
String connectionString;
- if (isBlank(user)) {
- if (isBlank(database)) {
- connectionString = format(CONNECTION_STRING_WITHOUT_USER_NO_DB, prefix, hostPort);
- } else {
- connectionString = format(CONNECTION_STRING_WITHOUT_USER, prefix, hostPort, database);
+
+ if (isBlank(database)) {
+ connectionString = format(CONNECTION_STRING_WITH_ONLY_HOST, prefix, hostPort);
+
+ if (isOptionsNotBlank) {
+ // We need a trailing slash before options otherwise Mongo throws "contains options without trailing slash" error
+ // If the database value is not present, then we haven't yet added the trailing slash (and hence add it here)
+ connectionString += "/";
}
} else {
- if (isBlank(database)) {
- connectionString = format(CONNECTION_STRING_WITH_USER_NO_DB, prefix, user, password, hostPort);
- } else {
- connectionString = format(CONNECTION_STRING_WITH_USER, prefix, user, password, hostPort, database);
- }
+ connectionString = format(CONNECTION_STRING_WITH_HOST_N_DB, prefix, hostPort, database);
+ }
+
+ if (isOptionsNotBlank) {
+ connectionString += "?" + options;
}
LOGGER.info(format("MongoDB connection string: [%s]", connectionString));
- return connectionString;
+
+ properties.put(MONGO_DB_CONNECTION_STRING, connectionString);
+ }
+
+ private void setUsername(ServiceBinding binding, Map properties) {
+ String username = getDbProperty(binding, DB_USER); // encodeIfContainsSpecialCharacters(getDbProperty(binding, DB_USER));
+ LOGGER.info(format("MongoDB username=%s", username));
+
+ properties.put(MONGO_DB_USERNAME, username);
+ }
+
+ private void setPassword(ServiceBinding binding, Map properties) {
+ properties.put(MONGO_DB_PASSWORD, getDbProperty(binding, DB_PASSWORD));
}
private String getDbProperty(ServiceBinding binding, String dbPropertyKey) {
@@ -110,24 +132,20 @@ private String getDbProperty(ServiceBinding binding, String dbPropertyKey) {
return dbPropertyValue;
}
- private String getHostPort(ServiceBinding binding) {
+ private String getHost(ServiceBinding binding) {
String host = getDbProperty(binding, DB_HOST);
- String port = getDbProperty(binding, DB_PORT);
- String hostPort = host;
- if (isNotBlank(host)) {
- if (isNotBlank(port)) {
- hostPort = host + ":" + port;
- }
- } else {
+ if (isBlank(host)) {
LOGGER.warn("Unable to get the host property. Connection string won't be correct");
}
- return hostPort;
+ return host;
}
- private boolean isPortProvided(String hostPort) {
- return hostPort != null && hostPort.contains(":");
+ private boolean isSrv(ServiceBinding binding) {
+ String srv = getDbProperty(binding, DB_SRV);
+
+ return isNotBlank(srv) && parseBoolean(srv);
}
private boolean isBlank(String value) {
diff --git a/extensions/mongodb-client/runtime/src/test/java/io/quarkus/mongodb/runtime/MongoServiceBindingConverterTest.java b/extensions/mongodb-client/runtime/src/test/java/io/quarkus/mongodb/runtime/MongoServiceBindingConverterTest.java
index db650ef0a3477..36370ed9e2f8e 100644
--- a/extensions/mongodb-client/runtime/src/test/java/io/quarkus/mongodb/runtime/MongoServiceBindingConverterTest.java
+++ b/extensions/mongodb-client/runtime/src/test/java/io/quarkus/mongodb/runtime/MongoServiceBindingConverterTest.java
@@ -2,14 +2,16 @@
import static io.quarkus.mongodb.runtime.MongoServiceBindingConverter.BINDING_CONFIG_SOURCE_NAME;
import static io.quarkus.mongodb.runtime.MongoServiceBindingConverter.DB_DATABASE;
-import static io.quarkus.mongodb.runtime.MongoServiceBindingConverter.DB_DEFAULT_OPTIONS;
import static io.quarkus.mongodb.runtime.MongoServiceBindingConverter.DB_HOST;
+import static io.quarkus.mongodb.runtime.MongoServiceBindingConverter.DB_OPTIONS;
import static io.quarkus.mongodb.runtime.MongoServiceBindingConverter.DB_PASSWORD;
-import static io.quarkus.mongodb.runtime.MongoServiceBindingConverter.DB_PORT;
import static io.quarkus.mongodb.runtime.MongoServiceBindingConverter.DB_PREFIX_SRV;
import static io.quarkus.mongodb.runtime.MongoServiceBindingConverter.DB_PREFIX_STANDARD;
+import static io.quarkus.mongodb.runtime.MongoServiceBindingConverter.DB_SRV;
import static io.quarkus.mongodb.runtime.MongoServiceBindingConverter.DB_USER;
import static io.quarkus.mongodb.runtime.MongoServiceBindingConverter.MONGO_DB_CONNECTION_STRING;
+import static io.quarkus.mongodb.runtime.MongoServiceBindingConverter.MONGO_DB_PASSWORD;
+import static io.quarkus.mongodb.runtime.MongoServiceBindingConverter.MONGO_DB_USERNAME;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
@@ -32,11 +34,19 @@ public class MongoServiceBindingConverterTest {
private final static String BINDING_DIRECTORY_NO_PORT = "no-port";
private final static String BINDING_DIRECTORY_NO_USER = "no-user";
+ private static final String DB_PORT = "port";
+
private static final String EXPECTED_USERNAME = "someUserName";
- private static final String EXPECTED_PASSWORD = "password123 isItAGoodPassword";
+ private static final String EXPECTED_PASSWORD = "password123 isItAGoodPassword 7@%|?^B6";
private static final String EXPECTED_HOST = "mongodb0.example.com";
+ private static final String EXPECTED_STANDARD_HOST_N_PORT_2 = "mongodb0.example.com:27017, mongodb1.example.com:27017, mongodb2.example.com:27017";
private static final String EXPECTED_DB = "random-DB";
private static final String EXPECTED_PORT = "11010";
+ private static final String EXPECTED_HOST_N_PORT = EXPECTED_HOST + ":" + EXPECTED_PORT;
+ private static final String DB_N_OPTIONS_SEPARATOR = "/";
+ private static final String OPTIONS_SEPARATOR = "?";
+ private static final String EXPECTED_OPTIONS = "retryWrites=true&w=majority";
+ private static final String EXPECTED_SRV = "true";
@Test
public void testBindingWithAllProperties() {
@@ -47,9 +57,10 @@ public void testBindingWithAllProperties() {
assertThat(serviceBinding.getProvider()).isEqualTo("atlas");
assertThat(serviceBinding.getProperties().get(DB_USER)).isEqualTo(EXPECTED_USERNAME);
assertThat(serviceBinding.getProperties().get(DB_PASSWORD)).isEqualTo(EXPECTED_PASSWORD);
- assertThat(serviceBinding.getProperties().get(DB_HOST)).isEqualTo(EXPECTED_HOST);
- assertThat(serviceBinding.getProperties().get(DB_PORT)).isEqualTo(EXPECTED_PORT);
+ assertThat(serviceBinding.getProperties().get(DB_HOST)).isEqualTo(EXPECTED_HOST_N_PORT);
+ assertThat(serviceBinding.getProperties().get(DB_PORT)).isNull();
assertThat(serviceBinding.getProperties().get(DB_DATABASE)).isEqualTo(EXPECTED_DB);
+ assertThat(serviceBinding.getProperties().get(DB_OPTIONS)).isEqualTo(EXPECTED_OPTIONS);
}
@Test
@@ -64,100 +75,190 @@ public void testBindingWithMissingProperties() {
assertThat(serviceBinding.getProperties().get(DB_HOST)).isEqualTo(EXPECTED_HOST);
assertThat(serviceBinding.getProperties().get(DB_PORT)).isNull();
assertThat(serviceBinding.getProperties().get(DB_DATABASE)).isEqualTo(EXPECTED_DB);
+ assertThat(serviceBinding.getProperties().get(DB_OPTIONS)).isEqualTo(EXPECTED_OPTIONS);
}
@Test
public void testConnectionStringWithPortPresent() {
- ServiceBindingConfigSource serviceBindingConfigSource = readBindingAndVerifyConfigSource(BINDING_DIRECTORY_ALL_PROPS);
+ HashMap properties = getMapWithAllValuesPopulated(EXPECTED_HOST_N_PORT);
+ properties.remove(DB_SRV); // Make it a Standard connection string
- String expectedConnString = DB_PREFIX_STANDARD + EXPECTED_USERNAME + ":" + EXPECTED_PASSWORD +
- "@" + EXPECTED_HOST + ":" + EXPECTED_PORT + "/" + EXPECTED_DB + DB_DEFAULT_OPTIONS;
+ ServiceBindingConfigSource serviceBindingConfigSource = readBindingAsSpyAndVerifyConfigSource(
+ BINDING_DIRECTORY_ALL_PROPS, properties);
+
+ String expectedConnString = DB_PREFIX_STANDARD + EXPECTED_HOST_N_PORT + DB_N_OPTIONS_SEPARATOR + EXPECTED_DB
+ + OPTIONS_SEPARATOR + EXPECTED_OPTIONS;
verifyConnectionString(serviceBindingConfigSource, DB_PREFIX_STANDARD, expectedConnString);
+ verifyUsernameAndPassword(serviceBindingConfigSource);
}
@Test
- public void testConnectionStringWithPortMissing() {
- ServiceBindingConfigSource serviceBindingConfigSource = readBindingAndVerifyConfigSource(BINDING_DIRECTORY_NO_PORT);
+ public void testSrvConnectionStringWithPortMissing() {
+ HashMap properties = getMapWithAllValuesPopulated(EXPECTED_HOST);
+
+ ServiceBindingConfigSource serviceBindingConfigSource = readBindingAsSpyAndVerifyConfigSource(BINDING_DIRECTORY_NO_PORT,
+ properties);
- String expectedConnString = DB_PREFIX_SRV + EXPECTED_USERNAME + ":" + EXPECTED_PASSWORD +
- "@" + EXPECTED_HOST + "/" + EXPECTED_DB + DB_DEFAULT_OPTIONS;
+ String expectedConnString = DB_PREFIX_SRV + EXPECTED_HOST + DB_N_OPTIONS_SEPARATOR + EXPECTED_DB + OPTIONS_SEPARATOR
+ + EXPECTED_OPTIONS;
verifyConnectionString(serviceBindingConfigSource, DB_PREFIX_SRV, expectedConnString);
+ verifyUsernameAndPassword(serviceBindingConfigSource);
+ }
+
+ @Test
+ public void testStandardConnectionStringWithPortMissing() {
+ HashMap properties = getMapWithAllValuesPopulated(EXPECTED_HOST);
+ properties.remove(DB_SRV); // Make it a Standard connection string
+
+ ServiceBindingConfigSource serviceBindingConfigSource = readBindingAsSpyAndVerifyConfigSource(BINDING_DIRECTORY_NO_PORT,
+ properties);
+
+ String expectedConnString = DB_PREFIX_STANDARD + EXPECTED_HOST + DB_N_OPTIONS_SEPARATOR + EXPECTED_DB
+ + OPTIONS_SEPARATOR + EXPECTED_OPTIONS;
+ verifyConnectionString(serviceBindingConfigSource, DB_PREFIX_STANDARD, expectedConnString);
+ verifyUsernameAndPassword(serviceBindingConfigSource);
+ }
+
+ @Test
+ public void testStandardConnectionStringWithMultipleHostAndPort() {
+ HashMap properties = getMapWithAllValuesPopulated(EXPECTED_STANDARD_HOST_N_PORT_2);
+ properties.remove(DB_SRV); // Make it a Standard connection string
+
+ ServiceBindingConfigSource serviceBindingConfigSource = readBindingAsSpyAndVerifyConfigSource(BINDING_DIRECTORY_NO_PORT,
+ properties);
+
+ String expectedConnString = DB_PREFIX_STANDARD + EXPECTED_STANDARD_HOST_N_PORT_2 + DB_N_OPTIONS_SEPARATOR + EXPECTED_DB
+ + OPTIONS_SEPARATOR + EXPECTED_OPTIONS;
+ verifyConnectionString(serviceBindingConfigSource, DB_PREFIX_STANDARD, expectedConnString);
+ verifyUsernameAndPassword(serviceBindingConfigSource);
}
@Test
public void testConnectionStringWithPortAndDatabaseMissing() {
- HashMap properties = getMapWithAllValuesPopulated();
- properties.remove(DB_PORT); // BindingDirectory doesn't have this property, but removing it since it's put back in the map
+ HashMap properties = getMapWithAllValuesPopulated(EXPECTED_HOST);
properties.remove(DB_DATABASE); // This is what we actually want to test out
ServiceBindingConfigSource serviceBindingConfigSource = readBindingAsSpyAndVerifyConfigSource(
BINDING_DIRECTORY_NO_PORT, properties);
- String expectedConnString = DB_PREFIX_SRV + EXPECTED_USERNAME + ":" + EXPECTED_PASSWORD +
- "@" + EXPECTED_HOST + DB_DEFAULT_OPTIONS;
+ String expectedConnString = DB_PREFIX_SRV + EXPECTED_HOST + DB_N_OPTIONS_SEPARATOR + OPTIONS_SEPARATOR
+ + EXPECTED_OPTIONS;
verifyConnectionString(serviceBindingConfigSource, DB_PREFIX_SRV, expectedConnString);
+ verifyUsernameAndPassword(serviceBindingConfigSource);
}
@Test
- public void testConnectionStringWithUserMissing() {
+ public void testConnectionStringWithUserAndOptionsMissing() {
ServiceBindingConfigSource serviceBindingConfigSource = readBindingAndVerifyConfigSource(BINDING_DIRECTORY_NO_USER);
- String expectedConnString = DB_PREFIX_STANDARD + EXPECTED_HOST + ":" + EXPECTED_PORT + "/" + EXPECTED_DB
- + DB_DEFAULT_OPTIONS;
+ String expectedConnString = DB_PREFIX_STANDARD + EXPECTED_HOST + ":" + EXPECTED_PORT + DB_N_OPTIONS_SEPARATOR
+ + EXPECTED_DB;
+ verifyConnectionString(serviceBindingConfigSource, DB_PREFIX_STANDARD, expectedConnString);
+ }
+
+ @Test
+ public void testConnectionStringWithUserMissing() {
+ HashMap properties = getMapWithAllValuesPopulated(EXPECTED_HOST_N_PORT);
+ properties.remove(DB_SRV); // BindingDirectory doesn't have this property, but removing it since it's put back in the map
+ properties.remove(DB_USER); // This is what we actually want to test out
+
+ ServiceBindingConfigSource serviceBindingConfigSource = readBindingAsSpyAndVerifyConfigSource(BINDING_DIRECTORY_NO_USER,
+ properties);
+
+ String expectedConnString = DB_PREFIX_STANDARD + EXPECTED_HOST + ":" + EXPECTED_PORT + DB_N_OPTIONS_SEPARATOR
+ + EXPECTED_DB + OPTIONS_SEPARATOR + EXPECTED_OPTIONS;
verifyConnectionString(serviceBindingConfigSource, DB_PREFIX_STANDARD, expectedConnString);
}
@Test
public void testConnectionStringWithUserAndPortMissing() {
- HashMap properties = getMapWithAllValuesPopulated();
+ HashMap properties = getMapWithAllValuesPopulated(EXPECTED_HOST);
properties.remove(DB_USER); // BindingDirectory doesn't have this property, but removing it since it's put back in the map
- properties.remove(DB_PORT); // This is what we actually want to test out
ServiceBindingConfigSource serviceBindingConfigSource = readBindingAsSpyAndVerifyConfigSource(
BINDING_DIRECTORY_NO_USER, properties);
- String expectedConnString = DB_PREFIX_SRV + EXPECTED_HOST + "/" + EXPECTED_DB + DB_DEFAULT_OPTIONS;
+ String expectedConnString = DB_PREFIX_SRV + EXPECTED_HOST + DB_N_OPTIONS_SEPARATOR + EXPECTED_DB
+ + OPTIONS_SEPARATOR + EXPECTED_OPTIONS;
verifyConnectionString(serviceBindingConfigSource, DB_PREFIX_SRV, expectedConnString);
}
@Test
public void testConnectionStringWithUserAndPortAndDatabaseMissing() {
- HashMap properties = getMapWithAllValuesPopulated();
+ HashMap properties = getMapWithAllValuesPopulated(EXPECTED_HOST);
properties.remove(DB_USER); // BindingDirectory doesn't have this property, but removing it since it's put back in the map
- properties.remove(DB_PORT); // This is what we actually want to test out
properties.remove(DB_DATABASE); // This is what we actually want to test out
ServiceBindingConfigSource serviceBindingConfigSource = readBindingAsSpyAndVerifyConfigSource(
BINDING_DIRECTORY_NO_USER, properties);
- String expectedConnString = DB_PREFIX_SRV + EXPECTED_HOST + DB_DEFAULT_OPTIONS;
+ String expectedConnString = DB_PREFIX_SRV + EXPECTED_HOST + DB_N_OPTIONS_SEPARATOR + OPTIONS_SEPARATOR
+ + EXPECTED_OPTIONS;
verifyConnectionString(serviceBindingConfigSource, DB_PREFIX_SRV, expectedConnString);
}
@Test
- public void testConnectionStringWithUserAndDatabaseMissing() {
- HashMap properties = getMapWithAllValuesPopulated();
+ public void testStandardConnectionStringWithUserAndDatabaseMissing() {
+ HashMap properties = getMapWithAllValuesPopulated(EXPECTED_HOST_N_PORT);
properties.remove(DB_USER); // BindingDirectory doesn't have this property, but removing it since it's put back in the map
+ properties.remove(DB_SRV);
properties.remove(DB_DATABASE); // This is what we actually want to test out
ServiceBindingConfigSource serviceBindingConfigSource = readBindingAsSpyAndVerifyConfigSource(
BINDING_DIRECTORY_NO_USER, properties);
- String expectedConnString = DB_PREFIX_STANDARD + EXPECTED_HOST + ":" + EXPECTED_PORT + DB_DEFAULT_OPTIONS;
+ String expectedConnString = DB_PREFIX_STANDARD + EXPECTED_HOST_N_PORT + DB_N_OPTIONS_SEPARATOR
+ + OPTIONS_SEPARATOR + EXPECTED_OPTIONS;
verifyConnectionString(serviceBindingConfigSource, DB_PREFIX_STANDARD, expectedConnString);
}
@Test
- public void testConnectionStringWithDatabaseMissing() {
- HashMap properties = getMapWithAllValuesPopulated();
+ public void testSrvConnectionStringWithUserAndDatabaseMissing() {
+ HashMap properties = getMapWithAllValuesPopulated(EXPECTED_HOST_N_PORT);
+ properties.remove(DB_USER); // BindingDirectory doesn't have this property, but removing it since it's put back in the map
+ properties.remove(DB_DATABASE); // This is what we actually want to test out
+
+ ServiceBindingConfigSource serviceBindingConfigSource = readBindingAsSpyAndVerifyConfigSource(
+ BINDING_DIRECTORY_NO_USER, properties);
+
+ String expectedConnString = DB_PREFIX_SRV + EXPECTED_HOST_N_PORT + DB_N_OPTIONS_SEPARATOR
+ + OPTIONS_SEPARATOR + EXPECTED_OPTIONS;
+ verifyConnectionString(serviceBindingConfigSource, DB_PREFIX_SRV, expectedConnString);
+ }
+
+ @Test
+ public void testStandardConnectionStringWithDatabaseMissing() {
+ HashMap properties = getMapWithAllValuesPopulated(EXPECTED_HOST_N_PORT);
+ properties.remove(DB_SRV); // BindingDirectory doesn't have this property, but removing it since it's put back in the map
properties.remove(DB_DATABASE); // This is what we actually want to test out
ServiceBindingConfigSource serviceBindingConfigSource = readBindingAsSpyAndVerifyConfigSource(
BINDING_DIRECTORY_ALL_PROPS, properties);
- String expectedConnString = DB_PREFIX_STANDARD + EXPECTED_USERNAME + ":" + EXPECTED_PASSWORD +
- "@" + EXPECTED_HOST + ":" + EXPECTED_PORT + DB_DEFAULT_OPTIONS;
+ String expectedConnString = DB_PREFIX_STANDARD +
+ EXPECTED_HOST_N_PORT + DB_N_OPTIONS_SEPARATOR + OPTIONS_SEPARATOR + EXPECTED_OPTIONS;
verifyConnectionString(serviceBindingConfigSource, DB_PREFIX_STANDARD, expectedConnString);
+
+ verifyUsernameAndPassword(serviceBindingConfigSource);
+ }
+
+ private void verifyUsernameAndPassword(ServiceBindingConfigSource serviceBindingConfigSource) {
+ assertThat(serviceBindingConfigSource.getProperties().get(MONGO_DB_USERNAME)).isEqualTo(EXPECTED_USERNAME);
+ assertThat(serviceBindingConfigSource.getProperties().get(MONGO_DB_PASSWORD)).isEqualTo(EXPECTED_PASSWORD);
+ }
+
+ @Test
+ public void testSrvConnectionStringWithDatabaseMissing() {
+ HashMap properties = getMapWithAllValuesPopulated(EXPECTED_HOST_N_PORT);
+ properties.remove(DB_DATABASE); // This is what we actually want to test out
+
+ ServiceBindingConfigSource serviceBindingConfigSource = readBindingAsSpyAndVerifyConfigSource(
+ BINDING_DIRECTORY_ALL_PROPS, properties);
+
+ String expectedConnString = DB_PREFIX_SRV + EXPECTED_HOST_N_PORT + DB_N_OPTIONS_SEPARATOR + OPTIONS_SEPARATOR
+ + EXPECTED_OPTIONS;
+ verifyConnectionString(serviceBindingConfigSource, DB_PREFIX_SRV, expectedConnString);
+ verifyUsernameAndPassword(serviceBindingConfigSource);
}
private ServiceBindingConfigSource readBindingAndVerifyConfigSource(String bindingDirectory) {
@@ -200,13 +301,14 @@ private void verifyConnectionString(ServiceBindingConfigSource serviceBindingCon
assertThat(serviceBindingConfigSource.getProperties().get(MONGO_DB_CONNECTION_STRING)).isEqualTo(expectedConnString);
}
- private HashMap getMapWithAllValuesPopulated() {
+ private HashMap getMapWithAllValuesPopulated(String host) {
HashMap map = new HashMap<>();
map.put(DB_USER, EXPECTED_USERNAME);
map.put(DB_PASSWORD, EXPECTED_PASSWORD);
- map.put(DB_HOST, EXPECTED_HOST);
- map.put(DB_PORT, EXPECTED_PORT);
+ map.put(DB_HOST, host);
map.put(DB_DATABASE, EXPECTED_DB);
+ map.put(DB_OPTIONS, EXPECTED_OPTIONS);
+ map.put(DB_SRV, EXPECTED_SRV);
return map;
}
diff --git a/extensions/mongodb-client/runtime/src/test/resources/service-binding/all-props/host b/extensions/mongodb-client/runtime/src/test/resources/service-binding/all-props/host
index bee4cd9950942..f5ed8d222efbd 100644
--- a/extensions/mongodb-client/runtime/src/test/resources/service-binding/all-props/host
+++ b/extensions/mongodb-client/runtime/src/test/resources/service-binding/all-props/host
@@ -1 +1 @@
-mongodb0.example.com
\ No newline at end of file
+mongodb0.example.com:11010
\ No newline at end of file
diff --git a/extensions/mongodb-client/runtime/src/test/resources/service-binding/all-props/options b/extensions/mongodb-client/runtime/src/test/resources/service-binding/all-props/options
new file mode 100644
index 0000000000000..92d1438443247
--- /dev/null
+++ b/extensions/mongodb-client/runtime/src/test/resources/service-binding/all-props/options
@@ -0,0 +1 @@
+retryWrites=true&w=majority
\ No newline at end of file
diff --git a/extensions/mongodb-client/runtime/src/test/resources/service-binding/all-props/password b/extensions/mongodb-client/runtime/src/test/resources/service-binding/all-props/password
index b4a5888fa99a2..fc237f1514422 100644
--- a/extensions/mongodb-client/runtime/src/test/resources/service-binding/all-props/password
+++ b/extensions/mongodb-client/runtime/src/test/resources/service-binding/all-props/password
@@ -1 +1 @@
-password123 isItAGoodPassword
\ No newline at end of file
+password123 isItAGoodPassword 7@%|?^B6
\ No newline at end of file
diff --git a/extensions/mongodb-client/runtime/src/test/resources/service-binding/all-props/port b/extensions/mongodb-client/runtime/src/test/resources/service-binding/all-props/port
deleted file mode 100644
index d0c0a5740ae48..0000000000000
--- a/extensions/mongodb-client/runtime/src/test/resources/service-binding/all-props/port
+++ /dev/null
@@ -1 +0,0 @@
-11010
\ No newline at end of file
diff --git a/extensions/mongodb-client/runtime/src/test/resources/service-binding/all-props/srv b/extensions/mongodb-client/runtime/src/test/resources/service-binding/all-props/srv
new file mode 100644
index 0000000000000..f32a5804e292d
--- /dev/null
+++ b/extensions/mongodb-client/runtime/src/test/resources/service-binding/all-props/srv
@@ -0,0 +1 @@
+true
\ No newline at end of file
diff --git a/extensions/mongodb-client/runtime/src/test/resources/service-binding/no-port/options b/extensions/mongodb-client/runtime/src/test/resources/service-binding/no-port/options
new file mode 100644
index 0000000000000..92d1438443247
--- /dev/null
+++ b/extensions/mongodb-client/runtime/src/test/resources/service-binding/no-port/options
@@ -0,0 +1 @@
+retryWrites=true&w=majority
\ No newline at end of file
diff --git a/extensions/mongodb-client/runtime/src/test/resources/service-binding/no-port/password b/extensions/mongodb-client/runtime/src/test/resources/service-binding/no-port/password
index b4a5888fa99a2..fc237f1514422 100644
--- a/extensions/mongodb-client/runtime/src/test/resources/service-binding/no-port/password
+++ b/extensions/mongodb-client/runtime/src/test/resources/service-binding/no-port/password
@@ -1 +1 @@
-password123 isItAGoodPassword
\ No newline at end of file
+password123 isItAGoodPassword 7@%|?^B6
\ No newline at end of file
diff --git a/extensions/mongodb-client/runtime/src/test/resources/service-binding/no-port/srv b/extensions/mongodb-client/runtime/src/test/resources/service-binding/no-port/srv
new file mode 100644
index 0000000000000..f32a5804e292d
--- /dev/null
+++ b/extensions/mongodb-client/runtime/src/test/resources/service-binding/no-port/srv
@@ -0,0 +1 @@
+true
\ No newline at end of file
diff --git a/extensions/mongodb-client/runtime/src/test/resources/service-binding/no-user/host b/extensions/mongodb-client/runtime/src/test/resources/service-binding/no-user/host
index bee4cd9950942..f5ed8d222efbd 100644
--- a/extensions/mongodb-client/runtime/src/test/resources/service-binding/no-user/host
+++ b/extensions/mongodb-client/runtime/src/test/resources/service-binding/no-user/host
@@ -1 +1 @@
-mongodb0.example.com
\ No newline at end of file
+mongodb0.example.com:11010
\ No newline at end of file
diff --git a/extensions/mongodb-client/runtime/src/test/resources/service-binding/no-user/port b/extensions/mongodb-client/runtime/src/test/resources/service-binding/no-user/port
deleted file mode 100644
index d0c0a5740ae48..0000000000000
--- a/extensions/mongodb-client/runtime/src/test/resources/service-binding/no-user/port
+++ /dev/null
@@ -1 +0,0 @@
-11010
\ No newline at end of file