Search in sources :

Example 81 with ServerAddress

use of com.mongodb.ServerAddress in project mongo-java-driver by mongodb.

the class MongoClientImplTest method testStartSession.

@Test
void testStartSession() {
    ServerDescription serverDescription = ServerDescription.builder().address(new ServerAddress()).state(ServerConnectionState.CONNECTED).maxWireVersion(8).build();
    MongoClientImpl mongoClient = createMongoClient();
    Cluster cluster = mongoClient.getCluster();
    when(cluster.getCurrentDescription()).thenReturn(new ClusterDescription(ClusterConnectionMode.SINGLE, ClusterType.STANDALONE, singletonList(serverDescription)));
    ServerSessionPool serverSessionPool = mock(ServerSessionPool.class);
    ClientSessionHelper clientSessionHelper = new ClientSessionHelper(mongoClient, serverSessionPool);
    assertAll("Start Session Tests", () -> assertAll("check validation", () -> assertThrows(IllegalArgumentException.class, () -> mongoClient.startSession(null))), () -> {
        Mono<ClientSession> expected = clientSessionHelper.createClientSessionMono(ClientSessionOptions.builder().build(), OPERATION_EXECUTOR);
        assertPublisherIsTheSameAs(expected, mongoClient.startSession(), "Default");
    }, () -> {
        ClientSessionOptions options = ClientSessionOptions.builder().causallyConsistent(true).defaultTransactionOptions(TransactionOptions.builder().readConcern(ReadConcern.LINEARIZABLE).build()).build();
        Mono<ClientSession> expected = clientSessionHelper.createClientSessionMono(options, OPERATION_EXECUTOR);
        assertPublisherIsTheSameAs(expected, mongoClient.startSession(options), "with options");
    });
}
Also used : ServerSessionPool(com.mongodb.internal.session.ServerSessionPool) ServerDescription(com.mongodb.connection.ServerDescription) ClientSession(com.mongodb.reactivestreams.client.ClientSession) ServerAddress(com.mongodb.ServerAddress) Cluster(com.mongodb.internal.connection.Cluster) ClusterDescription(com.mongodb.connection.ClusterDescription) ClientSessionOptions(com.mongodb.ClientSessionOptions) Test(org.junit.jupiter.api.Test)

Example 82 with ServerAddress

use of com.mongodb.ServerAddress in project mongomvcc by igd-geo.

the class MongoDBVDatabase method connect.

@Override
public void connect(String name, String host, int port) throws VException {
    Mongo mongo;
    try {
        mongo = new Mongo(new ServerAddress(host, port));
    } catch (UnknownHostException e) {
        throw new VException("Unknown host", e);
    }
    connectInternal(name, mongo);
}
Also used : UnknownHostException(java.net.UnknownHostException) VException(de.fhg.igd.mongomvcc.VException) Mongo(com.mongodb.Mongo) ServerAddress(com.mongodb.ServerAddress)

Example 83 with ServerAddress

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

the class MongoPropertiesClientSettingsBuilderCustomizer method applyHostAndPort.

private void applyHostAndPort(MongoClientSettings.Builder settings) {
    if (getEmbeddedPort() != null) {
        settings.applyConnectionString(new ConnectionString("mongodb://localhost:" + getEmbeddedPort()));
        return;
    }
    if (hasCustomAddress()) {
        String host = getOrDefault(this.properties.getHost(), "localhost");
        int port = getOrDefault(this.properties.getPort(), MongoProperties.DEFAULT_PORT);
        ServerAddress serverAddress = new ServerAddress(host, port);
        settings.applyToClusterSettings((cluster) -> cluster.hosts(Collections.singletonList(serverAddress)));
        return;
    }
    settings.applyConnectionString(new ConnectionString(this.properties.determineUri()));
}
Also used : ServerAddress(com.mongodb.ServerAddress) ConnectionString(com.mongodb.ConnectionString) ConnectionString(com.mongodb.ConnectionString)

Example 84 with ServerAddress

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

the class MongoPropertiesClientSettingsBuilderCustomizerTests method portCanBeCustomized.

@Test
void portCanBeCustomized() {
    this.properties.setPort(12345);
    MongoClientSettings settings = customizeSettings();
    List<ServerAddress> allAddresses = getAllAddresses(settings);
    assertThat(allAddresses).hasSize(1);
    assertServerAddress(allAddresses.get(0), "localhost", 12345);
}
Also used : ServerAddress(com.mongodb.ServerAddress) MongoClientSettings(com.mongodb.MongoClientSettings) Test(org.junit.jupiter.api.Test)

Example 85 with ServerAddress

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

the class MongoPropertiesClientSettingsBuilderCustomizerTests method noCustomAddressAndNoUriUsesDefaultUri.

@Test
void noCustomAddressAndNoUriUsesDefaultUri() {
    MongoClientSettings settings = customizeSettings();
    List<ServerAddress> allAddresses = getAllAddresses(settings);
    assertThat(allAddresses).hasSize(1);
    assertServerAddress(allAddresses.get(0), "localhost", 27017);
}
Also used : ServerAddress(com.mongodb.ServerAddress) MongoClientSettings(com.mongodb.MongoClientSettings) Test(org.junit.jupiter.api.Test)

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