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.UnsupportedEncodingException;
28 import javax.ws.rs.core.UriBuilder;
30 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
31 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
34 import org.junit.Ignore;
35 import org.junit.Test;
36 import org.onap.aai.AAISetup;
37 import org.onap.aai.exceptions.AAIException;
38 import org.onap.aai.introspection.ModelType;
39 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
40 import org.onap.aai.serialization.engines.QueryStyle;
41 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
42 import org.onap.aai.setup.SchemaVersion;
45 public class UniqueURIQueryTest extends AAISetup {
47 private TransactionalGraphEngine dbEngine;
48 private SchemaVersion version;
49 private DynamicJAXBContext context = nodeIngestor.getContextForVersion(version);
54 * @throws UnsupportedEncodingException the unsupported encoding exception
55 * @throws AAIException the AAI exception
58 public void parentQuery() throws UnsupportedEncodingException, AAIException {
59 version = new SchemaVersion("v10");
60 dbEngine = new JanusGraphDBEngine(QueryStyle.GREMLIN_UNIQUE,
61 loaderFactory.createLoaderForVersion(ModelType.MOXY, version), false);
62 URI uri = UriBuilder.fromPath("cloud-infrastructure/complexes/complex/key1").build();
63 String key = "complex/key1";
64 QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
65 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
66 String parentResultType = "";
67 String resultType = "complex";
68 String containerType = "";
70 testSet(query, expected, expected, parentResultType, resultType, containerType);
75 * Parent plural query.
77 * @throws UnsupportedEncodingException the unsupported encoding exception
78 * @throws AAIException the AAI exception
81 public void parentPluralQuery() throws UnsupportedEncodingException, AAIException {
82 URI uri = UriBuilder.fromPath("cloud-infrastructure/complexes").build();
83 QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
84 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-node-type", "complex");
85 String parentResultType = "";
86 String resultType = "complex";
87 String containerType = "complexes";
89 testSet(query, expected, expected, parentResultType, resultType, containerType);
96 * @throws UnsupportedEncodingException the unsupported encoding exception
97 * @throws AAIException the AAI exception
100 public void childQuery() throws UnsupportedEncodingException, AAIException {
101 URI uri = UriBuilder.fromPath("cloud-infrastructure/complexes/complex/key1/ctag-pools/ctag-pool/key2/key3")
103 QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
104 String parentKey = "complex/key1";
105 String key = parentKey + "/ctag-pool/key2/key3";
106 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
107 GraphTraversal<Vertex, Vertex> parentExpected = __.<Vertex>start().has("aai-unique-key", parentKey);
108 String parentResultType = "complex";
109 String resultType = "ctag-pool";
110 String containerType = "";
112 testSet(query, expected, parentExpected, parentResultType, resultType, containerType);
119 * @throws UnsupportedEncodingException the unsupported encoding exception
120 * @throws AAIException the AAI exception
123 public void namingExceptions() throws UnsupportedEncodingException, AAIException {
124 URI uri = UriBuilder.fromPath("network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655")
126 QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
127 String parentKey = "vce/key1/port-group/key2";
128 String key = parentKey + "/cvlan-tag/655";
129 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
130 GraphTraversal<Vertex, Vertex> parentExpected = __.<Vertex>start().has("aai-unique-key", parentKey);
131 String parentResultType = "port-group";
132 String resultType = "cvlan-tag";
133 String containerType = "";
135 testSet(query, expected, parentExpected, parentResultType, resultType, containerType);
143 * @throws UnsupportedEncodingException the unsupported encoding exception
144 * @throws AAIException the AAI exception
147 public void getAll() throws UnsupportedEncodingException, AAIException {
148 String parentURI = "network/vces/vce/key1/port-groups/port-group/key2";
149 String parentKey = "vce/key1/port-group/key2";
150 URI uri = UriBuilder.fromPath(parentURI + "/cvlan-tags").build();
151 QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
152 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", parentKey)
153 .in("org.onap.relationships.inventory.BelongsTo").has("aai-node-type", "cvlan-tag");
154 GraphTraversal<Vertex, Vertex> parentExpected = __.<Vertex>start().has("aai-unique-key", parentKey);
155 String parentResultType = "port-group";
156 String resultType = "cvlan-tag";
157 String containerType = "cvlan-tags";
159 testSet(query, expected, parentExpected, parentResultType, resultType, containerType);
166 * @param query the query
167 * @param expected the expected
168 * @param parentExpected the parent expected
169 * @param parentResultType the parent result type
170 * @param resultType the result type
171 * @param containerType the container type
173 public void testSet(QueryParser query, GraphTraversal<Vertex, Vertex> expected,
174 GraphTraversal<Vertex, Vertex> parentExpected, String parentResultType, String resultType,
175 String containerType) {
176 assertEquals("gremlin query should be " + expected, expected, query.getQueryBuilder().getQuery());
177 assertEquals("parent gremlin query should be " + parentExpected, parentExpected,
178 query.getQueryBuilder().getParentQuery().getQuery());
179 assertEquals("parent result type should be " + parentResultType, parentResultType, query.getParentResultType());
180 assertEquals("result type should be " + resultType, resultType, query.getResultType());
181 assertEquals("container type should be " + containerType, containerType, query.getContainerType());