org.apache.tinkerpop.gremlin.driver.Client Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.driver.Client. 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 shouldEnableSslAndFailIfCiphersDontMatch() {
    final Cluster cluster = TestClientFactory.build().enableSsl(true).keyStore(JKS_SERVER_KEY).keyStorePassword(KEY_PASS)
            .sslSkipCertValidation(true).sslCipherSuites(Arrays.asList("SSL_RSA_WITH_RC4_128_SHA")).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 #2
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReportErrorWhenRequestCantBeSerialized() throws Exception {
    final Cluster cluster = TestClientFactory.build().serializer(Serializers.GRAPHSON_V3D0).create();
    final Client client = cluster.connect().alias("g");

    try {
        final Map<String,Object> params = new HashMap<>();
        params.put("r", Color.RED);
        client.submit("r", params).all().get();
        fail("Should have thrown exception over bad serialization");
    } catch (Exception ex) {
        final Throwable inner = ExceptionUtils.getRootCause(ex);
        assertThat(inner, instanceOf(ResponseException.class));
        assertEquals(ResponseStatusCode.REQUEST_ERROR_SERIALIZATION, ((ResponseException) inner).getResponseStatusCode());
        assertTrue(ex.getMessage().contains("An error occurred during serialization of this request"));
    }

    // 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 #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: DriverRemoteConnection.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
public DriverRemoteConnection(final Configuration conf) {
    final boolean hasClusterConf = IteratorUtils.anyMatch(conf.getKeys(), k -> k.startsWith("clusterConfiguration"));
    if (conf.containsKey(GREMLIN_REMOTE_DRIVER_CLUSTERFILE) && hasClusterConf)
        throw new IllegalStateException(String.format("A configuration should not contain both '%s' and 'clusterConfiguration'", GREMLIN_REMOTE_DRIVER_CLUSTERFILE));

    remoteTraversalSourceName = conf.getString(GREMLIN_REMOTE_DRIVER_SOURCENAME, DEFAULT_TRAVERSAL_SOURCE);

    try {
        final Cluster cluster;
        if (!conf.containsKey(GREMLIN_REMOTE_DRIVER_CLUSTERFILE) && !hasClusterConf)
            cluster = Cluster.open();
        else
            cluster = conf.containsKey(GREMLIN_REMOTE_DRIVER_CLUSTERFILE) ?
                    Cluster.open(conf.getString(GREMLIN_REMOTE_DRIVER_CLUSTERFILE)) : Cluster.open(conf.subset("clusterConfiguration"));

        client = cluster.connect(Client.Settings.build().create()).alias(remoteTraversalSourceName);
    } catch (Exception ex) {
        throw new IllegalStateException(ex);
    }

    attachElements = false;

    tryCloseCluster = true;
    tryCloseClient = true;
    this.conf = Optional.of(conf);
}
 
Example #5
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 #6
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldProcessRequestsOutOfOrder() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect();

    final ResultSet rsFive = client.submit("Thread.sleep(5000);'five'");
    final ResultSet rsZero = client.submit("'zero'");

    final CompletableFuture<List<Result>> futureFive = rsFive.all();
    final CompletableFuture<List<Result>> futureZero = rsZero.all();

    final long start = System.nanoTime();
    assertFalse(futureFive.isDone());
    assertEquals("zero", futureZero.get().get(0).getString());

    logger.info("Eval of 'zero' complete: " + TimeUtil.millisSince(start));

    assertFalse(futureFive.isDone());
    assertEquals("five", futureFive.get(10, TimeUnit.SECONDS).get(0).getString());

    logger.info("Eval of 'five' complete: " + TimeUtil.millisSince(start));
}
 
Example #7
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldWaitForAllResultsToArrive() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect();

    final AtomicInteger checked = new AtomicInteger(0);
    final ResultSet results = client.submit("[1,2,3,4,5,6,7,8,9]");
    while (!results.allItemsAvailable()) {
        assertTrue(results.getAvailableItemCount() < 10);
        checked.incrementAndGet();
        Thread.sleep(100);
    }

    assertTrue(checked.get() > 0);
    assertEquals(9, results.getAvailableItemCount());
    cluster.close();
}
 
