mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-09-17 11:19:15 +00:00
added test case for MongoDB config management
Change-Id: I4f63358d27acb75b1e864b6e7d2976d0af897194
This commit is contained in:
+4
-4
@@ -18,13 +18,13 @@ public abstract class AbstractMongoDBTest {
|
||||
protected Mongo mongo;
|
||||
protected DB db;
|
||||
private final MongoDBConfiguration dbConfiguration;
|
||||
private MongoDBService racingEventService;
|
||||
private MongoDBService mongoDBService;
|
||||
|
||||
public AbstractMongoDBTest() throws UnknownHostException, MongoException {
|
||||
dbConfiguration = MongoDBConfiguration.getDefaultTestConfiguration();
|
||||
racingEventService = getDBConfiguration().getService();
|
||||
mongoDBService = getDBConfiguration().getService();
|
||||
mongo = newMongo();
|
||||
db = racingEventService.getDB();
|
||||
db = mongoDBService.getDB();
|
||||
}
|
||||
|
||||
protected MongoDBConfiguration getDBConfiguration() {
|
||||
@@ -51,6 +51,6 @@ public abstract class AbstractMongoDBTest {
|
||||
}
|
||||
|
||||
protected MongoDBService getMongoService() {
|
||||
return racingEventService;
|
||||
return mongoDBService;
|
||||
}
|
||||
}
|
||||
|
||||
Executable
+29
@@ -0,0 +1,29 @@
|
||||
package com.sap.sailing.mongodb.test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mongodb.ReadPreference;
|
||||
import com.sap.sse.mongodb.MongoDBConfiguration;
|
||||
|
||||
public class MongoClientURITest {
|
||||
@Test
|
||||
public void testDefaultConnectionOptions() {
|
||||
MongoDBConfiguration config = new MongoDBConfiguration("mongodb://humba:12345/mydb");
|
||||
assertEquals("humba", config.getHostname());
|
||||
assertEquals(12345, config.getPort());
|
||||
assertEquals("mydb", config.getDatabaseName());
|
||||
assertSame(ReadPreference.primaryPreferred(), config.getMongoClientURI().getOptions().getReadPreference());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpecifigConnectionOptions() {
|
||||
MongoDBConfiguration config = new MongoDBConfiguration("mongodb://humba:12345/mydb?replicaset=rs0&readpreference=primary");
|
||||
assertEquals("humba", config.getHostname());
|
||||
assertEquals(12345, config.getPort());
|
||||
assertEquals("mydb", config.getDatabaseName());
|
||||
assertSame(ReadPreference.primary(), config.getMongoClientURI().getOptions().getReadPreference());
|
||||
}
|
||||
}
|
||||
@@ -82,18 +82,22 @@ public class MongoDBConfiguration {
|
||||
return result;
|
||||
}
|
||||
|
||||
public MongoDBConfiguration(String mongoClientURIAsString) {
|
||||
this.mongoClientURI = new MongoClientURI(mongoClientURIAsString, getDefaultOptionsBuilder());
|
||||
}
|
||||
|
||||
public MongoDBConfiguration(MongoClientURI mongoClientURI) {
|
||||
this.mongoClientURI = mongoClientURI;
|
||||
}
|
||||
|
||||
|
||||
public MongoDBConfiguration(String hostName, String databaseName) {
|
||||
this.mongoClientURI = new MongoClientURI("mongodb://"+hostName+"/"+databaseName);
|
||||
this.mongoClientURI = new MongoClientURI("mongodb://"+hostName+"/"+databaseName, getDefaultOptionsBuilder());
|
||||
}
|
||||
|
||||
|
||||
public MongoDBConfiguration(String hostName, int port, String databaseName) {
|
||||
this.mongoClientURI = new MongoClientURI("mongodb://"+hostName+":"+port+"/"+databaseName);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates and returns a DB service for this configuration
|
||||
*/
|
||||
@@ -113,6 +117,10 @@ public class MongoDBConfiguration {
|
||||
return getMongoClientURI().getDatabase();
|
||||
}
|
||||
|
||||
public String getHostname() {
|
||||
return getMongoClientURI().getHosts().get(0).split(":")[0];
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
int result = ServerAddress.defaultPort();
|
||||
if (!getMongoClientURI().getHosts().isEmpty() && getMongoClientURI().getHosts().get(0).split(":").length > 1) {
|
||||
|
||||
Reference in New Issue
Block a user