2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.openecomp.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.T;
31 import org.apache.tinkerpop.gremlin.structure.Vertex;
32 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.junit.Ignore;
37 import org.openecomp.aai.serialization.engines.query.GraphTraversalQueryEngine;
39 public class TreeBackedVertexTest {
42 private static Graph graph = TinkerGraph.open();
43 private static Object startKey = null;
44 private static Tree<Element> tree = null;
45 private static Tree<Element> treeDepth1 = null;
46 private static Tree<Element> treeDepth0NodeOnly = null;
48 public static void configure() {
49 GraphTraversalSource g = graph.traversal();
51 startKey = g.addV(T.label, "vserver").as("v1").property("test", "hello")
52 .addV(T.label, "vserver").as("v2")
53 .addV(T.label, "interface").property("name", "interface 1").as("v7").addInE("hasChild", "v2").property("isParent", true)
54 .addV(T.label, "pserver").property("name", "pserver 1").as("v4").addOutE("runsOn", "v1").property("isParent", false)
55 .addV(T.label, "interface").property("name", "interface 2").as("v3").addInE("hasChild", "v1").property("isParent", true)
56 .addV(T.label, "address").property("name", "address 1").addInE("hasChild", "v3").property("isParent", true)
57 .addV(T.label, "address").property("name", "address 2").addInE("hasChild", "v3").property("isParent", true)
58 .addV(T.label, "complex").property("name", "complex 1").addInE("locatedIn", "v4").property("isParent", false)
59 .addV(T.label, "interface").property("name", "interface 3").addInE("hasChild", "v4").property("isParent", true)
60 .addV(T.label, "subnet").property("name", "subnet 1").as("v5").addInE("in", "v3").property("isParent", false)
61 .addV(T.label, "address").property("name", "address 3").as("v6").addInE("hasChild", "v5").property("isParent", true)
64 tree = new GraphTraversalQueryEngine(g).findSubGraph((Vertex)startKey);
65 treeDepth1 = new GraphTraversalQueryEngine(g).findSubGraph((Vertex)startKey, 1, false);
66 treeDepth0NodeOnly = new GraphTraversalQueryEngine(g).findSubGraph((Vertex)startKey, 0, true);
71 public void oneHopViaEdges() {
73 //BulkSet set = (BulkSet)result;
74 TreeBackedVertex v = new TreeBackedVertex((Vertex)tree.getObjectsAtDepth(1).iterator().next(), tree);
77 assertEquals("locate child", v.edges(Direction.OUT).next().inVertex().property("name").orElse(""), "interface 2");
78 assertEquals("locate cousin", v.edges(Direction.IN).next().outVertex().property("name").orElse(""), "pserver 1");
85 public void oneHopViaVertices() {
87 //BulkSet set = (BulkSet)result;
88 TreeBackedVertex v = new TreeBackedVertex((Vertex)tree.getObjectsAtDepth(1).iterator().next(), tree);
91 assertEquals("locate child", "interface 2", v.vertices(Direction.OUT).next().property("name").orElse(""));
92 assertEquals("locate cousin", "pserver 1", v.vertices(Direction.IN).next().property("name").orElse(""));
98 public void twoHopCousinViaVertices() {
100 //BulkSet set = (BulkSet)result;
101 TreeBackedVertex v = new TreeBackedVertex((Vertex)tree.getObjectsAtDepth(1).iterator().next(), tree);
104 assertEquals("locate child", "subnet 1", v.vertices(Direction.OUT).next().vertices(Direction.OUT, "in").next().property("name").orElse(""));
109 public void walkVerticesRestrictedDepth() {
111 //BulkSet set = (BulkSet)result;
112 TreeBackedVertex v = new TreeBackedVertex((Vertex)treeDepth1.getObjectsAtDepth(1).iterator().next(), treeDepth1);
115 assertEquals("nothing returned", false, v.vertices(Direction.OUT).next()
116 .vertices(Direction.OUT, "hasChild").hasNext());
121 public void walkVertices() {
122 TreeBackedVertex v = new TreeBackedVertex((Vertex)tree.getObjectsAtDepth(1).iterator().next(), tree);
123 assertEquals("locate child", "address 2", v.vertices(Direction.OUT).next()
124 .vertices(Direction.OUT, "hasChild").next().property("name").orElse(""));
128 public void walkEdges() {
129 TreeBackedVertex v = new TreeBackedVertex((Vertex)tree.getObjectsAtDepth(1).iterator().next(), tree);
131 assertEquals("locate child", "address 2", v.edges(Direction.OUT).next().inVertex()
132 .edges(Direction.OUT, "hasChild").next().inVertex().property("name").orElse(""));
136 public void noEdgesFoudWithLabelVertices() {
137 TreeBackedVertex v = new TreeBackedVertex((Vertex)tree.getObjectsAtDepth(1).iterator().next(), tree);
139 assertEquals("missing hello label", false , v.vertices(Direction.OUT, "hello").hasNext());
143 public void noEdgesFoudWithLabelEdges() {
144 TreeBackedVertex v = new TreeBackedVertex((Vertex)tree.getObjectsAtDepth(1).iterator().next(), tree);
146 assertEquals("missing hello label", false , v.edges(Direction.OUT, "hello").hasNext());
150 public void depthZeroNodeOnly() {
151 TreeBackedVertex v = new TreeBackedVertex((Vertex)treeDepth0NodeOnly.getObjectsAtDepth(1).iterator().next(), treeDepth0NodeOnly);
152 assertEquals("no edges returned", false, v.edges(Direction.BOTH).hasNext());