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

The following examples show how to use org.apache.tinkerpop.gremlin.driver.Cluster#close() . 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: GremlinServerSslIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldEnableSslAndClientCertificateAuthAndFailWithoutCert() {
    final Cluster cluster = TestClientFactory.build().enableSsl(true).keyStore(JKS_SERVER_KEY).keyStorePassword(KEY_PASS)
            .keyStoreType(KEYSTORE_TYPE_JKS).sslSkipCertValidation(true).create();
    final Client client = cluster.connect();

    try {
        client.submit("'test'").one();
        fail("Should throw exception because ssl client auth is enabled on the server but client does not have a cert");
    } catch (Exception x) {
        final Throwable root = ExceptionUtils.getRootCause(x);
        assertThat(root, instanceOf(TimeoutException.class));
    } finally {
        cluster.close();
    }
}
 
Example 2
Source File: GremlinServerIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReceiveFailureOnBadGryoSerialization() throws Exception {
    final Cluster cluster = TestClientFactory.build().serializer(Serializers.GRYO_V1D0).create();
    final Client client = cluster.connect();

    try {
        client.submit("java.awt.Color.RED").all().join();
        fail("Should throw an exception.");
    } catch (RuntimeException re) {
        final Throwable root = ExceptionUtils.getRootCause(re);
        assertThat(root.getMessage(), CoreMatchers.startsWith("Error during serialization: Class is not registered: java.awt.Color"));

        // validate that we can still send messages to the server
        assertEquals(2, client.submit("1+1").all().join().get(0).getInt());
    } finally {
        cluster.close();
    }
}
 
Example 3
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldExecuteScriptInSession() 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());

    final ResultSet results3 = client.submit("x[1]+2");
    assertEquals(4, results3.all().get().get(0).getInt());

    cluster.close();
}
 
Example 4
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 5
Source File: GremlinServerAuditLogIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAuditLogWithAllowAllAuthenticator() throws Exception {

    final Cluster cluster = TestClientFactory.build().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);

    // WebSocketChannelizer does not add SaslAuthenticationHandler for AllowAllAuthenticator,
    // so no authenticated user log line available
    assertTrue(recordingAppender.logMatchesAny(AUDIT_LOGGER_NAME, INFO, "User with address .*? requested: 1\\+1"));
    assertTrue(recordingAppender.logMatchesAny(AUDIT_LOGGER_NAME, INFO, "User with address .*? requested: 1\\+2"));
    assertTrue(recordingAppender.logMatchesAny(AUDIT_LOGGER_NAME, INFO, "User with address .*? requested: 1\\+3"));
}
 
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: GremlinServerIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldUseInterpreterMode() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect(name.getMethodName());

    client.submit("def subtractAway(x,y){x-y};[]").all().get();
    client.submit("multiplyIt = { x,y -> x * y};[]").all().get();

    assertEquals(2, client.submit("x = 1 + 1").all().get().get(0).getInt());
    assertEquals(3, client.submit("int y = x + 1").all().get().get(0).getInt());
    assertEquals(5, client.submit("def z = x + y").all().get().get(0).getInt());

    final Map<String,Object> m = new HashMap<>();
    m.put("x", 10);
    assertEquals(-5, client.submit("z - x", m).all().get().get(0).getInt());
    assertEquals(15, client.submit("addItUp(x,z)", m).all().get().get(0).getInt());
    assertEquals(5, client.submit("subtractAway(x,z)", m).all().get().get(0).getInt());
    assertEquals(50, client.submit("multiplyIt(x,z)", m).all().get().get(0).getInt());

    cluster.close();
}
 
Example 8
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldExecuteScriptInSessionOnTransactionalGraph() throws Exception {
    assumeNeo4jIsPresent();

    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect(name.getMethodName());

    final Vertex vertexBeforeTx = client.submit("v=g.addV(\"person\").property(\"name\",\"stephen\").next()").all().get().get(0).getVertex();
    assertEquals("person", vertexBeforeTx.label());

    final String nameValueFromV = client.submit("g.V().values('name').next()").all().get().get(0).getString();
    assertEquals("stephen", nameValueFromV);

    final Vertex vertexFromBinding = client.submit("v").all().get().get(0).getVertex();
    assertEquals("person", vertexFromBinding.label());

    final Map<String,Object> vertexAfterTx = client.submit("g.V(v).property(\"color\",\"blue\").iterate(); g.tx().commit(); g.V(v).valueMap().by(unfold())").all().get().get(0).get(Map.class);
    assertEquals("stephen", vertexAfterTx.get("name"));
    assertEquals("blue", vertexAfterTx.get("color"));

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

    try {
        client.submit("1+1").all().get();
        fail("This should not succeed as the client did not provide credentials");
    } catch(Exception ex) {
        final Throwable root = ExceptionUtils.getRootCause(ex);

        // depending on the configuration of the system environment you might get either of these
        assertThat(root, anyOf(instanceOf(GSSException.class), instanceOf(ResponseException.class)));
    } finally {
        cluster.close();
    }
}
 
