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").addV("vserver").as("v2").addV("interface")
52 .property("name", "interface 1").as("v10").addE("hasChild").from("v2")
53 .property(EdgeProperty.CONTAINS.toString(), true).addV("pserver").property("name", "pserver 1").as("v4")
54 .addE("runsOn").to("v1").property(EdgeProperty.CONTAINS.toString(), false).addV("interface")
55 .property("name", "interface 2").as("v3").addE("hasChild").from("v1")
56 .property(EdgeProperty.CONTAINS.toString(), true).addV("address").property("name", "address 1")
57 .addE("hasChild").from("v3").property(EdgeProperty.CONTAINS.toString(), true).addV("address")
58 .property("name", "address 2").addE("hasChild").from("v3")
59 .property(EdgeProperty.CONTAINS.toString(), true).addV("complex").property("name", "complex 1")
60 .addE("locatedIn").from("v4").property(EdgeProperty.CONTAINS.toString(), false).addV("interface")
61 .property("name", "interface 3").addE("hasChild").from("v4")
62 .property(EdgeProperty.CONTAINS.toString(), true).addV("subnet").property("name", "subnet 1").as("v5")
63 .addE("in").from("v3").property(EdgeProperty.CONTAINS.toString(), false).addV("address")
64 .property("name", "address 3").as("v6").addE("hasChild").from("v5")
65 .property(EdgeProperty.CONTAINS.toString(), true).select("v1").next();
67 tree = new GraphTraversalQueryEngine(g).findSubGraph((Vertex) startKey);
68 treeDepth1 = new GraphTraversalQueryEngine(g).findSubGraph((Vertex) startKey, 1, false);
69 treeDepth0NodeOnly = new GraphTraversalQueryEngine(g).findSubGraph((Vertex) startKey, 0, true);
74 public void oneHopViaEdges() {
76 // BulkSet set = (BulkSet)result;
77 TreeBackedVertex v = new TreeBackedVertex((Vertex) tree.getObjectsAtDepth(1).iterator().next(), tree);
79 assertEquals("locate child", v.edges(Direction.OUT).next().inVertex().property("name").orElse(""),
81 assertEquals("locate cousin", v.edges(Direction.IN).next().outVertex().property("name").orElse(""),
88 public void oneHopViaVertices() {
90 // BulkSet set = (BulkSet)result;
91 TreeBackedVertex v = new TreeBackedVertex((Vertex) tree.getObjectsAtDepth(1).iterator().next(), tree);
93 assertEquals("locate child", "interface 2", v.vertices(Direction.OUT).next().property("name").orElse(""));
94 assertEquals("locate cousin", "pserver 1", v.vertices(Direction.IN).next().property("name").orElse(""));
100 public void twoHopCousinViaVertices() {
102 // BulkSet set = (BulkSet)result;
103 TreeBackedVertex v = new TreeBackedVertex((Vertex) tree.getObjectsAtDepth(1).iterator().next(), tree);
105 assertEquals("locate child", "subnet 1",
106 v.vertices(Direction.OUT).next().vertices(Direction.OUT, "in").next().property("name").orElse(""));
111 public void walkVerticesRestrictedDepth() {
113 // BulkSet set = (BulkSet)result;
115 new TreeBackedVertex((Vertex) treeDepth1.getObjectsAtDepth(1).iterator().next(), treeDepth1);
117 assertEquals("nothing returned", false,
118 v.vertices(Direction.OUT).next().vertices(Direction.OUT, "hasChild").hasNext());
123 public void walkVertices() {
124 TreeBackedVertex v = new TreeBackedVertex((Vertex) tree.getObjectsAtDepth(1).iterator().next(), tree);
125 assertEquals("locate child", "address 2", v.vertices(Direction.OUT).next().vertices(Direction.OUT, "hasChild")
126 .next().property("name").orElse(""));
130 public void walkEdges() {
131 TreeBackedVertex v = new TreeBackedVertex((Vertex) tree.getObjectsAtDepth(1).iterator().next(), tree);
133 assertEquals("locate child", "address 2", v.edges(Direction.OUT).next().inVertex()
134 .edges(Direction.OUT, "hasChild").next().inVertex().property("name").orElse(""));
138 public void noEdgesFoudWithLabelVertices() {
139 TreeBackedVertex v = new TreeBackedVertex((Vertex) tree.getObjectsAtDepth(1).iterator().next(), tree);
141 assertEquals("missing hello label", false, v.vertices(Direction.OUT, "hello").hasNext());
145 public void noEdgesFoudWithLabelEdges() {
146 TreeBackedVertex v = new TreeBackedVertex((Vertex) tree.getObjectsAtDepth(1).iterator().next(), tree);
148 assertEquals("missing hello label", false, v.edges(Direction.OUT, "hello").hasNext());
152 public void depthZeroNodeOnly() {
153 TreeBackedVertex v = new TreeBackedVertex((Vertex) treeDepth0NodeOnly.getObjectsAtDepth(1).iterator().next(),
155 assertEquals("no edges returned", false, v.edges(Direction.BOTH).hasNext());