Search in sources :

Example 6 with GraphNode

use of com.datastax.dse.driver.api.core.graph.GraphNode in project java-driver by datastax.

the class GraphTraversalITBase method should_allow_use_of_dsl_graph_binary.

/**
 * A simple smoke test to ensure that a user can supply a custom {@link GraphTraversalSource} for
 * use with DSLs.
 *
 * @test_category dse:graph
 */
@Test
public void should_allow_use_of_dsl_graph_binary() throws Exception {
    Assumptions.assumeThat(isGraphBinary()).isTrue();
    SocialTraversalSource gSocial = socialTraversalSource();
    GraphStatement gs = newInstance(gSocial.persons("marko").knows("vadas").elementMap("name", "age"));
    GraphResultSet rs = session().execute(gs);
    List<GraphNode> results = rs.all();
    assertThat(results.size()).isEqualTo(1);
    assertThatContainsProperties(results.get(0).asMap(), "name", "marko", "age", 29);
    Assertions.assertThat(results.get(0).asMap().values()).contains("person");
}
Also used : GraphStatement(com.datastax.dse.driver.api.core.graph.GraphStatement) FluentGraphStatement(com.datastax.dse.driver.api.core.graph.FluentGraphStatement) ScriptGraphStatement(com.datastax.dse.driver.api.core.graph.ScriptGraphStatement) AsyncGraphResultSet(com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet) GraphResultSet(com.datastax.dse.driver.api.core.graph.GraphResultSet) GraphNode(com.datastax.dse.driver.api.core.graph.GraphNode) SocialTraversalSource(com.datastax.dse.driver.api.core.graph.SocialTraversalSource) Test(org.junit.Test)

Example 7 with GraphNode

use of com.datastax.dse.driver.api.core.graph.GraphNode in project java-driver by datastax.

the class GraphRequestHandlerTest method should_invoke_request_tracker_and_update_metrics.

@Test
@UseDataProvider("dseVersionsWithDefaultGraphProtocol")
public void should_invoke_request_tracker_and_update_metrics(GraphProtocol graphProtocol, Version dseVersion) throws IOException {
    when(nodeMetricUpdater1.isEnabled(DseNodeMetric.GRAPH_MESSAGES, DriverExecutionProfile.DEFAULT_NAME)).thenReturn(true);
    GraphRequestHandlerTestHarness.Builder builder = GraphRequestHandlerTestHarness.builder().withGraphProtocolForTestConfig(graphProtocol).withDseVersionInMetadata(dseVersion);
    PoolBehavior node1Behavior = builder.customBehavior(node);
    GraphRequestHandlerTestHarness harness = builder.build();
    GraphBinaryModule module = createGraphBinaryModule(harness.getContext());
    GraphSupportChecker graphSupportChecker = mock(GraphSupportChecker.class);
    when(graphSupportChecker.isPagingEnabled(any(), any())).thenReturn(false);
    when(graphSupportChecker.inferGraphProtocol(any(), any(), any())).thenReturn(graphProtocol);
    GraphRequestAsyncProcessor p = Mockito.spy(new GraphRequestAsyncProcessor(harness.getContext(), graphSupportChecker));
    when(p.getGraphBinaryModule()).thenReturn(module);
    RequestTracker requestTracker = mock(RequestTracker.class);
    when(harness.getContext().getRequestTracker()).thenReturn(requestTracker);
    GraphStatement<?> graphStatement = ScriptGraphStatement.newInstance("mockQuery");
    node1Behavior.setResponseSuccess(defaultDseFrameOf(singleGraphRow(graphProtocol, module)));
    GraphResultSet grs = new GraphRequestSyncProcessor(new GraphRequestAsyncProcessor(harness.getContext(), graphSupportChecker)).process(graphStatement, harness.getSession(), harness.getContext(), "test-graph");
    List<GraphNode> nodes = grs.all();
    assertThat(nodes.size()).isEqualTo(1);
    GraphNode graphNode = nodes.get(0);
    assertThat(graphNode.isVertex()).isTrue();
    Vertex actual = graphNode.asVertex();
    assertThat(actual.label()).isEqualTo("person");
    assertThat(actual.id()).isEqualTo(1);
    if (!graphProtocol.isGraphBinary()) {
        // GraphBinary does not encode properties regardless of whether they are present in the
        // parent element or not :/
        assertThat(actual.property("name").id()).isEqualTo(11);
        assertThat(actual.property("name").value()).isEqualTo("marko");
    }
    verify(requestTracker).onSuccess(eq(graphStatement), anyLong(), any(DriverExecutionProfile.class), eq(node), matches(LOG_PREFIX_PER_REQUEST));
    verify(requestTracker).onNodeSuccess(eq(graphStatement), anyLong(), any(DriverExecutionProfile.class), eq(node), matches(LOG_PREFIX_PER_REQUEST));
    verifyNoMoreInteractions(requestTracker);
    verify(nodeMetricUpdater1).isEnabled(DseNodeMetric.GRAPH_MESSAGES, DriverExecutionProfile.DEFAULT_NAME);
    verify(nodeMetricUpdater1).updateTimer(eq(DseNodeMetric.GRAPH_MESSAGES), eq(DriverExecutionProfile.DEFAULT_NAME), anyLong(), eq(TimeUnit.NANOSECONDS));
    verifyNoMoreInteractions(nodeMetricUpdater1);
    verify(harness.getSession().getMetricUpdater()).isEnabled(DseSessionMetric.GRAPH_REQUESTS, DriverExecutionProfile.DEFAULT_NAME);
    verify(harness.getSession().getMetricUpdater()).updateTimer(eq(DseSessionMetric.GRAPH_REQUESTS), eq(DriverExecutionProfile.DEFAULT_NAME), anyLong(), eq(TimeUnit.NANOSECONDS));
    verifyNoMoreInteractions(harness.getSession().getMetricUpdater());
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) PoolBehavior(com.datastax.oss.driver.internal.core.cql.PoolBehavior) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) GraphNode(com.datastax.dse.driver.api.core.graph.GraphNode) RequestTracker(com.datastax.oss.driver.api.core.tracker.RequestTracker) GraphTestUtils.createGraphBinaryModule(com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule) GraphBinaryModule(com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule) GraphResultSet(com.datastax.dse.driver.api.core.graph.GraphResultSet) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 8 with GraphNode

