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;
51 public class RelationshipQueryTest {
52 private ModelInjestor injestor = ModelInjestor.getInstance();
54 private TransactionalGraphEngine dbEngine =
55 new TitanDBEngine(QueryStyle.GREMLIN_TRAVERSAL,
56 LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v8),
58 private final Version version = Version.v8;
59 private DynamicJAXBContext context = injestor.getContextForVersion(version);
65 public static void configure() {
66 System.setProperty("AJSC_HOME", ".");
67 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
74 * @throws JAXBException the JAXB exception
75 * @throws UnsupportedEncodingException the unsupported encoding exception
76 * @throws AAIException the AAI exception
79 public void parentQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
83 + "\"related-to\" : \"pserver\","
84 + "\"relationship-data\" : [{"
85 + "\"relationship-key\" : \"pserver.hostname\","
86 + "\"relationship-value\" : \"key1\""
90 Unmarshaller unmarshaller = context.createUnmarshaller();
91 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
92 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
93 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
94 Object obj = context.newDynamicEntity("Relationship");
96 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
98 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
99 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
102 ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
104 "gremlin query should be " + expected,
106 query.getQueryBuilder().getQuery());
108 "parent gremlin query should be equal to normal query",
110 query.getQueryBuilder().getParentQuery().getQuery());
112 "result type should be pserver",
114 query.getResultType());
121 * @throws JAXBException the JAXB exception
122 * @throws UnsupportedEncodingException the unsupported encoding exception
123 * @throws AAIException the AAI exception
127 public void childQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
130 + "\"related-to\" : \"lag-interface\","
131 + "\"relationship-data\" : [{"
132 + "\"relationship-key\" : \"pserver.hostname\","
133 + "\"relationship-value\" : \"key1\""
135 + "\"relationship-key\" : \"lag-interface.interface-name\","
136 + "\"relationship-value\" : \"key2\""
140 Unmarshaller unmarshaller = context.createUnmarshaller();
141 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
142 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
143 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
144 Object obj = context.newDynamicEntity("Relationship");
146 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
148 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
149 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
151 ".has('hostname', 'key1').has('aai-node-type', 'pserver').out('hasLAGInterface').has('aai-node-type', 'lag-interface')"
152 + ".has('interface-name', 'key2')";
153 String parentExpected =
154 ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
156 "gremlin query should be for node",
158 query.getQueryBuilder().getQuery());
160 "parent gremlin query should be for parent",
162 query.getQueryBuilder().getParentQuery().getQuery());
164 "result type should be lag-interface",
166 query.getResultType());
172 * @throws JAXBException the JAXB exception
173 * @throws UnsupportedEncodingException the unsupported encoding exception
174 * @throws AAIException the AAI exception
178 public void namingExceptions() throws JAXBException, UnsupportedEncodingException, AAIException {
181 + "\"related-to\" : \"cvlan-tag\","
182 + "\"relationship-data\" : [{"
183 + "\"relationship-key\" : \"vce.vnf-id\","
184 + "\"relationship-value\" : \"key1\""
186 + "\"relationship-key\" : \"port-group.interface-id\","
187 + "\"relationship-value\" : \"key2\""
189 + "\"relationship-key\" : \"cvlan-tag.cvlan-tag\","
190 + "\"relationship-value\" : \"655\""
194 Unmarshaller unmarshaller = context.createUnmarshaller();
195 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
196 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
197 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
198 Object obj = context.newDynamicEntity("Relationship");
200 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
202 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
203 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
205 ".has('vnf-id', 'key1').has('aai-node-type', 'vce').in('org.onap.relationships.inventory.BelongsTo')"
206 + ".has('aai-node-type', 'port-group').has('interface-id', 'key2').in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'cvlan-tag')"
207 + ".has('cvlan-tag', 655)";
208 String expectedParent =
209 ".has('vnf-id', 'key1').has('aai-node-type', 'vce').in('org.onap.relationships.inventory.BelongsTo')"
210 + ".has('aai-node-type', 'port-group').has('interface-id', 'key2')";
213 "gremlin query should be " + expected,
215 query.getQueryBuilder().getQuery());
217 "parent gremlin query should be equal the query for port group",
219 query.getQueryBuilder().getParentQuery().getQuery());
221 "result type should be cvlan-tag",
223 query.getResultType());
230 * @throws JAXBException the JAXB exception
231 * @throws UnsupportedEncodingException the unsupported encoding exception
232 * @throws AAIException the AAI exception
236 public void doubleKey() throws JAXBException, UnsupportedEncodingException, AAIException {
239 + "\"related-to\" : \"ctag-pool\","
240 + "\"relationship-data\" : [{"
241 + "\"relationship-key\" : \"complex.physical-location-id\","
242 + "\"relationship-value\" : \"key1\""
244 + "\"relationship-key\" : \"ctag-pool.target-pe\","
245 + " \"relationship-value\" : \"key2\""
247 + "\"relationship-key\" : \"ctag-pool.availability-zone-name\","
248 + "\"relationship-value\" : \"key3\""
253 Unmarshaller unmarshaller = context.createUnmarshaller();
254 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
255 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
256 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
257 Object obj = context.newDynamicEntity("Relationship");
259 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
261 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
262 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
265 ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')"
266 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'ctag-pool')"
267 + ".has('target-pe', 'key2')"
268 + ".has('availability-zone-name', 'key3')";
269 String expectedParent =
270 ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')";
273 "gremlin query should be " + expected,
275 query.getQueryBuilder().getQuery());
277 "parent gremlin query should be equal the query for port group",
279 query.getQueryBuilder().getParentQuery().getQuery());
281 "result type should be ctag-pool",
283 query.getResultType());