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