2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.aai.serialization.tinkerpop;
23 import static org.junit.Assert.assertEquals;
25 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
26 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
27 import org.apache.tinkerpop.gremlin.structure.Direction;
28 import org.apache.tinkerpop.gremlin.structure.Element;
29 import org.apache.tinkerpop.gremlin.structure.Graph;
30 import org.apache.tinkerpop.gremlin.structure.Vertex;
31 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
32 import org.junit.Before;
33 import org.junit.Ignore;
34 import org.junit.Test;
35 import org.onap.aai.edges.enums.EdgeProperty;
36 import org.onap.aai.serialization.engines.query.GraphTraversalQueryEngine;
39 public class TreeBackedVertexTest {
41 private Graph graph = TinkerGraph.open();
42 private Object startKey = null;
43 private Tree<Element> tree = null;
44 private Tree<Element> treeDepth1 = null;
45 private Tree<Element> treeDepth0NodeOnly = null;
48 public void configure() {
49 GraphTraversalSource g = graph.traversal();
51 startKey = g.addV("vserver").as("v1").property("test", "hello")
52 .addV("vserver").as("v2")
53 .addV("interface").property("name", "interface 1").as("v10")
54 .addE("hasChild").from("v2").property(EdgeProperty.CONTAINS.toString(), true)
55 .addV("pserver").property("name", "pserver 1").as("v4")
56 .addE("runsOn").to("v1").property(EdgeProperty.CONTAINS.toString(), false)
57 .addV("interface").property("name", "interface 2").as("v3")
58 .addE("hasChild").from("v1").property(EdgeProperty.CONTAINS.toString(), true)
59 .addV("address").property("name", "address 1")
60 .addE("hasChild").from("v3").property(EdgeProperty.CONTAINS.toString(), true)
61 .addV("address").property("name", "address 2")
62 .addE("hasChild").from("v3").property(EdgeProperty.CONTAINS.toString(), true)
63 .addV("complex").property("name", "complex 1")
64 .addE("locatedIn").from("v4").property(EdgeProperty.CONTAINS.toString(), false)
65 .addV("interface").property("name", "interface 3")
66 .addE("hasChild").from("v4").property(EdgeProperty.CONTAINS.toString(), true)
67 .addV("subnet").property("name", "subnet 1").as("v5")
68 .addE("in").from("v3").property(EdgeProperty.CONTAINS.toString(), false)
69 .addV("address").property("name", "address 3").as("v6")
70 .addE("hasChild").from("v5").property(EdgeProperty.CONTAINS.toString(), true)
73 tree = new GraphTraversalQueryEngine(g).findSubGraph((Vertex) startKey);
74 treeDepth1 = new GraphTraversalQueryEngine(g).findSubGraph((Vertex) startKey, 1, false);
75 treeDepth0NodeOnly = new GraphTraversalQueryEngine(g).findSubGraph((Vertex) startKey, 0, true);
80 public void oneHopViaEdges() {
82 // BulkSet set = (BulkSet)result;
83 TreeBackedVertex v = new TreeBackedVertex((Vertex) tree.getObjectsAtDepth(1).iterator().next(), tree);
85 assertEquals("locate child", v.edges(Direction.OUT).next().inVertex().property("name").orElse(""),
87 assertEquals("locate cousin", v.edges(Direction.IN).next().outVertex().property("name").orElse(""),
94 public void oneHopViaVertices() {
96 // BulkSet set = (BulkSet)result;
97 TreeBackedVertex v = new TreeBackedVertex((Vertex) tree.getObjectsAtDepth(1).iterator().next(), tree);
99 assertEquals("locate child", "interface 2", v.vertices(Direction.OUT).next().property("name").orElse(""));
100 assertEquals("locate cousin", "pserver 1", v.vertices(Direction.IN).next().property("name").orElse(""));
106 public void twoHopCousinViaVertices() {
108 // BulkSet set = (BulkSet)result;
109 TreeBackedVertex v = new TreeBackedVertex((Vertex) tree.getObjectsAtDepth(1).iterator().next(), tree);
111 assertEquals("locate child", "subnet 1",
112 v.vertices(Direction.OUT).next().vertices(Direction.OUT, "in").next().property("name").orElse(""));
117 public void walkVerticesRestrictedDepth() {
119 // BulkSet set = (BulkSet)result;
121 new TreeBackedVertex((Vertex) treeDepth1.getObjectsAtDepth(1).iterator().next(), treeDepth1);
123 assertEquals("nothing returned", false,
124 v.vertices(Direction.OUT).next().vertices(Direction.OUT, "hasChild").hasNext());
129 public void walkVertices() {
130 TreeBackedVertex v = new TreeBackedVertex((Vertex) tree.getObjectsAtDepth(1).iterator().next(), tree);
131 assertEquals("locate child", "address 2", v.vertices(Direction.OUT).next().vertices(Direction.OUT, "hasChild")
132 .next().property("name").orElse(""));
136 public void walkEdges() {
137 TreeBackedVertex v = new TreeBackedVertex((Vertex) tree.getObjectsAtDepth(1).iterator().next(), tree);
139 assertEquals("locate child", "address 2", v.edges(Direction.OUT).next().inVertex()
140 .edges(Direction.OUT, "hasChild").next().inVertex().property("name").orElse(""));
144 public void noEdgesFoudWithLabelVertices() {
145 TreeBackedVertex v = new TreeBackedVertex((Vertex) tree.getObjectsAtDepth(1).iterator().next(), tree);
147 assertEquals("missing hello label", false, v.vertices(Direction.OUT, "hello").hasNext());
151 public void noEdgesFoudWithLabelEdges() {
152 TreeBackedVertex v = new TreeBackedVertex((Vertex) tree.getObjectsAtDepth(1).iterator().next(), tree);
154 assertEquals("missing hello label", false, v.edges(Direction.OUT, "hello").hasNext());
158 public void depthZeroNodeOnly() {
159 TreeBackedVertex v = new TreeBackedVertex((Vertex) treeDepth0NodeOnly.getObjectsAtDepth(1).iterator().next(),
161 assertEquals("no edges returned", false, v.edges(Direction.BOTH).hasNext());