Search in sources :

Example 1 with GraphNode

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

the class GraphTraversalITBase method should_deserialize_vertex_id_as_map.

/**
 * A sanity check that a returned {@link Vertex}'s id is a {@link Map}. This test could break in
 * the future if the format of a vertex ID changes from a Map to something else in DSE.
 *
 * @test_category dse:graph
 */
@Test
public void should_deserialize_vertex_id_as_map() {
    GraphResultSet resultSet = session().execute(newInstance(graphTraversalSource().V().hasLabel("person").has("name", "marko")));
    List<GraphNode> results = resultSet.all();
    assertThat(results.size()).isEqualTo(1);
    Vertex marko = results.get(0).asVertex();
    if (isGraphBinary()) {
        assertThat(((String) marko.id())).contains("marko");
        assertThat(marko.label()).isEqualTo("person");
    } else {
        assertThat(marko).hasProperty("name", "marko");
        @SuppressWarnings("unchecked") Map<String, String> id = (Map<String, String>) marko.id();
        assertThat(id).hasSize(3).containsEntry("~label", "person").containsKey("community_id").containsKey("member_id");
    }
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) 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) Map(java.util.Map) Test(org.junit.Test)

Example 2 with GraphNode

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

the class GraphTraversalITBase method should_parse_tree.

/**
 * Validates that a traversal returning a Tree structure is returned appropriately with the
 * expected contents.
 *
 * <p>Retrieves trees of people marko knows and the software they created.
 *
 * @test_category dse:graph
 */
@Test
public void should_parse_tree() {
    // Get a tree structure showing the paths from mark to people he knows to software they've
    // created.
    GraphResultSet rs = session().execute(newInstance(graphTraversalSource().V().hasLabel("person").out("knows").out("created").tree().by("name")));
    List<GraphNode> results = rs.all();
    assertThat(results.size()).isEqualTo(1);
    // [{key=marko, value=[{key=josh, value=[{key=ripple, value=[]}, {key=lop, value=[]}]}]}]
    GraphNode result = results.get(0);
    @SuppressWarnings("unchecked") Tree<String> tree = result.as(Tree.class);
    assertThat(tree).tree("marko").tree("josh").tree("lop").isLeaf();
    assertThat(tree).tree("marko").tree("josh").tree("ripple").isLeaf();
}
Also used : 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) Test(org.junit.Test)

Example 3 with GraphNode

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

the class GraphTraversalITBase method should_handle_result_object_of_mixed_types.

/**
 * Ensures that a traversal that returns a result of mixed types is interpreted as a {@link Map}
 * with {@link Object} values. Also uses {@link
 * org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal#by(org.apache.tinkerpop.gremlin.process.traversal.Traversal)}
 * with an anonymous traversal to get inbound 'created' edges and folds them into a list.
 *
 * <p>Executes a vertex traversal that binds label 'a' and 'b' to vertex properties and label 'c'
 * to vertices that have edges from that vertex.
 *
 * @test_category dse:graph
 */
