Migrate JAXB to jakarta
[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
21 package org.onap.aai.parsers.query;
22
23 import static org.hamcrest.CoreMatchers.containsString;
24 import static org.junit.Assert.assertEquals;
25
26 import java.io.StringReader;
27 import java.io.UnsupportedEncodingException;
28
29 import javax.xml.bind.JAXBException;
30 import javax.xml.bind.Unmarshaller;
31 import javax.xml.transform.stream.StreamSource;
32
33 import org.eclipse.persistence.dynamic.DynamicEntity;
34 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
35 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
36 import org.junit.Before;
37 import org.junit.Ignore;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.ExpectedException;
41 import org.onap.aai.AAISetup;
42 import org.onap.aai.exceptions.AAIException;
43 import org.onap.aai.introspection.Introspector;
44 import org.onap.aai.introspection.IntrospectorFactory;
45 import org.onap.aai.introspection.LoaderFactory;
46 import org.onap.aai.introspection.ModelType;
47 import org.onap.aai.nodes.NodeIngestor;
48 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
49 import org.onap.aai.serialization.engines.QueryStyle;
50 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
51 import org.onap.aai.setup.SchemaVersion;
52 import org.springframework.beans.factory.annotation.Autowired;
53
54 @Ignore
55 public class RelationshipGremlinQueryTest extends AAISetup {
56
57     @Autowired
58     private NodeIngestor injestor;
59     private TransactionalGraphEngine dbEngine;
60     private SchemaVersion version;
61     private DynamicJAXBContext context = injestor.getContextForVersion(version);
62
63     @Rule
64     public ExpectedException thrown = ExpectedException.none();
65
66     @Before
67     public void setup() {
68         version = new SchemaVersion("v10");
69         dbEngine = new JanusGraphDBEngine(QueryStyle.GREMLIN_TRAVERSAL,
70                 loaderFactory.createLoaderForVersion(ModelType.MOXY, version), false);
71     }
72
73     /**
74      * Parent query.
75      *
76      * @throws JAXBException the JAXB exception
77      * @throws UnsupportedEncodingException the unsupported encoding exception
78      * @throws AAIException the AAI exception
79      */
80     @Test
81     public void parentQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
82
83         String content = "{" + "\"related-to\" : \"pserver\"," + "\"relationship-data\" : [{"
84                 + "\"relationship-key\" : \"pserver.hostname\"," + "\"relationship-value\" : \"key1\"" + "}]" + "}";
85
86         Unmarshaller unmarshaller = context.createUnmarshaller();
87         unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
88         unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
89         unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
90         Object obj = context.newDynamicEntity("Relationship");
91
92         DynamicEntity entity = (DynamicEntity) unmarshaller
93                 .unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
94
95         Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
96         QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
97
98         String expected = ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
99         assertEquals("gremlin query should be " + expected, expected, query.getQueryBuilder().getQuery());
100         assertEquals("parent gremlin query should be equal to normal query", expected,
101                 query.getQueryBuilder().getParentQuery().getQuery());
102         assertEquals("result type should be pserver", "pserver", 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 = "{" + "\"related-to\" : \"lag-interface\"," + "\"relationship-data\" : [{"
116                 + "\"relationship-key\" : \"pserver.hostname\"," + "\"relationship-value\" : \"key1\"" + "}, {"
117                 + "\"relationship-key\" : \"lag-interface.interface-name\"," + "\"relationship-value\" : \"key2\""
118                 + "}]" + "}";
119
120         Unmarshaller unmarshaller = context.createUnmarshaller();
121         unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
122         unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
123         unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
124         Object obj = context.newDynamicEntity("Relationship");
125
126         DynamicEntity entity = (DynamicEntity) unmarshaller
127                 .unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
128
129         Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
130         QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
131
132         String expected =
133                 ".has('hostname', 'key1').has('aai-node-type', 'pserver').in('tosca.relationships.BindsTo').has('aai-node-type', 'lag-interface')"
134                         + ".has('interface-name', 'key2')";
135         String parentExpected = ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
136         assertEquals("gremlin query should be for node", expected, query.getQueryBuilder().getQuery());
137         assertEquals("parent gremlin query should be for parent", parentExpected,
138                 query.getQueryBuilder().getParentQuery().getQuery());
139         assertEquals("result type should be lag-interface", "lag-interface", query.getResultType());
140     }
141
142     /**
143      * Naming exceptions.
144      *
145      * @throws JAXBException the JAXB exception
146      * @throws UnsupportedEncodingException the unsupported encoding exception
147      * @throws AAIException the AAI exception
148      */
149     @Test
150     public void namingExceptions() throws JAXBException, UnsupportedEncodingException, AAIException {
151         String content = "{" + "\"related-to\" : \"cvlan-tag\"," + "\"relationship-data\" : [{"
152                 + "\"relationship-key\" : \"vce.vnf-id\"," + "\"relationship-value\" : \"key1\"" + "}, {"
153                 + "\"relationship-key\" : \"port-group.interface-id\"," + "\"relationship-value\" : \"key2\"" + "},{"
154                 + "\"relationship-key\" : \"cvlan-tag.cvlan-tag\"," + "\"relationship-value\" : \"655\"" + "}]" + "}";
155
156         Unmarshaller unmarshaller = context.createUnmarshaller();
157         unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
158         unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
159         unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
160         Object obj = context.newDynamicEntity("Relationship");
161
162         DynamicEntity entity = (DynamicEntity) unmarshaller
163                 .unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
164
165         Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
166         QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
167         String expected = ".has('vnf-id', 'key1').has('aai-node-type', 'vce')"
168                 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'port-group')"
169                 + ".has('interface-id', 'key2').in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'cvlan-tag')"
170                 + ".has('cvlan-tag', 655)";
171         String expectedParent = ".has('vnf-id', 'key1').has('aai-node-type', 'vce')"
172                 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'port-group')"
173                 + ".has('interface-id', 'key2')";
174         assertEquals("gremlin query should be " + expected, expected, query.getQueryBuilder().getQuery());
175         assertEquals("parent gremlin query should be equal the query for port group", expectedParent,
176                 query.getQueryBuilder().getParentQuery().getQuery());
177         assertEquals("result type should be cvlan-tag", "cvlan-tag", query.getResultType());
178
179     }
180
181     /**
182      * Scrambled relationship.
183      *
184      * @throws JAXBException the JAXB exception
185      * @throws UnsupportedEncodingException the unsupported encoding exception
186      * @throws AAIException the AAI exception
187      */
188     @Test
189     public void scrambledRelationship() throws JAXBException, UnsupportedEncodingException, AAIException {
190         String content = "{" + "\"related-to\" : \"l3-interface-ipv4-address-list\"," + "\"relationship-data\" : [{"
191                 + "\"relationship-key\" : \"l3-interface-ipv4-address-list.l3-interface-ipv4-address\","
192                 + "\"relationship-value\" : \"key5\"" + "},{"
193                 + "\"relationship-key\" : \"lag-interface.interface-name\"," + "\"relationship-value\" : \"key2\""
194                 + "},{" + "\"relationship-key\" : \"l-interface.interface-name\"," + "\"relationship-value\" : \"key3\""
195                 + "},{" + "\"relationship-key\" : \"vlan.vlan-interface\"," + "\"relationship-value\" : \"key4\""
196                 + "},{" + "\"relationship-key\" : \"generic-vnf.vnf-id\"," + "\"relationship-value\" : \"key1\"" + "}]"
197                 + "}";
198         scrambledRelationshipSpec(content);
199     }
200
201     /**
202      * Reversed relationship.
203      *
204      * @throws JAXBException the JAXB exception
205      * @throws UnsupportedEncodingException the unsupported encoding exception
206      * @throws AAIException the AAI exception
207      */
208     @Test
209     public void reversedRelationship() throws JAXBException, UnsupportedEncodingException, AAIException {
210         String content = "{" + "\"related-to\" : \"l3-interface-ipv4-address-list\"," + "\"relationship-data\" : [{"
211                 + "\"relationship-key\" : \"l3-interface-ipv4-address-list.l3-interface-ipv4-address\","
212                 + "\"relationship-value\" : \"key5\"" + "},{" + "\"relationship-key\" : \"vlan.vlan-interface\","
213                 + "\"relationship-value\" : \"key4\"" + "},{" + "\"relationship-key\" : \"l-interface.interface-name\","
214                 + "\"relationship-value\" : \"key3\"" + "},{"
215                 + "\"relationship-key\" : \"lag-interface.interface-name\"," + "\"relationship-value\" : \"key2\""
216                 + "},{" + "\"relationship-key\" : \"generic-vnf.vnf-id\"," + "\"relationship-value\" : \"key1\"" + "}]"
217                 + "}";
218         scrambledRelationshipSpec(content);
219     }
220
221     /**
222      * Ordered ambiguous relationship.
223      *
224      * @throws JAXBException the JAXB exception
225      * @throws UnsupportedEncodingException the unsupported encoding exception
226      * @throws AAIException the AAI exception
227      */
228     @Test
229     public void orderedAmbiguousRelationship() throws JAXBException, UnsupportedEncodingException, AAIException {
230         String content = "{" + "\"related-to\" : \"l3-interface-ipv4-address-list\"," + "\"relationship-data\" : [{"
231                 + "\"relationship-key\" : \"generic-vnf.vnf-id\"," + "\"relationship-value\" : \"key1\"" + "},{"
232                 + "\"relationship-key\" : \"lag-interface.interface-name\"," + "\"relationship-value\" : \"key2\""
233                 + "},{" + "\"relationship-key\" : \"l-interface.interface-name\"," + "\"relationship-value\" : \"key3\""
234                 + "},{" + "\"relationship-key\" : \"vlan.vlan-interface\"," + "\"relationship-value\" : \"key4\""
235                 + "},{" + "\"relationship-key\" : \"l3-interface-ipv4-address-list.l3-interface-ipv4-address\","
236                 + "\"relationship-value\" : \"key5\"" + "}]" + "}";
237         scrambledRelationshipSpec(content);
238     }
239
240     /**
241      * Scrambled relationship spec.
242      *
243      * @param content the content
244      * @throws JAXBException the JAXB exception
245      * @throws UnsupportedEncodingException the unsupported encoding exception
246      * @throws AAIException the AAI exception
247      */
248     public void scrambledRelationshipSpec(String content)
249             throws JAXBException, UnsupportedEncodingException, AAIException {
250
251         Unmarshaller unmarshaller = context.createUnmarshaller();
252         unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
253         unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
254         unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
255         Object obj = context.newDynamicEntity("Relationship");
256
257         DynamicEntity entity = (DynamicEntity) unmarshaller
258                 .unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
259
260         Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
261         QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
262         String expected = ".has('vnf-id', 'key1').has('aai-node-type', 'generic-vnf')"
263                 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'lag-interface')"
264                 + ".has('interface-name', 'key2').in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'l-interface')"
265                 + ".has('interface-name', 'key3').out('tosca.relationships.LinksTo').has('aai-node-type', 'vlan')"
266                 + ".has('vlan-interface', 'key4').in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'l3-interface-ipv4-address-list')"
267                 + ".has('l3-interface-ipv4-address', 'key5')";
268         String expectedParent = ".has('vnf-id', 'key1').has('aai-node-type', 'generic-vnf')"
269                 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'lag-interface')"
270                 + ".has('interface-name', 'key2').in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'l-interface')"
271                 + ".has('interface-name', 'key3').out('tosca.relationships.LinksTo').has('aai-node-type', 'vlan')"
272                 + ".has('vlan-interface', 'key4')";
273         assertEquals("gremlin query should be " + expected, expected, query.getQueryBuilder().getQuery());
274         assertEquals("parent gremlin query should be equal the query for vlan", expectedParent,
275                 query.getQueryBuilder().getParentQuery().getQuery());
276         assertEquals("result type should be l3-interface-ipv4-address-list", "l3-interface-ipv4-address-list",
277                 query.getResultType());
278
279     }
280
281     /**
282      * Short circuit.
283      *
284      * @throws JAXBException the JAXB exception
285      * @throws UnsupportedEncodingException the unsupported encoding exception
286      * @throws AAIException the AAI exception
287      */
288     @Test
289     public void shortCircuit() throws JAXBException, UnsupportedEncodingException, AAIException {
290         String content = "{" + "\"related-to\" : \"cvlan-tag\","
291                 + "\"related-link\" : \"http://mock-system-name.com:8443/aai/v6/network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655\","
292                 + "\"relationship-data\" : [{" + "\"relationship-key\" : \"vce.vnf-id\","
293                 + "\"relationship-value\" : \"key1\"" + "}, {" + "\"relationship-key\" : \"port-group.interface-id\","
294                 + "\"relationship-value\" : \"key2\"" + "},{" + "\"relationship-key\" : \"cvlan-tag.cvlan-tag\","
295                 + "\"relationship-value\" : \"655\"" + "}]" + "}";
296
297         Unmarshaller unmarshaller = context.createUnmarshaller();
298         unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
299         unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
300         unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
301         Object obj = context.newDynamicEntity("Relationship");
302
303         DynamicEntity entity = (DynamicEntity) unmarshaller
304                 .unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
305
306         Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
307         QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
308         String expected = ".has('vnf-id', 'key1').has('aai-node-type', 'vce')"
309                 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'port-group')"
310                 + ".has('interface-id', 'key2').in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'cvlan-tag')"
311                 + ".has('cvlan-tag', 655)";
312         String expectedParent = ".has('vnf-id', 'key1').has('aai-node-type', 'vce')"
313                 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'port-group')"
314                 + ".has('interface-id', 'key2')";
315         assertEquals("gremlin query should be " + expected, expected, query.getQueryBuilder().getQuery());
316         assertEquals("parent gremlin query should be equal the query for port group", expectedParent,
317                 query.getQueryBuilder().getParentQuery().getQuery());
318         assertEquals("result type should be cvlan-tag", "cvlan-tag", query.getResultType());
319
320     }
321
322     @Test
323     public void shorterCircuit() throws JAXBException, UnsupportedEncodingException, AAIException {
324         String content = "{" + "\"related-to\" : \"cvlan-tag\","
325                 + "\"related-link\" : \"file:///network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655\""
326                 + "}";
327
328         Unmarshaller unmarshaller = context.createUnmarshaller();
329         unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
330         unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
331         unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
332         Object obj = context.newDynamicEntity("Relationship");
333
334         DynamicEntity entity = (DynamicEntity) unmarshaller
335                 .unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
336
337         Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
338         QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
339         String expected = ".has('vnf-id', 'key1').has('aai-node-type', 'vce')"
340                 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'port-group')"
341                 + ".has('interface-id', 'key2').in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'cvlan-tag')"
342                 + ".has('cvlan-tag', 655)";
343         String expectedParent = ".has('vnf-id', 'key1').has('aai-node-type', 'vce')"
344                 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'port-group')"
345                 + ".has('interface-id', 'key2')";
346         assertEquals("gremlin query should be " + expected, expected, query.getQueryBuilder().getQuery());
347         assertEquals("parent gremlin query should be equal the query for port group", expectedParent,
348                 query.getQueryBuilder().getParentQuery().getQuery());
349         assertEquals("result type should be cvlan-tag", "cvlan-tag", query.getResultType());
350
351     }
352
353     /**
354      * Double key.
355      *
356      * @throws JAXBException the JAXB exception
357      * @throws UnsupportedEncodingException the unsupported encoding exception
358      * @throws AAIException the AAI exception
359      */
360     @Test
361     public void doubleKey() throws JAXBException, UnsupportedEncodingException, AAIException {
362         String content = "{" + "\"related-to\" : \"ctag-pool\"," + "\"relationship-data\" : [{"
363                 + "\"relationship-key\" : \"complex.physical-location-id\"," + "\"relationship-value\" : \"key1\""
364                 + " }, { " + "\"relationship-key\" : \"ctag-pool.target-pe\"," + " \"relationship-value\" : \"key2\""
365                 + " },{" + "\"relationship-key\" : \"ctag-pool.availability-zone-name\","
366                 + "\"relationship-value\" : \"key3\"" + "}]" + "}";
367
368         Unmarshaller unmarshaller = context.createUnmarshaller();
369         unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
370         unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
371         unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
372         Object obj = context.newDynamicEntity("Relationship");
373
374         DynamicEntity entity = (DynamicEntity) unmarshaller
375                 .unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
376
377         Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
378         QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
379
380         String expected = ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')"
381                 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'ctag-pool')"
382                 + ".has('target-pe', 'key2')" + ".has('availability-zone-name', 'key3')";
383         String expectedParent = ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')";
384
385         assertEquals("gremlin query should be " + expected, expected, query.getQueryBuilder().getQuery());
386         assertEquals("parent gremlin query should be equal the query for port group", expectedParent,
387                 query.getQueryBuilder().getParentQuery().getQuery());
388         assertEquals("result type should be ctag-pool", "ctag-pool", query.getResultType());
389
390     }
391
392     /**
393      * Abstract type.
394      *
395      * @throws JAXBException the JAXB exception
396      * @throws UnsupportedEncodingException the unsupported encoding exception
397      * @throws AAIException the AAI exception
398      */
399     @Test
400     public void abstractType() throws JAXBException, UnsupportedEncodingException, AAIException {
401         String content = "{" + "\"related-to\" : \"vnf\"," + "\"relationship-data\" : [{"
402                 + "\"relationship-key\" : \"vnf.vnf-id\"," + "\"relationship-value\" : \"key1\"" + " }]" + "}";
403
404         Unmarshaller unmarshaller = context.createUnmarshaller();
405         unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
406         unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
407         unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
408         Object obj = context.newDynamicEntity("Relationship");
409
410         DynamicEntity entity = (DynamicEntity) unmarshaller
411                 .unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
412
413         Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
414         QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
415
416         String expected = ".has('vnf-id', 'key1')" + ".has('aai-node-type', P.within('vce','generic-vnf'))";
417
418         String expectedParent = ".has('vnf-id', 'key1')" + ".has('aai-node-type', P.within('vce','generic-vnf'))";
419
420         assertEquals("gremlin query should be " + expected, expected, query.getQueryBuilder().getQuery());
421         assertEquals("parent gremlin query should be equal the query for port group", expectedParent,
422                 query.getQueryBuilder().getParentQuery().getQuery());
423         assertEquals("result type should be vnf", "vnf", query.getResultType());
424
425     }
426
427     /**
428      * Invalid node name.
429      *
430      * @throws JAXBException the JAXB exception
431      * @throws UnsupportedEncodingException the unsupported encoding exception
432      * @throws AAIException the AAI exception
433      */
434     @Test
435     public void invalidNodeName() throws JAXBException, UnsupportedEncodingException, AAIException {
436         String content = "{" + "\"related-to\" : \"l3-interface-ipv4-address-list\"," + "\"relationship-data\" : [{"
437                 + "\"relationship-key\" : \"generic-vnf.vnf-id\"," + "\"relationship-value\" : \"key1\"" + "},{"
438                 + "\"relationship-key\" : \"lag-interface.interface-name\"," + "\"relationship-value\" : \"key2\""
439                 + "},{" + "\"relationship-key\" : \"l-infeaterface.interface-name\","
440                 + "\"relationship-value\" : \"key3\"" + "},{" + "\"relationship-key\" : \"vlan.vlan-interface\","
441                 + "\"relationship-value\" : \"key4\"" + "},{"
442                 + "\"relationship-key\" : \"l3-interface-ipv4-address-list.l3-interface-ipv4-address\","
443                 + "\"relationship-value\" : \"key5\"" + "}]" + "}";
444         thrown.expect(AAIException.class);
445         thrown.expectMessage(containsString("invalid object name"));
446
447         Unmarshaller unmarshaller = context.createUnmarshaller();
448         unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
449         unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
450         unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
451         Object obj = context.newDynamicEntity("Relationship");
452
453         DynamicEntity entity = (DynamicEntity) unmarshaller
454                 .unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
455
456         Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
457         dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
458
459     }
460
461     /**
462      * Invalid property name.
463      *
464      * @throws JAXBException the JAXB exception
465      * @throws UnsupportedEncodingException the unsupported encoding exception
466      * @throws AAIException the AAI exception
467      */
468     @Test
469     public void invalidPropertyName() throws JAXBException, UnsupportedEncodingException, AAIException {
470         String content = "{" + "\"related-to\" : \"l3-interface-ipv4-address-list\"," + "\"relationship-data\" : [{"
471                 + "\"relationship-key\" : \"generic-vnf.vnf-id\"," + "\"relationship-value\" : \"key1\"" + "},{"
472                 + "\"relationship-key\" : \"lag-interface.intfdaferface-name\"," + "\"relationship-value\" : \"key2\""
473                 + "},{" + "\"relationship-key\" : \"l-interface.interface-name\"," + "\"relationship-value\" : \"key3\""
474                 + "},{" + "\"relationship-key\" : \"vlan.vlan-interface\"," + "\"relationship-value\" : \"key4\""
475                 + "},{" + "\"relationship-key\" : \"l3-interface-ipv4-address-list.l3-interface-ipv4-address\","
476                 + "\"relationship-value\" : \"key5\"" + "}]" + "}";
477         thrown.expect(AAIException.class);
478         thrown.expectMessage(containsString("invalid property name"));
479
480         Unmarshaller unmarshaller = context.createUnmarshaller();
481         unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
482         unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
483         unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
484         Object obj = context.newDynamicEntity("Relationship");
485
486         DynamicEntity entity = (DynamicEntity) unmarshaller
487                 .unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
488
489         Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
490         dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
491
492     }
493 }