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=========================================================
20 package org.openecomp.aai.query.builder;
22 import static org.junit.Assert.assertTrue;
24 import java.util.ArrayList;
25 import java.util.List;
26 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
27 import org.apache.tinkerpop.gremlin.structure.Graph;
28 import org.apache.tinkerpop.gremlin.structure.T;
29 import org.apache.tinkerpop.gremlin.structure.Vertex;
30 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.openecomp.aai.db.props.AAIProperties;
34 import org.openecomp.aai.exceptions.AAIException;
35 import org.openecomp.aai.introspection.Loader;
36 import org.openecomp.aai.introspection.LoaderFactory;
37 import org.openecomp.aai.introspection.ModelType;
38 import org.openecomp.aai.serialization.db.EdgeRules;
39 import org.openecomp.aai.serialization.db.EdgeType;
41 public class UntilTest {
46 System.setProperty("AJSC_HOME", ".");
47 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
48 loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, AAIProperties.LATEST);
52 private QueryBuilder<Vertex> buildTestQuery(QueryBuilder<Vertex> qb) throws AAIException{
53 return qb.until(qb.newInstance().getVerticesByProperty("aai-node-type", "l-interface")).repeat(
54 qb.newInstance().union(
55 qb.newInstance().createEdgeTraversal(EdgeType.TREE, "cloud-region", "tenant"),
56 qb.newInstance().createEdgeTraversal(EdgeType.TREE, "tenant", "vserver"),
57 qb.newInstance().createEdgeTraversal(EdgeType.TREE, "vserver", "l-interface")
58 )).store("x").cap("x").unfold().dedup();
62 public void gremlinQueryUntilTest() throws AAIException {
63 Graph graph = TinkerGraph.open();
64 EdgeRules rules = EdgeRules.getInstance();
65 GraphTraversalSource g = graph.traversal();
67 Vertex v1 = graph.addVertex(T.id, 1, "aai-node-type", "cloud-region");
68 Vertex v2 = graph.addVertex(T.id, 2, "aai-node-type", "tenant");
69 Vertex v3 = graph.addVertex(T.id, 3, "aai-node-type", "vserver");
70 Vertex v4 = graph.addVertex(T.id, 4, "aai-node-type", "l-interface");
71 rules.addTreeEdge(g, v1, v2);
72 rules.addTreeEdge(g, v2, v3);
73 rules.addTreeEdge(g, v3, v4);
74 List<Vertex> expected = new ArrayList<>();
77 GremlinTraversal<Vertex> qb = new GremlinTraversal<>(loader, g, v1);
78 QueryBuilder q = buildTestQuery(qb);
80 List<Vertex> results = q.toList();
82 assertTrue("results match", expected.containsAll(results) && results.containsAll(expected));
86 public void traversalQueryUntilTest() throws AAIException {
87 Graph graph = TinkerGraph.open();
88 EdgeRules rules = EdgeRules.getInstance();
89 GraphTraversalSource g = graph.traversal();
91 Vertex v1 = graph.addVertex(T.id, 1, "aai-node-type", "cloud-region");
92 Vertex v2 = graph.addVertex(T.id, 2, "aai-node-type", "tenant");
93 Vertex v3 = graph.addVertex(T.id, 3, "aai-node-type", "vserver");
94 Vertex v4 = graph.addVertex(T.id, 4, "aai-node-type", "l-interface");
95 rules.addTreeEdge(g, v1, v2);
96 rules.addTreeEdge(g, v2, v3);
97 rules.addTreeEdge(g, v3, v4);
98 List<Vertex> expected = new ArrayList<>();
101 TraversalQuery<Vertex> qb = new TraversalQuery<>(loader, g, v1);
102 QueryBuilder<Vertex> q = buildTestQuery(qb);
104 List<Vertex> results = q.toList();
106 assertTrue("results match", expected.containsAll(results) && results.containsAll(expected));