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");
}
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());
}
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");
}
}
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;
}
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;
}
Aggregations