2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 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 package org.onap.aai.parsers.query;
22 import org.eclipse.persistence.dynamic.DynamicEntity;
23 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
24 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
25 import org.junit.Before;
26 import org.junit.Ignore;
27 import org.junit.Test;
28 import org.onap.aai.AAISetup;
29 import org.onap.aai.exceptions.AAIException;
30 import org.onap.aai.introspection.*;
31 import org.onap.aai.setup.SchemaVersion;
32 import org.onap.aai.serialization.engines.QueryStyle;
33 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
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 {
48 private TransactionalGraphEngine dbEngine;
49 private SchemaVersion version;
50 private DynamicJAXBContext context = nodeIngestor.getContextForVersion(version);
54 version = new SchemaVersion("v8");
56 new JanusGraphDBEngine(QueryStyle.GREMLIN_TRAVERSAL,
57 loaderFactory.createLoaderForVersion(ModelType.MOXY, version),
63 * @throws JAXBException the JAXB exception
64 * @throws UnsupportedEncodingException the unsupported encoding exception
65 * @throws AAIException the AAI exception
68 public void parentQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
72 + "\"related-to\" : \"pserver\","
73 + "\"relationship-data\" : [{"
74 + "\"relationship-key\" : \"pserver.hostname\","
75 + "\"relationship-value\" : \"key1\""
79 Unmarshaller unmarshaller = context.createUnmarshaller();
80 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
81 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
82 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
83 Object obj = context.newDynamicEntity("Relationship");
85 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
87 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
88 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
91 ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
93 "gremlin query should be " + expected,
95 query.getQueryBuilder().getQuery());
97 "parent gremlin query should be equal to normal query",
99 query.getQueryBuilder().getParentQuery().getQuery());
101 "result type should be pserver",
103 query.getResultType());
110 * @throws JAXBException the JAXB exception
111 * @throws UnsupportedEncodingException the unsupported encoding exception
112 * @throws AAIException the AAI exception
116 public void childQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
119 + "\"related-to\" : \"lag-interface\","
120 + "\"relationship-data\" : [{"
121 + "\"relationship-key\" : \"pserver.hostname\","
122 + "\"relationship-value\" : \"key1\""
124 + "\"relationship-key\" : \"lag-interface.interface-name\","
125 + "\"relationship-value\" : \"key2\""
129 Unmarshaller unmarshaller = context.createUnmarshaller();
130 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
131 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
132 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
133 Object obj = context.newDynamicEntity("Relationship");
135 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
137 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
138 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
140 ".has('hostname', 'key1').has('aai-node-type', 'pserver').out('hasLAGInterface').has('aai-node-type', 'lag-interface')"
141 + ".has('interface-name', 'key2')";
142 String parentExpected =
143 ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
145 "gremlin query should be for node",
147 query.getQueryBuilder().getQuery());
149 "parent gremlin query should be for parent",
151 query.getQueryBuilder().getParentQuery().getQuery());
153 "result type should be lag-interface",
155 query.getResultType());
161 * @throws JAXBException the JAXB exception
162 * @throws UnsupportedEncodingException the unsupported encoding exception
163 * @throws AAIException the AAI exception
167 public void namingExceptions() throws JAXBException, UnsupportedEncodingException, AAIException {
170 + "\"related-to\" : \"cvlan-tag\","
171 + "\"relationship-data\" : [{"
172 + "\"relationship-key\" : \"vce.vnf-id\","
173 + "\"relationship-value\" : \"key1\""
175 + "\"relationship-key\" : \"port-group.interface-id\","
176 + "\"relationship-value\" : \"key2\""
178 + "\"relationship-key\" : \"cvlan-tag.cvlan-tag\","
179 + "\"relationship-value\" : \"655\""
183 Unmarshaller unmarshaller = context.createUnmarshaller();
184 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
185 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
186 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
187 Object obj = context.newDynamicEntity("Relationship");
189 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
191 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
192 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
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').in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'cvlan-tag')"
196 + ".has('cvlan-tag', 655)";
197 String expectedParent =
198 ".has('vnf-id', 'key1').has('aai-node-type', 'vce').in('org.onap.relationships.inventory.BelongsTo')"
199 + ".has('aai-node-type', 'port-group').has('interface-id', 'key2')";
202 "gremlin query should be " + expected,
204 query.getQueryBuilder().getQuery());
206 "parent gremlin query should be equal the query for port group",
208 query.getQueryBuilder().getParentQuery().getQuery());
210 "result type should be cvlan-tag",
212 query.getResultType());
219 * @throws JAXBException the JAXB exception
220 * @throws UnsupportedEncodingException the unsupported encoding exception
221 * @throws AAIException the AAI exception
225 public void doubleKey() throws JAXBException, UnsupportedEncodingException, AAIException {
228 + "\"related-to\" : \"ctag-pool\","
229 + "\"relationship-data\" : [{"
230 + "\"relationship-key\" : \"complex.physical-location-id\","
231 + "\"relationship-value\" : \"key1\""
233 + "\"relationship-key\" : \"ctag-pool.target-pe\","
234 + " \"relationship-value\" : \"key2\""
236 + "\"relationship-key\" : \"ctag-pool.availability-zone-name\","
237 + "\"relationship-value\" : \"key3\""
242 Unmarshaller unmarshaller = context.createUnmarshaller();
243 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
244 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
245 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
246 Object obj = context.newDynamicEntity("Relationship");
248 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
250 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
251 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
254 ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')"
255 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'ctag-pool')"
256 + ".has('target-pe', 'key2')"
257 + ".has('availability-zone-name', 'key3')";
258 String expectedParent =
259 ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')";
262 "gremlin query should be " + expected,
264 query.getQueryBuilder().getQuery());
266 "parent gremlin query should be equal the query for port group",
268 query.getQueryBuilder().getParentQuery().getQuery());
270 "result type should be ctag-pool",
272 query.getResultType());