Example 10
Source File: GremlinServerSslIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldEnableSslAndClientCertificateAuthAndFailWithIncorrectKeyStoreType() {
    final Cluster cluster = TestClientFactory.build().enableSsl(true)
            .keyStore(JKS_CLIENT_KEY).keyStorePassword(KEY_PASS).keyStoreType(KEYSTORE_TYPE_PKCS12)
            .trustStore(P12_CLIENT_TRUST).trustStorePassword(KEY_PASS).trustStoreType(TRUSTSTORE_TYPE_PKCS12)
            .create();
    final Client client = cluster.connect();

    try {
        String res = client.submit("'test'").one().getString();
        fail("Should throw exception because incorrect keyStoreType is specified");
    } catch (Exception x) {
        final Throwable root = ExceptionUtils.getRootCause(x);
        assertThat(root, instanceOf(TimeoutException.class));
    } finally {
        cluster.close();
    }
}
 
Example 11
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 12
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDeserializeWithCustomClassesV1() throws Exception {
    final Map<String, Object> m = new HashMap<>();
    m.put("custom", Collections.singletonList(String.format("%s;%s", JsonBuilder.class.getCanonicalName(), JsonBuilderGryoSerializer.class.getCanonicalName())));
    final GryoMessageSerializerV1d0 serializer = new GryoMessageSerializerV1d0();
    serializer.configure(m, null);

    final Cluster cluster = TestClientFactory.build().serializer(serializer).create();
    final Client client = cluster.connect();

    final List<Result> json = client.submit("b = new groovy.json.JsonBuilder();b.people{person {fname 'stephen'\nlname 'mallette'}};b").all().join();
    assertEquals("{\"people\":{\"person\":{\"fname\":\"stephen\",\"lname\":\"mallette\"}}}", json.get(0).getString());
    cluster.close();
}
 
Example 13
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldProcessTraversalInterruption() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect();

    try {
        client.submit("g.inject(1).sideEffect{Thread.sleep(5000)}").all().get();
        fail("Should have timed out");
    } catch (Exception ex) {
        final ResponseException re = (ResponseException) ex.getCause();
        assertEquals(ResponseStatusCode.SERVER_ERROR_TIMEOUT, re.getResponseStatusCode());
    }

    cluster.close();
}
 
Example 14
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldExecuteScriptInSessionOnTransactionalWithManualTransactionsGraph() throws Exception {
    assumeNeo4jIsPresent();

    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect(name.getMethodName());
    final Client sessionlessClient = cluster.connect();
    client.submit("graph.tx().onReadWrite(Transaction.READ_WRITE_BEHAVIOR.MANUAL);null").all().get();
    client.submit("graph.tx().open()").all().get();

    final Vertex vertexBeforeTx = client.submit("v=g.addV(\"person\").property(\"name\", \"stephen\").next()").all().get().get(0).getVertex();
    assertEquals("person", vertexBeforeTx.label());

    final String nameValueFromV = client.submit("g.V().values(\"name\").next()").all().get().get(0).getString();
    assertEquals("stephen", nameValueFromV);

    final Vertex vertexFromBinding = client.submit("v").all().get().get(0).getVertex();
    assertEquals("person", vertexFromBinding.label());

    client.submit("g.V(v).property(\"color\",\"blue\")").all().get();
    client.submit("g.tx().commit()").all().get();

    // Run a sessionless request to change transaction.readWriteConsumer back to AUTO
    // The will make the next in session request fail if consumers aren't ThreadLocal
    sessionlessClient.submit("g.V().next()").all().get();

    client.submit("g.tx().open()").all().get();

    final Map<String,Object> vertexAfterTx = client.submit("g.V().valueMap().by(unfold())").all().get().get(0).get(Map.class);
    assertEquals("stephen", vertexAfterTx.get("name"));
    assertEquals("blue", vertexAfterTx.get("color"));

    client.submit("g.tx().rollback()").all().get();

    cluster.close();
}
 
