Search in sources :

Example 1 with ConnectionString

use of com.couchbase.client.core.util.ConnectionString in project couchbase-jvm-clients by couchbase.

the class AsyncCluster method extractClusterEnvironment.

/**
 * Helper method to extract the cluster environment from the connection string and options.
 *
 * @param connectionString the connection string which is used to populate settings into it.
 * @param opts the cluster options.
 * @return the cluster environment, created if not passed in or the one supplied from the user.
 */
static Supplier<ClusterEnvironment> extractClusterEnvironment(final String connectionString, final ClusterOptions.Built opts) {
    ConnectionString connStr = ConnectionString.create(connectionString);
    Supplier<ClusterEnvironment> envSupplier;
    if (opts.environment() == null) {
        ClusterEnvironment.Builder builder = ClusterEnvironment.builder();
        if (opts.environmentCustomizer() != null) {
            opts.environmentCustomizer().accept(builder);
        }
        builder.load(new ConnectionStringPropertyLoader(connStr));
        envSupplier = new OwnedSupplier<>(builder.build());
    } else {
        envSupplier = opts::environment;
    }
    boolean ownsEnvironment = envSupplier instanceof OwnedSupplier;
    checkConnectionString(envSupplier.get(), ownsEnvironment, connStr);
    return envSupplier;
}
Also used : OwnedSupplier(com.couchbase.client.core.env.OwnedSupplier) ClusterEnvironment(com.couchbase.client.java.env.ClusterEnvironment) ConnectionStringPropertyLoader(com.couchbase.client.core.env.ConnectionStringPropertyLoader) ConnectionStringUtil.asConnectionString(com.couchbase.client.core.util.ConnectionStringUtil.asConnectionString) ConnectionStringUtil.checkConnectionString(com.couchbase.client.core.util.ConnectionStringUtil.checkConnectionString) ConnectionString(com.couchbase.client.core.util.ConnectionString)

Example 2 with ConnectionString

use of com.couchbase.client.core.util.ConnectionString in project couchbase-jvm-clients by couchbase.

the class ConnectionStringTest method shouldAcceptIPv6WithPortType.

@Test
void shouldAcceptIPv6WithPortType() {
    ConnectionString parsed = ConnectionString.create("couchbases://[::1]:8091=http,[::1]:11210=kv");
    assertEquals(ConnectionString.Scheme.COUCHBASES, parsed.scheme());
    assertEquals(2, parsed.hosts().size());
    assertEquals("::1", parsed.hosts().get(0).hostname());
    assertEquals(8091, parsed.hosts().get(0).port());
    assertEquals(ConnectionString.PortType.MANAGER, parsed.hosts().get(0).portType().get());
    assertEquals("::1", parsed.hosts().get(1).hostname());
    assertEquals(11210, parsed.hosts().get(1).port());
    assertEquals(ConnectionString.PortType.KV, parsed.hosts().get(1).portType().get());
    assertTrue(parsed.params().isEmpty());
}
Also used : ConnectionStringUtil.asConnectionString(com.couchbase.client.core.util.ConnectionStringUtil.asConnectionString) ConnectionString(com.couchbase.client.core.util.ConnectionString) Test(org.junit.jupiter.api.Test)

Example 3 with ConnectionString

use of com.couchbase.client.core.util.ConnectionString in project couchbase-jvm-clients by couchbase.

the class ConnectionStringTest method shouldParseUsername.

@Test
void shouldParseUsername() {
    ConnectionString parsed = ConnectionString.create("couchbase://user@localhost?foo=bar");
    assertEquals(ConnectionString.Scheme.COUCHBASE, parsed.scheme());
    assertEquals("user", parsed.username());
    assertEquals("localhost", parsed.hosts().get(0).hostname());
    assertEquals(0, parsed.hosts().get(0).port());
    assertEquals(1, parsed.params().size());
    assertEquals("bar", parsed.params().get("foo"));
    parsed = ConnectionString.create("couchbase://user123@host1,host2?foo=bar&setting=true");
    assertEquals(ConnectionString.Scheme.COUCHBASE, parsed.scheme());
    assertEquals("user123", parsed.username());
    assertEquals("host1", parsed.hosts().get(0).hostname());
    assertEquals("host2", parsed.hosts().get(1).hostname());
    assertEquals(2, parsed.params().size());
    assertEquals("bar", parsed.params().get("foo"));
    assertEquals("true", parsed.params().get("setting"));
}
Also used : ConnectionStringUtil.asConnectionString(com.couchbase.client.core.util.ConnectionStringUtil.asConnectionString) ConnectionString(com.couchbase.client.core.util.ConnectionString) Test(org.junit.jupiter.api.Test)

