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