Search in sources :

Example 91 with ServerAddress

use of com.mongodb.ServerAddress in project spring-data-mongodb by spring-projects.

the class MongoNamespaceReplicaSetTests method testMongoWithReplicaSets.

@Test
@Ignore("CI infrastructure does not yet support replica sets")
public void testMongoWithReplicaSets() {
    MongoClient mongo = ctx.getBean(MongoClient.class);
    assertThat(mongo.getClusterDescription().getClusterSettings().getHosts()).isEqualTo(2);
    List<ServerAddress> servers = mongo.getClusterDescription().getClusterSettings().getHosts();
    assertThat(servers.get(0).getHost()).isEqualTo("127.0.0.1");
    assertThat(servers.get(1).getHost()).isEqualTo("localhost");
    assertThat(servers.get(0).getPort()).isEqualTo(10001);
    assertThat(servers.get(1).getPort()).isEqualTo(10002);
    MongoTemplate template = new MongoTemplate(mongo, "admin");
    Document result = template.executeCommand("{replSetGetStatus : 1}");
    assertThat(result.get("set").toString()).isEqualTo("blort");
}
Also used : MongoClient(com.mongodb.client.MongoClient) ServerAddress(com.mongodb.ServerAddress) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) Document(org.bson.Document) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 92 with ServerAddress

use of com.mongodb.ServerAddress in project spring-data-mongodb by spring-projects.

the class MongoNamespaceTests method testSecondMongoDbFactory.

@Test
public void testSecondMongoDbFactory() {
    assertThat(ctx.containsBean("secondMongoDbFactory")).isTrue();
    MongoDatabaseFactory dbf = (MongoDatabaseFactory) ctx.getBean("secondMongoDbFactory");
    MongoClient mongo = (MongoClient) getField(dbf, "mongoClient");
    assertThat(mongo.getClusterDescription().getClusterSettings().getHosts()).containsExactly(new ServerAddress());
    assertThat(getField(dbf, "databaseName")).isEqualTo("database");
}
Also used : MongoClient(com.mongodb.client.MongoClient) MongoDatabaseFactory(org.springframework.data.mongodb.MongoDatabaseFactory) ServerAddress(com.mongodb.ServerAddress) Test(org.junit.Test)

Example 93 with ServerAddress

use of com.mongodb.ServerAddress in project spring-data-mongodb by spring-projects.

the class MongoExceptionTranslatorUnitTests method translateSocketExceptionSubclasses.

// GH-3568
@Test
void translateSocketExceptionSubclasses() {
    expectExceptionWithCauseMessage(translator.translateExceptionIfPossible(new MongoSocketWriteException("intermediate message", new ServerAddress(), new Exception(EXCEPTION_MESSAGE))), DataAccessResourceFailureException.class, EXCEPTION_MESSAGE);
    expectExceptionWithCauseMessage(translator.translateExceptionIfPossible(new MongoSocketReadTimeoutException("intermediate message", new ServerAddress(), new Exception(EXCEPTION_MESSAGE))), DataAccessResourceFailureException.class, EXCEPTION_MESSAGE);
}
Also used : MongoSocketWriteException(com.mongodb.MongoSocketWriteException) ServerAddress(com.mongodb.ServerAddress) MongoSocketReadTimeoutException(com.mongodb.MongoSocketReadTimeoutException) MongoInternalException(com.mongodb.MongoInternalException) NestedRuntimeException(org.springframework.core.NestedRuntimeException) DataAccessException(org.springframework.dao.DataAccessException) MongoException(com.mongodb.MongoException) MongoTransactionException(org.springframework.data.mongodb.MongoTransactionException) MongoSocketWriteException(com.mongodb.MongoSocketWriteException) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) MongoSocketException(com.mongodb.MongoSocketException) ClientSessionException(org.springframework.data.mongodb.ClientSessionException) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) MongoCursorNotFoundException(com.mongodb.MongoCursorNotFoundException) InvalidDataAccessResourceUsageException(org.springframework.dao.InvalidDataAccessResourceUsageException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) MongoSocketReadTimeoutException(com.mongodb.MongoSocketReadTimeoutException) UncategorizedMongoDbException(org.springframework.data.mongodb.UncategorizedMongoDbException) Test(org.junit.jupiter.api.Test)

Example 94 with ServerAddress

use of com.mongodb.ServerAddress in project spring-data-mongodb by spring-projects.

the class MongoClientFactoryBeanUnitTests method hostPortParametersNotOverriddenByDefaults.

// DATAMONGO-2427
@Test
void hostPortParametersNotOverriddenByDefaults() {
    MongoClientFactoryBean factoryBean = new MongoClientFactoryBean();
    factoryBean.setPort(2500);
    factoryBean.setHost("db2.example.net");
    factoryBean.setReplicaSet("rs0");
    factoryBean.setMongoClientSettings(MongoClientSettings.builder().build());
    MongoClientSettings settings = factoryBean.computeClientSetting();
    assertThat(settings.getClusterSettings().getRequiredReplicaSetName()).isEqualTo("rs0");
    assertThat(settings.getClusterSettings().getHosts()).containsExactly(new ServerAddress("db2.example.net", 2500));
}
Also used : ServerAddress(com.mongodb.ServerAddress) MongoClientSettings(com.mongodb.MongoClientSettings) Test(org.junit.jupiter.api.Test)

Example 95 with ServerAddress

use of com.mongodb.ServerAddress in project spring-data-mongodb by spring-projects.

the class DbRefMappingBenchmark method setUp.

@Setup
public void setUp() throws Exception {
    client = new MongoClient(new ServerAddress());
    template = new MongoTemplate(client, DB_NAME);
    List<RefObject> refObjects = new ArrayList<>();
    for (int i = 0; i < 1; i++) {
        RefObject o = new RefObject();
        template.save(o);
        refObjects.add(o);
    }
    ObjectWithDBRef singleDBRef = new ObjectWithDBRef();
    singleDBRef.ref = refObjects.iterator().next();
    template.save(singleDBRef);
    ObjectWithDBRef multipleDBRefs = new ObjectWithDBRef();
    multipleDBRefs.refList = refObjects;
    template.save(multipleDBRefs);
    queryObjectWithDBRef = query(where("id").is(singleDBRef.id));
    queryObjectWithDBRefList = query(where("id").is(multipleDBRefs.id));
}
Also used : MongoClient(com.mongodb.MongoClient) ServerAddress(com.mongodb.ServerAddress) ArrayList(java.util.ArrayList) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) Setup(org.openjdk.jmh.annotations.Setup)

Aggregations

ServerAddress (com.mongodb.ServerAddress)201 Test (org.junit.Test)49 MongoClient (com.mongodb.MongoClient)48 ArrayList (java.util.ArrayList)31 Test (org.junit.jupiter.api.Test)31 MongoCredential (com.mongodb.MongoCredential)30 ClusterSettings (com.mongodb.connection.ClusterSettings)20 ClusterId (com.mongodb.connection.ClusterId)19 MongoClientSettings (com.mongodb.MongoClientSettings)18 BsonDocument (org.bson.BsonDocument)18 Before (org.junit.Before)18 ClusterDescription (com.mongodb.connection.ClusterDescription)15 Document (org.bson.Document)14 ServerDescription (com.mongodb.connection.ServerDescription)13 List (java.util.List)12 RepeatedTest (org.junit.jupiter.api.RepeatedTest)12 ConnectionString (com.mongodb.ConnectionString)10 MongoClient (com.mongodb.client.MongoClient)9 MongoDatabase (com.mongodb.client.MongoDatabase)9 ServerId (com.mongodb.connection.ServerId)8