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.BeforeClass;
31 import org.junit.Ignore;
32 import org.junit.Test;
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 {
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;
62 public static void configure() {
63 System.setProperty("AJSC_HOME", ".");
64 System.setProperty("BUNDLECONFIG_DIR", "bundleconfig-local");
71 * @throws JAXBException the JAXB exception
74 public void setup() throws JAXBException {
75 unmarshaller = context.createUnmarshaller();
76 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
77 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
78 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
84 * @throws JAXBException the JAXB exception
85 * @throws UnsupportedEncodingException the unsupported encoding exception
86 * @throws AAIException the AAI exception
89 public void parentQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
93 + "\"related-to\" : \"pserver\","
94 + "\"relationship-data\" : [{"
95 + "\"relationship-key\" : \"pserver.hostname\","
96 + "\"relationship-value\" : \"key1\""
100 Object obj = context.newDynamicEntity("Relationship");
102 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
104 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
105 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
106 String key = "pserver/key1";
107 GraphTraversal<Vertex, Vertex> expected =
108 __.<Vertex>start().has("aai-unique-key", key);
109 String resultType = "pserver";
110 String containerType = "";
112 testSet(query, expected, expected, resultType, containerType);
119 * @throws JAXBException the JAXB exception
120 * @throws UnsupportedEncodingException the unsupported encoding exception
121 * @throws AAIException the AAI exception
124 public void childQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
127 + "\"related-to\" : \"lag-interface\","
128 + "\"relationship-data\" : [{"
129 + "\"relationship-key\" : \"pserver.hostname\","
130 + "\"relationship-value\" : \"key1\""
132 + "\"relationship-key\" : \"lag-interface.interface-name\","
133 + "\"relationship-value\" : \"key2\""
137 Object obj = context.newDynamicEntity("Relationship");
139 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
141 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
142 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
144 String key = "pserver/key1/lag-interface/key2";
145 GraphTraversal<Vertex, Vertex> expected =
146 __.<Vertex>start().has("aai-unique-key", key);
147 GraphTraversal<Vertex, Vertex> parentExpected =
148 __.<Vertex>start().has("aai-unique-key", "pserver/key1");
149 String resultType = "lag-interface";
150 String containerType = "";
152 testSet(query, expected, parentExpected, resultType, containerType);
158 * @throws JAXBException the JAXB exception
159 * @throws UnsupportedEncodingException the unsupported encoding exception
160 * @throws AAIException the AAI exception
163 public void namingExceptions() throws JAXBException, UnsupportedEncodingException, AAIException {
166 + "\"related-to\" : \"cvlan-tag\","
167 + "\"relationship-data\" : [{"
168 + "\"relationship-key\" : \"vce.vnf-id\","
169 + "\"relationship-value\" : \"key1\""
171 + "\"relationship-key\" : \"port-group.interface-id\","
172 + "\"relationship-value\" : \"key2\""
174 + "\"relationship-key\" : \"cvlan-tag.cvlan-tag\","
175 + "\"relationship-value\" : \"655\""
179 Object obj = context.newDynamicEntity("Relationship");
181 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
183 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
184 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
185 String key = "vce/key1/port-group/key2/cvlan-tag/655";
186 GraphTraversal<Vertex, Vertex> expected =
187 __.<Vertex>start().has("aai-unique-key", key);
188 GraphTraversal<Vertex, Vertex> parentExpected =
189 __.<Vertex>start().has("aai-unique-key", "vce/key1/port-group/key2");
190 String resultType = "cvlan-tag";
191 String containerType = "";
193 testSet(query, expected, parentExpected, resultType, containerType);
200 * @throws JAXBException the JAXB exception
201 * @throws UnsupportedEncodingException the unsupported encoding exception
202 * @throws AAIException the AAI exception
205 public void doubleKey() throws JAXBException, UnsupportedEncodingException, AAIException {
208 + "\"related-to\" : \"service-capability\","
209 + "\"relationship-data\" : [{"
210 + "\"relationship-key\" : \"service-capability.service-type\","
211 + "\"relationship-value\" : \"key1\""
213 + "\"relationship-key\" : \"service-capability.vnf-type\","
214 + " \"relationship-value\" : \"key2\""
218 Object obj = context.newDynamicEntity("Relationship");
220 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
222 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
223 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
225 String key = "service-capability/key1/key2";
226 GraphTraversal<Vertex, Vertex> expected =
227 __.<Vertex>start().has("aai-unique-key", key);
228 GraphTraversal<Vertex, Vertex> parentExpected =
229 __.<Vertex>start().has("aai-unique-key", "service-capability/key1/key2");
230 String resultType = "service-capability";
231 String containerType = "";
233 testSet(query, expected, parentExpected, resultType, containerType);
240 * @throws JAXBException the JAXB exception
241 * @throws UnsupportedEncodingException the unsupported encoding exception
242 * @throws AAIException the AAI exception
245 public void shortCircuit() throws JAXBException, UnsupportedEncodingException, AAIException {
248 + "\"related-to\" : \"cvlan-tag\","
249 + "\"related-link\" : \"http://mock-system-name.com:8443/aai/v6/network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655\","
250 + "\"relationship-data\" : [{"
251 + "\"relationship-key\" : \"vce.hostname\","
252 + "\"relationship-value\" : \"key1\""
254 + "\"relationship-key\" : \"port-group.interface-name\","
255 + "\"relationship-value\" : \"key2\""
257 + "\"relationship-key\" : \"cvlan-tag.-name\","
258 + "\"relationship-value\" : \"655\""
262 Object obj = context.newDynamicEntity("Relationship");
264 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
266 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
267 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
268 String key = "vce/key1/port-group/key2/cvlan-tag/655";
269 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
270 GraphTraversal<Vertex, Vertex> parentExpected = __.<Vertex>start().has("aai-unique-key", "vce/key1/port-group/key2");
271 String resultType = "cvlan-tag";
272 String containerType = "";
274 testSet(query, expected, parentExpected, resultType, containerType);
281 * @param query the query
282 * @param expected the expected
283 * @param parentExpected the parent expected
284 * @param resultType the result type
285 * @param containerType the container type
287 public void testSet(QueryParser query, GraphTraversal<Vertex, Vertex> expected, GraphTraversal<Vertex, Vertex> parentExpected, String resultType, String containerType) {
289 "gremlin query should be " + expected,
291 query.getQueryBuilder().getQuery());
293 "parent gremlin query should be " + parentExpected,
295 query.getParentQueryBuilder().getParentQuery());
297 "result type should be " + resultType,
299 query.getResultType());
301 "container type should be " + containerType,
303 query.getContainerType());