Integrate aai-schema-ingest library into aai-core
[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-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.db.props.AAIProperties;
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 org.onap.aai.edges.enums.EdgeType;
39
40 import java.util.ArrayList;
41 import java.util.List;
42
43 import static org.junit.Assert.assertTrue;
44
45 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
46 public class UntilTest extends AAISetup {
47
48         private Loader loader;
49         
50         @Autowired
51         EdgeSerializer edgeSer;
52         
53         @Before
54         public void setup() throws Exception {
55                 loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
56         }
57         
58         private QueryBuilder<Vertex> buildTestQuery(QueryBuilder<Vertex> qb) throws AAIException{
59                 return qb.until(qb.newInstance().getVerticesByProperty("aai-node-type", "l-interface")).repeat(
60                                 qb.newInstance().union(
61                                                 qb.newInstance().createEdgeTraversal(EdgeType.TREE, "cloud-region", "tenant"),
62                                                 qb.newInstance().createEdgeTraversal(EdgeType.TREE, "tenant", "vserver"),
63                                                 qb.newInstance().createEdgeTraversal(EdgeType.TREE, "vserver", "l-interface")
64                         )).store("x").cap("x").unfold().dedup();
65         }
66         
67         @Test
68         public void gremlinQueryUntilTest() throws AAIException {
69                 Graph graph = TinkerGraph.open();
70                 GraphTraversalSource g = graph.traversal();
71                 
72                 Vertex v1 = graph.addVertex(T.id, 1, "aai-node-type", "cloud-region");
73                 Vertex v2 = graph.addVertex(T.id, 2, "aai-node-type", "tenant");
74                 Vertex v3 = graph.addVertex(T.id, 3, "aai-node-type", "vserver");
75                 Vertex v4 = graph.addVertex(T.id, 4, "aai-node-type", "l-interface");
76                 edgeSer.addTreeEdge(g, v1, v2);
77                 edgeSer.addTreeEdge(g, v2, v3);
78                 edgeSer.addTreeEdge(g, v3, v4);
79                 List<Vertex> expected = new ArrayList<>();
80                 expected.add(v4);
81                 
82                 GremlinTraversal<Vertex> qb =  new GremlinTraversal<>(loader, g, v1);
83                 QueryBuilder q = buildTestQuery(qb);
84                 
85                 List<Vertex> results = q.toList();
86
87                 assertTrue("results match", expected.containsAll(results) && results.containsAll(expected));
88         }
89
90         @Test
91         public void traversalQueryUntilTest() throws AAIException {
92                 Graph graph = TinkerGraph.open();
93                 GraphTraversalSource g = graph.traversal();
94                 
95                 Vertex v1 = graph.addVertex(T.id, 1, "aai-node-type", "cloud-region");
96                 Vertex v2 = graph.addVertex(T.id, 2, "aai-node-type", "tenant");
97                 Vertex v3 = graph.addVertex(T.id, 3, "aai-node-type", "vserver");
98                 Vertex v4 = graph.addVertex(T.id, 4, "aai-node-type", "l-interface");
99                 edgeSer.addTreeEdge(g, v1, v2);
100                 edgeSer.addTreeEdge(g, v2, v3);
101                 edgeSer.addTreeEdge(g, v3, v4);
102                 List<Vertex> expected = new ArrayList<>();
103                 expected.add(v4);
104                 
105                 TraversalQuery<Vertex> qb =  new TraversalQuery<>(loader, g, v1);
106                 QueryBuilder<Vertex> q = buildTestQuery(qb);
107                 
108                 List<Vertex> results = q.toList();
109
110                 assertTrue("results match", expected.containsAll(results) && results.containsAll(expected));
111         }
112         
113         
114
115 }