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