Merge "Release 1.14.0 maven artifact"
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / parsers / query / GraphTraversalTest.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.parsers.query;
22
23 import static org.hamcrest.CoreMatchers.containsString;
24 import static org.hamcrest.Matchers.hasProperty;
25 import static org.hamcrest.Matchers.is;
26 import static org.junit.Assert.assertEquals;
27
28 import java.io.UnsupportedEncodingException;
29 import java.net.URI;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.List;
34
35 import javax.ws.rs.core.MultivaluedHashMap;
36 import javax.ws.rs.core.MultivaluedMap;
37 import javax.ws.rs.core.UriBuilder;
38
39 import org.apache.tinkerpop.gremlin.process.traversal.P;
40 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
41 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
42 import org.apache.tinkerpop.gremlin.structure.Vertex;
43 import org.junit.Before;
44 import org.junit.Rule;
45 import org.junit.Test;
46 import org.junit.experimental.categories.Category;
47 import org.junit.rules.ExpectedException;
48 import org.junit.runner.RunWith;
49 import org.junit.runners.Parameterized;
50 import org.onap.aai.DataLinkSetup;
51 import org.onap.aai.TinkerpopUpgrade;
52 import org.onap.aai.db.props.AAIProperties;
53 import org.onap.aai.exceptions.AAIException;
54 import org.onap.aai.introspection.ModelType;
55 import org.onap.aai.rest.RestTokens;
56 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
57 import org.onap.aai.serialization.engines.QueryStyle;
58 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
59 import org.springframework.test.annotation.DirtiesContext;
60
61 @Category(TinkerpopUpgrade.class)
62 @RunWith(value = Parameterized.class)
63 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
64 public class GraphTraversalTest extends DataLinkSetup {
65
66     private TransactionalGraphEngine dbEngine;
67     private TransactionalGraphEngine dbEngineDepthVersion;
68
69     @Parameterized.Parameter(value = 0)
70     public QueryStyle queryStyle;
71
72     @Parameterized.Parameters(name = "QueryStyle.{0}")
73     public static Collection<Object[]> data() {
74         return Arrays.asList(new Object[][] {{QueryStyle.TRAVERSAL}, {QueryStyle.TRAVERSAL_URI}});
75     }
76
77     @Rule
78     public ExpectedException thrown = ExpectedException.none();
79
80     /**
81      * Configure.
82      * 
83      * @throws Exception
84      * @throws SecurityException
85      * @throws NoSuchFieldException
86      */
87     @Before
88     public void configure() throws Exception {
89         dbEngine = new JanusGraphDBEngine(queryStyle,
90                 loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion()), false);
91
92         dbEngineDepthVersion = new JanusGraphDBEngine(queryStyle,
93                 loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDepthVersion()), false);
94     }
95
96     /**
97      * Parent query.
98      *
99      * @throws UnsupportedEncodingException the unsupported encoding exception
100      * @throws AAIException the AAI exception
101      */
102     @Test
103     public void parentQuery() throws UnsupportedEncodingException, AAIException {
104         URI uri = UriBuilder.fromPath("cloud-infrastructure/complexes/complex/key1").build();
105
106         QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
107
108         GraphTraversal<Vertex, Vertex> expected =
109                 __.<Vertex>start().has("physical-location-id", "key1").has("aai-node-type", "complex");
110         assertEquals("gremlin query should be " + expected.toString(), expected.toString(),
111                 query.getQueryBuilder().getQuery().toString());
112         assertEquals("parent gremlin query should be equal to normal query", expected.toString(),
113                 query.getQueryBuilder().getParentQuery().getQuery().toString());
114         assertEquals("result type should be complex", "complex", query.getResultType());
115         assertEquals("result type should be empty", "", query.getParentResultType());
116         assertEquals("dependent", false, query.isDependent());
117
118     }
119
120     /**
121      * Child query.
122      *
123      * @throws UnsupportedEncodingException the unsupported encoding exception
124      * @throws AAIException the AAI exception
125      */
126     @Test
127     public void childQuery() throws UnsupportedEncodingException, AAIException {
128         URI uri = UriBuilder.fromPath("cloud-infrastructure/complexes/complex/key1/ctag-pools/ctag-pool/key2/key3")
129                 .build();
130         QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
131         GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("physical-location-id", "key1")
132                 .has("aai-node-type", "complex").in("org.onap.relationships.inventory.BelongsTo")
133                 .has("aai-node-type", "ctag-pool").has("target-pe", "key2").has("availability-zone-name", "key3");
134         GraphTraversal<Vertex, Vertex> expectedParent =
135                 __.<Vertex>start().has("physical-location-id", "key1").has("aai-node-type", "complex");
136         assertEquals("gremlin query should be " + expected.toString(), expected.toString(),
137                 query.getQueryBuilder().getQuery().toString());
138         assertEquals("parent gremlin query should be equal the query for complex", expectedParent.toString(),
139                 query.getQueryBuilder().getParentQuery().getQuery().toString());
140         assertEquals("result type should be complex", "complex", query.getParentResultType());
141         assertEquals("result type should be ctag-pool", "ctag-pool", query.getResultType());
142         assertEquals("dependent", true, query.isDependent());
143
144     }
145
146     /**
147      * Naming exceptions.
148      *
149      * @throws UnsupportedEncodingException the unsupported encoding exception
150      * @throws AAIException the AAI exception
151      */
152     @Test
153     public void namingExceptions() throws UnsupportedEncodingException, AAIException {
154         URI uri = UriBuilder.fromPath("network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655")
155                 .build();
156         QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
157         GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("vnf-id", "key1").has("aai-node-type", "vce")
158                 .in("org.onap.relationships.inventory.BelongsTo").has("aai-node-type", "port-group")
159                 .has("interface-id", "key2").in("org.onap.relationships.inventory.BelongsTo")
160                 .has("aai-node-type", "cvlan-tag").has("cvlan-tag", 655);
161         GraphTraversal<Vertex, Vertex> expectedParent = __.<Vertex>start().has("vnf-id", "key1")
162                 .has("aai-node-type", "vce").in("org.onap.relationships.inventory.BelongsTo")
163                 .has("aai-node-type", "port-group").has("interface-id", "key2");
164         assertEquals("gremlin query should be " + expected.toString(), expected.toString(),
165                 query.getQueryBuilder().getQuery().toString());
166         assertEquals("parent gremlin query should be equal the query for port group", expectedParent.toString(),
167                 query.getQueryBuilder().getParentQuery().getQuery().toString());
168         assertEquals("result type should be cvlan-tag", "cvlan-tag", query.getResultType());
169         assertEquals("result type should be port-group", "port-group", query.getParentResultType());
170         assertEquals("contaner type should be empty", "", query.getContainerType());
171         assertEquals("dependent", true, query.isDependent());
172
173     }
174
175     /**
176      * Gets the all.
177      *
178      * @return the all
179      * @throws UnsupportedEncodingException the unsupported encoding exception
180      * @throws AAIException the AAI exception
181      */
182     @Test
183     public void getAll() throws UnsupportedEncodingException, AAIException {
184         URI uri = UriBuilder.fromPath("network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags").build();
185         QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
186         GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("vnf-id", "key1").has("aai-node-type", "vce")
187                 .in("org.onap.relationships.inventory.BelongsTo").has("aai-node-type", "port-group")
188                 .has("interface-id", "key2").in("org.onap.relationships.inventory.BelongsTo")
189                 .has("aai-node-type", "cvlan-tag");
190         GraphTraversal<Vertex, Vertex> expectedParent = __.<Vertex>start().has("vnf-id", "key1")
191                 .has("aai-node-type", "vce").in("org.onap.relationships.inventory.BelongsTo")
192                 .has("aai-node-type", "port-group").has("interface-id", "key2");
193         assertEquals("gremlin query should be " + expected.toString(), expected.toString(),
194                 query.getQueryBuilder().getQuery().toString());
195         assertEquals("parent gremlin query should be equal the query for port group", expectedParent.toString(),
196                 query.getQueryBuilder().getParentQuery().getQuery().toString());
197         assertEquals("result type should be port-group", "port-group", query.getParentResultType());
198         assertEquals("result type should be cvlan-tag", "cvlan-tag", query.getResultType());
199         assertEquals("container type should be cvlan-tags", "cvlan-tags", query.getContainerType());
200         assertEquals("dependent", true, query.isDependent());
201
202     }
203
204     @Test
205     public void getAllParent() throws UnsupportedEncodingException, AAIException {
206         URI uri = UriBuilder.fromPath("cloud-infrastructure/pservers").build();
207         QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
208         GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-node-type", "pserver");
209         GraphTraversal<Vertex, Vertex> expectedParent = __.<Vertex>start().has("aai-node-type", "pserver");
210         assertEquals("gremlin query should be " + expected.toString(), expected.toString(),
211                 query.getQueryBuilder().getQuery().toString());
212         assertEquals("parent gremlin query should be equal the query for pserver", expectedParent.toString(),
213                 query.getQueryBuilder().getParentQuery().getQuery().toString());
214         assertEquals("parent result type should be empty", "", query.getParentResultType());
215         assertEquals("result type should be pserver", "pserver", query.getResultType());
216         assertEquals("container type should be pservers", "pservers", query.getContainerType());
217         assertEquals("dependent", false, query.isDependent());
218
219     }
220
221     /**
222      * Gets the via query param.
223      *
224      * @return the via query param
225      * @throws UnsupportedEncodingException the unsupported encoding exception
226      * @throws AAIException the AAI exception
227      */
228     @Test
229     public void getViaQueryParam() throws UnsupportedEncodingException, AAIException {
230         URI uri = UriBuilder
231                 .fromPath("cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant")
232                 .build();
233         MultivaluedMap<String, String> map = new MultivaluedHashMap<>();
234         map.putSingle("tenant-name", "Tenant1");
235         QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri, map);
236         GraphTraversal<Vertex, Vertex> expected =
237                 __.<Vertex>start().has("cloud-owner", "mycloudowner").has("cloud-region-id", "mycloudregionid")
238                         .has("aai-node-type", "cloud-region").in("org.onap.relationships.inventory.BelongsTo")
239                         .has("aai-node-type", "tenant").has("tenant-name", "Tenant1");
240
241         GraphTraversal<Vertex, Vertex> expectedParent = __.<Vertex>start().has("cloud-owner", "mycloudowner")
242                 .has("cloud-region-id", "mycloudregionid").has("aai-node-type", "cloud-region");
243
244         assertEquals("gremlin query should be " + expected.toString(), expected.toString(),
245                 query.getQueryBuilder().getQuery().toString());
246         assertEquals("parent gremlin query should be equal the query for cloud-region", expectedParent.toString(),
247                 query.getQueryBuilder().getParentQuery().getQuery().toString());
248         assertEquals("result type should be cloud-region", "cloud-region", query.getParentResultType());
249         assertEquals("result type should be tenant", "tenant", query.getResultType());
250         assertEquals("container type should be empty", "", query.getContainerType());
251         assertEquals("dependent", true, query.isDependent());
252
253     }
254
255     @Test
256     public void getViaDuplicateQueryParam() throws UnsupportedEncodingException, AAIException {
257         URI uri = UriBuilder
258                 .fromPath("cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant")
259                 .build();
260         MultivaluedMap<String, String> map = new MultivaluedHashMap<>();
261         List<String> values = new ArrayList<>();
262         values.add("Tenant1");
263         values.add("Tenant2");
264         map.put("tenant-name", values);
265         QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri, map);
266         GraphTraversal<Vertex, Vertex> expected =
267                 __.<Vertex>start().has("cloud-owner", "mycloudowner").has("cloud-region-id", "mycloudregionid")
268                         .has("aai-node-type", "cloud-region").in("org.onap.relationships.inventory.BelongsTo")
269                         .has("aai-node-type", "tenant").has("tenant-name", P.within(values));
270
271         GraphTraversal<Vertex, Vertex> expectedParent = __.<Vertex>start().has("cloud-owner", "mycloudowner")
272                 .has("cloud-region-id", "mycloudregionid").has("aai-node-type", "cloud-region");
273
274         assertEquals("gremlin query should be " + expected.toString(), expected.toString(),
275                 query.getQueryBuilder().getQuery().toString());
276         assertEquals("parent gremlin query should be equal the query for cloud-region", expectedParent.toString(),
277                 query.getQueryBuilder().getParentQuery().getQuery().toString());
278         assertEquals("result type should be cloud-region", "cloud-region", query.getParentResultType());
279         assertEquals("result type should be tenant", "tenant", query.getResultType());
280         assertEquals("container type should be empty", "", query.getContainerType());
281         assertEquals("dependent", true, query.isDependent());
282
283     }
284
285     /**
286      * Gets the plural via query param.
287      *
288      * @return the plural via query param
289      * @throws UnsupportedEncodingException the unsupported encoding exception
290      * @throws AAIException the AAI exception
291      */
292     @Test
293     public void getPluralViaQueryParam() throws UnsupportedEncodingException, AAIException {
294         URI uri = UriBuilder.fromPath("network/vnfcs").build();
295         MultivaluedMap<String, String> map = new MultivaluedHashMap<>();
296         map.putSingle("prov-status", "up");
297         QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri, map);
298         GraphTraversal<Vertex, Vertex> expected =
299                 __.<Vertex>start().has("aai-node-type", "vnfc").has("prov-status", "up");
300
301         GraphTraversal<Vertex, Vertex> expectedParent = __.<Vertex>start().has("aai-node-type", "vnfc");
302
303         assertEquals("gremlin query should be " + expected.toString(), expected.toString(),
304                 query.getQueryBuilder().getQuery().toString());
305         assertEquals("parent", expectedParent.toString(),
306                 query.getQueryBuilder().getParentQuery().getQuery().toString());
307         assertEquals("parent result type should be empty", "", query.getParentResultType());
308         assertEquals("result type should be vnfc", "vnfc", query.getResultType());
309         assertEquals("container type should be empty", "vnfcs", query.getContainerType());
310         assertEquals("dependent", true, query.isDependent());
311
312     }
313
314     /**
315      * Gets the all query param naming exception.
316      *
317      * @return the all query param naming exception
318      * @throws UnsupportedEncodingException the unsupported encoding exception
319      * @throws AAIException the AAI exception
320      */
321     @Test
322     public void getAllQueryParamNamingException() throws UnsupportedEncodingException, AAIException {
323         URI uri = UriBuilder.fromPath("network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags").build();
324         MultivaluedMap<String, String> map = new MultivaluedHashMap<>();
325         map.putSingle("cvlan-tag", "333");
326         QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri, map);
327
328         GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("vnf-id", "key1").has("aai-node-type", "vce")
329                 .in("org.onap.relationships.inventory.BelongsTo").has("aai-node-type", "port-group")
330                 .has("interface-id", "key2").in("org.onap.relationships.inventory.BelongsTo")
331                 .has("aai-node-type", "cvlan-tag").has("cvlan-tag", 333);
332         GraphTraversal<Vertex, Vertex> expectedParent = __.<Vertex>start().has("vnf-id", "key1")
333                 .has("aai-node-type", "vce").in("org.onap.relationships.inventory.BelongsTo")
334                 .has("aai-node-type", "port-group").has("interface-id", "key2");
335         assertEquals("gremlin query should be " + expected.toString(), expected.toString(),
336                 query.getQueryBuilder().getQuery().toString());
337         assertEquals("parent gremlin query should be equal the query for port group", expectedParent.toString(),
338                 query.getQueryBuilder().getParentQuery().getQuery().toString());
339         assertEquals("result type should be port-group", "port-group", query.getParentResultType());
340         assertEquals("result type should be cvlan-tag", "cvlan-tag", query.getResultType());
341         assertEquals("container type should be cvlan-tags", "cvlan-tags", query.getContainerType());
342         assertEquals("dependent", true, query.isDependent());
343
344     }
345
346     /**
347      * Abstract type.
348      *
349      * @throws UnsupportedEncodingException the unsupported encoding exception
350      * @throws AAIException the AAI exception
351      */
352     @Test
353     public void abstractType() throws UnsupportedEncodingException, AAIException {
354         URI uri = UriBuilder.fromPath("vnf/key1").build();
355
356         QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
357
358         GraphTraversal<Vertex, Vertex> expected =
359                 __.<Vertex>start().has("vnf-id", "key1").has(AAIProperties.NODE_TYPE, P.within("vce", "generic-vnf"));
360
361         GraphTraversal<Vertex, Vertex> expectedParent = expected;
362         assertEquals("gremlin query should be " + expected.toString(), expected.toString(),
363                 query.getQueryBuilder().getQuery().toString());
364         assertEquals("parent gremlin query should be equal the query for port group", expectedParent.toString(),
365                 query.getQueryBuilder().getParentQuery().getQuery().toString());
366         assertEquals("result type should be empty", "", query.getParentResultType());
367         assertEquals("result type should be vnf", "vnf", query.getResultType());
368
369         assertEquals("dependent", false, query.isDependent());
370
371     }
372
373     /**
374      * Non parent abstract type.
375      *
376      * @throws UnsupportedEncodingException the unsupported encoding exception
377      * @throws AAIException the AAI exception
378      */
379     @Test
380     public void nonParentAbstractType() throws UnsupportedEncodingException, AAIException {
381         URI uri = UriBuilder.fromPath("cloud-infrastructure/pservers/pserver/key2/vnf/key1").build();
382         thrown.expect(AAIException.class);
383         thrown.expectMessage(containsString("not a valid path"));
384         dbEngine.getQueryBuilder().createQueryFromURI(uri);
385     }
386
387     @Test
388     public void parentAbstractTypeWithNesting() throws UnsupportedEncodingException, AAIException {
389         URI uri = UriBuilder.fromPath("vnf/key1/vf-modules/vf-module/key2").build();
390
391         QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
392
393         GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("vnf-id", "key1")
394                 .has(AAIProperties.NODE_TYPE, P.within("vce", "generic-vnf"))
395                 .union(__.in("org.onap.relationships.inventory.BelongsTo").has(AAIProperties.NODE_TYPE, "vf-module"))
396                 .has("vf-module-id", "key2");
397
398         GraphTraversal<Vertex, Vertex> expectedParent =
399                 __.<Vertex>start().has("vnf-id", "key1").has(AAIProperties.NODE_TYPE, P.within("vce", "generic-vnf"));
400         assertEquals("gremlin query should be " + expected.toString(), expected.toString(),
401                 query.getQueryBuilder().getQuery().toString());
402         assertEquals("parent gremlin query should be equal the query for ", expectedParent.toString(),
403                 query.getQueryBuilder().getParentQuery().getQuery().toString());
404         assertEquals("result type should be vnf", "vnf", query.getParentResultType());
405         assertEquals("result type should be vf-module", "vf-module", query.getResultType());
406
407         assertEquals("dependent", true, query.isDependent());
408
409     }
410
411     @Test
412     public void getViaBadQueryParam() throws UnsupportedEncodingException, AAIException {
413         URI uri = UriBuilder.fromPath("cloud-infrastructure/cloud-regions/cloud-region/a/b/tenants/tenant").build();
414         MultivaluedMap<String, String> map = new MultivaluedHashMap<>();
415         map.putSingle("tenant-n231ame", "Tenant1");
416         thrown.expect(AAIException.class);
417         thrown.expect(hasProperty("code", is("AAI_3000")));
418
419         QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri, map);
420
421     }
422
423     @Test
424     public void getPluralViaBadQueryParam() throws UnsupportedEncodingException, AAIException {
425         URI uri = UriBuilder.fromPath("cloud-infrastructure/cloud-regions/cloud-region/a/b/tenants").build();
426         MultivaluedMap<String, String> map = new MultivaluedHashMap<>();
427         map.putSingle("tenant-n231ame", "Tenant1");
428         thrown.expect(AAIException.class);
429         thrown.expect(hasProperty("code", is("AAI_3000")));
430
431         QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri, map);
432
433     }
434
435     @Test
436     public void getPluralViaDuplicateQueryParam() throws UnsupportedEncodingException, AAIException {
437         URI uri = UriBuilder.fromPath("network/vnfcs").build();
438         MultivaluedMap<String, String> map = new MultivaluedHashMap<>();
439         List<String> values = new ArrayList<>();
440         values.add("up");
441         values.add("down");
442         values.add("left");
443         values.add("right");
444         values.add("start");
445         map.put("prov-status", values);
446         QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri, map);
447         GraphTraversal<Vertex, Vertex> expected =
448                 __.<Vertex>start().has("aai-node-type", "vnfc").has("prov-status", P.within(values));
449
450         GraphTraversal<Vertex, Vertex> expectedParent = __.<Vertex>start().has("aai-node-type", "vnfc");
451
452         assertEquals("gremlin query should be " + expected.toString(), expected.toString(),
453                 query.getQueryBuilder().getQuery().toString());
454         assertEquals("parent", expectedParent.toString(),
455                 query.getQueryBuilder().getParentQuery().getQuery().toString());
456         assertEquals("parent result type should be empty", "", query.getParentResultType());
457         assertEquals("result type should be vnfc", "vnfc", query.getResultType());
458         assertEquals("container type should be empty", "vnfcs", query.getContainerType());
459         assertEquals("dependent", true, query.isDependent());
460
461     }
462
463     @Test
464     public void dbAliasedSearch() throws UnsupportedEncodingException, AAIException {
465         URI uri = UriBuilder.fromPath("network/generic-vnfs").build();
466         MultivaluedMap<String, String> map = new MultivaluedHashMap<>();
467         map.putSingle("persona-model-customization-id", "key2");
468         QueryParser query = dbEngineDepthVersion.getQueryBuilder().createQueryFromURI(uri, map);
469         GraphTraversal<Vertex, Vertex> expected =
470                 __.<Vertex>start().has("aai-node-type", "generic-vnf").has("model-customization-id", "key2");
471         GraphTraversal<Vertex, Vertex> expectedParent = __.<Vertex>start().has("aai-node-type", "generic-vnf");
472
473         assertEquals("gremlin query should be " + expected.toString(), expected.toString(),
474                 query.getQueryBuilder().getQuery().toString());
475         assertEquals("parent", expectedParent.toString(),
476                 query.getQueryBuilder().getParentQuery().getQuery().toString());
477
478         assertEquals("result type should be", "generic-vnf", query.getResultType());
479         assertEquals("result type should be empty", "", query.getParentResultType());
480         assertEquals("dependent", true, query.isDependent());
481
482     }
483
484     @Test
485     public void dataLinkedSearch() throws UnsupportedEncodingException, AAIException {
486         URI uri = UriBuilder.fromPath("network/vpn-bindings").build();
487         MultivaluedMap<String, String> map = new MultivaluedHashMap<>();
488         map.putSingle("global-route-target", "key2");
489         QueryParser query = dbEngineDepthVersion.getQueryBuilder().createQueryFromURI(uri, map);
490         GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-node-type", "vpn-binding")
491                 .where(__.in("org.onap.relationships.inventory.BelongsTo").has(AAIProperties.NODE_TYPE, "route-target")
492                         .has("global-route-target", "key2"));
493         GraphTraversal<Vertex, Vertex> expectedParent = __.<Vertex>start().has("aai-node-type", "vpn-binding");
494
495         assertEquals("gremlin query should be " + expected.toString(), expected.toString(),
496                 query.getQueryBuilder().getQuery().toString());
497         assertEquals("parent", expectedParent.toString(),
498                 query.getQueryBuilder().getParentQuery().getQuery().toString());
499
500         assertEquals("result type should be", "vpn-binding", query.getResultType());
501         assertEquals("result type should be empty", "", query.getParentResultType());
502         assertEquals("dependent", true, query.isDependent());
503     }
504
505     @Test
506     public void pluralCousin() throws UnsupportedEncodingException, AAIException {
507         URI uri = UriBuilder.fromPath("cloud-infrastructure/complexes/complex/key1/related-to/pservers").build();
508
509         QueryParser query = dbEngineDepthVersion.getQueryBuilder().createQueryFromURI(uri);
510         GraphTraversal<Vertex, Vertex> expected =
511                 __.<Vertex>start().has("physical-location-id", "key1").has("aai-node-type", "complex")
512                         .in("org.onap.relationships.inventory.LocatedIn").has("aai-node-type", "pserver");
513         GraphTraversal<Vertex, Vertex> expectedParent =
514                 __.<Vertex>start().has("physical-location-id", "key1").has("aai-node-type", "complex");
515
516         assertEquals("gremlin query should be " + expected.toString(), expected.toString(),
517                 query.getQueryBuilder().getQuery().toString());
518         assertEquals("parent", expectedParent.toString(),
519                 query.getQueryBuilder().getParentQuery().getQuery().toString());
520
521         assertEquals("result type should be", "pserver", query.getResultType());
522         assertEquals("result type should be", "complex", query.getParentResultType());
523         // this is controversial but we're not allowing writes on this currently
524         assertEquals("dependent", true, query.isDependent());
525     }
526
527     @Test
528     public void specificCousin() throws UnsupportedEncodingException, AAIException {
529         URI uri = UriBuilder.fromPath("cloud-infrastructure/complexes/complex/key1/related-to/pservers/pserver/key2")
530                 .build();
531
532         QueryParser query = dbEngineDepthVersion.getQueryBuilder().createQueryFromURI(uri);
533         GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("physical-location-id", "key1")
534                 .has("aai-node-type", "complex").in("org.onap.relationships.inventory.LocatedIn")
535                 .has("aai-node-type", "pserver").has("hostname", "key2");
536         GraphTraversal<Vertex, Vertex> expectedParent =
537                 __.<Vertex>start().has("physical-location-id", "key1").has("aai-node-type", "complex");
538
539         assertEquals("gremlin query should be " + expected.toString(), expected.toString(),
540                 query.getQueryBuilder().getQuery().toString());
541         assertEquals("parent", expectedParent.toString(),
542                 query.getQueryBuilder().getParentQuery().getQuery().toString());
543
544         assertEquals("result type should be", "pserver", query.getResultType());
545         assertEquals("result type should be", "complex", query.getParentResultType());
546         // this is controversial but we're not allowing writes on this currently
547         assertEquals("dependent", true, query.isDependent());
548     }
549
550     @Test
551     public void doubleSpecificCousin() throws UnsupportedEncodingException, AAIException {
552         URI uri = UriBuilder.fromPath(
553                 "cloud-infrastructure/complexes/complex/key1/related-to/pservers/pserver/key2/related-to/vservers/vserver/key3")
554                 .build();
555
556         QueryParser query = dbEngineDepthVersion.getQueryBuilder().createQueryFromURI(uri);
557         GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("physical-location-id", "key1")
558                 .has("aai-node-type", "complex").in("org.onap.relationships.inventory.LocatedIn")
559                 .has("aai-node-type", "pserver").has("hostname", "key2").in("tosca.relationships.HostedOn")
560                 .has("aai-node-type", "vserver").has("vserver-id", "key3");
561         GraphTraversal<Vertex, Vertex> expectedParent = __.<Vertex>start().has("physical-location-id", "key1")
562                 .has("aai-node-type", "complex").in("org.onap.relationships.inventory.LocatedIn")
563                 .has("aai-node-type", "pserver").has("hostname", "key2");
564
565         assertEquals("gremlin query should be " + expected.toString(), expected.toString(),
566                 query.getQueryBuilder().getQuery().toString());
567         assertEquals("parent", expectedParent.toString(),
568                 query.getQueryBuilder().getParentQuery().getQuery().toString());
569
570         assertEquals("result type should be", "vserver", query.getResultType());
571         assertEquals("result type should be", "pserver", query.getParentResultType());
572         // this is controversial but we're not allowing writes on this currently
573         assertEquals("dependent", true, query.isDependent());
574     }
575
576     @Test
577     public void traversalEndsInRelatedTo() throws UnsupportedEncodingException, AAIException {
578         URI uri = UriBuilder.fromPath("cloud-infrastructure/complexes/complex/key1/related-to").build();
579
580         thrown.expect(AAIException.class);
581         thrown.expectMessage(containsString(RestTokens.COUSIN.toString()));
582         QueryParser query = dbEngineDepthVersion.getQueryBuilder().createQueryFromURI(uri);
583
584     }
585
586     @Test
587     public void pluralCousinToPluralCousin() throws UnsupportedEncodingException, AAIException {
588         URI uri = UriBuilder.fromPath("cloud-infrastructure/complexes/related-to/pservers").build();
589
590         thrown.expect(AAIException.class);
591         thrown.expectMessage(containsString("chain plurals"));
592         QueryParser query = dbEngineDepthVersion.getQueryBuilder().createQueryFromURI(uri);
593
594     }
595 }