072ba694c1dc0e0119c96f5a89e242563800ea38
[aai/aai-common.git] / aai-core / src / test / java / org / openecomp / aai / query / builder / SimplePathTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
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
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 package org.openecomp.aai.query.builder;
21
22 import static org.junit.Assert.*;
23
24 import java.util.List;
25
26 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
27 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
28 import org.apache.tinkerpop.gremlin.structure.Graph;
29 import org.apache.tinkerpop.gremlin.structure.T;
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.Test;
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.introspection.Version;
39 import org.openecomp.aai.serialization.db.EdgeRules;
40 import org.openecomp.aai.serialization.db.EdgeType;
41 import org.openecomp.aai.serialization.db.exceptions.NoEdgeRuleFoundException;
42
43 public class SimplePathTest {
44         public Loader loader;
45         
46         @Before
47         public void setup() {
48                 System.setProperty("AJSC_HOME", ".");
49                 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
50                 loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.getLatest());
51         }
52
53         private QueryBuilder<Vertex> buildTestQuery(QueryBuilder<Vertex> qb) throws AAIException {
54                 return qb.createEdgeTraversal(EdgeType.TREE, "generic-vnf", "l-interface")
55                                 .until(qb.newInstance().getVerticesByProperty("aai-node-type", "generic-vnf"))
56                                 .repeat(qb.newInstance().union(
57                                                         qb.newInstance().createEdgeTraversal(EdgeType.TREE, "generic-vnf", "l-interface"),
58                                                         qb.newInstance().createEdgeTraversal(EdgeType.TREE, "l-interface", "generic-vnf"),
59                                                         qb.newInstance().createEdgeTraversal(EdgeType.COUSIN, "l-interface", "logical-link"),
60                                                         qb.newInstance().createEdgeTraversal(EdgeType.COUSIN, "logical-link", "l-interface")
61                                                 ).simplePath())
62                                 .store("x").cap("x").unfold().dedup();
63         }
64         
65         private GraphTraversalSource setupGraph() throws AAIException{
66                 Graph graph = TinkerGraph.open();
67                 GraphTraversalSource g = graph.traversal();
68                 EdgeRules rules = EdgeRules.getInstance();
69                 
70                 Vertex gvnf1 = graph.addVertex(T.label, "generic-vnf", T.id, "00", "aai-node-type", "generic-vnf", 
71                                 "vnf-id", "gvnf1", "vnf-name", "genvnfname1", "nf-type", "sample-nf-type");
72
73                 Vertex lint1 = graph.addVertex(T.label, "l-interface", T.id, "10", "aai-node-type", "l-interface",
74                                                 "interface-name", "lint1", "is-port-mirrored", "true", "in-maint", "true", "is-ip-unnumbered", "false");
75                 
76                 Vertex loglink1 = graph.addVertex(T.label, "logical-link", T.id, "20", "aai-node-type", "logical-link",
77                                                 "link-name", "loglink1", "in-maint", "false", "link-type", "sausage");
78                 
79                 Vertex lint2 = graph.addVertex(T.label, "l-interface", T.id, "11", "aai-node-type", "l-interface",
80                                                 "interface-name", "lint2", "is-port-mirrored", "true", "in-maint", "true", "is-ip-unnumbered", "false");
81                 
82                 Vertex loglink2 = graph.addVertex(T.label, "logical-link", T.id, "21", "aai-node-type", "logical-link",
83                                 "link-name", "loglink2", "in-maint", "false", "link-type", "sausage");
84                 
85                 Vertex lint3 = graph.addVertex(T.label, "l-interface", T.id, "12", "aai-node-type", "l-interface",
86                                 "interface-name", "lint3", "is-port-mirrored", "true", "in-maint", "true", "is-ip-unnumbered", "false");
87                 
88                 Vertex gvnf2 = graph.addVertex(T.label, "generic-vnf", T.id, "01", "aai-node-type", "generic-vnf", 
89                                 "vnf-id", "gvnf2", "vnf-name", "genvnfname2", "nf-type", "sample-nf-type");
90                 
91                 rules.addTreeEdge(g, gvnf1, lint1);
92                 rules.addEdge(g, lint1, loglink1);
93                 rules.addEdge(g, loglink1, lint2);
94                 rules.addEdge(g, lint2, loglink2);
95                 rules.addEdge(g, loglink2, lint3);
96                 rules.addTreeEdge(g, gvnf2, lint3);
97                 
98                 return g;
99         }
100         
101         @Test
102         public void gremlinQueryTest() throws AAIException {
103                 GraphTraversalSource g = setupGraph();
104                 List<Vertex> expected = g.V("01").toList();
105                 Vertex start = g.V("00").toList().get(0);
106                 
107                 GremlinTraversal<Vertex> qb = new GremlinTraversal<>(loader, g, start);
108                 QueryBuilder<Vertex> q = buildTestQuery(qb);
109                 List<Vertex> results = q.toList();
110                 assertTrue("results match", expected.containsAll(results) && results.containsAll(expected));
111         }
112
113         @Test
114         public void traversalQueryTest() throws AAIException {
115                 GraphTraversalSource g = setupGraph();
116                 List<Vertex> expected = g.V("01").toList();
117                 Vertex start = g.V("00").toList().get(0);
118                 
119                 TraversalQuery<Vertex> qb = new TraversalQuery<>(loader, g, start);
120                 QueryBuilder<Vertex> q = buildTestQuery(qb);
121                 List<Vertex> results = q.toList();
122                 assertTrue("results match", expected.containsAll(results) && results.containsAll(expected));
123         }
124 }