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.nodes.NodeIngestor;
35 import org.onap.aai.setup.SchemaVersion;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.onap.aai.serialization.engines.QueryStyle;
38 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
39 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
41 import javax.xml.bind.JAXBException;
42 import javax.xml.bind.Unmarshaller;
43 import javax.xml.transform.stream.StreamSource;
44 import java.io.StringReader;
45 import java.io.UnsupportedEncodingException;
47 import static org.junit.Assert.assertEquals;
50 public class UniqueRelationshipQueryTest extends AAISetup {
53 private NodeIngestor ingestor ;
55 private TransactionalGraphEngine dbEngine;
56 private SchemaVersion version ;
57 private DynamicJAXBContext context = ingestor.getContextForVersion(version);
58 private Unmarshaller unmarshaller = null;
63 * @throws JAXBException the JAXB exception
66 public void setup() throws JAXBException {
67 version = new SchemaVersion("v8");
68 dbEngine = new JanusGraphDBEngine(QueryStyle.GREMLIN_UNIQUE,
69 loaderFactory.createLoaderForVersion(ModelType.MOXY, version),
71 unmarshaller = context.createUnmarshaller();
72 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
73 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
74 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
80 * @throws JAXBException the JAXB exception
81 * @throws UnsupportedEncodingException the unsupported encoding exception
82 * @throws AAIException the AAI exception
85 public void parentQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
89 + "\"related-to\" : \"pserver\","
90 + "\"relationship-data\" : [{"
91 + "\"relationship-key\" : \"pserver.hostname\","
92 + "\"relationship-value\" : \"key1\""
96 Object obj = context.newDynamicEntity("Relationship");
98 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
100 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
101 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
102 String key = "pserver/key1";
103 GraphTraversal<Vertex, Vertex> expected =
104 __.<Vertex>start().has("aai-unique-key", key);
105 String resultType = "pserver";
106 String containerType = "";
108 testSet(query, expected, expected, resultType, containerType);
115 * @throws JAXBException the JAXB exception
116 * @throws UnsupportedEncodingException the unsupported encoding exception
117 * @throws AAIException the AAI exception
120 public void childQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
123 + "\"related-to\" : \"lag-interface\","
124 + "\"relationship-data\" : [{"
125 + "\"relationship-key\" : \"pserver.hostname\","
126 + "\"relationship-value\" : \"key1\""
128 + "\"relationship-key\" : \"lag-interface.interface-name\","
129 + "\"relationship-value\" : \"key2\""
133 Object obj = context.newDynamicEntity("Relationship");
135 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
137 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
138 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
140 String key = "pserver/key1/lag-interface/key2";
141 GraphTraversal<Vertex, Vertex> expected =
142 __.<Vertex>start().has("aai-unique-key", key);
143 GraphTraversal<Vertex, Vertex> parentExpected =
144 __.<Vertex>start().has("aai-unique-key", "pserver/key1");
145 String resultType = "lag-interface";
146 String containerType = "";
148 testSet(query, expected, parentExpected, resultType, containerType);
154 * @throws JAXBException the JAXB exception
155 * @throws UnsupportedEncodingException the unsupported encoding exception
156 * @throws AAIException the AAI exception
159 public void namingExceptions() throws JAXBException, UnsupportedEncodingException, AAIException {
162 + "\"related-to\" : \"cvlan-tag\","
163 + "\"relationship-data\" : [{"
164 + "\"relationship-key\" : \"vce.vnf-id\","
165 + "\"relationship-value\" : \"key1\""
167 + "\"relationship-key\" : \"port-group.interface-id\","
168 + "\"relationship-value\" : \"key2\""
170 + "\"relationship-key\" : \"cvlan-tag.cvlan-tag\","
171 + "\"relationship-value\" : \"655\""
175 Object obj = context.newDynamicEntity("Relationship");
177 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
179 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
180 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
181 String key = "vce/key1/port-group/key2/cvlan-tag/655";
182 GraphTraversal<Vertex, Vertex> expected =
183 __.<Vertex>start().has("aai-unique-key", key);
184 GraphTraversal<Vertex, Vertex> parentExpected =
185 __.<Vertex>start().has("aai-unique-key", "vce/key1/port-group/key2");
186 String resultType = "cvlan-tag";
187 String containerType = "";
189 testSet(query, expected, parentExpected, resultType, containerType);
196 * @throws JAXBException the JAXB exception
197 * @throws UnsupportedEncodingException the unsupported encoding exception
198 * @throws AAIException the AAI exception
201 public void doubleKey() throws JAXBException, UnsupportedEncodingException, AAIException {
204 + "\"related-to\" : \"service-capability\","
205 + "\"relationship-data\" : [{"
206 + "\"relationship-key\" : \"service-capability.service-type\","
207 + "\"relationship-value\" : \"key1\""
209 + "\"relationship-key\" : \"service-capability.vnf-type\","
210 + " \"relationship-value\" : \"key2\""
214 Object obj = context.newDynamicEntity("Relationship");
216 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
218 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
219 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
221 String key = "service-capability/key1/key2";
222 GraphTraversal<Vertex, Vertex> expected =
223 __.<Vertex>start().has("aai-unique-key", key);
224 GraphTraversal<Vertex, Vertex> parentExpected =
225 __.<Vertex>start().has("aai-unique-key", "service-capability/key1/key2");
226 String resultType = "service-capability";
227 String containerType = "";
229 testSet(query, expected, parentExpected, resultType, containerType);
236 * @throws JAXBException the JAXB exception
237 * @throws UnsupportedEncodingException the unsupported encoding exception
238 * @throws AAIException the AAI exception
241 public void shortCircuit() throws JAXBException, UnsupportedEncodingException, AAIException {
244 + "\"related-to\" : \"cvlan-tag\","
245 + "\"related-link\" : \"http://mock-system-name.com:8443/aai/v6/network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655\","
246 + "\"relationship-data\" : [{"
247 + "\"relationship-key\" : \"vce.hostname\","
248 + "\"relationship-value\" : \"key1\""
250 + "\"relationship-key\" : \"port-group.interface-name\","
251 + "\"relationship-value\" : \"key2\""
253 + "\"relationship-key\" : \"cvlan-tag.-name\","
254 + "\"relationship-value\" : \"655\""
258 Object obj = context.newDynamicEntity("Relationship");
260 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
262 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
263 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
264 String key = "vce/key1/port-group/key2/cvlan-tag/655";
265 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
266 GraphTraversal<Vertex, Vertex> parentExpected = __.<Vertex>start().has("aai-unique-key", "vce/key1/port-group/key2");
267 String resultType = "cvlan-tag";
268 String containerType = "";
270 testSet(query, expected, parentExpected, resultType, containerType);
277 * @param query the query
278 * @param expected the expected
279 * @param parentExpected the parent expected
280 * @param resultType the result type
281 * @param containerType the container type
283 public void testSet(QueryParser query, GraphTraversal<Vertex, Vertex> expected, GraphTraversal<Vertex, Vertex> parentExpected, String resultType, String containerType) {
285 "gremlin query should be " + expected,
287 query.getQueryBuilder().getQuery());
289 "parent gremlin query should be " + parentExpected,
291 query.getParentQueryBuilder().getParentQuery());
293 "result type should be " + resultType,
295 query.getResultType());
297 "container type should be " + containerType,
299 query.getContainerType());