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 org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
24 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
25 import org.apache.tinkerpop.gremlin.structure.Vertex;
26 import org.eclipse.persistence.dynamic.DynamicEntity;
27 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
28 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
29 import org.junit.Before;
30 import org.junit.Ignore;
31 import org.junit.Test;
32 import org.openecomp.aai.AAISetup;
33 import org.openecomp.aai.exceptions.AAIException;
34 import org.openecomp.aai.introspection.*;
35 import org.openecomp.aai.serialization.engines.QueryStyle;
36 import org.openecomp.aai.serialization.engines.TitanDBEngine;
37 import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
39 import javax.xml.bind.JAXBException;
40 import javax.xml.bind.Unmarshaller;
41 import javax.xml.transform.stream.StreamSource;
42 import java.io.StringReader;
43 import java.io.UnsupportedEncodingException;
45 import static org.junit.Assert.assertEquals;
48 public class UniqueRelationshipQueryTest extends AAISetup {
50 private ModelInjestor injestor = ModelInjestor.getInstance();
51 private TransactionalGraphEngine dbEngine =
52 new TitanDBEngine(QueryStyle.GREMLIN_UNIQUE,
53 LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v8),
55 private final Version version = Version.v8;
56 private DynamicJAXBContext context = injestor.getContextForVersion(version);
57 private Unmarshaller unmarshaller = null;
62 * @throws JAXBException the JAXB exception
65 public void setup() throws JAXBException {
66 unmarshaller = context.createUnmarshaller();
67 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
68 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
69 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
75 * @throws JAXBException the JAXB exception
76 * @throws UnsupportedEncodingException the unsupported encoding exception
77 * @throws AAIException the AAI exception
80 public void parentQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
84 + "\"related-to\" : \"pserver\","
85 + "\"relationship-data\" : [{"
86 + "\"relationship-key\" : \"pserver.hostname\","
87 + "\"relationship-value\" : \"key1\""
91 Object obj = context.newDynamicEntity("Relationship");
93 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
95 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
96 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
97 String key = "pserver/key1";
98 GraphTraversal<Vertex, Vertex> expected =
99 __.<Vertex>start().has("aai-unique-key", key);
100 String resultType = "pserver";
101 String containerType = "";
103 testSet(query, expected, expected, resultType, containerType);
110 * @throws JAXBException the JAXB exception
111 * @throws UnsupportedEncodingException the unsupported encoding exception
112 * @throws AAIException the AAI exception
115 public void childQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
118 + "\"related-to\" : \"lag-interface\","
119 + "\"relationship-data\" : [{"
120 + "\"relationship-key\" : \"pserver.hostname\","
121 + "\"relationship-value\" : \"key1\""
123 + "\"relationship-key\" : \"lag-interface.interface-name\","
124 + "\"relationship-value\" : \"key2\""
128 Object obj = context.newDynamicEntity("Relationship");
130 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
132 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
133 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
135 String key = "pserver/key1/lag-interface/key2";
136 GraphTraversal<Vertex, Vertex> expected =
137 __.<Vertex>start().has("aai-unique-key", key);
138 GraphTraversal<Vertex, Vertex> parentExpected =
139 __.<Vertex>start().has("aai-unique-key", "pserver/key1");
140 String resultType = "lag-interface";
141 String containerType = "";
143 testSet(query, expected, parentExpected, resultType, containerType);
149 * @throws JAXBException the JAXB exception
150 * @throws UnsupportedEncodingException the unsupported encoding exception
151 * @throws AAIException the AAI exception
154 public void namingExceptions() throws JAXBException, UnsupportedEncodingException, AAIException {
157 + "\"related-to\" : \"cvlan-tag\","
158 + "\"relationship-data\" : [{"
159 + "\"relationship-key\" : \"vce.vnf-id\","
160 + "\"relationship-value\" : \"key1\""
162 + "\"relationship-key\" : \"port-group.interface-id\","
163 + "\"relationship-value\" : \"key2\""
165 + "\"relationship-key\" : \"cvlan-tag.cvlan-tag\","
166 + "\"relationship-value\" : \"655\""
170 Object obj = context.newDynamicEntity("Relationship");
172 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
174 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
175 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
176 String key = "vce/key1/port-group/key2/cvlan-tag/655";
177 GraphTraversal<Vertex, Vertex> expected =
178 __.<Vertex>start().has("aai-unique-key", key);
179 GraphTraversal<Vertex, Vertex> parentExpected =
180 __.<Vertex>start().has("aai-unique-key", "vce/key1/port-group/key2");
181 String resultType = "cvlan-tag";
182 String containerType = "";
184 testSet(query, expected, parentExpected, resultType, containerType);
191 * @throws JAXBException the JAXB exception
192 * @throws UnsupportedEncodingException the unsupported encoding exception
193 * @throws AAIException the AAI exception
196 public void doubleKey() throws JAXBException, UnsupportedEncodingException, AAIException {
199 + "\"related-to\" : \"service-capability\","
200 + "\"relationship-data\" : [{"
201 + "\"relationship-key\" : \"service-capability.service-type\","
202 + "\"relationship-value\" : \"key1\""
204 + "\"relationship-key\" : \"service-capability.vnf-type\","
205 + " \"relationship-value\" : \"key2\""
209 Object obj = context.newDynamicEntity("Relationship");
211 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
213 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
214 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
216 String key = "service-capability/key1/key2";
217 GraphTraversal<Vertex, Vertex> expected =
218 __.<Vertex>start().has("aai-unique-key", key);
219 GraphTraversal<Vertex, Vertex> parentExpected =
220 __.<Vertex>start().has("aai-unique-key", "service-capability/key1/key2");
221 String resultType = "service-capability";
222 String containerType = "";
224 testSet(query, expected, parentExpected, resultType, containerType);
231 * @throws JAXBException the JAXB exception
232 * @throws UnsupportedEncodingException the unsupported encoding exception
233 * @throws AAIException the AAI exception
236 public void shortCircuit() throws JAXBException, UnsupportedEncodingException, AAIException {
239 + "\"related-to\" : \"cvlan-tag\","
240 + "\"related-link\" : \"http://mock-system-name.com:8443/aai/v6/network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655\","
241 + "\"relationship-data\" : [{"
242 + "\"relationship-key\" : \"vce.hostname\","
243 + "\"relationship-value\" : \"key1\""
245 + "\"relationship-key\" : \"port-group.interface-name\","
246 + "\"relationship-value\" : \"key2\""
248 + "\"relationship-key\" : \"cvlan-tag.-name\","
249 + "\"relationship-value\" : \"655\""
253 Object obj = context.newDynamicEntity("Relationship");
255 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
257 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
258 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
259 String key = "vce/key1/port-group/key2/cvlan-tag/655";
260 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
261 GraphTraversal<Vertex, Vertex> parentExpected = __.<Vertex>start().has("aai-unique-key", "vce/key1/port-group/key2");
262 String resultType = "cvlan-tag";
263 String containerType = "";
265 testSet(query, expected, parentExpected, resultType, containerType);
272 * @param query the query
273 * @param expected the expected
274 * @param parentExpected the parent expected
275 * @param resultType the result type
276 * @param containerType the container type
278 public void testSet(QueryParser query, GraphTraversal<Vertex, Vertex> expected, GraphTraversal<Vertex, Vertex> parentExpected, String resultType, String containerType) {
280 "gremlin query should be " + expected,
282 query.getQueryBuilder().getQuery());
284 "parent gremlin query should be " + parentExpected,
286 query.getParentQueryBuilder().getParentQuery());
288 "result type should be " + resultType,
290 query.getResultType());
292 "container type should be " + containerType,
294 query.getContainerType());