Merge "[AAI] Fix doc config files"
[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
21 package org.onap.aai.query.builder;
22
23 import static org.junit.Assert.assertTrue;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
29 import org.apache.tinkerpop.gremlin.structure.Graph;
30 import org.apache.tinkerpop.gremlin.structure.T;
31 import org.apache.tinkerpop.gremlin.structure.Vertex;
32 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.aai.AAISetup;
36 import org.onap.aai.edges.enums.EdgeType;
37 import org.onap.aai.exceptions.AAIException;
38 import org.onap.aai.introspection.Loader;
39 import org.onap.aai.introspection.ModelType;
40 import org.onap.aai.serialization.db.EdgeSerializer;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.test.annotation.DirtiesContext;
43
44 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
45 public class UntilTest extends AAISetup {
46
47     private Loader loader;
48
49     @Autowired
50     EdgeSerializer edgeSer;
51
52     @Before
53     public void setup() throws Exception {
54         loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
55     }
56
57     private QueryBuilder<Vertex> buildTestQuery(QueryBuilder<Vertex> qb) throws AAIException {
58         return qb.until(qb.newInstance().getVerticesByProperty("aai-node-type", "l-interface"))
59                 .repeat(qb.newInstance().union(
60                         qb.newInstance().createEdgeTraversal(EdgeType.TREE, "cloud-region", "tenant"),
61                         qb.newInstance().createEdgeTraversal(EdgeType.TREE, "tenant", "vserver"),
62                         qb.newInstance().createEdgeTraversal(EdgeType.TREE, "vserver", "l-interface")))
63                 .store("x").cap("x").unfold().dedup();
64     }
65
66     @Test
67     public void gremlinQueryUntilTest() throws AAIException {
68         Graph graph = TinkerGraph.open();
69         GraphTraversalSource g = graph.traversal();
70
71         Vertex v1 = graph.addVertex(T.id, 1, "aai-node-type", "cloud-region");
72         Vertex v2 = graph.addVertex(T.id, 2, "aai-node-type", "tenant");
73         Vertex v3 = graph.addVertex(T.id, 3, "aai-node-type", "vserver");
74         Vertex v4 = graph.addVertex(T.id, 4, "aai-node-type", "l-interface");
75         edgeSer.addTreeEdge(g, v1, v2);
76         edgeSer.addTreeEdge(g, v2, v3);
77         edgeSer.addTreeEdge(g, v3, v4);
78         List<Vertex> expected = new ArrayList<>();
79         expected.add(v4);
80
81         GremlinTraversal<Vertex> qb = new GremlinTraversal<>(loader, g, v1);
82         QueryBuilder q = buildTestQuery(qb);
83
84         List<Vertex> results = q.toList();
85
86         assertTrue("results match", expected.containsAll(results) && results.containsAll(expected));
87     }
88
89     @Test
90     public void traversalQueryUntilTest() throws AAIException {
91         Graph graph = TinkerGraph.open();
92         GraphTraversalSource g = graph.traversal();
93
94         Vertex v1 = graph.addVertex(T.id, 1, "aai-node-type", "cloud-region");
95         Vertex v2 = graph.addVertex(T.id, 2, "aai-node-type", "tenant");
96         Vertex v3 = graph.addVertex(T.id, 3, "aai-node-type", "vserver");
97         Vertex v4 = graph.addVertex(T.id, 4, "aai-node-type", "l-interface");
98         edgeSer.addTreeEdge(g, v1, v2);
99         edgeSer.addTreeEdge(g, v2, v3);
100         edgeSer.addTreeEdge(g, v3, v4);
101         List<Vertex> expected = new ArrayList<>();
102         expected.add(v4);
103
104         TraversalQuery<Vertex> qb = new TraversalQuery<>(loader, g, v1);
105         QueryBuilder<Vertex> q = buildTestQuery(qb);
106
107         List<Vertex> results = q.toList();
108
109         assertTrue("results match", expected.containsAll(results) && results.containsAll(expected));
110     }
111
112 }