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