@Test
public void should_handle_result_object_of_mixed_types() {
    // find all software vertices and select name, language, and find all vertices that created such
    // software.
    GraphResultSet rs = session().execute(newInstance(graphTraversalSource().V().hasLabel("software").as("a", "b", "c").select("a", "b", "c").by("name").by("lang").by(__.in("created").fold())));
    List<GraphNode> results = rs.all();
    assertThat(results.size()).isEqualTo(2);
    // Ensure that we got 'lop' and 'ripple' for property a.
    assertThat(results).extracting(m -> m.getByKey("a").as(Object.class)).containsOnly("lop", "ripple");
    for (GraphNode result : results) {
        // The row should represent a map with a, b, and c keys.
        assertThat(ImmutableList.<Object>copyOf(result.keys())).containsOnlyOnce("a", "b", "c");
        // 'e' should not exist, thus it should be null.
        assertThat(result.getByKey("e")).isNull();
        // both software are written in java.
        assertThat(result.getByKey("b").isNull()).isFalse();
        assertThat(result.getByKey("b").asString()).isEqualTo("java");
        GraphNode c = result.getByKey("c");
        assertThat(c.isList()).isTrue();
        if (result.getByKey("a").asString().equals("lop")) {
            if (isGraphBinary()) {
                // should contain three vertices
                Assertions.assertThat(c.size()).isEqualTo(3);
            } else {
                // 'c' should contain marko, josh, peter.
                // Ensure we have three vertices.
                assertThat(c.size()).isEqualTo(3);
                List<Vertex> vertices = Lists.newArrayList(c.getByIndex(0).asVertex(), c.getByIndex(1).asVertex(), c.getByIndex(2).asVertex());
                assertThat(vertices).extracting(vertex -> vertex.property("name").value()).containsOnly("marko", "josh", "peter");
            }
        } else {
            if (isGraphBinary()) {
                // has only one label
                Assertions.assertThat(c.size()).isEqualTo(1);
            } else {
                // ripple, 'c' should contain josh.
                // Ensure we have 1 vertex.
                assertThat(c.size()).isEqualTo(1);
                Vertex vertex = c.getByIndex(0).asVertex();
                assertThat(vertex).hasProperty("name", "josh");
            }
        }
    }
}
Also used : GraphNode(com.datastax.dse.driver.api.core.graph.GraphNode) AsyncGraphResultSet(com.datastax.dse.driver.api.core.graph.AsyncGraphResultSet) Graph(org.apache.tinkerpop.gremlin.structure.Graph) GenericType(com.datastax.oss.driver.api.core.type.reflect.GenericType) GraphTestUtils.assertThatContainsLabel(com.datastax.dse.driver.internal.core.graph.GraphTestUtils.assertThatContainsLabel) Tree(org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree) GraphResultSet(com.datastax.dse.driver.api.core.graph.GraphResultSet) Version(com.datastax.oss.driver.api.core.Version) Lists(com.datastax.oss.driver.shaded.guava.common.collect.Lists) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) GraphStatement(com.datastax.dse.driver.api.core.graph.GraphStatement) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Map(java.util.Map) TinkerGraphAssertions.assertThat(com.datastax.dse.driver.api.core.graph.TinkerGraphAssertions.assertThat) Assertions(org.assertj.core.api.Assertions) Path(org.apache.tinkerpop.gremlin.process.traversal.Path) InvalidQueryException(com.datastax.oss.driver.api.core.servererrors.InvalidQueryException) Edge(org.apache.tinkerpop.gremlin.structure.Edge) TinkerPathAssert.validatePathObjects(com.datastax.dse.driver.api.core.graph.TinkerPathAssert.validatePathObjects) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) Test(org.junit.Test) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Collectors(java.util.stream.Collectors) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) ImmutableList(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList) CustomCcmRule(com.datastax.oss.driver.api.testinfra.ccm.CustomCcmRule) ExecutionException(java.util.concurrent.ExecutionException) Assumptions(org.assertj.core.api.Assumptions) List(java.util.List) Direction(org.apache.tinkerpop.gremlin.structure.Direction) Assertions.fail(org.assertj.core.api.Assertions.fail) CompletionStage(java.util.concurrent.CompletionStage) GraphTestUtils.assertThatContainsProperties(com.datastax.dse.driver.internal.core.graph.GraphTestUtils.assertThatContainsProperties) SocialTraversalSource(com.datastax.dse.driver.api.core.graph.SocialTraversalSource) FluentGraphStatement.newInstance(com.datastax.dse.driver.api.core.graph.FluentGraphStatement.newInstance) FluentGraphStatement(com.datastax.dse.driver.api.core.graph.FluentGraphStatement) ScriptGraphStatement(com.datastax.dse.driver.api.core.graph.ScriptGraphStatement) GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) 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) Test(org.junit.Test)

Example 4 with GraphNode

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

the class GraphTraversalITBase method should_use_vertex_id_as_parameter.

/**
 * Ensures that a previously returned {@link Vertex}'s {@link Vertex#id()} can be used as an input
 * to {@link
 * org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource#V(Object...)} to
 * retrieve the {@link Vertex} and that the returned {@link Vertex} is the same.
 *
 * @test_category dse:graph
 */
@Test
public void should_use_vertex_id_as_parameter() {
    GraphTraversal<Vertex, Vertex> query = graphTraversalSource().V().hasLabel("person").has("name", "marko");
    GraphResultSet resultSet = session().execute(newInstance(query));
    List<GraphNode> results = resultSet.all();
    assertThat(results.size()).isEqualTo(1);
    Vertex marko = results.get(0).asVertex();
    if (isGraphBinary()) {
        Map<Object, Object> properties = session().execute(newInstance(query.elementMap("name"))).one().asMap();
        assertThatContainsProperties(properties, "name", "marko");
    } else {
        assertThat(marko).hasProperty("name", "marko");
    }
    resultSet = session().execute(newInstance(graphTraversalSource().V(marko.id())));
    results = resultSet.all();
    assertThat(results.size()).isEqualTo(1);
    Vertex marko2 = results.get(0).asVertex();
    // Ensure that the returned vertex is the same as the first.
    assertThat(marko2).isEqualTo(marko);
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) 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) Test(org.junit.Test)

Example 5 with GraphNode

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

the class GraphTraversalITBase method should_resolve_path_without_labels.

/**
 * Validates that when traversing a path and labeling none of the elements during the traversal
 * that all the labels are empty in the result.
 *
 * @test_category dse:graph
 */
@Test
public void should_resolve_path_without_labels() {
    GraphResultSet rs = session().execute(newInstance(graphTraversalSource().V().hasLabel("person").has("name", "marko").outE("knows").inV().outE("created").inV().path()));
    List<GraphNode> results = rs.all();
    assertThat(results.size()).isEqualTo(2);
    for (GraphNode result : results) {
        Path path = result.asPath();
        validatePathObjects(path);
        assertThat(path.labels()).hasSize(5);
        for (int i = 0; i < 5; i++) assertThat(path).hasNoLabel(i);
    }
}
Also used : Path(org.apache.tinkerpop.gremlin.process.traversal.Path) 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) Test(org.junit.Test)

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