2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.parsers.query;
24 import org.eclipse.persistence.dynamic.DynamicEntity;
25 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
26 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
27 import org.junit.Ignore;
28 import org.junit.Test;
29 import org.onap.aai.AAISetup;
30 import org.onap.aai.exceptions.AAIException;
31 import org.onap.aai.introspection.*;
32 import org.onap.aai.serialization.engines.QueryStyle;
33 import org.onap.aai.serialization.engines.TitanDBEngine;
34 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
36 import javax.xml.bind.JAXBException;
37 import javax.xml.bind.Unmarshaller;
38 import javax.xml.transform.stream.StreamSource;
39 import java.io.StringReader;
40 import java.io.UnsupportedEncodingException;
42 import static org.junit.Assert.assertEquals;
45 public class RelationshipQueryTest extends AAISetup {
47 private ModelInjestor injestor = ModelInjestor.getInstance();
49 private TransactionalGraphEngine dbEngine =
50 new TitanDBEngine(QueryStyle.GREMLIN_TRAVERSAL,
51 LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v8),
53 private final Version version = Version.v8;
54 private DynamicJAXBContext context = injestor.getContextForVersion(version);
59 * @throws JAXBException the JAXB exception
60 * @throws UnsupportedEncodingException the unsupported encoding exception
61 * @throws AAIException the AAI exception
64 public void parentQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
68 + "\"related-to\" : \"pserver\","
69 + "\"relationship-data\" : [{"
70 + "\"relationship-key\" : \"pserver.hostname\","
71 + "\"relationship-value\" : \"key1\""
75 Unmarshaller unmarshaller = context.createUnmarshaller();
76 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
77 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
78 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
79 Object obj = context.newDynamicEntity("Relationship");
81 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
83 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
84 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
87 ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
89 "gremlin query should be " + expected,
91 query.getQueryBuilder().getQuery());
93 "parent gremlin query should be equal to normal query",
95 query.getQueryBuilder().getParentQuery().getQuery());
97 "result type should be pserver",
99 query.getResultType());
106 * @throws JAXBException the JAXB exception
107 * @throws UnsupportedEncodingException the unsupported encoding exception
108 * @throws AAIException the AAI exception
112 public void childQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
115 + "\"related-to\" : \"lag-interface\","
116 + "\"relationship-data\" : [{"
117 + "\"relationship-key\" : \"pserver.hostname\","
118 + "\"relationship-value\" : \"key1\""
120 + "\"relationship-key\" : \"lag-interface.interface-name\","
121 + "\"relationship-value\" : \"key2\""
125 Unmarshaller unmarshaller = context.createUnmarshaller();
126 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
127 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
128 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
129 Object obj = context.newDynamicEntity("Relationship");
131 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
133 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
134 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
136 ".has('hostname', 'key1').has('aai-node-type', 'pserver').out('hasLAGInterface').has('aai-node-type', 'lag-interface')"
137 + ".has('interface-name', 'key2')";
138 String parentExpected =
139 ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
141 "gremlin query should be for node",
143 query.getQueryBuilder().getQuery());
145 "parent gremlin query should be for parent",
147 query.getQueryBuilder().getParentQuery().getQuery());
149 "result type should be lag-interface",
151 query.getResultType());
157 * @throws JAXBException the JAXB exception
158 * @throws UnsupportedEncodingException the unsupported encoding exception
159 * @throws AAIException the AAI exception
163 public void namingExceptions() throws JAXBException, UnsupportedEncodingException, AAIException {
166 + "\"related-to\" : \"cvlan-tag\","
167 + "\"relationship-data\" : [{"
168 + "\"relationship-key\" : \"vce.vnf-id\","
169 + "\"relationship-value\" : \"key1\""
171 + "\"relationship-key\" : \"port-group.interface-id\","
172 + "\"relationship-value\" : \"key2\""
174 + "\"relationship-key\" : \"cvlan-tag.cvlan-tag\","
175 + "\"relationship-value\" : \"655\""
179 Unmarshaller unmarshaller = context.createUnmarshaller();
180 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
181 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
182 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
183 Object obj = context.newDynamicEntity("Relationship");
185 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
187 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
188 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
190 ".has('vnf-id', 'key1').has('aai-node-type', 'vce').in('org.onap.relationships.inventory.BelongsTo')"
191 + ".has('aai-node-type', 'port-group').has('interface-id', 'key2').in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'cvlan-tag')"
192 + ".has('cvlan-tag', 655)";
193 String expectedParent =
194 ".has('vnf-id', 'key1').has('aai-node-type', 'vce').in('org.onap.relationships.inventory.BelongsTo')"
195 + ".has('aai-node-type', 'port-group').has('interface-id', 'key2')";
198 "gremlin query should be " + expected,
200 query.getQueryBuilder().getQuery());
202 "parent gremlin query should be equal the query for port group",
204 query.getQueryBuilder().getParentQuery().getQuery());
206 "result type should be cvlan-tag",
208 query.getResultType());
215 * @throws JAXBException the JAXB exception
216 * @throws UnsupportedEncodingException the unsupported encoding exception
217 * @throws AAIException the AAI exception
221 public void doubleKey() throws JAXBException, UnsupportedEncodingException, AAIException {
224 + "\"related-to\" : \"ctag-pool\","
225 + "\"relationship-data\" : [{"
226 + "\"relationship-key\" : \"complex.physical-location-id\","
227 + "\"relationship-value\" : \"key1\""
229 + "\"relationship-key\" : \"ctag-pool.target-pe\","
230 + " \"relationship-value\" : \"key2\""
232 + "\"relationship-key\" : \"ctag-pool.availability-zone-name\","
233 + "\"relationship-value\" : \"key3\""
238 Unmarshaller unmarshaller = context.createUnmarshaller();
239 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
240 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
241 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
242 Object obj = context.newDynamicEntity("Relationship");
244 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
246 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
247 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
250 ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')"
251 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'ctag-pool')"
252 + ".has('target-pe', 'key2')"
253 + ".has('availability-zone-name', 'key3')";
254 String expectedParent =
255 ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')";
258 "gremlin query should be " + expected,
260 query.getQueryBuilder().getQuery());
262 "parent gremlin query should be equal the query for port group",
264 query.getQueryBuilder().getParentQuery().getQuery());
266 "result type should be ctag-pool",
268 query.getResultType());