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=========================================================
21 package org.onap.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.Ignore;
40 import org.junit.Test;
41 import org.onap.aai.AAISetup;
42 import org.onap.aai.exceptions.AAIException;
43 import org.onap.aai.introspection.*;
44 import org.onap.aai.nodes.NodeIngestor;
45 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
46 import org.onap.aai.serialization.engines.QueryStyle;
47 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
48 import org.onap.aai.setup.SchemaVersion;
49 import org.springframework.beans.factory.annotation.Autowired;
52 public class UniqueRelationshipQueryTest extends AAISetup {
55 private NodeIngestor ingestor;
57 private TransactionalGraphEngine dbEngine;
58 private SchemaVersion version;
59 private DynamicJAXBContext context = ingestor.getContextForVersion(version);
60 private Unmarshaller unmarshaller = null;
65 * @throws JAXBException the JAXB exception
68 public void setup() throws JAXBException {
69 version = new SchemaVersion("v10");
70 dbEngine = new JanusGraphDBEngine(QueryStyle.GREMLIN_UNIQUE,
71 loaderFactory.createLoaderForVersion(ModelType.MOXY, version), false);
72 unmarshaller = context.createUnmarshaller();
73 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
74 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
75 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
81 * @throws JAXBException the JAXB exception
82 * @throws UnsupportedEncodingException the unsupported encoding exception
83 * @throws AAIException the AAI exception
86 public void parentQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
88 String content = "{" + "\"related-to\" : \"pserver\"," + "\"relationship-data\" : [{"
89 + "\"relationship-key\" : \"pserver.hostname\"," + "\"relationship-value\" : \"key1\"" + "}]" + "}";
91 Object obj = context.newDynamicEntity("Relationship");
93 DynamicEntity entity = (DynamicEntity) unmarshaller
94 .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 = __.<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 {
116 String content = "{" + "\"related-to\" : \"lag-interface\"," + "\"relationship-data\" : [{"
117 + "\"relationship-key\" : \"pserver.hostname\"," + "\"relationship-value\" : \"key1\"" + "}, {"
118 + "\"relationship-key\" : \"lag-interface.interface-name\"," + "\"relationship-value\" : \"key2\""
121 Object obj = context.newDynamicEntity("Relationship");
123 DynamicEntity entity = (DynamicEntity) unmarshaller
124 .unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
126 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
127 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
129 String key = "pserver/key1/lag-interface/key2";
130 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
131 GraphTraversal<Vertex, Vertex> parentExpected = __.<Vertex>start().has("aai-unique-key", "pserver/key1");
132 String resultType = "lag-interface";
133 String containerType = "";
135 testSet(query, expected, parentExpected, resultType, containerType);
141 * @throws JAXBException the JAXB exception
142 * @throws UnsupportedEncodingException the unsupported encoding exception
143 * @throws AAIException the AAI exception
146 public void namingExceptions() throws JAXBException, UnsupportedEncodingException, AAIException {
147 String content = "{" + "\"related-to\" : \"cvlan-tag\"," + "\"relationship-data\" : [{"
148 + "\"relationship-key\" : \"vce.vnf-id\"," + "\"relationship-value\" : \"key1\"" + "}, {"
149 + "\"relationship-key\" : \"port-group.interface-id\"," + "\"relationship-value\" : \"key2\"" + "},{"
150 + "\"relationship-key\" : \"cvlan-tag.cvlan-tag\"," + "\"relationship-value\" : \"655\"" + "}]" + "}";
152 Object obj = context.newDynamicEntity("Relationship");
154 DynamicEntity entity = (DynamicEntity) unmarshaller
155 .unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
157 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
158 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
159 String key = "vce/key1/port-group/key2/cvlan-tag/655";
160 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
161 GraphTraversal<Vertex, Vertex> parentExpected =
162 __.<Vertex>start().has("aai-unique-key", "vce/key1/port-group/key2");
163 String resultType = "cvlan-tag";
164 String containerType = "";
166 testSet(query, expected, parentExpected, resultType, containerType);
173 * @throws JAXBException the JAXB exception
174 * @throws UnsupportedEncodingException the unsupported encoding exception
175 * @throws AAIException the AAI exception
178 public void doubleKey() throws JAXBException, UnsupportedEncodingException, AAIException {
179 String content = "{" + "\"related-to\" : \"service-capability\"," + "\"relationship-data\" : [{"
180 + "\"relationship-key\" : \"service-capability.service-type\"," + "\"relationship-value\" : \"key1\""
181 + " }, { " + "\"relationship-key\" : \"service-capability.vnf-type\","
182 + " \"relationship-value\" : \"key2\"" + " }]" + "}";
184 Object obj = context.newDynamicEntity("Relationship");
186 DynamicEntity entity = (DynamicEntity) unmarshaller
187 .unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
189 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
190 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
192 String key = "service-capability/key1/key2";
193 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
194 GraphTraversal<Vertex, Vertex> parentExpected =
195 __.<Vertex>start().has("aai-unique-key", "service-capability/key1/key2");
196 String resultType = "service-capability";
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 shortCircuit() throws JAXBException, UnsupportedEncodingException, AAIException {
212 String content = "{" + "\"related-to\" : \"cvlan-tag\","
213 + "\"related-link\" : \"http://mock-system-name.com:8443/aai/v6/network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655\","
214 + "\"relationship-data\" : [{" + "\"relationship-key\" : \"vce.hostname\","
215 + "\"relationship-value\" : \"key1\"" + "}, {" + "\"relationship-key\" : \"port-group.interface-name\","
216 + "\"relationship-value\" : \"key2\"" + "},{" + "\"relationship-key\" : \"cvlan-tag.-name\","
217 + "\"relationship-value\" : \"655\"" + "}]" + "}";
219 Object obj = context.newDynamicEntity("Relationship");
221 DynamicEntity entity = (DynamicEntity) unmarshaller
222 .unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
224 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
225 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
226 String key = "vce/key1/port-group/key2/cvlan-tag/655";
227 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
228 GraphTraversal<Vertex, Vertex> parentExpected =
229 __.<Vertex>start().has("aai-unique-key", "vce/key1/port-group/key2");
230 String resultType = "cvlan-tag";
231 String containerType = "";
233 testSet(query, expected, parentExpected, resultType, containerType);
240 * @param query the query
241 * @param expected the expected
242 * @param parentExpected the parent expected
243 * @param resultType the result type
244 * @param containerType the container type
246 public void testSet(QueryParser query, GraphTraversal<Vertex, Vertex> expected,
247 GraphTraversal<Vertex, Vertex> parentExpected, String resultType, String containerType) {
248 assertEquals("gremlin query should be " + expected, expected, query.getQueryBuilder().getQuery());
249 assertEquals("parent gremlin query should be " + parentExpected, parentExpected,
250 query.getParentQueryBuilder().getParentQuery());
251 assertEquals("result type should be " + resultType, resultType, query.getResultType());
252 assertEquals("container type should be " + containerType, containerType, query.getContainerType());