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 static org.junit.Assert.assertEquals;
25 import java.io.StringReader;
26 import java.io.UnsupportedEncodingException;
28 import javax.xml.bind.JAXBException;
29 import javax.xml.bind.Unmarshaller;
30 import javax.xml.transform.stream.StreamSource;
32 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
33 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
34 import org.apache.tinkerpop.gremlin.structure.Vertex;
35 import org.eclipse.persistence.dynamic.DynamicEntity;
36 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
37 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Ignore;
41 import org.junit.Test;
43 import org.openecomp.aai.exceptions.AAIException;
44 import org.openecomp.aai.introspection.Introspector;
45 import org.openecomp.aai.introspection.IntrospectorFactory;
46 import org.openecomp.aai.introspection.LoaderFactory;
47 import org.openecomp.aai.introspection.ModelInjestor;
48 import org.openecomp.aai.introspection.ModelType;
49 import org.openecomp.aai.introspection.Version;
50 import org.openecomp.aai.serialization.engines.QueryStyle;
51 import org.openecomp.aai.serialization.engines.TitanDBEngine;
52 import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
54 public class UniqueRelationshipQueryTest {
55 private ModelInjestor injestor = ModelInjestor.getInstance();
56 private TransactionalGraphEngine dbEngine =
57 new TitanDBEngine(QueryStyle.GREMLIN_UNIQUE,
58 LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v8),
60 private final Version version = Version.v8;
61 private DynamicJAXBContext context = injestor.getContextForVersion(version);
62 private Unmarshaller unmarshaller = null;
68 public static void configure() {
69 System.setProperty("AJSC_HOME", ".");
70 System.setProperty("BUNDLECONFIG_DIR", "bundleconfig-local");
77 * @throws JAXBException the JAXB exception
80 public void setup() throws JAXBException {
81 unmarshaller = context.createUnmarshaller();
82 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
83 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
84 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
90 * @throws JAXBException the JAXB exception
91 * @throws UnsupportedEncodingException the unsupported encoding exception
92 * @throws AAIException the AAI exception
95 public void parentQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
99 + "\"related-to\" : \"pserver\","
100 + "\"relationship-data\" : [{"
101 + "\"relationship-key\" : \"pserver.hostname\","
102 + "\"relationship-value\" : \"key1\""
106 Object obj = context.newDynamicEntity("Relationship");
108 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
110 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
111 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
112 String key = "pserver/key1";
113 GraphTraversal<Vertex, Vertex> expected =
114 __.<Vertex>start().has("aai-unique-key", key);
115 String resultType = "pserver";
116 String containerType = "";
118 testSet(query, expected, expected, resultType, containerType);
125 * @throws JAXBException the JAXB exception
126 * @throws UnsupportedEncodingException the unsupported encoding exception
127 * @throws AAIException the AAI exception
130 public void childQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
133 + "\"related-to\" : \"lag-interface\","
134 + "\"relationship-data\" : [{"
135 + "\"relationship-key\" : \"pserver.hostname\","
136 + "\"relationship-value\" : \"key1\""
138 + "\"relationship-key\" : \"lag-interface.interface-name\","
139 + "\"relationship-value\" : \"key2\""
143 Object obj = context.newDynamicEntity("Relationship");
145 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
147 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
148 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
150 String key = "pserver/key1/lag-interface/key2";
151 GraphTraversal<Vertex, Vertex> expected =
152 __.<Vertex>start().has("aai-unique-key", key);
153 GraphTraversal<Vertex, Vertex> parentExpected =
154 __.<Vertex>start().has("aai-unique-key", "pserver/key1");
155 String resultType = "lag-interface";
156 String containerType = "";
158 testSet(query, expected, parentExpected, resultType, containerType);
164 * @throws JAXBException the JAXB exception
165 * @throws UnsupportedEncodingException the unsupported encoding exception
166 * @throws AAIException the AAI exception
169 public void namingExceptions() throws JAXBException, UnsupportedEncodingException, AAIException {
172 + "\"related-to\" : \"cvlan-tag\","
173 + "\"relationship-data\" : [{"
174 + "\"relationship-key\" : \"vce.vnf-id\","
175 + "\"relationship-value\" : \"key1\""
177 + "\"relationship-key\" : \"port-group.interface-id\","
178 + "\"relationship-value\" : \"key2\""
180 + "\"relationship-key\" : \"cvlan-tag.cvlan-tag\","
181 + "\"relationship-value\" : \"655\""
185 Object obj = context.newDynamicEntity("Relationship");
187 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
189 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
190 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
191 String key = "vce/key1/port-group/key2/cvlan-tag/655";
192 GraphTraversal<Vertex, Vertex> expected =
193 __.<Vertex>start().has("aai-unique-key", key);
194 GraphTraversal<Vertex, Vertex> parentExpected =
195 __.<Vertex>start().has("aai-unique-key", "vce/key1/port-group/key2");
196 String resultType = "cvlan-tag";
197 String containerType = "";
199 testSet(query, expected, parentExpected, resultType, containerType);
206 * @throws JAXBException the JAXB exception
207 * @throws UnsupportedEncodingException the unsupported encoding exception
208 * @throws AAIException the AAI exception
211 public void doubleKey() throws JAXBException, UnsupportedEncodingException, AAIException {
214 + "\"related-to\" : \"service-capability\","
215 + "\"relationship-data\" : [{"
216 + "\"relationship-key\" : \"service-capability.service-type\","
217 + "\"relationship-value\" : \"key1\""
219 + "\"relationship-key\" : \"service-capability.vnf-type\","
220 + " \"relationship-value\" : \"key2\""
224 Object obj = context.newDynamicEntity("Relationship");
226 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
228 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
229 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
231 String key = "service-capability/key1/key2";
232 GraphTraversal<Vertex, Vertex> expected =
233 __.<Vertex>start().has("aai-unique-key", key);
234 GraphTraversal<Vertex, Vertex> parentExpected =
235 __.<Vertex>start().has("aai-unique-key", "service-capability/key1/key2");
236 String resultType = "service-capability";
237 String containerType = "";
239 testSet(query, expected, parentExpected, resultType, containerType);
246 * @throws JAXBException the JAXB exception
247 * @throws UnsupportedEncodingException the unsupported encoding exception
248 * @throws AAIException the AAI exception
251 public void shortCircuit() throws JAXBException, UnsupportedEncodingException, AAIException {
254 + "\"related-to\" : \"cvlan-tag\","
255 + "\"related-link\" : \"http://mock-system-name.com:8443/aai/v6/network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655\","
256 + "\"relationship-data\" : [{"
257 + "\"relationship-key\" : \"vce.hostname\","
258 + "\"relationship-value\" : \"key1\""
260 + "\"relationship-key\" : \"port-group.interface-name\","
261 + "\"relationship-value\" : \"key2\""
263 + "\"relationship-key\" : \"cvlan-tag.-name\","
264 + "\"relationship-value\" : \"655\""
268 Object obj = context.newDynamicEntity("Relationship");
270 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
272 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
273 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
274 String key = "vce/key1/port-group/key2/cvlan-tag/655";
275 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
276 GraphTraversal<Vertex, Vertex> parentExpected = __.<Vertex>start().has("aai-unique-key", "vce/key1/port-group/key2");
277 String resultType = "cvlan-tag";
278 String containerType = "";
280 testSet(query, expected, parentExpected, resultType, containerType);
287 * @param query the query
288 * @param expected the expected
289 * @param parentExpected the parent expected
290 * @param resultType the result type
291 * @param containerType the container type
293 public void testSet(QueryParser query, GraphTraversal<Vertex, Vertex> expected, GraphTraversal<Vertex, Vertex> parentExpected, String resultType, String containerType) {
295 "gremlin query should be " + expected,
297 query.getQueryBuilder().getQuery());
299 "parent gremlin query should be " + parentExpected,
301 query.getParentQueryBuilder().getParentQuery());
303 "result type should be " + resultType,
305 query.getResultType());
307 "container type should be " + containerType,
309 query.getContainerType());