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