Java Code Examples for org.apache.tinkerpop.gremlin.driver.Cluster#connect()

The following examples show how to use org.apache.tinkerpop.gremlin.driver.Cluster#connect() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldWorkWithGraphSONV2Serialization() throws Exception {
    final Cluster cluster = TestClientFactory.build().serializer(Serializers.GRAPHSON_V2D0).create();
    final Client client = cluster.connect();

    final List<Result> r = client.submit("TinkerFactory.createModern().traversal().V(1)").all().join();
    assertEquals(1, r.size());

    final Vertex v = r.get(0).get(DetachedVertex.class);
    assertEquals(1, v.id());
    assertEquals("person", v.label());

    assertEquals(2, IteratorUtils.count(v.properties()));
    assertEquals("marko", v.value("name"));
    assertEquals(29, Integer.parseInt(v.value("age").toString()));

    cluster.close();
}
 
Example 2
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAliasGraphVariablesInSession() throws Exception {
    final Cluster cluster = TestClientFactory.build().serializer(Serializers.GRYO_V3D0).create();
    final Client client = cluster.connect(name.getMethodName());

    try {
        client.submit("g.addVertex('name','stephen');").all().get().get(0).getVertex();
        fail("Should have tossed an exception because \"g\" does not have the addVertex method under default config");
    } catch (Exception ex) {
        final Throwable root = ExceptionUtils.getRootCause(ex);
        assertThat(root, instanceOf(ResponseException.class));
        final ResponseException re = (ResponseException) root;
        assertEquals(ResponseStatusCode.SERVER_ERROR_EVALUATION, re.getResponseStatusCode());
    }

    final Client aliased = client.alias("graph");
    assertEquals("jason", aliased.submit("n='jason'").all().get().get(0).getString());
    final Vertex v = aliased.submit("g.addVertex('name',n)").all().get().get(0).getVertex();
    assertEquals("jason", v.value("name"));

    cluster.close();
}
 
Example 3
Source File: GremlinServerSslIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldEnableSslAndFailIfProtocolsDontMatch() {
    final Cluster cluster = TestClientFactory.build().enableSsl(true).keyStore(JKS_SERVER_KEY).keyStorePassword(KEY_PASS)
            .sslSkipCertValidation(true).sslEnabledProtocols(Arrays.asList("TLSv1.2")).create();
    final Client client = cluster.connect();

    try {
        client.submit("'test'").one();
        fail("Should throw exception because ssl client requires TLSv1.2 whereas server supports only TLSv1.1");
    } catch (Exception x) {
        final Throwable root = ExceptionUtils.getRootCause(x);
        assertThat(root, instanceOf(TimeoutException.class));
    } finally {
        cluster.close();
    }
}
 
Example 4
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldFailWithBadClientSideSerialization() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect();

    final ResultSet results = client.submit("java.awt.Color.RED");

    try {
        results.all().join();
        fail("Should have thrown exception over bad serialization");
    } catch (Exception ex) {
        final Throwable inner = ExceptionUtils.getRootCause(ex);
        assertThat(inner, instanceOf(ResponseException.class));
        assertEquals(ResponseStatusCode.SERVER_ERROR_SERIALIZATION, ((ResponseException) inner).getResponseStatusCode());
    }

    // should not die completely just because we had a bad serialization error.  that kind of stuff happens
    // from time to time, especially in the console if you're just exploring.
    assertEquals(2, client.submit("1+1").all().get().get(0).getInt());

    cluster.close();
}
 
Example 5
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAliasGraphVariables() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect();

    try {
        client.submit("g.addVertex(label,'person','name','stephen');").all().get().get(0).getVertex();
        fail("Should have tossed an exception because \"g\" does not have the addVertex method under default config");
    } catch (Exception ex) {
        final Throwable root = ExceptionUtils.getRootCause(ex);
        assertThat(root, instanceOf(ResponseException.class));
        final ResponseException re = (ResponseException) root;
        assertEquals(ResponseStatusCode.SERVER_ERROR_EVALUATION, re.getResponseStatusCode());
    }

    final Client rebound = cluster.connect().alias("graph");
    final Vertex v = rebound.submit("g.addVertex(label,'person','name','jason')").all().get().get(0).getVertex();
    assertEquals("person", v.label());

    cluster.close();
}
 
