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