5c31fd016e37b1c7e45372f481ec2d8de3301cdd
[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-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
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 package org.onap.aai.parsers.query;
21
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.jaxb.dynamic.DynamicJAXBContext;
26 import org.junit.Ignore;
27 import org.junit.Test;
28 import org.onap.aai.AAISetup;
29 import org.onap.aai.exceptions.AAIException;
30
31 import org.onap.aai.introspection.ModelType;
32 import org.onap.aai.setup.SchemaVersion;
33 import org.onap.aai.serialization.engines.QueryStyle;
34 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
35 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
36
37 import javax.ws.rs.core.UriBuilder;
38 import java.io.UnsupportedEncodingException;
39 import java.net.URI;
40
41 import static org.junit.Assert.assertEquals;
42
43 @Ignore
44 public class UniqueURIQueryTest extends AAISetup {
45
46
47         private TransactionalGraphEngine dbEngine;
48         private SchemaVersion version;
49         private DynamicJAXBContext context = nodeIngestor.getContextForVersion(version);
50         
51         /**
52          * Parent query.
53          *
54          * @throws UnsupportedEncodingException the unsupported encoding exception
55          * @throws AAIException the AAI exception
56          */
57         @Test
58     public void parentQuery() throws UnsupportedEncodingException, AAIException {
59             version = new SchemaVersion("v8");
60             dbEngine = new JanusGraphDBEngine(QueryStyle.GREMLIN_UNIQUE,
61                                 loaderFactory.createLoaderForVersion(ModelType.MOXY, version),
62                                 false);
63                 URI uri = UriBuilder.fromPath("cloud-infrastructure/complexes/complex/key1").build();
64                 String key = "complex/key1";
65                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
66                 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
67                 String parentResultType = "";
68                 String resultType = "complex";
69                 String containerType = "";
70                 
71                 testSet(query, expected, expected, parentResultType, resultType, containerType);
72                 
73     }
74         
75         /**
76          * Parent plural query.
77          *
78          * @throws UnsupportedEncodingException the unsupported encoding exception
79          * @throws AAIException the AAI exception
80          */
81         @Test
82     public void parentPluralQuery() throws UnsupportedEncodingException, AAIException {
83                 URI uri = UriBuilder.fromPath("cloud-infrastructure/complexes").build();
84                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
85                 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-node-type", "complex");
86                 String parentResultType = "";
87                 String resultType = "complex";
88                 String containerType = "complexes";
89                 
90                 testSet(query, expected, expected, parentResultType, resultType, containerType);
91                 
92     }
93
94         /**
95          * Child query.
96          *
97          * @throws UnsupportedEncodingException the unsupported encoding exception
98          * @throws AAIException the AAI exception
99          */
100         @Test
101     public void childQuery() throws UnsupportedEncodingException, AAIException {
102                 URI uri = UriBuilder.fromPath("cloud-infrastructure/complexes/complex/key1/ctag-pools/ctag-pool/key2/key3").build();
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 = "";
111                 
112                 testSet(query, expected, parentExpected, parentResultType, resultType, containerType);
113                 
114     }
115         
116         /**
117          * Naming exceptions.
118          *
119          * @throws UnsupportedEncodingException the unsupported encoding exception
120          * @throws AAIException the AAI exception
121          */
122         @Test
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").build();
125                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
126                 String parentKey = "vce/key1/port-group/key2";
127                 String key = parentKey + "/cvlan-tag/655";
128                 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
129                 GraphTraversal<Vertex, Vertex> parentExpected = __.<Vertex>start().has("aai-unique-key", parentKey);
130                 String parentResultType = "port-group";
131                 String resultType = "cvlan-tag";
132                 String containerType = "";
133                 
134                 testSet(query, expected, parentExpected, parentResultType, resultType, containerType);
135                 
136     }
137         
138         /**
139          * Gets the all.
140          *
141          * @return the all
142          * @throws UnsupportedEncodingException the unsupported encoding exception
143          * @throws AAIException the AAI exception
144          */
145         @Test
146     public void getAll() throws UnsupportedEncodingException, AAIException {
147                 String parentURI = "network/vces/vce/key1/port-groups/port-group/key2";
148                 String parentKey = "vce/key1/port-group/key2";
149                 URI uri = UriBuilder.fromPath(parentURI + "/cvlan-tags").build();
150                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
151                 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", parentKey).in("org.onap.relationships.inventory.BelongsTo").has("aai-node-type", "cvlan-tag");
152                 GraphTraversal<Vertex, Vertex> parentExpected = __.<Vertex>start().has("aai-unique-key",parentKey);
153                 String parentResultType = "port-group";
154                 String resultType = "cvlan-tag";
155                 String containerType = "cvlan-tags";
156                 
157                 testSet(query, expected, parentExpected, parentResultType, resultType, containerType);
158                 
159     }
160         
161         /**
162          * Test set.
163          *
164          * @param query the query
165          * @param expected the expected
166          * @param parentExpected the parent expected
167          * @param parentResultType the parent result type
168          * @param resultType the result type
169          * @param containerType the container type
170          */
171         public void testSet(QueryParser query, GraphTraversal<Vertex, Vertex> expected, GraphTraversal<Vertex, Vertex> parentExpected, String parentResultType, String resultType, String containerType) {
172                 assertEquals(
173                                 "gremlin query should be " + expected,
174                                 expected,
175                                 query.getQueryBuilder().getQuery());
176                 assertEquals(
177                                 "parent gremlin query should be " + parentExpected,
178                                 parentExpected,
179                                 query.getQueryBuilder().getParentQuery().getQuery());
180                 assertEquals(
181                                 "parent result type should be " + parentResultType,
182                                 parentResultType,
183                                 query.getParentResultType());
184                 assertEquals(
185                                 "result type should be " + resultType,
186                                 resultType,
187                                 query.getResultType());
188                 assertEquals(
189                                 "container type should be " + containerType,
190                                 containerType,
191                                 query.getContainerType());
192         }
193 }