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