use of com.datastax.dse.driver.api.core.graph.GraphNode in project java-driver by datastax.

the class GraphRequestHandlerTest method should_return_results_for_statements.

@Test
@UseDataProvider("supportedGraphProtocolsWithDseVersions")
public void should_return_results_for_statements(GraphProtocol graphProtocol, Version dseVersion) throws IOException {
    GraphRequestHandlerTestHarness.Builder builder = GraphRequestHandlerTestHarness.builder().withGraphProtocolForTestConfig(graphProtocol).withDseVersionInMetadata(dseVersion);
    PoolBehavior node1Behavior = builder.customBehavior(node);
    GraphRequestHandlerTestHarness harness = builder.build();
    GraphBinaryModule module = createGraphBinaryModule(harness.getContext());
    // ideally we would be able to provide a function here to
    // produce results instead of a static predefined response.
    // Function to which we would pass the harness instance or a (mocked)DriverContext.
    // Since that's not possible in the RequestHandlerTestHarness API at the moment, we
    // have to use another DseDriverContext and GraphBinaryModule here,
    // instead of reusing the one in the harness' DriverContext
    node1Behavior.setResponseSuccess(defaultDseFrameOf(singleGraphRow(graphProtocol, module)));
    GraphSupportChecker graphSupportChecker = mock(GraphSupportChecker.class);
    when(graphSupportChecker.isPagingEnabled(any(), any())).thenReturn(false);
    when(graphSupportChecker.inferGraphProtocol(any(), any(), any())).thenReturn(graphProtocol);
    GraphRequestAsyncProcessor p = Mockito.spy(new GraphRequestAsyncProcessor(harness.getContext(), graphSupportChecker));
    when(p.getGraphBinaryModule()).thenReturn(module);
    GraphStatement<?> graphStatement = ScriptGraphStatement.newInstance("mockQuery").setExecutionProfileName("test-graph");
    GraphResultSet grs = new GraphRequestSyncProcessor(p).process(graphStatement, harness.getSession(), harness.getContext(), "test-graph");
    List<GraphNode> nodes = grs.all();
    assertThat(nodes.size()).isEqualTo(1);
    GraphNode graphNode = nodes.get(0);
    assertThat(graphNode.isVertex()).isTrue();
    Vertex vRead = graphNode.asVertex();
    assertThat(vRead.label()).isEqualTo("person");
    assertThat(vRead.id()).isEqualTo(1);
    if (!graphProtocol.isGraphBinary()) {
        // GraphBinary does not encode properties regardless of whether they are present in the
        // parent element or not :/
        assertThat(vRead.property("name").id()).isEqualTo(11);
        assertThat(vRead.property("name").value()).isEqualTo("marko");
    }
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) PoolBehavior(com.datastax.oss.driver.internal.core.cql.PoolBehavior) GraphResultSet(com.datastax.dse.driver.api.core.graph.GraphResultSet) GraphNode(com.datastax.dse.driver.api.core.graph.GraphNode) GraphTestUtils.createGraphBinaryModule(com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule) GraphBinaryModule(com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 9 with GraphNode

use of com.datastax.dse.driver.api.core.graph.GraphNode in project java-driver by datastax.

the class GraphResultSetTestBase method mockRow.

private GraphNode mockRow(int index) {
    GraphNode row = mock(GraphNode.class);
    when(row.asInt()).thenReturn(index);
    return row;
}
Also used : GraphNode(com.datastax.dse.driver.api.core.graph.GraphNode)

Example 10 with GraphNode

use of com.datastax.dse.driver.api.core.graph.GraphNode in project java-driver by datastax.

the class GraphResultSetTestBase method mockPage.

/**
 * Mocks an async result set where column 0 has type INT, with rows with the provided data.
 */
protected AsyncGraphResultSet mockPage(boolean nextPage, Integer... data) {
    AsyncGraphResultSet page = mock(AsyncGraphResultSet.class);
    ExecutionInfo executionInfo = mock(ExecutionInfo.class);
    when(page.getRequestExecutionInfo()).thenReturn(executionInfo);
    if (nextPage) {
        when(page.hasMorePages()).thenReturn(true);
        when(page.fetchNextPage()).thenReturn(spy(new CompletableFuture<>()));
    } else {
        when(page.hasMorePages()).thenReturn(false);
        when(page.fetchNextPage()).thenThrow(new IllegalStateException());
    }
    // Emulate DefaultAsyncResultSet's internals (this is a bit sketchy, maybe it would be better
    // to use real DefaultAsyncResultSet instances)
    Queue<Integer> queue = Lists.newLinkedList(Arrays.asList(data));
    CountingIterator<GraphNode> iterator = new CountingIterator<GraphNode>(queue.size()) {

        @Override
        protected GraphNode computeNext() {
            Integer index = queue.poll();
            return (index == null) ? endOfData() : mockRow(index);
        }
    };
    when(page.currentPage()).thenReturn(() -> iterator);
    when(page.remaining()).thenAnswer(invocation -> iterator.remaining());
    return page;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) CountingIterator(com.datastax.oss.driver.internal.core.util.CountingIterator) AsyncGraphResultSet(com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet) ExecutionInfo(com.datastax.oss.driver.api.core.cql.ExecutionInfo) GraphNode(com.datastax.dse.driver.api.core.graph.GraphNode)

Aggregations

GraphNode (com.datastax.dse.driver.api.core.graph.GraphNode)41 Test (org.junit.Test)34 AsyncGraphResultSet (com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet)19 GraphResultSet (com.datastax.dse.driver.api.core.graph.GraphResultSet)17 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)13 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)6 Path (org.apache.tinkerpop.gremlin.process.traversal.Path)4 FluentGraphStatement (com.datastax.dse.driver.api.core.graph.FluentGraphStatement)3 GraphStatement (com.datastax.dse.driver.api.core.graph.GraphStatement)3 ScriptGraphStatement (com.datastax.dse.driver.api.core.graph.ScriptGraphStatement)3 SocialTraversalSource (com.datastax.dse.driver.api.core.graph.SocialTraversalSource)3 GraphTestUtils.createGraphBinaryModule (com.datastax.dse.driver.internal.core.graph.GraphTestUtils.createGraphBinaryModule)3 GraphBinaryModule (com.datastax.dse.driver.internal.core.graph.binary.GraphBinaryModule)3 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)3 ExecutionInfo (com.datastax.oss.driver.api.core.cql.ExecutionInfo)3 PoolBehavior (com.datastax.oss.driver.internal.core.cql.PoolBehavior)3 ByteBuffer (java.nio.ByteBuffer)3 DetachedVertex (org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertex)3 ArrayDeque (java.util.ArrayDeque)2 Map (java.util.Map)2