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.Test;
38 import org.openecomp.aai.exceptions.AAIException;
39 import org.openecomp.aai.introspection.Introspector;
40 import org.openecomp.aai.introspection.IntrospectorFactory;
41 import org.openecomp.aai.introspection.LoaderFactory;
42 import org.openecomp.aai.introspection.ModelInjestor;
43 import org.openecomp.aai.introspection.ModelType;
44 import org.openecomp.aai.introspection.Version;
45 import org.openecomp.aai.serialization.engines.QueryStyle;
46 import org.openecomp.aai.serialization.engines.TitanDBEngine;
47 import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
49 public class RelationshipQueryTest {
50 private ModelInjestor injestor = ModelInjestor.getInstance();
52 private TransactionalGraphEngine dbEngine =
53 new TitanDBEngine(QueryStyle.GREMLIN_TRAVERSAL,
54 LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v8),
56 private final Version version = Version.v8;
57 private DynamicJAXBContext context = injestor.getContextForVersion(version);
63 public static void configure() {
64 System.setProperty("AJSC_HOME", "./src/test/resources/");
65 System.setProperty("BUNDLECONFIG_DIR", "bundleconfig-local");
72 * @throws JAXBException the JAXB exception
73 * @throws UnsupportedEncodingException the unsupported encoding exception
74 * @throws AAIException the AAI exception
77 public void parentQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
81 + "\"related-to\" : \"pserver\","
82 + "\"relationship-data\" : [{"
83 + "\"relationship-key\" : \"pserver.hostname\","
84 + "\"relationship-value\" : \"key1\""
88 Unmarshaller unmarshaller = context.createUnmarshaller();
89 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
90 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
91 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
92 Object obj = context.newDynamicEntity("Relationship");
94 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
96 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
97 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
100 ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
102 "gremlin query should be " + expected,
104 query.getQueryBuilder().getQuery());
106 "parent gremlin query should be equal to normal query",
108 query.getQueryBuilder().getParentQuery().getQuery());
110 "result type should be pserver",
112 query.getResultType());
119 * @throws JAXBException the JAXB exception
120 * @throws UnsupportedEncodingException the unsupported encoding exception
121 * @throws AAIException the AAI exception
124 public void childQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
127 + "\"related-to\" : \"lag-interface\","
128 + "\"relationship-data\" : [{"
129 + "\"relationship-key\" : \"pserver.hostname\","
130 + "\"relationship-value\" : \"key1\""
132 + "\"relationship-key\" : \"lag-interface.interface-name\","
133 + "\"relationship-value\" : \"key2\""
137 Unmarshaller unmarshaller = context.createUnmarshaller();
138 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
139 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
140 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
141 Object obj = context.newDynamicEntity("Relationship");
143 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
145 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
146 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
148 ".has('hostname', 'key1').has('aai-node-type', 'pserver').in('tosca.relationships.BindsTo').has('aai-node-type', 'lag-interface')"
149 + ".has('interface-name', 'key2')";
150 String parentExpected =
151 ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
153 "gremlin query should be for node",
155 query.getQueryBuilder().getQuery());
157 "parent gremlin query should be for parent",
159 query.getQueryBuilder().getParentQuery().getQuery());
161 "result type should be lag-interface",
163 query.getResultType());
169 * @throws JAXBException the JAXB exception
170 * @throws UnsupportedEncodingException the unsupported encoding exception
171 * @throws AAIException the AAI exception
174 public void namingExceptions() throws JAXBException, UnsupportedEncodingException, AAIException {
177 + "\"related-to\" : \"cvlan-tag\","
178 + "\"relationship-data\" : [{"
179 + "\"relationship-key\" : \"vce.vnf-id\","
180 + "\"relationship-value\" : \"key1\""
182 + "\"relationship-key\" : \"port-group.interface-id\","
183 + "\"relationship-value\" : \"key2\""
185 + "\"relationship-key\" : \"cvlan-tag.cvlan-tag\","
186 + "\"relationship-value\" : \"655\""
190 Unmarshaller unmarshaller = context.createUnmarshaller();
191 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
192 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
193 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
194 Object obj = context.newDynamicEntity("Relationship");
196 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
198 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
199 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
201 ".has('vnf-id', 'key1').has('aai-node-type', 'vce').in('org.onap.relationships.inventory.BelongsTo')"
202 + ".has('aai-node-type', 'port-group').has('interface-id', 'key2').in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'cvlan-tag')"
203 + ".has('cvlan-tag', 655)";
204 String expectedParent =
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')";
209 "gremlin query should be " + expected,
211 query.getQueryBuilder().getQuery());
213 "parent gremlin query should be equal the query for port group",
215 query.getQueryBuilder().getParentQuery().getQuery());
217 "result type should be cvlan-tag",
219 query.getResultType());
226 * @throws JAXBException the JAXB exception
227 * @throws UnsupportedEncodingException the unsupported encoding exception
228 * @throws AAIException the AAI exception
231 public void doubleKey() throws JAXBException, UnsupportedEncodingException, AAIException {
234 + "\"related-to\" : \"ctag-pool\","
235 + "\"relationship-data\" : [{"
236 + "\"relationship-key\" : \"complex.physical-location-id\","
237 + "\"relationship-value\" : \"key1\""
239 + "\"relationship-key\" : \"ctag-pool.target-pe\","
240 + " \"relationship-value\" : \"key2\""
242 + "\"relationship-key\" : \"ctag-pool.availability-zone-name\","
243 + "\"relationship-value\" : \"key3\""
248 Unmarshaller unmarshaller = context.createUnmarshaller();
249 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
250 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
251 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
252 Object obj = context.newDynamicEntity("Relationship");
254 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
256 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
257 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
260 ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')"
261 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'ctag-pool')"
262 + ".has('target-pe', 'key2')"
263 + ".has('availability-zone-name', 'key3')";
264 String expectedParent =
265 ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')";
268 "gremlin query should be " + expected,
270 query.getQueryBuilder().getQuery());
272 "parent gremlin query should be equal the query for port group",
274 query.getQueryBuilder().getParentQuery().getQuery());
276 "result type should be ctag-pool",
278 query.getResultType());