Example 15
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetOneThenSomeThenSomeMore() 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 Result one = results.one();
    final CompletableFuture<List<Result>> batch1 = results.some(4);
    final CompletableFuture<List<Result>> batch2 = results.some(5);
    final CompletableFuture<List<Result>> batchNothingLeft = results.some(5);

    assertEquals(1, one.getInt());

    assertEquals(4, batch1.get().size());
    assertEquals(2, batch1.get().get(0).getInt());
    assertEquals(3, batch1.get().get(1).getInt());
    assertEquals(4, batch1.get().get(2).getInt());
    assertEquals(5, batch1.get().get(3).getInt());

    assertEquals(4, batch2.get().size());
    assertEquals(6, batch2.get().get(0).getInt());
    assertEquals(7, batch2.get().get(1).getInt());
    assertEquals(8, batch2.get().get(2).getInt());
    assertEquals(9, batch2.get().get(3).getInt());

    assertEquals(0, batchNothingLeft.get().size());

    cluster.close();
}
 
Example 16
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEventuallySucceedOnSameServerWithDefault() throws Exception {
    stopServer();

    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect();

    try {
        client.submit("1+1").all().join().get(0).getInt();
        fail("Should not have gone through because the server is not running");
    } catch (Exception i) {
        final Throwable root = ExceptionUtils.getRootCause(i);
        assertThat(root, instanceOf(TimeoutException.class));
    }

    startServer();

    // default reconnect time is 1 second so wait some extra time to be sure it has time to try to bring it
    // back to life. usually this passes on the first attempt, but docker is sometimes slow and we get failures
    // waiting for Gremlin Server to pop back up
    for (int ix = 3; ix < 13; ix++) {
        TimeUnit.SECONDS.sleep(ix);
        try {
            final int result = client.submit("1+1").all().join().get(0).getInt();
            assertEquals(2, result);
            break;
        } catch (Exception ignored) {
            logger.warn("Attempt {} failed on shouldEventuallySucceedOnSameServerWithDefault", ix);
        }
    }

    cluster.close();
}
 
Example 17
Source File: GremlinServerSslIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEnableSsl() {
    final Cluster cluster = TestClientFactory.build().enableSsl(true).keyStore(JKS_SERVER_KEY).keyStorePassword(KEY_PASS).sslSkipCertValidation(true).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 18
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSerializeToStringWhenRequestedGraphBinaryV1() throws Exception {
    final Map<String, Object> m = new HashMap<>();
    m.put("serializeResultToString", true);
    final GraphBinaryMessageSerializerV1 serializer = new GraphBinaryMessageSerializerV1();
    serializer.configure(m, null);

    final Cluster cluster = TestClientFactory.build().serializer(serializer).create();
    final Client client = cluster.connect();

    final ResultSet resultSet = client.submit("TinkerFactory.createClassic()");
    final List<Result> results = resultSet.all().join();
    assertEquals(1, results.size());
    assertEquals("tinkergraph[vertices:6 edges:6]", results.get(0).getString());

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

    stopServer();

    cluster.close();
}
 
Example 20
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetSomeThenSomeMore() 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 CompletableFuture<List<Result>> batch1 = results.some(5);
    final CompletableFuture<List<Result>> batch2 = results.some(5);
    final CompletableFuture<List<Result>> batchNothingLeft = results.some(5);

    assertEquals(5, batch1.get().size());
    assertEquals(1, batch1.get().get(0).getInt());
    assertEquals(2, batch1.get().get(1).getInt());
    assertEquals(3, batch1.get().get(2).getInt());
    assertEquals(4, batch1.get().get(3).getInt());
    assertEquals(5, batch1.get().get(4).getInt());

    assertEquals(4, batch2.get().size());
    assertEquals(6, batch2.get().get(0).getInt());
    assertEquals(7, batch2.get().get(1).getInt());
    assertEquals(8, batch2.get().get(2).getInt());
    assertEquals(9, batch2.get().get(3).getInt());

    assertEquals(0, batchNothingLeft.get().size());

    cluster.close();
}