Example 6
Source File: GremlinServerSslIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldEnableSslWithSslContextProgrammaticallySpecified() throws Exception {
    // just for testing - this is not good for production use
    final SslContextBuilder builder = SslContextBuilder.forClient();
    builder.trustManager(InsecureTrustManagerFactory.INSTANCE);
    builder.sslProvider(SslProvider.JDK);

    final Cluster cluster = TestClientFactory.build().enableSsl(true).sslContext(builder.build()).create();
    final Client client = cluster.connect();

    try {
        // this should return "nothing" - there should be no exception
        assertEquals("test", client.submit("'test'").one().getString());
    } finally {
        cluster.close();
    }
}
 
Example 7
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldStream() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect();

    final ResultSet results = client.submit("[1,2,3,4,5,6,7,8,9]");
    final AtomicInteger counter = new AtomicInteger(0);
    results.stream().map(i -> i.get(Integer.class) * 2).forEach(i -> assertEquals(counter.incrementAndGet() * 2, Integer.parseInt(i.toString())));
    assertEquals(9, counter.get());
    assertThat(results.allItemsAvailable(), is(true));

    // cant stream it again
    assertThat(results.stream().iterator().hasNext(), is(false));

    cluster.close();
}
 
Example 8
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldFailWithBadServerSideSerialization() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect();

    final ResultSet results = client.submit("TinkerGraph.open().variables()");

    try {
        results.all().join();
        fail();
    } catch (Exception ex) {
        final Throwable inner = ExceptionUtils.getRootCause(ex);
        assertTrue(inner instanceof ResponseException);
        assertEquals(ResponseStatusCode.SERVER_ERROR_SERIALIZATION, ((ResponseException) inner).getResponseStatusCode());
    }

    // should not die completely just because we had a bad serialization error.  that kind of stuff happens
    // from time to time, especially in the console if you're just exploring.
    assertEquals(2, client.submit("1+1").all().get().get(0).getInt());

    cluster.close();
}
 
Example 9
Source File: GremlinServerAuthKrb5IntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAuthenticateWithQop() throws Exception {
    final String oldQop = System.getProperty("javax.security.sasl.qop", "");
    System.setProperty("javax.security.sasl.qop", "auth-conf");
    final Cluster cluster = TestClientFactory.build().jaasEntry(TESTCONSOLE)
            .protocol(kdcServer.serverPrincipalName).addContactPoint(kdcServer.hostname).create();
    final Client client = cluster.connect();
    try {
        assertEquals(2, client.submit("1+1").all().get().get(0).getInt());
        assertEquals(3, client.submit("1+2").all().get().get(0).getInt());
        assertEquals(4, client.submit("1+3").all().get().get(0).getInt());
    } finally {
        cluster.close();
        System.setProperty("javax.security.sasl.qop", oldQop);
    }
}
 
Example 10
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldIterate() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect();

    final ResultSet results = client.submit("[1,2,3,4,5,6,7,8,9]");
    final Iterator<Result> itty = results.iterator();
    final AtomicInteger counter = new AtomicInteger(0);
    while (itty.hasNext()) {
        counter.incrementAndGet();
        assertEquals(counter.get(), itty.next().getInt());
    }

    assertEquals(9, counter.get());
    assertThat(results.allItemsAvailable(), is(true));

    // can't stream it again
    assertThat(results.iterator().hasNext(), is(false));

    cluster.close();
}
 
Example 11
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldCloseSession() throws Exception {
    final Cluster cluster = TestClientFactory.build().create();
    final Client client = cluster.connect(name.getMethodName());

    final ResultSet results1 = client.submit("x = [1,2,3,4,5,6,7,8,9]");
    assertEquals(9, results1.all().get().size());
    final ResultSet results2 = client.submit("x[0]+1");
    assertEquals(2, results2.all().get().get(0).getInt());

    client.close();

    try {
        client.submit("x[0]+1").all().get();
        fail("Should have thrown an exception because the connection is closed");
    } catch (Exception ex) {
        final Throwable root = ExceptionUtils.getRootCause(ex);
        assertThat(root, instanceOf(IllegalStateException.class));
    } finally {
        cluster.close();
    }
}
 
