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", "./src/test/resources/");
66 System.setProperty("BUNDLECONFIG_DIR", "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
125 public void childQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
128 + "\"related-to\" : \"lag-interface\","
129 + "\"relationship-data\" : [{"
130 + "\"relationship-key\" : \"pserver.hostname\","
131 + "\"relationship-value\" : \"key1\""
133 + "\"relationship-key\" : \"lag-interface.interface-name\","
134 + "\"relationship-value\" : \"key2\""
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");
144 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
146 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
147 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
149 ".has('hostname', 'key1').has('aai-node-type', 'pserver').out('hasLAGInterface').has('aai-node-type', 'lag-interface')"
150 + ".has('interface-name', 'key2')";
151 String parentExpected =
152 ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
154 "gremlin query should be for node",
156 query.getQueryBuilder().getQuery());
158 "parent gremlin query should be for parent",
160 query.getQueryBuilder().getParentQuery().getQuery());
162 "result type should be lag-interface",
164 query.getResultType());
170 * @throws JAXBException the JAXB exception
171 * @throws UnsupportedEncodingException the unsupported encoding exception
172 * @throws AAIException the AAI exception
176 public void namingExceptions() throws JAXBException, UnsupportedEncodingException, AAIException {
179 + "\"related-to\" : \"cvlan-tag\","
180 + "\"relationship-data\" : [{"
181 + "\"relationship-key\" : \"vce.vnf-id\","
182 + "\"relationship-value\" : \"key1\""
184 + "\"relationship-key\" : \"port-group.interface-id\","
185 + "\"relationship-value\" : \"key2\""
187 + "\"relationship-key\" : \"cvlan-tag.cvlan-tag\","
188 + "\"relationship-value\" : \"655\""
192 Unmarshaller unmarshaller = context.createUnmarshaller();
193 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
194 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
195 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
196 Object obj = context.newDynamicEntity("Relationship");
198 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
200 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
201 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
203 ".has('vnf-id', 'key1').has('aai-node-type', 'vce').in('org.onap.relationships.inventory.BelongsTo')"
204 + ".has('aai-node-type', 'port-group').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').in('org.onap.relationships.inventory.BelongsTo')"
208 + ".has('aai-node-type', 'port-group').has('interface-id', 'key2')";
211 "gremlin query should be " + expected,
213 query.getQueryBuilder().getQuery());
215 "parent gremlin query should be equal the query for port group",
217 query.getQueryBuilder().getParentQuery().getQuery());
219 "result type should be cvlan-tag",
221 query.getResultType());
228 * @throws JAXBException the JAXB exception
229 * @throws UnsupportedEncodingException the unsupported encoding exception
230 * @throws AAIException the AAI exception
234 public void doubleKey() throws JAXBException, UnsupportedEncodingException, AAIException {
237 + "\"related-to\" : \"ctag-pool\","
238 + "\"relationship-data\" : [{"
239 + "\"relationship-key\" : \"complex.physical-location-id\","
240 + "\"relationship-value\" : \"key1\""
242 + "\"relationship-key\" : \"ctag-pool.target-pe\","
243 + " \"relationship-value\" : \"key2\""
245 + "\"relationship-key\" : \"ctag-pool.availability-zone-name\","
246 + "\"relationship-value\" : \"key3\""
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");
257 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
259 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
260 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
263 ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')"
264 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'ctag-pool')"
265 + ".has('target-pe', 'key2')"
266 + ".has('availability-zone-name', 'key3')";
267 String expectedParent =
268 ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')";
271 "gremlin query should be " + expected,
273 query.getQueryBuilder().getQuery());
275 "parent gremlin query should be equal the query for port group",
277 query.getQueryBuilder().getParentQuery().getQuery());
279 "result type should be ctag-pool",
281 query.getResultType());