a7c8470ccc37f79faf7a326ab36049bc57038e31
[aai/traversal.git] / aai-traversal / src / test / java / org / onap / aai / rest / search / QueryTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.rest.search;
23
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.Matchers.any;
26 import static org.mockito.Mockito.when;
27
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
33 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
34 import org.apache.tinkerpop.gremlin.structure.Graph;
35 import org.apache.tinkerpop.gremlin.structure.Vertex;
36 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.onap.aai.exceptions.AAIException;
40 import org.onap.aai.introspection.Loader;
41 import org.onap.aai.introspection.LoaderFactory;
42 import org.onap.aai.introspection.ModelType;
43 import org.onap.aai.introspection.Version;
44 import org.onap.aai.query.builder.GremlinTraversal;
45 import org.onap.aai.serialization.db.EdgeRules;
46 import org.onap.aai.serialization.db.exceptions.NoEdgeRuleFoundException;
47 import org.onap.aai.serialization.engines.QueryStyle;
48 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
49
50 public abstract class QueryTest {
51         
52         protected Graph graph;
53         private GremlinServerSingleton gremlinServerSingleton;
54         private GremlinGroovyShellSingleton shell;
55         @Mock private TransactionalGraphEngine dbEngine;
56         protected final List<Vertex> expectedResult = new ArrayList<>();
57         protected final EdgeRules rules = EdgeRules.getInstance();
58         protected Loader loader;
59         
60         public QueryTest() throws AAIException, NoEdgeRuleFoundException {
61                 setUp();
62         }
63         public void setUp() throws AAIException, NoEdgeRuleFoundException {
64                 System.setProperty("AJSC_HOME", ".");
65                 System.setProperty("BUNDLECONFIG_DIR", "bundleconfig-local");
66                 MockitoAnnotations.initMocks(this);
67                 graph = TinkerGraph.open();
68                 createGraph();
69                 gremlinServerSingleton = GremlinServerSingleton.getInstance();
70                 shell = GremlinGroovyShellSingleton.getInstance();
71                 loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.getLatest());
72         }
73         
74         public void run() {
75                 
76                 String query = gremlinServerSingleton.getStoredQueryFromConfig(getQueryName());
77                 Map<String, Object> params = new HashMap<>();
78                 addParam(params);
79                 when(dbEngine.getQueryBuilder(any(QueryStyle.class))).thenReturn(new GremlinTraversal<>(loader, graph.traversal()));
80                 query = GroovyQueryBuilderSingleton.getInstance().executeTraversal(dbEngine, query, params);
81                 query = "g" + query;
82                 GraphTraversal<Vertex, Vertex> g = graph.traversal().V();
83                 addStartNode(g);
84                 params.put("g", g);
85                 GraphTraversal<Vertex, Vertex> result = (GraphTraversal<Vertex, Vertex>)shell.executeTraversal(query, params);
86                 
87                 List<Vertex> vertices = result.toList();
88                 assertTrue("all vertices found", vertices.containsAll(expectedResult) && expectedResult.containsAll(vertices));
89
90         }
91         
92         protected abstract void createGraph() throws AAIException, NoEdgeRuleFoundException;
93                 
94         protected abstract String getQueryName();
95         
96         protected abstract void addStartNode(GraphTraversal<Vertex, Vertex> g);
97         
98         protected abstract void addParam(Map<String, Object> params);
99 }