Example #8
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 #9
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 #10
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 #11
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSerializeToStringWhenRequestedGryoV3() throws Exception {
    final Map<String, Object> m = new HashMap<>();
    m.put("serializeResultToString", true);
    final GryoMessageSerializerV3d0 serializer = new GryoMessageSerializerV3d0();
    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 #12
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 #13
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 #14
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldFailClientSideWithTooLargeAResponse() {
    final Cluster cluster = TestClientFactory.build().maxContentLength(1).create();
    final Client client = cluster.connect();

    try {
        final String fatty = IntStream.range(0, 100).mapToObj(String::valueOf).collect(Collectors.joining());
        client.submit("'" + fatty + "'").all().get();
        fail("Should throw an exception.");
    } catch (Exception re) {
        final Throwable root = ExceptionUtils.getRootCause(re);
        assertTrue(root.getMessage().equals("Max frame length of 1 has been exceeded."));
    } finally {
        cluster.close();
    }
}
 
Example #15
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 #16
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSerializeToStringWhenRequestedGryoV1() throws Exception {
    final Map<String, Object> m = new HashMap<>();
    m.put("serializeResultToString", true);
    final GryoMessageSerializerV1d0 serializer = new GryoMessageSerializerV1d0();
    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 #17
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldExecuteInSessionAndSessionlessWithoutOpeningTransaction() throws Exception {
    assumeNeo4jIsPresent();

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

    //open transaction in session, then add vertex and commit
    sessionClient.submit("g.tx().open()").all().get();
    final Vertex vertexBeforeTx = sessionClient.submit("v=g.addV(\"person\").property(\"name\",\"stephen\").next()").all().get().get(0).getVertex();
    assertEquals("person", vertexBeforeTx.label());
    sessionClient.submit("g.tx().commit()").all().get();

    // check that session transaction is closed
    final boolean isOpen = sessionClient.submit("g.tx().isOpen()").all().get().get(0).getBoolean();
    assertTrue("Transaction should be closed", !isOpen);

    //run a sessionless read
    sessionlessClient.submit("g.V()").all().get();

    // check that session transaction is still closed
    final boolean isOpenAfterSessionless = sessionClient.submit("g.tx().isOpen()").all().get().get(0).getBoolean();
    assertTrue("Transaction should stil be closed", !isOpenAfterSessionless);

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

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

    // this line is important because it tests GraphTraversal which has a certain transactional path
    final Vertex vertexRequest1 = client.submit("g.addV().property(\"name\",\"stephen\")").all().get().get(0).getVertex();

    final Vertex vertexRequest2 = client.submit("graph.vertices().next()").all().get().get(0).getVertex();
    assertEquals(vertexRequest1.id(), vertexRequest2.id());

    // this line is important because it tests the other transactional path
    client.submit("graph.addVertex(\"name\",\"marko\")").all().get().get(0).getVertex();

    assertEquals(2, client.submit("g.V().count()").all().get().get(0).getLong());

    cluster.close();
}
 
Example #19
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldExecuteScriptsInMultipleSession() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client1 = cluster.connect(name.getMethodName() + "1");
    final Client client2 = cluster.connect(name.getMethodName() + "2");
    final Client client3 = cluster.connect(name.getMethodName() + "3");

    final ResultSet results11 = client1.submit("x = 1");
    final ResultSet results21 = client2.submit("x = 2");
    final ResultSet results31 = client3.submit("x = 3");
    assertEquals(1, results11.all().get().get(0).getInt());
    assertEquals(2, results21.all().get().get(0).getInt());
    assertEquals(3, results31.all().get().get(0).getInt());

    final ResultSet results12 = client1.submit("x + 100");
    final ResultSet results22 = client2.submit("x * 2");
    final ResultSet results32 = client3.submit("x * 10");
    assertEquals(101, results12.all().get().get(0).getInt());
    assertEquals(4, results22.all().get().get(0).getInt());
    assertEquals(30, results32.all().get().get(0).getInt());

    cluster.close();
}
 
Example #20
Source File: GremlinServerIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldProvideBetterExceptionForMethodCodeTooLarge() {
    final int numberOfParameters = 4000;
    final Map<String,Object> b = new HashMap<>();

    // generate a script with a ton of bindings usage to generate a "code too large" exception
    String script = "x = 0";
    for (int ix = 0; ix < numberOfParameters; ix++) {
        if (ix > 0 && ix % 100 == 0) {
            script = script + ";" + System.lineSeparator() + "x = x";
        }
        script = script + " + x" + ix;
        b.put("x" + ix, ix);
    }

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

    try {
        client.submit(script, b).all().get();
        fail("Should have tanked out because of number of parameters used and size of the compile script");
    } catch (Exception ex) {
        assertThat(ex.getMessage(), containsString("The Gremlin statement that was submitted exceeds the maximum compilation size allowed by the JVM"));
    }
}
 
Example #21
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAliasGraphVariablesInStrictTransactionMode() throws Exception {
    assumeNeo4jIsPresent();

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

    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.REQUEST_ERROR_INVALID_REQUEST_ARGUMENTS, re.getResponseStatusCode());
    }

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

    cluster.close();
}
 
