d87eb675d6dab5d577ed6478927d63e0c7004360
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / query / builder / UntilTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  *
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.query.builder;
23
24 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
25 import org.apache.tinkerpop.gremlin.structure.Graph;
26 import org.apache.tinkerpop.gremlin.structure.T;
27 import org.apache.tinkerpop.gremlin.structure.Vertex;
28 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.aai.AAISetup;
32 import org.onap.aai.db.props.AAIProperties;
33 import org.onap.aai.exceptions.AAIException;
34 import org.onap.aai.introspection.Loader;
35 import org.onap.aai.introspection.LoaderFactory;
36 import org.onap.aai.introspection.ModelType;
37 import org.onap.aai.serialization.db.EdgeRules;
38 import org.onap.aai.serialization.db.EdgeType;
39
40 import java.util.ArrayList;
41 import java.util.List;
42
43 import static org.junit.Assert.assertTrue;
44
45 public class UntilTest extends AAISetup {
46
47         private Loader loader;
48         
49         @Before
50         public void setup() throws Exception {
51                 loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, AAIProperties.LATEST);
52         }
53         
54         private QueryBuilder<Vertex> buildTestQuery(QueryBuilder<Vertex> qb) throws AAIException{
55                 return qb.until(qb.newInstance().getVerticesByProperty("aai-node-type", "l-interface")).repeat(
56                                 qb.newInstance().union(
57                                                 qb.newInstance().createEdgeTraversal(EdgeType.TREE, "cloud-region", "tenant"),
58                                                 qb.newInstance().createEdgeTraversal(EdgeType.TREE, "tenant", "vserver"),
59                                                 qb.newInstance().createEdgeTraversal(EdgeType.TREE, "vserver", "l-interface")
60                         )).store("x").cap("x").unfold().dedup();
61         }
62         
63         @Test
64         public void gremlinQueryUntilTest() throws AAIException {
65                 Graph graph = TinkerGraph.open();
66                 EdgeRules rules = EdgeRules.getInstance();
67                 GraphTraversalSource g = graph.traversal();
68                 
69                 Vertex v1 = graph.addVertex(T.id, 1, "aai-node-type", "cloud-region");
70                 Vertex v2 = graph.addVertex(T.id, 2, "aai-node-type", "tenant");
71                 Vertex v3 = graph.addVertex(T.id, 3, "aai-node-type", "vserver");
72                 Vertex v4 = graph.addVertex(T.id, 4, "aai-node-type", "l-interface");
73                 rules.addTreeEdge(g, v1, v2);
74                 rules.addTreeEdge(g, v2, v3);
75                 rules.addTreeEdge(g, v3, v4);
76                 List<Vertex> expected = new ArrayList<>();
77                 expected.add(v4);
78                 
79                 GremlinTraversal<Vertex> qb =  new GremlinTraversal<>(loader, g, v1);
80                 QueryBuilder q = buildTestQuery(qb);
81                 
82                 List<Vertex> results = q.toList();
83
84                 assertTrue("results match", expected.containsAll(results) && results.containsAll(expected));
85         }
86
87         @Test
88         public void traversalQueryUntilTest() throws AAIException {
89                 Graph graph = TinkerGraph.open();
90                 EdgeRules rules = EdgeRules.getInstance();
91                 GraphTraversalSource g = graph.traversal();
92                 
93                 Vertex v1 = graph.addVertex(T.id, 1, "aai-node-type", "cloud-region");
94                 Vertex v2 = graph.addVertex(T.id, 2, "aai-node-type", "tenant");
95                 Vertex v3 = graph.addVertex(T.id, 3, "aai-node-type", "vserver");
96                 Vertex v4 = graph.addVertex(T.id, 4, "aai-node-type", "l-interface");
97                 rules.addTreeEdge(g, v1, v2);
98                 rules.addTreeEdge(g, v2, v3);
99                 rules.addTreeEdge(g, v3, v4);
100                 List<Vertex> expected = new ArrayList<>();
101                 expected.add(v4);
102                 
103                 TraversalQuery<Vertex> qb =  new TraversalQuery<>(loader, g, v1);
104                 QueryBuilder<Vertex> q = buildTestQuery(qb);
105                 
106                 List<Vertex> results = q.toList();
107
108                 assertTrue("results match", expected.containsAll(results) && results.containsAll(expected));
109         }
110         
111         
112
113 }