Example 12
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReturnNiceMessageFromOpSelector() {
    final Cluster cluster = TestClientFactory.build().create();
    final Client client = cluster.connect();

    try {
        final Map m = new HashMap<>();
        m.put(null, "a null key will force a throw of OpProcessorException in message validation");
        client.submit("1+1", m).all().get();
        fail("Should throw an exception.");
    } catch (Exception re) {
        final Throwable root = ExceptionUtils.getRootCause(re);
        assertEquals("The [eval] message is using one or more invalid binding keys - they must be of type String and cannot be null", root.getMessage());
    } finally {
        cluster.close();
    }
}
 
Example 13
Source File: GremlinServerIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldUseSimpleSandbox() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect();

    assertEquals(2, client.submit("1+1").all().get().get(0).getInt());

    try {
        // this should return "nothing" - there should be no exception
        client.submit("java.lang.System.exit(0)").all().get();
        fail("The above should not have executed in any successful way as sandboxing is enabled");
    } catch (Exception ex) {
        assertThat(ex.getCause().getMessage(), containsString("[Static type checking] - Not authorized to call this method: java.lang.System#exit(int)"));
    } finally {
        cluster.close();
    }
}
 
Example 14
Source File: GremlinServerSslIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldEnableSslAndClientCertificateAuthWithDifferentStoreType() {
    final Cluster cluster = TestClientFactory.build().enableSsl(true)
            .keyStore(JKS_CLIENT_KEY).keyStorePassword(KEY_PASS).keyStoreType(KEYSTORE_TYPE_JKS)
            .trustStore(P12_CLIENT_TRUST).trustStorePassword(KEY_PASS).trustStoreType(TRUSTSTORE_TYPE_PKCS12)
            .create();
    final Client client = cluster.connect();

    try {
        assertEquals("test", client.submit("'test'").one().getString());
    } finally {
        cluster.close();
    }

    final Cluster cluster2 = TestClientFactory.build().enableSsl(true)
            .keyStore(P12_CLIENT_KEY).keyStorePassword(KEY_PASS).keyStoreType(KEYSTORE_TYPE_PKCS12)
            .trustStore(JKS_CLIENT_TRUST).trustStorePassword(KEY_PASS).trustStoreType(TRUSTSTORE_TYPE_JKS)
            .create();
    final Client client2 = cluster2.connect();

    try {
        assertEquals("test", client2.submit("'test'").one().getString());
    } finally {
        cluster2.close();
    }
}
 
Example 15
Source File: GremlinServerAuthIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAuthenticateAndWorkWithVariablesOverDefaultJsonSerialization() throws Exception {
    final Cluster cluster = TestClientFactory.build().serializer(Serializers.GRAPHSON)
            .credentials("stephen", "password").create();
    final Client client = cluster.connect(name.getMethodName());

    try {
        final Vertex vertex = (Vertex) client.submit("v=graph.addVertex(\"name\", \"stephen\")").all().get().get(0).getObject();
        assertEquals("stephen", vertex.value("name"));

        final Property vpName = (Property)client.submit("v.property('name')").all().get().get(0).getObject();
        assertEquals("stephen", vpName.value());
    } finally {
        cluster.close();
    }
}
 
Example 16
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotThrowNoSuchElementException() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect();

    try {
        // this should return "nothing" - there should be no exception
        assertNull(client.submit("g.V().has('name','kadfjaldjfla')").one());
    } finally {
        cluster.close();
    }
}
 
