d9dcabd344d8ac16ceb0f1089a2dcc2e9494b929
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / parsers / query / UniqueURIQueryTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  *
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.parsers.query;
23
24 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
25 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
26 import org.apache.tinkerpop.gremlin.structure.Vertex;
27 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
28 import org.junit.Ignore;
29 import org.junit.Test;
30 import org.onap.aai.AAISetup;
31 import org.onap.aai.exceptions.AAIException;
32 import org.onap.aai.introspection.LoaderFactory;
33 import org.onap.aai.introspection.ModelInjestor;
34 import org.onap.aai.introspection.ModelType;
35 import org.onap.aai.introspection.Version;
36 import org.onap.aai.serialization.engines.QueryStyle;
37 import org.onap.aai.serialization.engines.TitanDBEngine;
38 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
39
40 import javax.ws.rs.core.UriBuilder;
41 import java.io.UnsupportedEncodingException;
42 import java.net.URI;
43
44 import static org.junit.Assert.assertEquals;
45
46 @Ignore
47 public class UniqueURIQueryTest extends AAISetup {
48
49         private ModelInjestor injestor = ModelInjestor.getInstance();
50         private TransactionalGraphEngine dbEngine = 
51                         new TitanDBEngine(QueryStyle.GREMLIN_UNIQUE, 
52                                 LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v8),
53                                 false);
54         private final Version version = Version.v8;
55         private DynamicJAXBContext context = injestor.getContextForVersion(version);
56         
57         /**
58          * Parent query.
59          *
60          * @throws UnsupportedEncodingException the unsupported encoding exception
61          * @throws AAIException the AAI exception
62          */
63         @Test
64     public void parentQuery() throws UnsupportedEncodingException, AAIException {
65                 URI uri = UriBuilder.fromPath("cloud-infrastructure/complexes/complex/key1").build();
66                 String key = "complex/key1";
67                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
68                 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
69                 String parentResultType = "";
70                 String resultType = "complex";
71                 String containerType = "";
72                 
73                 testSet(query, expected, expected, parentResultType, resultType, containerType);
74                 
75     }
76         
77         /**
78          * Parent plural query.
79          *
80          * @throws UnsupportedEncodingException the unsupported encoding exception
81          * @throws AAIException the AAI exception
82          */
83         @Test
84     public void parentPluralQuery() throws UnsupportedEncodingException, AAIException {
85                 URI uri = UriBuilder.fromPath("cloud-infrastructure/complexes").build();
86                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
87                 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-node-type", "complex");
88                 String parentResultType = "";
89                 String resultType = "complex";
90                 String containerType = "complexes";
91                 
92                 testSet(query, expected, expected, parentResultType, resultType, containerType);
93                 
94     }
95
96         /**
97          * Child query.
98          *
99          * @throws UnsupportedEncodingException the unsupported encoding exception
100          * @throws AAIException the AAI exception
101          */
102         @Test
103     public void childQuery() throws UnsupportedEncodingException, AAIException {
104                 URI uri = UriBuilder.fromPath("cloud-infrastructure/complexes/complex/key1/ctag-pools/ctag-pool/key2/key3").build();
105                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
106                 String parentKey = "complex/key1";
107                 String key = parentKey + "/ctag-pool/key2/key3";
108                 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
109                 GraphTraversal<Vertex, Vertex> parentExpected =  __.<Vertex>start().has("aai-unique-key", parentKey);
110                 String parentResultType = "complex";
111                 String resultType = "ctag-pool";
112                 String containerType = "";
113                 
114                 testSet(query, expected, parentExpected, parentResultType, resultType, containerType);
115                 
116     }
117         
118         /**
119          * Naming exceptions.
120          *
121          * @throws UnsupportedEncodingException the unsupported encoding exception
122          * @throws AAIException the AAI exception
123          */
124         @Test
125     public void namingExceptions() throws UnsupportedEncodingException, AAIException {
126                 URI uri = UriBuilder.fromPath("network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655").build();
127                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
128                 String parentKey = "vce/key1/port-group/key2";
129                 String key = parentKey + "/cvlan-tag/655";
130                 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
131                 GraphTraversal<Vertex, Vertex> parentExpected = __.<Vertex>start().has("aai-unique-key", parentKey);
132                 String parentResultType = "port-group";
133                 String resultType = "cvlan-tag";
134                 String containerType = "";
135                 
136                 testSet(query, expected, parentExpected, parentResultType, resultType, containerType);
137                 
138     }
139         
140         /**
141          * Gets the all.
142          *
143          * @return the all
144          * @throws UnsupportedEncodingException the unsupported encoding exception
145          * @throws AAIException the AAI exception
146          */
147         @Test
148     public void getAll() throws UnsupportedEncodingException, AAIException {
149                 String parentURI = "network/vces/vce/key1/port-groups/port-group/key2";
150                 String parentKey = "vce/key1/port-group/key2";
151                 URI uri = UriBuilder.fromPath(parentURI + "/cvlan-tags").build();
152                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
153                 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", parentKey).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";
158                 
159                 testSet(query, expected, parentExpected, parentResultType, resultType, containerType);
160                 
161     }
162         
163         /**
164          * Test set.
165          *
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
172          */
173         public void testSet(QueryParser query, GraphTraversal<Vertex, Vertex> expected, GraphTraversal<Vertex, Vertex> parentExpected, String parentResultType, String resultType, String containerType) {
174                 assertEquals(
175                                 "gremlin query should be " + expected,
176                                 expected,
177                                 query.getQueryBuilder().getQuery());
178                 assertEquals(
179                                 "parent gremlin query should be " + parentExpected,
180                                 parentExpected,
181                                 query.getQueryBuilder().getParentQuery().getQuery());
182                 assertEquals(
183                                 "parent result type should be " + parentResultType,
184                                 parentResultType,
185                                 query.getParentResultType());
186                 assertEquals(
187                                 "result type should be " + resultType,
188                                 resultType,
189                                 query.getResultType());
190                 assertEquals(
191                                 "container type should be " + containerType,
192                                 containerType,
193                                 query.getContainerType());
194         }
195 }