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.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
23 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
24 import org.apache.tinkerpop.gremlin.structure.Vertex;
25 import org.eclipse.persistence.dynamic.DynamicEntity;
26 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
27 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
28 import org.junit.Before;
29 import org.junit.Ignore;
30 import org.junit.Test;
31 import org.onap.aai.AAISetup;
32 import org.onap.aai.exceptions.AAIException;
33 import org.onap.aai.introspection.*;
34 import org.onap.aai.serialization.engines.QueryStyle;
35 import org.onap.aai.serialization.engines.TitanDBEngine;
36 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
38 import javax.xml.bind.JAXBException;
39 import javax.xml.bind.Unmarshaller;
40 import javax.xml.transform.stream.StreamSource;
41 import java.io.StringReader;
42 import java.io.UnsupportedEncodingException;
44 import static org.junit.Assert.assertEquals;
47 public class UniqueRelationshipQueryTest extends AAISetup {
49 private ModelInjestor injestor = ModelInjestor.getInstance();
50 private TransactionalGraphEngine dbEngine =
51 new TitanDBEngine(QueryStyle.GREMLIN_UNIQUE,
52 LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v8),
54 private final Version version = Version.v8;
55 private DynamicJAXBContext context = injestor.getContextForVersion(version);
56 private Unmarshaller unmarshaller = null;
61 * @throws JAXBException the JAXB exception
64 public void setup() throws JAXBException {
65 unmarshaller = context.createUnmarshaller();
66 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
67 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
68 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
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 Object obj = context.newDynamicEntity("Relationship");
92 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
94 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
95 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
96 String key = "pserver/key1";
97 GraphTraversal<Vertex, Vertex> expected =
98 __.<Vertex>start().has("aai-unique-key", key);
99 String resultType = "pserver";
100 String containerType = "";
102 testSet(query, expected, expected, resultType, containerType);
109 * @throws JAXBException the JAXB exception
110 * @throws UnsupportedEncodingException the unsupported encoding exception
111 * @throws AAIException the AAI exception
114 public void childQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
117 + "\"related-to\" : \"lag-interface\","
118 + "\"relationship-data\" : [{"
119 + "\"relationship-key\" : \"pserver.hostname\","
120 + "\"relationship-value\" : \"key1\""
122 + "\"relationship-key\" : \"lag-interface.interface-name\","
123 + "\"relationship-value\" : \"key2\""
127 Object obj = context.newDynamicEntity("Relationship");
129 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
131 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
132 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
134 String key = "pserver/key1/lag-interface/key2";
135 GraphTraversal<Vertex, Vertex> expected =
136 __.<Vertex>start().has("aai-unique-key", key);
137 GraphTraversal<Vertex, Vertex> parentExpected =
138 __.<Vertex>start().has("aai-unique-key", "pserver/key1");
139 String resultType = "lag-interface";
140 String containerType = "";
142 testSet(query, expected, parentExpected, resultType, containerType);
148 * @throws JAXBException the JAXB exception
149 * @throws UnsupportedEncodingException the unsupported encoding exception
150 * @throws AAIException the AAI exception
153 public void namingExceptions() throws JAXBException, UnsupportedEncodingException, AAIException {
156 + "\"related-to\" : \"cvlan-tag\","
157 + "\"relationship-data\" : [{"
158 + "\"relationship-key\" : \"vce.vnf-id\","
159 + "\"relationship-value\" : \"key1\""
161 + "\"relationship-key\" : \"port-group.interface-id\","
162 + "\"relationship-value\" : \"key2\""
164 + "\"relationship-key\" : \"cvlan-tag.cvlan-tag\","
165 + "\"relationship-value\" : \"655\""
169 Object obj = context.newDynamicEntity("Relationship");
171 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
173 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
174 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
175 String key = "vce/key1/port-group/key2/cvlan-tag/655";
176 GraphTraversal<Vertex, Vertex> expected =
177 __.<Vertex>start().has("aai-unique-key", key);
178 GraphTraversal<Vertex, Vertex> parentExpected =
179 __.<Vertex>start().has("aai-unique-key", "vce/key1/port-group/key2");
180 String resultType = "cvlan-tag";
181 String containerType = "";
183 testSet(query, expected, parentExpected, resultType, containerType);
190 * @throws JAXBException the JAXB exception
191 * @throws UnsupportedEncodingException the unsupported encoding exception
192 * @throws AAIException the AAI exception
195 public void doubleKey() throws JAXBException, UnsupportedEncodingException, AAIException {
198 + "\"related-to\" : \"service-capability\","
199 + "\"relationship-data\" : [{"
200 + "\"relationship-key\" : \"service-capability.service-type\","
201 + "\"relationship-value\" : \"key1\""
203 + "\"relationship-key\" : \"service-capability.vnf-type\","
204 + " \"relationship-value\" : \"key2\""
208 Object obj = context.newDynamicEntity("Relationship");
210 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
212 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
213 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
215 String key = "service-capability/key1/key2";
216 GraphTraversal<Vertex, Vertex> expected =
217 __.<Vertex>start().has("aai-unique-key", key);
218 GraphTraversal<Vertex, Vertex> parentExpected =
219 __.<Vertex>start().has("aai-unique-key", "service-capability/key1/key2");
220 String resultType = "service-capability";
221 String containerType = "";
223 testSet(query, expected, parentExpected, resultType, containerType);
230 * @throws JAXBException the JAXB exception
231 * @throws UnsupportedEncodingException the unsupported encoding exception
232 * @throws AAIException the AAI exception
235 public void shortCircuit() throws JAXBException, UnsupportedEncodingException, AAIException {
238 + "\"related-to\" : \"cvlan-tag\","
239 + "\"related-link\" : \"http://mock-system-name.com:8443/aai/v6/network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655\","
240 + "\"relationship-data\" : [{"
241 + "\"relationship-key\" : \"vce.hostname\","
242 + "\"relationship-value\" : \"key1\""
244 + "\"relationship-key\" : \"port-group.interface-name\","
245 + "\"relationship-value\" : \"key2\""
247 + "\"relationship-key\" : \"cvlan-tag.-name\","
248 + "\"relationship-value\" : \"655\""
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);
258 String key = "vce/key1/port-group/key2/cvlan-tag/655";
259 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
260 GraphTraversal<Vertex, Vertex> parentExpected = __.<Vertex>start().has("aai-unique-key", "vce/key1/port-group/key2");
261 String resultType = "cvlan-tag";
262 String containerType = "";
264 testSet(query, expected, parentExpected, resultType, containerType);
271 * @param query the query
272 * @param expected the expected
273 * @param parentExpected the parent expected
274 * @param resultType the result type
275 * @param containerType the container type
277 public void testSet(QueryParser query, GraphTraversal<Vertex, Vertex> expected, GraphTraversal<Vertex, Vertex> parentExpected, String resultType, String containerType) {
279 "gremlin query should be " + expected,
281 query.getQueryBuilder().getQuery());
283 "parent gremlin query should be " + parentExpected,
285 query.getParentQueryBuilder().getParentQuery());
287 "result type should be " + resultType,
289 query.getResultType());
291 "container type should be " + containerType,
293 query.getContainerType());