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