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.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
25 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
26 import org.apache.tinkerpop.gremlin.structure.Vertex;
27 import org.eclipse.persistence.dynamic.DynamicEntity;
28 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
29 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
30 import org.junit.Before;
31 import org.junit.Ignore;
32 import org.junit.Test;
33 import org.onap.aai.AAISetup;
34 import org.onap.aai.exceptions.AAIException;
35 import org.onap.aai.introspection.*;
36 import org.onap.aai.serialization.engines.QueryStyle;
37 import org.onap.aai.serialization.engines.TitanDBEngine;
38 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
40 import javax.xml.bind.JAXBException;
41 import javax.xml.bind.Unmarshaller;
42 import javax.xml.transform.stream.StreamSource;
43 import java.io.StringReader;
44 import java.io.UnsupportedEncodingException;
46 import static org.junit.Assert.assertEquals;
49 public class UniqueRelationshipQueryTest extends AAISetup {
51 private ModelInjestor injestor = ModelInjestor.getInstance();
52 private TransactionalGraphEngine dbEngine =
53 new TitanDBEngine(QueryStyle.GREMLIN_UNIQUE,
54 LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v8),
56 private final Version version = Version.v8;
57 private DynamicJAXBContext context = injestor.getContextForVersion(version);
58 private Unmarshaller unmarshaller = null;
63 * @throws JAXBException the JAXB exception
66 public void setup() throws JAXBException {
67 unmarshaller = context.createUnmarshaller();
68 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
69 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
70 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
76 * @throws JAXBException the JAXB exception
77 * @throws UnsupportedEncodingException the unsupported encoding exception
78 * @throws AAIException the AAI exception
81 public void parentQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
85 + "\"related-to\" : \"pserver\","
86 + "\"relationship-data\" : [{"
87 + "\"relationship-key\" : \"pserver.hostname\","
88 + "\"relationship-value\" : \"key1\""
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);
98 String key = "pserver/key1";
99 GraphTraversal<Vertex, Vertex> expected =
100 __.<Vertex>start().has("aai-unique-key", key);
101 String resultType = "pserver";
102 String containerType = "";
104 testSet(query, expected, expected, resultType, containerType);
111 * @throws JAXBException the JAXB exception
112 * @throws UnsupportedEncodingException the unsupported encoding exception
113 * @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 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 String key = "pserver/key1/lag-interface/key2";
137 GraphTraversal<Vertex, Vertex> expected =
138 __.<Vertex>start().has("aai-unique-key", key);
139 GraphTraversal<Vertex, Vertex> parentExpected =
140 __.<Vertex>start().has("aai-unique-key", "pserver/key1");
141 String resultType = "lag-interface";
142 String containerType = "";
144 testSet(query, expected, parentExpected, resultType, containerType);
150 * @throws JAXBException the JAXB exception
151 * @throws UnsupportedEncodingException the unsupported encoding exception
152 * @throws AAIException the AAI exception
155 public void namingExceptions() throws JAXBException, UnsupportedEncodingException, AAIException {
158 + "\"related-to\" : \"cvlan-tag\","
159 + "\"relationship-data\" : [{"
160 + "\"relationship-key\" : \"vce.vnf-id\","
161 + "\"relationship-value\" : \"key1\""
163 + "\"relationship-key\" : \"port-group.interface-id\","
164 + "\"relationship-value\" : \"key2\""
166 + "\"relationship-key\" : \"cvlan-tag.cvlan-tag\","
167 + "\"relationship-value\" : \"655\""
171 Object obj = context.newDynamicEntity("Relationship");
173 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
175 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
176 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
177 String key = "vce/key1/port-group/key2/cvlan-tag/655";
178 GraphTraversal<Vertex, Vertex> expected =
179 __.<Vertex>start().has("aai-unique-key", key);
180 GraphTraversal<Vertex, Vertex> parentExpected =
181 __.<Vertex>start().has("aai-unique-key", "vce/key1/port-group/key2");
182 String resultType = "cvlan-tag";
183 String containerType = "";
185 testSet(query, expected, parentExpected, resultType, containerType);
192 * @throws JAXBException the JAXB exception
193 * @throws UnsupportedEncodingException the unsupported encoding exception
194 * @throws AAIException the AAI exception
197 public void doubleKey() throws JAXBException, UnsupportedEncodingException, AAIException {
200 + "\"related-to\" : \"service-capability\","
201 + "\"relationship-data\" : [{"
202 + "\"relationship-key\" : \"service-capability.service-type\","
203 + "\"relationship-value\" : \"key1\""
205 + "\"relationship-key\" : \"service-capability.vnf-type\","
206 + " \"relationship-value\" : \"key2\""
210 Object obj = context.newDynamicEntity("Relationship");
212 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
214 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
215 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
217 String key = "service-capability/key1/key2";
218 GraphTraversal<Vertex, Vertex> expected =
219 __.<Vertex>start().has("aai-unique-key", key);
220 GraphTraversal<Vertex, Vertex> parentExpected =
221 __.<Vertex>start().has("aai-unique-key", "service-capability/key1/key2");
222 String resultType = "service-capability";
223 String containerType = "";
225 testSet(query, expected, parentExpected, resultType, containerType);
232 * @throws JAXBException the JAXB exception
233 * @throws UnsupportedEncodingException the unsupported encoding exception
234 * @throws AAIException the AAI exception
237 public void shortCircuit() throws JAXBException, UnsupportedEncodingException, AAIException {
240 + "\"related-to\" : \"cvlan-tag\","
241 + "\"related-link\" : \"http://mock-system-name.com:8443/aai/v6/network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655\","
242 + "\"relationship-data\" : [{"
243 + "\"relationship-key\" : \"vce.hostname\","
244 + "\"relationship-value\" : \"key1\""
246 + "\"relationship-key\" : \"port-group.interface-name\","
247 + "\"relationship-value\" : \"key2\""
249 + "\"relationship-key\" : \"cvlan-tag.-name\","
250 + "\"relationship-value\" : \"655\""
254 Object obj = context.newDynamicEntity("Relationship");
256 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
258 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
259 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
260 String key = "vce/key1/port-group/key2/cvlan-tag/655";
261 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
262 GraphTraversal<Vertex, Vertex> parentExpected = __.<Vertex>start().has("aai-unique-key", "vce/key1/port-group/key2");
263 String resultType = "cvlan-tag";
264 String containerType = "";
266 testSet(query, expected, parentExpected, resultType, containerType);
273 * @param query the query
274 * @param expected the expected
275 * @param parentExpected the parent expected
276 * @param resultType the result type
277 * @param containerType the container type
279 public void testSet(QueryParser query, GraphTraversal<Vertex, Vertex> expected, GraphTraversal<Vertex, Vertex> parentExpected, String resultType, String containerType) {
281 "gremlin query should be " + expected,
283 query.getQueryBuilder().getQuery());
285 "parent gremlin query should be " + parentExpected,
287 query.getParentQueryBuilder().getParentQuery());
289 "result type should be " + resultType,
291 query.getResultType());
293 "container type should be " + containerType,
295 query.getContainerType());