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.query.builder;
23 import static org.junit.Assert.assertTrue;
25 import java.util.ArrayList;
26 import java.util.List;
28 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
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.Before;
34 import org.junit.Test;
35 import org.onap.aai.AAISetup;
36 import org.onap.aai.edges.enums.EdgeType;
37 import org.onap.aai.exceptions.AAIException;
38 import org.onap.aai.introspection.Loader;
39 import org.onap.aai.introspection.ModelType;
40 import org.onap.aai.serialization.db.EdgeSerializer;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.test.annotation.DirtiesContext;
44 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
45 public class UntilTest extends AAISetup {
47 private Loader loader;
50 EdgeSerializer edgeSer;
53 public void setup() throws Exception {
54 loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
57 private QueryBuilder<Vertex> buildTestQuery(QueryBuilder<Vertex> qb) throws AAIException {
58 return qb.until(qb.newInstance().getVerticesByProperty("aai-node-type", "l-interface"))
59 .repeat(qb.newInstance().union(
60 qb.newInstance().createEdgeTraversal(EdgeType.TREE, "cloud-region", "tenant"),
61 qb.newInstance().createEdgeTraversal(EdgeType.TREE, "tenant", "vserver"),
62 qb.newInstance().createEdgeTraversal(EdgeType.TREE, "vserver", "l-interface")))
63 .store("x").cap("x").unfold().dedup();
67 public void gremlinQueryUntilTest() throws AAIException {
68 Graph graph = TinkerGraph.open();
69 GraphTraversalSource g = graph.traversal();
71 Vertex v1 = graph.addVertex(T.id, 1, "aai-node-type", "cloud-region");
72 Vertex v2 = graph.addVertex(T.id, 2, "aai-node-type", "tenant");
73 Vertex v3 = graph.addVertex(T.id, 3, "aai-node-type", "vserver");
74 Vertex v4 = graph.addVertex(T.id, 4, "aai-node-type", "l-interface");
75 edgeSer.addTreeEdge(g, v1, v2);
76 edgeSer.addTreeEdge(g, v2, v3);
77 edgeSer.addTreeEdge(g, v3, v4);
78 List<Vertex> expected = new ArrayList<>();
81 GremlinTraversal<Vertex> qb = new GremlinTraversal<>(loader, g, v1);
82 QueryBuilder q = buildTestQuery(qb);
84 List<Vertex> results = q.toList();
86 assertTrue("results match", expected.containsAll(results) && results.containsAll(expected));
90 public void traversalQueryUntilTest() throws AAIException {
91 Graph graph = TinkerGraph.open();
92 GraphTraversalSource g = graph.traversal();
94 Vertex v1 = graph.addVertex(T.id, 1, "aai-node-type", "cloud-region");
95 Vertex v2 = graph.addVertex(T.id, 2, "aai-node-type", "tenant");
96 Vertex v3 = graph.addVertex(T.id, 3, "aai-node-type", "vserver");
97 Vertex v4 = graph.addVertex(T.id, 4, "aai-node-type", "l-interface");
98 edgeSer.addTreeEdge(g, v1, v2);
99 edgeSer.addTreeEdge(g, v2, v3);
100 edgeSer.addTreeEdge(g, v3, v4);
101 List<Vertex> expected = new ArrayList<>();
104 TraversalQuery<Vertex> qb = new TraversalQuery<>(loader, g, v1);
105 QueryBuilder<Vertex> q = buildTestQuery(qb);
107 List<Vertex> results = q.toList();
109 assertTrue("results match", expected.containsAll(results) && results.containsAll(expected));