534386d030bf8cd12bfaa5f1c6df385b1da4f7ae
[aai/aai-common.git] / aai-core / src / test / java / org / openecomp / aai / parsers / query / UniqueURIQueryTest.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.parsers.query;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.io.UnsupportedEncodingException;
26 import java.net.URI;
27
28 import javax.ws.rs.core.UriBuilder;
29
30 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
31 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
34 import org.junit.BeforeClass;
35 import org.junit.Ignore;
36 import org.junit.Test;
37
38 import org.openecomp.aai.exceptions.AAIException;
39 import org.openecomp.aai.introspection.LoaderFactory;
40 import org.openecomp.aai.introspection.ModelInjestor;
41 import org.openecomp.aai.introspection.ModelType;
42 import org.openecomp.aai.introspection.Version;
43 import org.openecomp.aai.serialization.engines.QueryStyle;
44 import org.openecomp.aai.serialization.engines.TitanDBEngine;
45 import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
46
47 @Ignore
48 public class UniqueURIQueryTest {
49         private ModelInjestor injestor = ModelInjestor.getInstance();
50         private TransactionalGraphEngine dbEngine = 
51                         new TitanDBEngine(QueryStyle.GREMLIN_UNIQUE, 
52                                 LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v8),
53                                 false);
54         private final Version version = Version.v8;
55         private DynamicJAXBContext context = injestor.getContextForVersion(version);
56         
57         /**
58          * Configure.
59          */
60         @BeforeClass
61         public static void configure() {
62                 System.setProperty("AJSC_HOME", ".");
63                 System.setProperty("BUNDLECONFIG_DIR", "bundleconfig-local");
64         }
65         
66         
67         /**
68          * Parent query.
69          *
70          * @throws UnsupportedEncodingException the unsupported encoding exception
71          * @throws AAIException the AAI exception
72          */
73         @Test
74     public void parentQuery() throws UnsupportedEncodingException, AAIException {
75                 URI uri = UriBuilder.fromPath("cloud-infrastructure/complexes/complex/key1").build();
76                 String key = "complex/key1";
77                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
78                 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
79                 String parentResultType = "";
80                 String resultType = "complex";
81                 String containerType = "";
82                 
83                 testSet(query, expected, expected, parentResultType, resultType, containerType);
84                 
85     }
86         
87         /**
88          * Parent plural query.
89          *
90          * @throws UnsupportedEncodingException the unsupported encoding exception
91          * @throws AAIException the AAI exception
92          */
93         @Test
94     public void parentPluralQuery() throws UnsupportedEncodingException, AAIException {
95                 URI uri = UriBuilder.fromPath("cloud-infrastructure/complexes").build();
96                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
97                 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-node-type", "complex");
98                 String parentResultType = "";
99                 String resultType = "complex";
100                 String containerType = "complexes";
101                 
102                 testSet(query, expected, expected, parentResultType, resultType, containerType);
103                 
104     }
105
106         /**
107          * Child query.
108          *
109          * @throws UnsupportedEncodingException the unsupported encoding exception
110          * @throws AAIException the AAI exception
111          */
112         @Test
113     public void childQuery() throws UnsupportedEncodingException, AAIException {
114                 URI uri = UriBuilder.fromPath("cloud-infrastructure/complexes/complex/key1/ctag-pools/ctag-pool/key2/key3").build();
115                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
116                 String parentKey = "complex/key1";
117                 String key = parentKey + "/ctag-pool/key2/key3";
118                 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
119                 GraphTraversal<Vertex, Vertex> parentExpected =  __.<Vertex>start().has("aai-unique-key", parentKey);
120                 String parentResultType = "complex";
121                 String resultType = "ctag-pool";
122                 String containerType = "";
123                 
124                 testSet(query, expected, parentExpected, parentResultType, resultType, containerType);
125                 
126     }
127         
128         /**
129          * Naming exceptions.
130          *
131          * @throws UnsupportedEncodingException the unsupported encoding exception
132          * @throws AAIException the AAI exception
133          */
134         @Test
135     public void namingExceptions() throws UnsupportedEncodingException, AAIException {
136                 URI uri = UriBuilder.fromPath("network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655").build();
137                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
138                 String parentKey = "vce/key1/port-group/key2";
139                 String key = parentKey + "/cvlan-tag/655";
140                 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
141                 GraphTraversal<Vertex, Vertex> parentExpected = __.<Vertex>start().has("aai-unique-key", parentKey);
142                 String parentResultType = "port-group";
143                 String resultType = "cvlan-tag";
144                 String containerType = "";
145                 
146                 testSet(query, expected, parentExpected, parentResultType, resultType, containerType);
147                 
148     }
149         
150         /**
151          * Gets the all.
152          *
153          * @return the all
154          * @throws UnsupportedEncodingException the unsupported encoding exception
155          * @throws AAIException the AAI exception
156          */
157         @Test
158     public void getAll() throws UnsupportedEncodingException, AAIException {
159                 String parentURI = "network/vces/vce/key1/port-groups/port-group/key2";
160                 String parentKey = "vce/key1/port-group/key2";
161                 URI uri = UriBuilder.fromPath(parentURI + "/cvlan-tags").build();
162                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
163                 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", parentKey).out("hasCTag").has("aai-node-type", "cvlan-tag");
164                 GraphTraversal<Vertex, Vertex> parentExpected = __.<Vertex>start().has("aai-unique-key",parentKey);
165                 String parentResultType = "port-group";
166                 String resultType = "cvlan-tag";
167                 String containerType = "cvlan-tags";
168                 
169                 testSet(query, expected, parentExpected, parentResultType, resultType, containerType);
170                 
171     }
172         
173         /**
174          * Test set.
175          *
176          * @param query the query
177          * @param expected the expected
178          * @param parentExpected the parent expected
179          * @param parentResultType the parent result type
180          * @param resultType the result type
181          * @param containerType the container type
182          */
183         public void testSet(QueryParser query, GraphTraversal<Vertex, Vertex> expected, GraphTraversal<Vertex, Vertex> parentExpected, String parentResultType, String resultType, String containerType) {
184                 assertEquals(
185                                 "gremlin query should be " + expected,
186                                 expected,
187                                 query.getQueryBuilder().getQuery());
188                 assertEquals(
189                                 "parent gremlin query should be " + parentExpected,
190                                 parentExpected,
191                                 query.getQueryBuilder().getParentQuery().getQuery());
192                 assertEquals(
193                                 "parent result type should be " + parentResultType,
194                                 parentResultType,
195                                 query.getParentResultType());
196                 assertEquals(
197                                 "result type should be " + resultType,
198                                 resultType,
199                                 query.getResultType());
200                 assertEquals(
201                                 "container type should be " + containerType,
202                                 containerType,
203                                 query.getContainerType());
204         }
205 }