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