EdgeRules throws descriptive error on invalid rule
[aai/aai-common.git] / aai-core / src / test / java / org / openecomp / aai / parsers / query / UniqueRelationshipQueryTest.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.dynamic.DynamicEntity;
27 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
28 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
29 import org.junit.Before;
30 import org.junit.Ignore;
31 import org.junit.Test;
32 import org.openecomp.aai.AAISetup;
33 import org.openecomp.aai.exceptions.AAIException;
34 import org.openecomp.aai.introspection.*;
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.xml.bind.JAXBException;
40 import javax.xml.bind.Unmarshaller;
41 import javax.xml.transform.stream.StreamSource;
42 import java.io.StringReader;
43 import java.io.UnsupportedEncodingException;
44
45 import static org.junit.Assert.assertEquals;
46
47 @Ignore
48 public class UniqueRelationshipQueryTest extends AAISetup {
49
50         private ModelInjestor injestor = ModelInjestor.getInstance();
51         private TransactionalGraphEngine dbEngine = 
52                         new TitanDBEngine(QueryStyle.GREMLIN_UNIQUE, 
53                                 LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v8),
54                                 false);
55         private final Version version = Version.v8;
56         private DynamicJAXBContext context = injestor.getContextForVersion(version);
57         private Unmarshaller unmarshaller = null;
58         
59         /**
60          * Setup.
61          *
62          * @throws JAXBException the JAXB exception
63          */
64         @Before
65         public void setup() throws JAXBException {
66                 unmarshaller = context.createUnmarshaller();
67             unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
68             unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
69                 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
70         }
71         
72         /**
73          * Parent query.
74          *
75          * @throws JAXBException the JAXB exception
76          * @throws UnsupportedEncodingException the unsupported encoding exception
77          * @throws AAIException the AAI exception
78          */
79         @Test
80     public void parentQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
81                 
82                 String content =
83                                 "{"
84                                 + "\"related-to\" : \"pserver\","
85                                 + "\"relationship-data\" : [{"
86                                 + "\"relationship-key\" : \"pserver.hostname\","
87                                 + "\"relationship-value\" : \"key1\""
88                                 + "}]"
89                                 + "}";
90                                                 
91                 Object obj = context.newDynamicEntity("Relationship");
92
93                 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
94                         
95                 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
96                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
97                 String key = "pserver/key1";
98                 GraphTraversal<Vertex, Vertex> expected = 
99                                 __.<Vertex>start().has("aai-unique-key", key);
100                 String resultType = "pserver";
101                 String containerType = "";
102                 
103                 testSet(query, expected, expected, resultType, containerType);
104                 
105     }
106
107         /**
108          * Child query.
109          *
110          * @throws JAXBException the JAXB exception
111          * @throws UnsupportedEncodingException the unsupported encoding exception
112          * @throws AAIException the AAI exception
113          */
114         @Test
115     public void childQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
116                 String content =
117                                 "{"
118                                 + "\"related-to\" : \"lag-interface\","
119                                 + "\"relationship-data\" : [{"
120                                 + "\"relationship-key\" : \"pserver.hostname\","
121                                 + "\"relationship-value\" : \"key1\""
122                                 + "}, {"
123                                 + "\"relationship-key\" : \"lag-interface.interface-name\","
124                                 + "\"relationship-value\" : \"key2\""
125                                 + "}]"
126                                 + "}";
127                                                 
128                 Object obj = context.newDynamicEntity("Relationship");
129
130                 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
131                         
132                 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
133                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
134
135                 String key = "pserver/key1/lag-interface/key2";
136                 GraphTraversal<Vertex, Vertex> expected = 
137                                 __.<Vertex>start().has("aai-unique-key", key);
138                 GraphTraversal<Vertex, Vertex> parentExpected = 
139                                 __.<Vertex>start().has("aai-unique-key", "pserver/key1");
140                 String resultType = "lag-interface";
141                 String containerType = "";
142                 
143                 testSet(query, expected, parentExpected, resultType, containerType);
144     }
145         
146         /**
147          * Naming exceptions.
148          *
149          * @throws JAXBException the JAXB exception
150          * @throws UnsupportedEncodingException the unsupported encoding exception
151          * @throws AAIException the AAI exception
152          */
153         @Test
154     public void namingExceptions() throws JAXBException, UnsupportedEncodingException, AAIException {
155                 String content =
156                                 "{"
157                                 + "\"related-to\" : \"cvlan-tag\","
158                                 + "\"relationship-data\" : [{"
159                                 + "\"relationship-key\" : \"vce.vnf-id\","
160                                 + "\"relationship-value\" : \"key1\""
161                                 + "}, {"
162                                 + "\"relationship-key\" : \"port-group.interface-id\","
163                                 + "\"relationship-value\" : \"key2\""
164                                 + "},{"
165                                 + "\"relationship-key\" : \"cvlan-tag.cvlan-tag\","
166                                 + "\"relationship-value\" : \"655\""
167                                 + "}]"
168                                 + "}";
169                                                 
170                 Object obj = context.newDynamicEntity("Relationship");
171
172                 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
173                         
174                 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
175                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
176                 String key = "vce/key1/port-group/key2/cvlan-tag/655";
177                 GraphTraversal<Vertex, Vertex> expected = 
178                                 __.<Vertex>start().has("aai-unique-key", key);
179                 GraphTraversal<Vertex, Vertex> parentExpected = 
180                                 __.<Vertex>start().has("aai-unique-key", "vce/key1/port-group/key2");
181                 String resultType = "cvlan-tag";
182                 String containerType = "";
183                 
184                 testSet(query, expected, parentExpected, resultType, containerType);
185                 
186     }
187         
188         /**
189          * Double key.
190          *
191          * @throws JAXBException the JAXB exception
192          * @throws UnsupportedEncodingException the unsupported encoding exception
193          * @throws AAIException the AAI exception
194          */
195         @Test
196     public void doubleKey() throws JAXBException, UnsupportedEncodingException, AAIException {
197                 String content =
198                                 "{"
199                                 + "\"related-to\" : \"service-capability\","
200                                 + "\"relationship-data\" : [{"
201                                 + "\"relationship-key\" : \"service-capability.service-type\","
202                                 + "\"relationship-value\" : \"key1\""
203                                 + " }, { "
204                                 + "\"relationship-key\" : \"service-capability.vnf-type\","
205                                 + " \"relationship-value\" : \"key2\""
206                                 + " }]"
207                                 + "}";
208                                                 
209                 Object obj = context.newDynamicEntity("Relationship");
210
211                 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
212                         
213                 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
214                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
215
216                 String key = "service-capability/key1/key2";
217                 GraphTraversal<Vertex, Vertex> expected = 
218                                 __.<Vertex>start().has("aai-unique-key", key);
219                 GraphTraversal<Vertex, Vertex> parentExpected = 
220                                 __.<Vertex>start().has("aai-unique-key", "service-capability/key1/key2");
221                 String resultType = "service-capability";
222                 String containerType = "";
223                 
224                 testSet(query, expected, parentExpected, resultType, containerType);
225                 
226     }
227         
228         /**
229          * Short circuit.
230          *
231          * @throws JAXBException the JAXB exception
232          * @throws UnsupportedEncodingException the unsupported encoding exception
233          * @throws AAIException the AAI exception
234          */
235         @Test
236     public void shortCircuit() throws JAXBException, UnsupportedEncodingException, AAIException {
237                 String content =
238                                 "{"
239                                 + "\"related-to\" : \"cvlan-tag\","
240                                 + "\"related-link\" : \"http://mock-system-name.com:8443/aai/v6/network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655\","
241                                 + "\"relationship-data\" : [{"
242                                 + "\"relationship-key\" : \"vce.hostname\","
243                                 + "\"relationship-value\" : \"key1\""
244                                 + "}, {"
245                                 + "\"relationship-key\" : \"port-group.interface-name\","
246                                 + "\"relationship-value\" : \"key2\""
247                                 + "},{"
248                                 + "\"relationship-key\" : \"cvlan-tag.-name\","
249                                 + "\"relationship-value\" : \"655\""
250                                 + "}]"
251                                 + "}";
252                                                 
253                 Object obj = context.newDynamicEntity("Relationship");
254
255                 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
256                         
257                 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
258                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
259                 String key = "vce/key1/port-group/key2/cvlan-tag/655";
260                 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
261                 GraphTraversal<Vertex, Vertex> parentExpected = __.<Vertex>start().has("aai-unique-key", "vce/key1/port-group/key2");
262                 String resultType = "cvlan-tag";
263                 String containerType = "";
264                 
265                 testSet(query, expected, parentExpected, resultType, containerType);
266                 
267     }
268         
269         /**
270          * Test set.
271          *
272          * @param query the query
273          * @param expected the expected
274          * @param parentExpected the parent expected
275          * @param resultType the result type
276          * @param containerType the container type
277          */
278         public void testSet(QueryParser query, GraphTraversal<Vertex, Vertex> expected, GraphTraversal<Vertex, Vertex> parentExpected, String resultType, String containerType) {
279                 assertEquals(
280                                 "gremlin query should be " + expected,
281                                 expected,
282                                 query.getQueryBuilder().getQuery());
283                 assertEquals(
284                                 "parent gremlin query should be " + parentExpected,
285                                 parentExpected,
286                                 query.getParentQueryBuilder().getParentQuery());
287                 assertEquals(
288                                 "result type should be " + resultType,
289                                 resultType,
290                                 query.getResultType());
291                 assertEquals(
292                                 "container type should be " + containerType,
293                                 containerType,
294                                 query.getContainerType());
295         }
296 }