Example #22
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 #23
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 #24
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 #25
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAliasTraversalSourceVariablesInSession() throws Exception {
    final Cluster cluster = TestClientFactory.build().serializer(Serializers.GRYO_V3D0).create();
    final Client client = cluster.connect(name.getMethodName());

    try {
        client.submit("g.addV().property('name','stephen')").all().get().get(0).getVertex();
        fail("Should have tossed an exception because \"g\" is readonly in this context");
    } 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 clientAliased = client.alias("g1");
    assertEquals("jason", clientAliased.submit("n='jason'").all().get().get(0).getString());
    final Vertex v = clientAliased.submit("g.addV().property('name',n)").all().get().get(0).getVertex();
    assertEquals("jason", v.value("name"));

    cluster.close();
}
 
Example #26
Source File: GremlinServerSslIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldEnableSslAndClientCertificateAuthAndFailWithoutTrustedClientCert() {
    final Cluster cluster = TestClientFactory.build().enableSsl(true).keyStore(JKS_CLIENT_KEY).keyStorePassword(KEY_PASS)
            .keyStoreType(KEYSTORE_TYPE_JKS).trustStore(JKS_CLIENT_TRUST).trustStorePassword(KEY_PASS).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 does not trust client's cert");
    } catch (Exception x) {
        final Throwable root = ExceptionUtils.getRootCause(x);
        assertThat(root, instanceOf(TimeoutException.class));
    } finally {
        cluster.close();
    }
}
 
Example #27
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSendUserAgentBytecode() {
    final Cluster cluster = TestClientFactory.build().serializer(Serializers.GRAPHSON_V3D0).create();
    final Client client = Mockito.spy(cluster.connect().alias("g"));
    Mockito.when(client.alias("g")).thenReturn(client);
    GraphTraversalSource g = AnonymousTraversalSource.traversal().withRemote(DriverRemoteConnection.using(client));
    g.with(Tokens.ARGS_USER_AGENT, "test").V().iterate();
    cluster.close();
    ArgumentCaptor<RequestOptions> requestOptionsCaptor = ArgumentCaptor.forClass(RequestOptions.class);
    verify(client).submitAsync(Mockito.any(Bytecode.class), requestOptionsCaptor.capture());
    RequestOptions requestOptions = requestOptionsCaptor.getValue();
    assertEquals("test", requestOptions.getUserAgent().get());

    ArgumentCaptor<RequestMessage> requestMessageCaptor = ArgumentCaptor.forClass(RequestMessage.class);
    verify(client).submitAsync(requestMessageCaptor.capture());
    RequestMessage requestMessage = requestMessageCaptor.getValue();
    assertEquals("test", requestMessage.getArgs().getOrDefault(Tokens.ARGS_USER_AGENT, null));
}
 
Example #28
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 #29
Source File: GremlinServerAuthIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldFailIfSslEnabledOnServerButNotClient() 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 enable SSL");
    } catch(Exception ex) {
        final Throwable root = ExceptionUtils.getRootCause(ex);
        assertEquals(TimeoutException.class, root.getClass());
        assertThat(root.getMessage(), startsWith("Timed out while waiting for an available host"));
    } finally {
        cluster.close();
    }
}
 
Example #30
Source File: GremlinServerIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReceiveFailureOnBadGraphSONSerialization() throws Exception {
    final Cluster cluster = TestClientFactory.build().serializer(Serializers.GRAPHSON_V3D0).create();
    final Client client = cluster.connect();

    try {
        client.submit("class C { def C getC(){return this}}; new C()").all().join();
        fail("Should throw an exception.");
    } catch (RuntimeException re) {
        final Throwable root = ExceptionUtils.getRootCause(re);
        assertThat(root.getMessage(), CoreMatchers.startsWith("Error during serialization: Direct self-reference leading to cycle (through reference chain:"));

        // validate that we can still send messages to the server
        assertEquals(2, client.submit("1+1").all().join().get(0).getInt());
    } finally {
        cluster.close();
    }
}