2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
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=========================================================
21 package org.openecomp.aai.parsers.query;
23 import org.eclipse.persistence.dynamic.DynamicEntity;
24 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
25 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
26 import org.junit.Ignore;
27 import org.junit.Test;
28 import org.openecomp.aai.AAISetup;
29 import org.openecomp.aai.exceptions.AAIException;
30 import org.openecomp.aai.introspection.*;
31 import org.openecomp.aai.serialization.engines.QueryStyle;
32 import org.openecomp.aai.serialization.engines.TitanDBEngine;
33 import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
35 import javax.xml.bind.JAXBException;
36 import javax.xml.bind.Unmarshaller;
37 import javax.xml.transform.stream.StreamSource;
38 import java.io.StringReader;
39 import java.io.UnsupportedEncodingException;
41 import static org.junit.Assert.assertEquals;
44 public class RelationshipQueryTest extends AAISetup {
46 private ModelInjestor injestor = ModelInjestor.getInstance();
48 private TransactionalGraphEngine dbEngine =
49 new TitanDBEngine(QueryStyle.GREMLIN_TRAVERSAL,
50 LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v8),
52 private final Version version = Version.v8;
53 private DynamicJAXBContext context = injestor.getContextForVersion(version);
58 * @throws JAXBException the JAXB exception
59 * @throws UnsupportedEncodingException the unsupported encoding exception
60 * @throws AAIException the AAI exception
63 public void parentQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
67 + "\"related-to\" : \"pserver\","
68 + "\"relationship-data\" : [{"
69 + "\"relationship-key\" : \"pserver.hostname\","
70 + "\"relationship-value\" : \"key1\""
74 Unmarshaller unmarshaller = context.createUnmarshaller();
75 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
76 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
77 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
78 Object obj = context.newDynamicEntity("Relationship");
80 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
82 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
83 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
86 ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
88 "gremlin query should be " + expected,
90 query.getQueryBuilder().getQuery());
92 "parent gremlin query should be equal to normal query",
94 query.getQueryBuilder().getParentQuery().getQuery());
96 "result type should be pserver",
98 query.getResultType());
105 * @throws JAXBException the JAXB exception
106 * @throws UnsupportedEncodingException the unsupported encoding exception
107 * @throws AAIException the AAI exception
111 public void childQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
114 + "\"related-to\" : \"lag-interface\","
115 + "\"relationship-data\" : [{"
116 + "\"relationship-key\" : \"pserver.hostname\","
117 + "\"relationship-value\" : \"key1\""
119 + "\"relationship-key\" : \"lag-interface.interface-name\","
120 + "\"relationship-value\" : \"key2\""
124 Unmarshaller unmarshaller = context.createUnmarshaller();
125 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
126 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
127 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
128 Object obj = context.newDynamicEntity("Relationship");
130 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
132 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
133 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
135 ".has('hostname', 'key1').has('aai-node-type', 'pserver').out('hasLAGInterface').has('aai-node-type', 'lag-interface')"
136 + ".has('interface-name', 'key2')";
137 String parentExpected =
138 ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
140 "gremlin query should be for node",
142 query.getQueryBuilder().getQuery());
144 "parent gremlin query should be for parent",
146 query.getQueryBuilder().getParentQuery().getQuery());
148 "result type should be lag-interface",
150 query.getResultType());
156 * @throws JAXBException the JAXB exception
157 * @throws UnsupportedEncodingException the unsupported encoding exception
158 * @throws AAIException the AAI exception
162 public void namingExceptions() throws JAXBException, UnsupportedEncodingException, AAIException {
165 + "\"related-to\" : \"cvlan-tag\","
166 + "\"relationship-data\" : [{"
167 + "\"relationship-key\" : \"vce.vnf-id\","
168 + "\"relationship-value\" : \"key1\""
170 + "\"relationship-key\" : \"port-group.interface-id\","
171 + "\"relationship-value\" : \"key2\""
173 + "\"relationship-key\" : \"cvlan-tag.cvlan-tag\","
174 + "\"relationship-value\" : \"655\""
178 Unmarshaller unmarshaller = context.createUnmarshaller();
179 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
180 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
181 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
182 Object obj = context.newDynamicEntity("Relationship");
184 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
186 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
187 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
189 ".has('vnf-id', 'key1').has('aai-node-type', 'vce').in('org.onap.relationships.inventory.BelongsTo')"
190 + ".has('aai-node-type', 'port-group').has('interface-id', 'key2').in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'cvlan-tag')"
191 + ".has('cvlan-tag', 655)";
192 String expectedParent =
193 ".has('vnf-id', 'key1').has('aai-node-type', 'vce').in('org.onap.relationships.inventory.BelongsTo')"
194 + ".has('aai-node-type', 'port-group').has('interface-id', 'key2')";
197 "gremlin query should be " + expected,
199 query.getQueryBuilder().getQuery());
201 "parent gremlin query should be equal the query for port group",
203 query.getQueryBuilder().getParentQuery().getQuery());
205 "result type should be cvlan-tag",
207 query.getResultType());
214 * @throws JAXBException the JAXB exception
215 * @throws UnsupportedEncodingException the unsupported encoding exception
216 * @throws AAIException the AAI exception
220 public void doubleKey() throws JAXBException, UnsupportedEncodingException, AAIException {
223 + "\"related-to\" : \"ctag-pool\","
224 + "\"relationship-data\" : [{"
225 + "\"relationship-key\" : \"complex.physical-location-id\","
226 + "\"relationship-value\" : \"key1\""
228 + "\"relationship-key\" : \"ctag-pool.target-pe\","
229 + " \"relationship-value\" : \"key2\""
231 + "\"relationship-key\" : \"ctag-pool.availability-zone-name\","
232 + "\"relationship-value\" : \"key3\""
237 Unmarshaller unmarshaller = context.createUnmarshaller();
238 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
239 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
240 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
241 Object obj = context.newDynamicEntity("Relationship");
243 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
245 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
246 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
249 ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')"
250 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'ctag-pool')"
251 + ".has('target-pe', 'key2')"
252 + ".has('availability-zone-name', 'key3')";
253 String expectedParent =
254 ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')";
257 "gremlin query should be " + expected,
259 query.getQueryBuilder().getQuery());
261 "parent gremlin query should be equal the query for port group",
263 query.getQueryBuilder().getParentQuery().getQuery());
265 "result type should be ctag-pool",
267 query.getResultType());