eef3529c660fae1309bb046bf42239a66776e12d
[aai/aai-common.git] / aai-core / src / test / java / org / openecomp / aai / parsers / query / RelationshipGremlinQueryTest.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.hamcrest.CoreMatchers.containsString;
24 import static org.junit.Assert.assertEquals;
25
26 import java.io.StringReader;
27 import java.io.UnsupportedEncodingException;
28
29 import javax.xml.bind.JAXBException;
30 import javax.xml.bind.Unmarshaller;
31 import javax.xml.transform.stream.StreamSource;
32
33 import org.eclipse.persistence.dynamic.DynamicEntity;
34 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
35 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
36 import org.junit.BeforeClass;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.rules.ExpectedException;
40
41 import org.openecomp.aai.exceptions.AAIException;
42 import org.openecomp.aai.introspection.Introspector;
43 import org.openecomp.aai.introspection.IntrospectorFactory;
44 import org.openecomp.aai.introspection.LoaderFactory;
45 import org.openecomp.aai.introspection.ModelInjestor;
46 import org.openecomp.aai.introspection.ModelType;
47 import org.openecomp.aai.introspection.Version;
48 import org.openecomp.aai.serialization.engines.QueryStyle;
49 import org.openecomp.aai.serialization.engines.TitanDBEngine;
50 import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
51
52 public class RelationshipGremlinQueryTest {
53
54         private ModelInjestor injestor = ModelInjestor.getInstance();
55         private TransactionalGraphEngine dbEngine = 
56                         new TitanDBEngine(QueryStyle.GREMLIN_TRAVERSAL, 
57                                 LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v8),
58                                 false);
59         private final Version version = Version.v8;
60         private DynamicJAXBContext context = injestor.getContextForVersion(version);
61         
62         @Rule
63         public ExpectedException thrown = ExpectedException.none();
64         
65         /**
66          * Configure.
67          */
68         @BeforeClass
69         public static void configure() {
70                 System.setProperty("AJSC_HOME", "./src/test/resources/");
71                 System.setProperty("BUNDLECONFIG_DIR", "bundleconfig-local");
72         }
73         
74         
75         /**
76          * Parent query.
77          *
78          * @throws JAXBException the JAXB exception
79          * @throws UnsupportedEncodingException the unsupported encoding exception
80          * @throws AAIException the AAI exception
81          */
82         @Test
83     public void parentQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
84                 
85                 String content =
86                                 "{"
87                                 + "\"related-to\" : \"pserver\","
88                                 + "\"relationship-data\" : [{"
89                                 + "\"relationship-key\" : \"pserver.hostname\","
90                                 + "\"relationship-value\" : \"key1\""
91                                 + "}]"
92                                 + "}";
93                                                 
94                 Unmarshaller unmarshaller = context.createUnmarshaller();
95             unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
96             unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
97                 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
98                 Object obj = context.newDynamicEntity("Relationship");
99
100                 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
101                 
102                 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
103                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
104
105                 String expected = 
106                                 ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
107                 assertEquals(
108                                 "gremlin query should be " + expected,
109                                 expected,
110                                 query.getQueryBuilder().getQuery());
111                 assertEquals(
112                                 "parent gremlin query should be equal to normal query",
113                                 expected,
114                                 query.getQueryBuilder().getParentQuery().getQuery());
115                 assertEquals(
116                                 "result type should be pserver",
117                                 "pserver",
118                                 query.getResultType());
119                 
120     }
121
122         /**
123          * Child query.
124          *
125          * @throws JAXBException the JAXB exception
126          * @throws UnsupportedEncodingException the unsupported encoding exception
127          * @throws AAIException the AAI exception
128          */
129         @Test
130     public void childQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
131                 String content =
132                                 "{"
133                                 + "\"related-to\" : \"lag-interface\","
134                                 + "\"relationship-data\" : [{"
135                                 + "\"relationship-key\" : \"pserver.hostname\","
136                                 + "\"relationship-value\" : \"key1\""
137                                 + "}, {"
138                                 + "\"relationship-key\" : \"lag-interface.interface-name\","
139                                 + "\"relationship-value\" : \"key2\""
140                                 + "}]"
141                                 + "}";
142                                                 
143                 Unmarshaller unmarshaller = context.createUnmarshaller();
144             unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
145             unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
146                 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
147                 Object obj = context.newDynamicEntity("Relationship");
148
149                 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
150                         
151                 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
152                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
153
154                 String expected = ".has('hostname', 'key1').has('aai-node-type', 'pserver').in('tosca.relationships.BindsTo').has('aai-node-type', 'lag-interface')"
155                                 + ".has('interface-name', 'key2')";
156                 String parentExpected =
157                                 ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
158                 assertEquals(
159                                 "gremlin query should be for node",
160                                 expected,
161                                 query.getQueryBuilder().getQuery());
162                 assertEquals(
163                                 "parent gremlin query should be for parent",
164                                 parentExpected,
165                                 query.getQueryBuilder().getParentQuery().getQuery());
166                 assertEquals(
167                                 "result type should be lag-interface",
168                                 "lag-interface",
169                                 query.getResultType());
170     }
171         
172         /**
173          * Naming exceptions.
174          *
175          * @throws JAXBException the JAXB exception
176          * @throws UnsupportedEncodingException the unsupported encoding exception
177          * @throws AAIException the AAI exception
178          */
179         @Test
180     public void namingExceptions() throws JAXBException, UnsupportedEncodingException, AAIException {
181                 String content =
182                                 "{"
183                                 + "\"related-to\" : \"cvlan-tag\","
184                                 + "\"relationship-data\" : [{"
185                                 + "\"relationship-key\" : \"vce.vnf-id\","
186                                 + "\"relationship-value\" : \"key1\""
187                                 + "}, {"
188                                 + "\"relationship-key\" : \"port-group.interface-id\","
189                                 + "\"relationship-value\" : \"key2\""
190                                 + "},{"
191                                 + "\"relationship-key\" : \"cvlan-tag.cvlan-tag\","
192                                 + "\"relationship-value\" : \"655\""
193                                 + "}]"
194                                 + "}";
195                                                 
196                 Unmarshaller unmarshaller = context.createUnmarshaller();
197             unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
198             unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
199                 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
200                 Object obj = context.newDynamicEntity("Relationship");
201
202                 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
203                         
204                 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
205                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
206                 String expected = 
207                                 ".has('vnf-id', 'key1').has('aai-node-type', 'vce')"
208                                 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'port-group')"
209                                 + ".has('interface-id', 'key2').in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'cvlan-tag')"
210                                 + ".has('cvlan-tag', 655)";
211                 String expectedParent = 
212                                                 ".has('vnf-id', 'key1').has('aai-node-type', 'vce')"
213                                                 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'port-group')"
214                                                 + ".has('interface-id', 'key2')";
215                 assertEquals(
216                                 "gremlin query should be " + expected,
217                                 expected,
218                                 query.getQueryBuilder().getQuery());
219                 assertEquals(
220                                 "parent gremlin query should be equal the query for port group",
221                                 expectedParent,
222                                 query.getQueryBuilder().getParentQuery().getQuery());
223                 assertEquals(
224                                 "result type should be cvlan-tag",
225                                 "cvlan-tag",
226                                 query.getResultType());
227                 
228     }
229         
230         /**
231          * Scrambled relationship.
232          *
233          * @throws JAXBException the JAXB exception
234          * @throws UnsupportedEncodingException the unsupported encoding exception
235          * @throws AAIException the AAI exception
236          */
237         @Test
238         public void scrambledRelationship() throws JAXBException, UnsupportedEncodingException, AAIException {
239                 String content =
240                                 "{"
241                                 + "\"related-to\" : \"l3-interface-ipv4-address-list\","
242                                 + "\"relationship-data\" : [{"
243                                 + "\"relationship-key\" : \"l3-interface-ipv4-address-list.l3-interface-ipv4-address\","
244                                 + "\"relationship-value\" : \"key5\""
245                                 + "},{"
246                                 + "\"relationship-key\" : \"lag-interface.interface-name\","
247                                 + "\"relationship-value\" : \"key2\""
248                                 + "},{"
249                                 + "\"relationship-key\" : \"l-interface.interface-name\","
250                                 + "\"relationship-value\" : \"key3\""
251                                 + "},{"
252                                 + "\"relationship-key\" : \"vlan.vlan-interface\","
253                                 + "\"relationship-value\" : \"key4\""
254                                 + "},{"
255                                 + "\"relationship-key\" : \"generic-vnf.vnf-id\","
256                                 + "\"relationship-value\" : \"key1\""
257                                 + "}]"
258                                 + "}";
259                 scrambledRelationshipSpec(content);
260         }
261         
262         /**
263          * Reversed relationship.
264          *
265          * @throws JAXBException the JAXB exception
266          * @throws UnsupportedEncodingException the unsupported encoding exception
267          * @throws AAIException the AAI exception
268          */
269         @Test
270         public void reversedRelationship() throws JAXBException, UnsupportedEncodingException, AAIException {
271                 String content =
272                                 "{"
273                                 + "\"related-to\" : \"l3-interface-ipv4-address-list\","
274                                 + "\"relationship-data\" : [{"
275                                 + "\"relationship-key\" : \"l3-interface-ipv4-address-list.l3-interface-ipv4-address\","
276                                 + "\"relationship-value\" : \"key5\""
277                                 + "},{"
278                                 + "\"relationship-key\" : \"vlan.vlan-interface\","
279                                 + "\"relationship-value\" : \"key4\""
280                                 + "},{"
281                                 + "\"relationship-key\" : \"l-interface.interface-name\","
282                                 + "\"relationship-value\" : \"key3\""
283                                 + "},{"
284                                 + "\"relationship-key\" : \"lag-interface.interface-name\","
285                                 + "\"relationship-value\" : \"key2\""
286                                 + "},{"
287                                 + "\"relationship-key\" : \"generic-vnf.vnf-id\","
288                                 + "\"relationship-value\" : \"key1\""
289                                 + "}]"
290                                 + "}";
291                 scrambledRelationshipSpec(content);
292         }
293         
294         /**
295          * Ordered ambiguous relationship.
296          *
297          * @throws JAXBException the JAXB exception
298          * @throws UnsupportedEncodingException the unsupported encoding exception
299          * @throws AAIException the AAI exception
300          */
301         @Test
302         public void orderedAmbiguousRelationship() throws JAXBException, UnsupportedEncodingException, AAIException {
303                 String content =
304                                 "{"
305                                 + "\"related-to\" : \"l3-interface-ipv4-address-list\","
306                                 + "\"relationship-data\" : [{"
307                                 + "\"relationship-key\" : \"generic-vnf.vnf-id\","
308                                 + "\"relationship-value\" : \"key1\""
309                                 + "},{"
310                                 + "\"relationship-key\" : \"lag-interface.interface-name\","
311                                 + "\"relationship-value\" : \"key2\""
312                                 + "},{"
313                                 + "\"relationship-key\" : \"l-interface.interface-name\","
314                                 + "\"relationship-value\" : \"key3\""
315                                 + "},{"
316                                 + "\"relationship-key\" : \"vlan.vlan-interface\","
317                                 + "\"relationship-value\" : \"key4\""
318                                 + "},{"
319                                 + "\"relationship-key\" : \"l3-interface-ipv4-address-list.l3-interface-ipv4-address\","
320                                 + "\"relationship-value\" : \"key5\""
321                                 + "}]"
322                                 + "}";
323                 scrambledRelationshipSpec(content);
324         }
325     
326     /**
327      * Scrambled relationship spec.
328      *
329      * @param content the content
330      * @throws JAXBException the JAXB exception
331      * @throws UnsupportedEncodingException the unsupported encoding exception
332      * @throws AAIException the AAI exception
333      */
334     public void scrambledRelationshipSpec(String content) throws JAXBException, UnsupportedEncodingException, AAIException {
335
336                                                 
337                 Unmarshaller unmarshaller = context.createUnmarshaller();
338             unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
339             unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
340                 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
341                 Object obj = context.newDynamicEntity("Relationship");
342
343                 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
344                         
345                 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
346                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
347                 String expected = 
348                                 ".has('vnf-id', 'key1').has('aai-node-type', 'generic-vnf')"
349                                 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'lag-interface')"
350                                 + ".has('interface-name', 'key2').in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'l-interface')"
351                                 + ".has('interface-name', 'key3').out('tosca.relationships.LinksTo').has('aai-node-type', 'vlan')"
352                                 + ".has('vlan-interface', 'key4').in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'l3-interface-ipv4-address-list')"
353                                 + ".has('l3-interface-ipv4-address', 'key5')";
354                 String expectedParent = 
355                                 ".has('vnf-id', 'key1').has('aai-node-type', 'generic-vnf')"
356                                                 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'lag-interface')"
357                                                 + ".has('interface-name', 'key2').in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'l-interface')"
358                                                 + ".has('interface-name', 'key3').out('tosca.relationships.LinksTo').has('aai-node-type', 'vlan')"
359                                                 + ".has('vlan-interface', 'key4')";
360                 assertEquals(
361                                 "gremlin query should be " + expected,
362                                 expected,
363                                 query.getQueryBuilder().getQuery());
364                 assertEquals(
365                                 "parent gremlin query should be equal the query for vlan",
366                                 expectedParent,
367                                 query.getQueryBuilder().getParentQuery().getQuery());
368                 assertEquals(
369                                 "result type should be l3-interface-ipv4-address-list",
370                                 "l3-interface-ipv4-address-list",
371                                 query.getResultType());
372                 
373     }
374         
375         /**
376          * Short circuit.
377          *
378          * @throws JAXBException the JAXB exception
379          * @throws UnsupportedEncodingException the unsupported encoding exception
380          * @throws AAIException the AAI exception
381          */
382         @Test
383     public void shortCircuit() throws JAXBException, UnsupportedEncodingException, AAIException {
384                 String content =
385                                 "{"
386                                 + "\"related-to\" : \"cvlan-tag\","
387                                 + "\"related-link\" : \"http://mock-system-name.com:8443/aai/v6/network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655\","
388                                 + "\"relationship-data\" : [{"
389                                 + "\"relationship-key\" : \"vce.vnf-id\","
390                                 + "\"relationship-value\" : \"key1\""
391                                 + "}, {"
392                                 + "\"relationship-key\" : \"port-group.interface-id\","
393                                 + "\"relationship-value\" : \"key2\""
394                                 + "},{"
395                                 + "\"relationship-key\" : \"cvlan-tag.cvlan-tag\","
396                                 + "\"relationship-value\" : \"655\""
397                                 + "}]"
398                                 + "}";
399                                                 
400                 Unmarshaller unmarshaller = context.createUnmarshaller();
401             unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
402             unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
403                 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
404                 Object obj = context.newDynamicEntity("Relationship");
405
406                 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
407                         
408                 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
409                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
410                 String expected = 
411                                 ".has('vnf-id', 'key1').has('aai-node-type', 'vce')"
412                                 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'port-group')"
413                                 + ".has('interface-id', 'key2').in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'cvlan-tag')"
414                                 + ".has('cvlan-tag', 655)";
415                 String expectedParent = 
416                                                 ".has('vnf-id', 'key1').has('aai-node-type', 'vce')"
417                                                 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'port-group')"
418                                                 + ".has('interface-id', 'key2')";
419                 assertEquals(
420                                 "gremlin query should be " + expected,
421                                 expected,
422                                 query.getQueryBuilder().getQuery());
423                 assertEquals(
424                                 "parent gremlin query should be equal the query for port group",
425                                 expectedParent,
426                                 query.getQueryBuilder().getParentQuery().getQuery());
427                 assertEquals(
428                                 "result type should be cvlan-tag",
429                                 "cvlan-tag",
430                                 query.getResultType());
431                 
432     }
433         
434         @Test
435     public void shorterCircuit() throws JAXBException, UnsupportedEncodingException, AAIException {
436                 String content =
437                                 "{"
438                                 + "\"related-to\" : \"cvlan-tag\","
439                                 + "\"related-link\" : \"file:///network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655\""
440                                 + "}";
441                                                 
442                 Unmarshaller unmarshaller = context.createUnmarshaller();
443             unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
444             unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
445                 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
446                 Object obj = context.newDynamicEntity("Relationship");
447
448                 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
449                         
450                 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
451                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
452                 String expected = 
453                                 ".has('vnf-id', 'key1').has('aai-node-type', 'vce')"
454                                 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'port-group')"
455                                 + ".has('interface-id', 'key2').in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'cvlan-tag')"
456                                 + ".has('cvlan-tag', 655)";
457                 String expectedParent = 
458                                                 ".has('vnf-id', 'key1').has('aai-node-type', 'vce')"
459                                                 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'port-group')"
460                                                 + ".has('interface-id', 'key2')";
461                 assertEquals(
462                                 "gremlin query should be " + expected,
463                                 expected,
464                                 query.getQueryBuilder().getQuery());
465                 assertEquals(
466                                 "parent gremlin query should be equal the query for port group",
467                                 expectedParent,
468                                 query.getQueryBuilder().getParentQuery().getQuery());
469                 assertEquals(
470                                 "result type should be cvlan-tag",
471                                 "cvlan-tag",
472                                 query.getResultType());
473                 
474     }
475         
476         /**
477          * Double key.
478          *
479          * @throws JAXBException the JAXB exception
480          * @throws UnsupportedEncodingException the unsupported encoding exception
481          * @throws AAIException the AAI exception
482          */
483         @Test
484     public void doubleKey() throws JAXBException, UnsupportedEncodingException, AAIException {
485                 String content =
486                                 "{"
487                                 + "\"related-to\" : \"ctag-pool\","
488                                 + "\"relationship-data\" : [{"
489                                 + "\"relationship-key\" : \"complex.physical-location-id\","
490                                 + "\"relationship-value\" : \"key1\""
491                                 + " }, { "
492                                 + "\"relationship-key\" : \"ctag-pool.target-pe\","
493                                 + " \"relationship-value\" : \"key2\""
494                                 + " },{"
495                                 + "\"relationship-key\" : \"ctag-pool.availability-zone-name\","
496                                 + "\"relationship-value\" : \"key3\""
497                                 + "}]"
498                                 + "}";
499                                                 
500                                                 
501                 Unmarshaller unmarshaller = context.createUnmarshaller();
502             unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
503             unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
504                 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
505                 Object obj = context.newDynamicEntity("Relationship");
506
507                 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
508                         
509                 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
510                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
511
512                 String expected = 
513                                 ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')"
514                                 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'ctag-pool')"
515                                 + ".has('target-pe', 'key2')"
516                                 + ".has('availability-zone-name', 'key3')";
517                 String expectedParent = 
518                                 ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')";
519
520                 assertEquals(
521                                 "gremlin query should be " + expected,
522                                 expected,
523                                 query.getQueryBuilder().getQuery());
524                 assertEquals(
525                                 "parent gremlin query should be equal the query for port group",
526                                 expectedParent,
527                                 query.getQueryBuilder().getParentQuery().getQuery());
528                 assertEquals(
529                                 "result type should be ctag-pool",
530                                 "ctag-pool",
531                                 query.getResultType());
532                 
533     }
534         
535         /**
536          * Abstract type.
537          *
538          * @throws JAXBException the JAXB exception
539          * @throws UnsupportedEncodingException the unsupported encoding exception
540          * @throws AAIException the AAI exception
541          */
542         @Test
543     public void abstractType() throws JAXBException, UnsupportedEncodingException, AAIException {
544                 String content =
545                                 "{"
546                                 + "\"related-to\" : \"vnf\","
547                                 + "\"relationship-data\" : [{"
548                                 + "\"relationship-key\" : \"vnf.vnf-id\","
549                                 + "\"relationship-value\" : \"key1\""
550                                 + " }]"
551                                 + "}";
552                                                 
553                                                 
554                 Unmarshaller unmarshaller = context.createUnmarshaller();
555             unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
556             unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
557                 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
558                 Object obj = context.newDynamicEntity("Relationship");
559
560                 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
561                         
562                 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
563                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
564
565                 String expected = 
566                                 ".has('vnf-id', 'key1')"
567                                 + ".has('aai-node-type', P.within('vce','vpe','generic-vnf'))";
568                                 
569                 String expectedParent = 
570                                 ".has('vnf-id', 'key1')"
571                                                 + ".has('aai-node-type', P.within('vce','vpe','generic-vnf'))";
572                                                 
573                 assertEquals(
574                                 "gremlin query should be " + expected,
575                                 expected,
576                                 query.getQueryBuilder().getQuery());
577                 assertEquals(
578                                 "parent gremlin query should be equal the query for port group",
579                                 expectedParent,
580                                 query.getQueryBuilder().getParentQuery().getQuery());
581                 assertEquals(
582                                 "result type should be vnf",
583                                 "vnf",
584                                 query.getResultType());
585                 
586     }
587         
588         /**
589          * Invalid node name.
590          *
591          * @throws JAXBException the JAXB exception
592          * @throws UnsupportedEncodingException the unsupported encoding exception
593          * @throws AAIException the AAI exception
594          */
595         @Test
596         public void invalidNodeName() throws JAXBException, UnsupportedEncodingException, AAIException {
597                 String content =
598                                 "{"
599                                 + "\"related-to\" : \"l3-interface-ipv4-address-list\","
600                                 + "\"relationship-data\" : [{"
601                                 + "\"relationship-key\" : \"generic-vnf.vnf-id\","
602                                 + "\"relationship-value\" : \"key1\""
603                                 + "},{"
604                                 + "\"relationship-key\" : \"lag-interface.interface-name\","
605                                 + "\"relationship-value\" : \"key2\""
606                                 + "},{"
607                                 + "\"relationship-key\" : \"l-infeaterface.interface-name\","
608                                 + "\"relationship-value\" : \"key3\""
609                                 + "},{"
610                                 + "\"relationship-key\" : \"vlan.vlan-interface\","
611                                 + "\"relationship-value\" : \"key4\""
612                                 + "},{"
613                                 + "\"relationship-key\" : \"l3-interface-ipv4-address-list.l3-interface-ipv4-address\","
614                                 + "\"relationship-value\" : \"key5\""
615                                 + "}]"
616                                 + "}";
617                 thrown.expect(AAIException.class);
618                 thrown.expectMessage(containsString("invalid object name"));
619                 
620                 Unmarshaller unmarshaller = context.createUnmarshaller();
621             unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
622             unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
623                 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
624                 Object obj = context.newDynamicEntity("Relationship");
625
626                 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
627                         
628                 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
629                 dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
630
631         }
632         
633         /**
634          * Invalid property name.
635          *
636          * @throws JAXBException the JAXB exception
637          * @throws UnsupportedEncodingException the unsupported encoding exception
638          * @throws AAIException the AAI exception
639          */
640         @Test
641         public void invalidPropertyName() throws JAXBException, UnsupportedEncodingException, AAIException {
642                 String content =
643                                 "{"
644                                 + "\"related-to\" : \"l3-interface-ipv4-address-list\","
645                                 + "\"relationship-data\" : [{"
646                                 + "\"relationship-key\" : \"generic-vnf.vnf-id\","
647                                 + "\"relationship-value\" : \"key1\""
648                                 + "},{"
649                                 + "\"relationship-key\" : \"lag-interface.intfdaferface-name\","
650                                 + "\"relationship-value\" : \"key2\""
651                                 + "},{"
652                                 + "\"relationship-key\" : \"l-interface.interface-name\","
653                                 + "\"relationship-value\" : \"key3\""
654                                 + "},{"
655                                 + "\"relationship-key\" : \"vlan.vlan-interface\","
656                                 + "\"relationship-value\" : \"key4\""
657                                 + "},{"
658                                 + "\"relationship-key\" : \"l3-interface-ipv4-address-list.l3-interface-ipv4-address\","
659                                 + "\"relationship-value\" : \"key5\""
660                                 + "}]"
661                                 + "}";
662                 thrown.expect(AAIException.class);
663                 thrown.expectMessage(containsString("invalid property name"));
664                 
665                 Unmarshaller unmarshaller = context.createUnmarshaller();
666             unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
667             unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
668                 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
669                 Object obj = context.newDynamicEntity("Relationship");
670
671                 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
672                         
673                 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
674                 dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
675
676         }
677 }