Skip to content

Commit c1aeb53

Browse files
committed
allow more configuration of the database source
1 parent 8670510 commit c1aeb53

File tree

3 files changed

+107
-3
lines changed

3 files changed

+107
-3
lines changed

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.kapeta</groupId>
88
<artifactId>nosql-mongodb</artifactId>
9-
<version>1.1.1</version>
9+
<version>1.1.2</version>
1010

1111
<name>${project.groupId}:${project.artifactId}</name>
1212
<description>MongoDB support for Kapeta Spring Boot SDK</description>
@@ -158,6 +158,11 @@
158158
<version>[1.0,2.0)</version>
159159
<scope>provided</scope>
160160
</dependency>
161+
<dependency>
162+
<groupId>org.junit.jupiter</groupId>
163+
<artifactId>junit-jupiter</artifactId>
164+
<scope>test</scope>
165+
</dependency>
161166
</dependencies>
162167

163168
</project>

src/main/java/com/kapeta/spring/mongo/AbstractMongoDBConfig.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,25 @@ private MongoProperties createMongoProperties(String databaseName, String dbAuth
8787
return properties;
8888
}
8989

90-
private MongoProperties createMongoUriProperties(String databaseName, String dbAuthDB, ResourceInfo mongoInfo) {
90+
protected MongoProperties createMongoUriProperties(String databaseName, String dbAuthDB, ResourceInfo mongoInfo) {
9191
String username = mongoInfo.getCredentials().get("username");
9292
String password = mongoInfo.getCredentials().getOrDefault("password","");
9393

94-
String uri = String.format("mongodb+srv://%s:%s@%s/%s?ssl=false&authSource=%s", username, password, mongoInfo.getHost(), databaseName, dbAuthDB);
94+
String ssl = "ssl=false";
95+
if(mongoInfo.getOptions().containsKey("ssl")) {
96+
if (Boolean.parseBoolean(mongoInfo.getOptions().get("ssl").toString())) {
97+
ssl = "ssl=true";
98+
}
99+
}
100+
String dbAuthDBStr = String.format("&authSource=%s", dbAuthDB);
101+
if (StringUtils.isEmpty(dbAuthDB)) {
102+
dbAuthDBStr = "";
103+
}
104+
105+
String uri = String.format("mongodb+srv://%s:%s@%s/%s?%s%s", username, password, mongoInfo.getHost(), databaseName,ssl, dbAuthDBStr);
106+
// Override with environment variable if set
95107
if(!StringUtils.isEmpty(System.getenv("SPRING_DATA_MONGODB_URI"))) {
108+
log.info("Overriding MongoDB URI with environment variable SPRING_DATA_MONGODB_URI");
96109
uri = System.getenv("SPRING_DATA_MONGODB_URI");
97110
}
98111

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.kapeta.spring.mongo;
2+
3+
import com.kapeta.spring.config.providers.TestConfigProvider;
4+
import com.kapeta.spring.config.providers.types.ResourceInfo;
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
7+
8+
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
14+
public class AbstractMongoDBConfigTest {
15+
16+
17+
@Test
18+
public void testCreateMongoUriProperties() {
19+
Map<String, String> credentials = new HashMap<>();
20+
credentials.put("username", "testUser");
21+
credentials.put("password", "testPass");
22+
23+
Map<String, Object> options = new HashMap<>();
24+
options.put("ssl", "true");
25+
26+
TestMongoDBConfig mongoDBConfig = new TestMongoDBConfig("testResource");
27+
28+
ResourceInfo resourceInfo = new ResourceInfo();
29+
resourceInfo.setCredentials(credentials);
30+
resourceInfo.setOptions(options);
31+
resourceInfo.setHost("testHost");
32+
33+
MongoProperties properties = mongoDBConfig.createMongoUriProperties("testDB", "admin", resourceInfo);
34+
35+
assertEquals("mongodb+srv://testUser:testPass@testHost/testDB?ssl=true&authSource=admin", properties.getUri());
36+
}
37+
38+
39+
@Test
40+
public void testSSLFalse() {
41+
Map<String, String> credentials = new HashMap<>();
42+
credentials.put("username", "testUser");
43+
credentials.put("password", "testPass");
44+
45+
Map<String, Object> options = new HashMap<>();
46+
options.put("ssl", "false");
47+
48+
TestMongoDBConfig mongoDBConfig = new TestMongoDBConfig("testResource");
49+
50+
ResourceInfo resourceInfo = new ResourceInfo();
51+
resourceInfo.setCredentials(credentials);
52+
resourceInfo.setOptions(options);
53+
resourceInfo.setHost("testHost");
54+
55+
MongoProperties properties = mongoDBConfig.createMongoUriProperties("testDB", "admin", resourceInfo);
56+
57+
assertEquals("mongodb+srv://testUser:testPass@testHost/testDB?ssl=false&authSource=admin", properties.getUri());
58+
}
59+
60+
@Test
61+
public void testEmptyAuthSource() {
62+
Map<String, String> credentials = new HashMap<>();
63+
credentials.put("username", "testUser");
64+
credentials.put("password", "testPass");
65+
66+
Map<String, Object> options = new HashMap<>();
67+
options.put("ssl", "true");
68+
69+
TestMongoDBConfig mongoDBConfig = new TestMongoDBConfig("testResource");
70+
71+
ResourceInfo resourceInfo = new ResourceInfo();
72+
resourceInfo.setCredentials(credentials);
73+
resourceInfo.setOptions(options);
74+
resourceInfo.setHost("testHost");
75+
76+
MongoProperties properties = mongoDBConfig.createMongoUriProperties("testDB", "", resourceInfo);
77+
78+
assertEquals("mongodb+srv://testUser:testPass@testHost/testDB?ssl=true", properties.getUri());
79+
}
80+
81+
private class TestMongoDBConfig extends AbstractMongoDBConfig {
82+
public TestMongoDBConfig(String resourceName) {
83+
super(resourceName);
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)