AAI-1523 checkstyle warnings for aai-core query
[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.edges.enums.EdgeType;
31 import org.onap.aai.exceptions.AAIException;
32 import org.onap.aai.introspection.Loader;
33 import org.onap.aai.introspection.ModelType;
34
35 import org.onap.aai.serialization.db.EdgeSerializer;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.test.annotation.DirtiesContext;
38 import java.util.List;
39
40 import static org.junit.Assert.assertTrue;
41
42
43 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
44 public class SimplePathTest extends AAISetup {
45
46     public Loader loader;
47     
48     @Autowired
49     EdgeSerializer edgeSer;
50     
51     @Before
52     public void setup() throws Exception {
53         loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
54     }
55
56     private QueryBuilder<Vertex> buildTestQuery(QueryBuilder<Vertex> qb) throws AAIException {
57         return qb.createEdgeTraversal(EdgeType.TREE, "generic-vnf", "l-interface")
58                 .until(qb.newInstance().getVerticesByProperty("aai-node-type", "generic-vnf"))
59                 .repeat(qb.newInstance().union(
60                             qb.newInstance().createEdgeTraversal(EdgeType.TREE, "generic-vnf", "l-interface"),
61                             qb.newInstance().createEdgeTraversal(EdgeType.TREE, "l-interface", "generic-vnf"),
62                             qb.newInstance().createEdgeTraversal(EdgeType.COUSIN, "l-interface", "logical-link"),
63                             qb.newInstance().createEdgeTraversal(EdgeType.COUSIN, "logical-link", "l-interface")
64                         ).simplePath())
65                 .store("x").cap("x").unfold().dedup();
66     }
67     
68     private GraphTraversalSource setupGraph() throws AAIException{
69         Graph graph = TinkerGraph.open();
70         GraphTraversalSource g = graph.traversal();
71         
72         Vertex gvnf1 = graph.addVertex(T.label, "generic-vnf", T.id, "00", "aai-node-type", "generic-vnf", 
73                 "vnf-id", "gvnf1", "vnf-name", "genvnfname1", "nf-type", "sample-nf-type");
74
75         Vertex lint1 = graph.addVertex(T.label, "l-interface", T.id, "10", "aai-node-type", "l-interface",
76                         "interface-name", "lint1", "is-port-mirrored", "true", "in-maint", "true", "is-ip-unnumbered", "false");
77         
78         Vertex loglink1 = graph.addVertex(T.label, "logical-link", T.id, "20", "aai-node-type", "logical-link",
79                         "link-name", "loglink1", "in-maint", "false", "link-type", "sausage");
80         
81         Vertex lint2 = graph.addVertex(T.label, "l-interface", T.id, "11", "aai-node-type", "l-interface",
82                         "interface-name", "lint2", "is-port-mirrored", "true", "in-maint", "true", "is-ip-unnumbered", "false");
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         edgeSer.addTreeEdge(g, gvnf1, lint1);
91         edgeSer.addEdge(g, lint1, loglink1);
92         edgeSer.addEdge(g, loglink1, lint2);
93         edgeSer.addEdge(g, loglink1, lint3);
94         edgeSer.addTreeEdge(g, gvnf2, lint3);
95         
96         return g;
97     }
98     
99     @Test
100     public void gremlinQueryTest() throws AAIException {
101         GraphTraversalSource g = setupGraph();
102         List<Vertex> expected = g.V("01").toList();
103         Vertex start = g.V("00").toList().get(0);
104         
105         GremlinTraversal<Vertex> qb = new GremlinTraversal<>(loader, g, start);
106         QueryBuilder<Vertex> q = buildTestQuery(qb);
107         List<Vertex> results = q.toList();
108         assertTrue("results match", expected.containsAll(results) && results.containsAll(expected));
109     }
110
111     @Test
112     public void traversalQueryTest() throws AAIException {
113         GraphTraversalSource g = setupGraph();
114         List<Vertex> expected = g.V("01").toList();
115         Vertex start = g.V("00").toList().get(0);
116         
117         TraversalQuery<Vertex> qb = new TraversalQuery<>(loader, g, start);
118         QueryBuilder<Vertex> q = buildTestQuery(qb);
119         List<Vertex> results = q.toList();
120         assertTrue("results match", expected.containsAll(results) && results.containsAll(expected));
121     }
122 }