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