Example 4 with ConnectionString

use of com.couchbase.client.core.util.ConnectionString in project couchbase-jvm-clients by couchbase.

the class ConnectionStringTest method shouldParseHostList.

@Test
void shouldParseHostList() {
    ConnectionString parsed = ConnectionString.create("couchbase://localhost");
    assertEquals(ConnectionString.Scheme.COUCHBASE, parsed.scheme());
    assertTrue(parsed.params().isEmpty());
    assertEquals(1, parsed.hosts().size());
    assertEquals("localhost", parsed.hosts().get(0).hostname());
    assertEquals(0, parsed.hosts().get(0).port());
    assertNull(parsed.username());
    parsed = ConnectionString.create("couchbase://localhost:1234");
    assertEquals(ConnectionString.Scheme.COUCHBASE, parsed.scheme());
    assertTrue(parsed.params().isEmpty());
    assertEquals(1, parsed.hosts().size());
    assertEquals("localhost", parsed.hosts().get(0).hostname());
    assertEquals(1234, parsed.hosts().get(0).port());
    assertNull(parsed.username());
    parsed = ConnectionString.create("couchbase://foo:1234,bar:5678");
    assertEquals(ConnectionString.Scheme.COUCHBASE, parsed.scheme());
    assertTrue(parsed.params().isEmpty());
    assertEquals(2, parsed.hosts().size());
    assertEquals("foo", parsed.hosts().get(0).hostname());
    assertEquals(1234, parsed.hosts().get(0).port());
    assertEquals("bar", parsed.hosts().get(1).hostname());
    assertEquals(5678, parsed.hosts().get(1).port());
    assertNull(parsed.username());
    parsed = ConnectionString.create("couchbase://foo,bar:5678,baz");
    assertEquals(ConnectionString.Scheme.COUCHBASE, parsed.scheme());
    assertTrue(parsed.params().isEmpty());
    assertEquals(3, parsed.hosts().size());
    assertEquals("foo", parsed.hosts().get(0).hostname());
    assertEquals(0, parsed.hosts().get(0).port());
    assertEquals("bar", parsed.hosts().get(1).hostname());
    assertEquals(5678, parsed.hosts().get(1).port());
    assertEquals("baz", parsed.hosts().get(2).hostname());
    assertEquals(0, parsed.hosts().get(2).port());
    assertNull(parsed.username());
}
Also used : ConnectionStringUtil.asConnectionString(com.couchbase.client.core.util.ConnectionStringUtil.asConnectionString) ConnectionString(com.couchbase.client.core.util.ConnectionString) Test(org.junit.jupiter.api.Test)

Example 5 with ConnectionString

use of com.couchbase.client.core.util.ConnectionString in project couchbase-jvm-clients by couchbase.

the class ConnectionStringTest method shouldParseValidSchemes.

@Test
void shouldParseValidSchemes() {
    ConnectionString parsed = ConnectionString.create("couchbase://");
    assertEquals(ConnectionString.Scheme.COUCHBASE, parsed.scheme());
    assertTrue(parsed.hosts().isEmpty());
    assertTrue(parsed.params().isEmpty());
    parsed = ConnectionString.create("couchbases://");
    assertEquals(ConnectionString.Scheme.COUCHBASES, parsed.scheme());
    assertTrue(parsed.hosts().isEmpty());
    assertTrue(parsed.params().isEmpty());
}
Also used : ConnectionStringUtil.asConnectionString(com.couchbase.client.core.util.ConnectionStringUtil.asConnectionString) ConnectionString(com.couchbase.client.core.util.ConnectionString) Test(org.junit.jupiter.api.Test)

Aggregations

ConnectionString (com.couchbase.client.core.util.ConnectionString)14 ConnectionStringUtil.asConnectionString (com.couchbase.client.core.util.ConnectionStringUtil.asConnectionString)12 Test (org.junit.jupiter.api.Test)11 Authenticator (com.couchbase.client.core.env.Authenticator)1 CertificateAuthenticator (com.couchbase.client.core.env.CertificateAuthenticator)1 ConnectionStringPropertyLoader (com.couchbase.client.core.env.ConnectionStringPropertyLoader)1 OwnedSupplier (com.couchbase.client.core.env.OwnedSupplier)1 PasswordAuthenticator (com.couchbase.client.core.env.PasswordAuthenticator)1 ConnectionStringUtil.checkConnectionString (com.couchbase.client.core.util.ConnectionStringUtil.checkConnectionString)1 ClusterEnvironment (com.couchbase.client.java.env.ClusterEnvironment)1 Duration (java.time.Duration)1