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