Example 17
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldBeThreadSafeToUseOneClient() throws Exception {
    final Cluster cluster = TestClientFactory.build().workerPoolSize(2)
            .maxInProcessPerConnection(64)
            .minInProcessPerConnection(32)
            .maxConnectionPoolSize(16)
            .minConnectionPoolSize(8).create();
    final Client client = cluster.connect();

    final Map<Integer, Integer> results = new ConcurrentHashMap<>();
    final List<Thread> threads = new ArrayList<>();
    for (int ix = 0; ix < 100; ix++) {
        final int otherNum = ix;
        final Thread t = new Thread(()->{
            try {
                results.put(otherNum, client.submit("1000+" + otherNum).all().get().get(0).getInt());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }, name.getMethodName() + "-" + ix);

        t.start();
        threads.add(t);
    }

    threads.forEach(FunctionUtils.wrapConsumer(Thread::join));

    for (int ix = 0; ix < results.size(); ix++) {
        assertThat(results.containsKey(ix), is(true));
        assertEquals(1000 + ix, results.get(ix).intValue());
    }

    cluster.close();
}
 
Example 18
Source File: GremlinServerAuditLogIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldAuditLogWithKrb5Authenticator() throws Exception {
    final Cluster cluster = TestClientFactory.build().jaasEntry(TESTCONSOLE)
            .protocol(kdcServer.serverPrincipalName).addContactPoint(kdcServer.hostname).create();
    final Client client = cluster.connect();
    try {
        assertEquals(2, client.submit("1+1").all().get().get(0).getInt());
        assertEquals(3, client.submit("1+2").all().get().get(0).getInt());
        assertEquals(4, client.submit("1+3").all().get().get(0).getInt());
    } finally {
        cluster.close();
    }

    // wait for logger to flush - (don't think there is a way to detect this)
    stopServer();
    Thread.sleep(1000);

    final List<LoggingEvent> log = recordingAppender.getEvents();
    final Stream<LoggingEvent> auditEvents = log.stream().filter(event -> event.getLoggerName().equals(AUDIT_LOGGER_NAME));
    final LoggingEvent authEvent = auditEvents
            .filter(event -> event.getMessage().toString().contains("Krb5Authenticator")).iterator().next();
    final String authMsg = authEvent.getMessage().toString();
    assertTrue(authEvent.getLevel() == INFO &&
            authMsg.matches(String.format("User %s with address .*? authenticated by Krb5Authenticator", kdcServer.clientPrincipalName)));
    assertTrue(log.stream().anyMatch(item -> item.getLevel() == INFO &&
            item.getMessage().toString().matches("User with address .*? requested: 1\\+1")));
    assertTrue(log.stream().anyMatch(item -> item.getLevel() == INFO &&
            item.getMessage().toString().matches("User with address .*? requested: 1\\+2")));
    assertTrue(log.stream().anyMatch(item -> item.getLevel() == INFO &&
            item.getMessage().toString().matches("User with address .*? requested: 1\\+3")));
}
 
Example 19
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldFailWithScriptExecutionException() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect();

    final ResultSet results = client.submit("1/0");

    try {
        results.all().join();
        fail("Should have thrown exception over bad serialization");
    } catch (Exception ex) {
        final Throwable inner = ExceptionUtils.getRootCause(ex);
        assertTrue(inner instanceof ResponseException);
        assertThat(inner.getMessage(), endsWith("Division by zero"));

        final ResponseException rex = (ResponseException) inner;
        assertEquals("java.lang.ArithmeticException", rex.getRemoteExceptionHierarchy().get().get(0));
        assertEquals(1, rex.getRemoteExceptionHierarchy().get().size());
        assertThat(rex.getRemoteStackTrace().get(), startsWith("java.lang.ArithmeticException: Division by zero\n\tat java.math.BigDecimal.divide(BigDecimal.java"));
    }

    // should not die completely just because we had a bad serialization error.  that kind of stuff happens
    // from time to time, especially in the console if you're just exploring.
    assertEquals(2, client.submit("1+1").all().get().get(0).getInt());

    cluster.close();
}
 
Example 20
Source File: GremlinServerAuthKrb5IntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldAuthenticateWithDefaults() throws Exception {
    final Cluster cluster = TestClientFactory.build().jaasEntry(TESTCONSOLE)
            .protocol(kdcServer.serverPrincipalName).addContactPoint(kdcServer.hostname).create();
    final Client client = cluster.connect();
    assertConnection(cluster, client);
}