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 static org.junit.Assert.assertEquals;
25 import java.io.StringReader;
26 import java.io.UnsupportedEncodingException;
28 import javax.xml.bind.JAXBException;
29 import javax.xml.bind.Unmarshaller;
30 import javax.xml.transform.stream.StreamSource;
32 import org.eclipse.persistence.dynamic.DynamicEntity;
33 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
34 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
35 import org.junit.BeforeClass;
36 import org.junit.Ignore;
37 import org.junit.Test;
39 import org.openecomp.aai.exceptions.AAIException;
40 import org.openecomp.aai.introspection.Introspector;
41 import org.openecomp.aai.introspection.IntrospectorFactory;
42 import org.openecomp.aai.introspection.LoaderFactory;
43 import org.openecomp.aai.introspection.ModelInjestor;
44 import org.openecomp.aai.introspection.ModelType;
45 import org.openecomp.aai.introspection.Version;
46 import org.openecomp.aai.serialization.engines.QueryStyle;
47 import org.openecomp.aai.serialization.engines.TitanDBEngine;
48 import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
50 public class RelationshipQueryTest {
51 private ModelInjestor injestor = ModelInjestor.getInstance();
53 private TransactionalGraphEngine dbEngine =
54 new TitanDBEngine(QueryStyle.GREMLIN_TRAVERSAL,
55 LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v8),
57 private final Version version = Version.v8;
58 private DynamicJAXBContext context = injestor.getContextForVersion(version);
64 public static void configure() {
65 System.setProperty("AJSC_HOME", ".");
66 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
73 * @throws JAXBException the JAXB exception
74 * @throws UnsupportedEncodingException the unsupported encoding exception
75 * @throws AAIException the AAI exception
78 public void parentQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
82 + "\"related-to\" : \"pserver\","
83 + "\"relationship-data\" : [{"
84 + "\"relationship-key\" : \"pserver.hostname\","
85 + "\"relationship-value\" : \"key1\""
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");
95 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
97 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
98 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
101 ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
103 "gremlin query should be " + expected,
105 query.getQueryBuilder().getQuery());
107 "parent gremlin query should be equal to normal query",
109 query.getQueryBuilder().getParentQuery().getQuery());
111 "result type should be pserver",
113 query.getResultType());
120 * @throws JAXBException the JAXB exception
121 * @throws UnsupportedEncodingException the unsupported encoding exception
122 * @throws AAIException the AAI exception
126 public void childQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
129 + "\"related-to\" : \"lag-interface\","
130 + "\"relationship-data\" : [{"
131 + "\"relationship-key\" : \"pserver.hostname\","
132 + "\"relationship-value\" : \"key1\""
134 + "\"relationship-key\" : \"lag-interface.interface-name\","
135 + "\"relationship-value\" : \"key2\""
139 Unmarshaller unmarshaller = context.createUnmarshaller();
140 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
141 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
142 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
143 Object obj = context.newDynamicEntity("Relationship");
145 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
147 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
148 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
150 ".has('hostname', 'key1').has('aai-node-type', 'pserver').out('hasLAGInterface').has('aai-node-type', 'lag-interface')"
151 + ".has('interface-name', 'key2')";
152 String parentExpected =
153 ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
155 "gremlin query should be for node",
157 query.getQueryBuilder().getQuery());
159 "parent gremlin query should be for parent",
161 query.getQueryBuilder().getParentQuery().getQuery());
163 "result type should be lag-interface",
165 query.getResultType());
171 * @throws JAXBException the JAXB exception
172 * @throws UnsupportedEncodingException the unsupported encoding exception
173 * @throws AAIException the AAI exception
177 public void namingExceptions() throws JAXBException, UnsupportedEncodingException, AAIException {
180 + "\"related-to\" : \"cvlan-tag\","
181 + "\"relationship-data\" : [{"
182 + "\"relationship-key\" : \"vce.vnf-id\","
183 + "\"relationship-value\" : \"key1\""
185 + "\"relationship-key\" : \"port-group.interface-id\","
186 + "\"relationship-value\" : \"key2\""
188 + "\"relationship-key\" : \"cvlan-tag.cvlan-tag\","
189 + "\"relationship-value\" : \"655\""
193 Unmarshaller unmarshaller = context.createUnmarshaller();
194 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
195 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
196 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
197 Object obj = context.newDynamicEntity("Relationship");
199 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
201 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
202 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
204 ".has('vnf-id', 'key1').has('aai-node-type', 'vce').in('org.onap.relationships.inventory.BelongsTo')"
205 + ".has('aai-node-type', 'port-group').has('interface-id', 'key2').in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'cvlan-tag')"
206 + ".has('cvlan-tag', 655)";
207 String expectedParent =
208 ".has('vnf-id', 'key1').has('aai-node-type', 'vce').in('org.onap.relationships.inventory.BelongsTo')"
209 + ".has('aai-node-type', 'port-group').has('interface-id', 'key2')";
212 "gremlin query should be " + expected,
214 query.getQueryBuilder().getQuery());
216 "parent gremlin query should be equal the query for port group",
218 query.getQueryBuilder().getParentQuery().getQuery());
220 "result type should be cvlan-tag",
222 query.getResultType());
229 * @throws JAXBException the JAXB exception
230 * @throws UnsupportedEncodingException the unsupported encoding exception
231 * @throws AAIException the AAI exception
235 public void doubleKey() throws JAXBException, UnsupportedEncodingException, AAIException {
238 + "\"related-to\" : \"ctag-pool\","
239 + "\"relationship-data\" : [{"
240 + "\"relationship-key\" : \"complex.physical-location-id\","
241 + "\"relationship-value\" : \"key1\""
243 + "\"relationship-key\" : \"ctag-pool.target-pe\","
244 + " \"relationship-value\" : \"key2\""
246 + "\"relationship-key\" : \"ctag-pool.availability-zone-name\","
247 + "\"relationship-value\" : \"key3\""
252 Unmarshaller unmarshaller = context.createUnmarshaller();
253 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
254 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
255 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
256 Object obj = context.newDynamicEntity("Relationship");
258 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
260 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
261 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
264 ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')"
265 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'ctag-pool')"
266 + ".has('target-pe', 'key2')"
267 + ".has('availability-zone-name', 'key3')";
268 String expectedParent =
269 ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')";
272 "gremlin query should be " + expected,
274 query.getQueryBuilder().getQuery());
276 "parent gremlin query should be equal the query for port group",
278 query.getQueryBuilder().getParentQuery().getQuery());
280 "result type should be ctag-pool",
282 query.getResultType());