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