2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.parsers.query;
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.Test;
27 import org.onap.aai.AAISetup;
28 import org.onap.aai.exceptions.AAIException;
29 import org.onap.aai.introspection.*;
30 import org.onap.aai.serialization.engines.QueryStyle;
31 import org.onap.aai.serialization.engines.TitanDBEngine;
32 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
34 import javax.xml.bind.JAXBException;
35 import javax.xml.bind.Unmarshaller;
36 import javax.xml.transform.stream.StreamSource;
37 import java.io.StringReader;
38 import java.io.UnsupportedEncodingException;
40 import static org.junit.Assert.assertEquals;
43 public class RelationshipQueryTest extends AAISetup {
45 private ModelInjestor injestor = ModelInjestor.getInstance();
47 private TransactionalGraphEngine dbEngine =
48 new TitanDBEngine(QueryStyle.GREMLIN_TRAVERSAL,
49 LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v8),
51 private final Version version = Version.v8;
52 private DynamicJAXBContext context = injestor.getContextForVersion(version);
57 * @throws JAXBException the JAXB exception
58 * @throws UnsupportedEncodingException the unsupported encoding exception
59 * @throws AAIException the AAI exception
62 public void parentQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
66 + "\"related-to\" : \"pserver\","
67 + "\"relationship-data\" : [{"
68 + "\"relationship-key\" : \"pserver.hostname\","
69 + "\"relationship-value\" : \"key1\""
73 Unmarshaller unmarshaller = context.createUnmarshaller();
74 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
75 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
76 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
77 Object obj = context.newDynamicEntity("Relationship");
79 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
81 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
82 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
85 ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
87 "gremlin query should be " + expected,
89 query.getQueryBuilder().getQuery());
91 "parent gremlin query should be equal to normal query",
93 query.getQueryBuilder().getParentQuery().getQuery());
95 "result type should be pserver",
97 query.getResultType());
104 * @throws JAXBException the JAXB exception
105 * @throws UnsupportedEncodingException the unsupported encoding exception
106 * @throws AAIException the AAI exception
110 public void childQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
113 + "\"related-to\" : \"lag-interface\","
114 + "\"relationship-data\" : [{"
115 + "\"relationship-key\" : \"pserver.hostname\","
116 + "\"relationship-value\" : \"key1\""
118 + "\"relationship-key\" : \"lag-interface.interface-name\","
119 + "\"relationship-value\" : \"key2\""
123 Unmarshaller unmarshaller = context.createUnmarshaller();
124 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
125 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
126 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
127 Object obj = context.newDynamicEntity("Relationship");
129 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
131 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
132 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
134 ".has('hostname', 'key1').has('aai-node-type', 'pserver').out('hasLAGInterface').has('aai-node-type', 'lag-interface')"
135 + ".has('interface-name', 'key2')";
136 String parentExpected =
137 ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
139 "gremlin query should be for node",
141 query.getQueryBuilder().getQuery());
143 "parent gremlin query should be for parent",
145 query.getQueryBuilder().getParentQuery().getQuery());
147 "result type should be lag-interface",
149 query.getResultType());
155 * @throws JAXBException the JAXB exception
156 * @throws UnsupportedEncodingException the unsupported encoding exception
157 * @throws AAIException the AAI exception
161 public void namingExceptions() throws JAXBException, UnsupportedEncodingException, AAIException {
164 + "\"related-to\" : \"cvlan-tag\","
165 + "\"relationship-data\" : [{"
166 + "\"relationship-key\" : \"vce.vnf-id\","
167 + "\"relationship-value\" : \"key1\""
169 + "\"relationship-key\" : \"port-group.interface-id\","
170 + "\"relationship-value\" : \"key2\""
172 + "\"relationship-key\" : \"cvlan-tag.cvlan-tag\","
173 + "\"relationship-value\" : \"655\""
177 Unmarshaller unmarshaller = context.createUnmarshaller();
178 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
179 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
180 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
181 Object obj = context.newDynamicEntity("Relationship");
183 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
185 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
186 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
188 ".has('vnf-id', 'key1').has('aai-node-type', 'vce').in('org.onap.relationships.inventory.BelongsTo')"
189 + ".has('aai-node-type', 'port-group').has('interface-id', 'key2').in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'cvlan-tag')"
190 + ".has('cvlan-tag', 655)";
191 String expectedParent =
192 ".has('vnf-id', 'key1').has('aai-node-type', 'vce').in('org.onap.relationships.inventory.BelongsTo')"
193 + ".has('aai-node-type', 'port-group').has('interface-id', 'key2')";
196 "gremlin query should be " + expected,
198 query.getQueryBuilder().getQuery());
200 "parent gremlin query should be equal the query for port group",
202 query.getQueryBuilder().getParentQuery().getQuery());
204 "result type should be cvlan-tag",
206 query.getResultType());
213 * @throws JAXBException the JAXB exception
214 * @throws UnsupportedEncodingException the unsupported encoding exception
215 * @throws AAIException the AAI exception
219 public void doubleKey() throws JAXBException, UnsupportedEncodingException, AAIException {
222 + "\"related-to\" : \"ctag-pool\","
223 + "\"relationship-data\" : [{"
224 + "\"relationship-key\" : \"complex.physical-location-id\","
225 + "\"relationship-value\" : \"key1\""
227 + "\"relationship-key\" : \"ctag-pool.target-pe\","
228 + " \"relationship-value\" : \"key2\""
230 + "\"relationship-key\" : \"ctag-pool.availability-zone-name\","
231 + "\"relationship-value\" : \"key3\""
236 Unmarshaller unmarshaller = context.createUnmarshaller();
237 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
238 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
239 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
240 Object obj = context.newDynamicEntity("Relationship");
242 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
244 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
245 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
248 ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')"
249 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'ctag-pool')"
250 + ".has('target-pe', 'key2')"
251 + ".has('availability-zone-name', 'key3')";
252 String expectedParent =
253 ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')";
256 "gremlin query should be " + expected,
258 query.getQueryBuilder().getQuery());
260 "parent gremlin query should be equal the query for port group",
262 query.getQueryBuilder().getParentQuery().getQuery());
264 "result type should be ctag-pool",
266 query.getResultType());