4baa944bc4cf8bc5724e3ef295d17c6b7ab068f9
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / parsers / query / RelationshipQueryTest.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.eclipse.persistence.dynamic.DynamicEntity;
23 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
24 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
25 import org.junit.Before;
26 import org.junit.Ignore;
27 import org.junit.Test;
28 import org.onap.aai.AAISetup;
29 import org.onap.aai.exceptions.AAIException;
30 import org.onap.aai.introspection.*;
31 import org.onap.aai.setup.SchemaVersion;
32 import org.onap.aai.serialization.engines.QueryStyle;
33 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
34 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
35
36 import javax.xml.bind.JAXBException;
37 import javax.xml.bind.Unmarshaller;
38 import javax.xml.transform.stream.StreamSource;
39 import java.io.StringReader;
40 import java.io.UnsupportedEncodingException;
41
42 import static org.junit.Assert.assertEquals;
43
44 @Ignore
45 public class RelationshipQueryTest extends AAISetup {
46
47
48         private TransactionalGraphEngine dbEngine;
49         private SchemaVersion version;
50         private DynamicJAXBContext context = nodeIngestor.getContextForVersion(version);
51
52         @Before
53         public void setup(){
54             version = new SchemaVersion("v10");
55                 dbEngine =
56                         new JanusGraphDBEngine(QueryStyle.GREMLIN_TRAVERSAL,
57                                 loaderFactory.createLoaderForVersion(ModelType.MOXY, version),
58                                 false);
59         }
60         /**
61          * Parent query.
62          *
63          * @throws JAXBException the JAXB exception
64          * @throws UnsupportedEncodingException the unsupported encoding exception
65          * @throws AAIException the AAI exception
66          */
67         @Test
68     public void parentQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
69
70                 String content =
71                                 "{"
72                                 + "\"related-to\" : \"pserver\","
73                                 + "\"relationship-data\" : [{"
74                                 + "\"relationship-key\" : \"pserver.hostname\","
75                                 + "\"relationship-value\" : \"key1\""
76                                 + "}]"
77                                 + "}";
78
79                 Unmarshaller unmarshaller = context.createUnmarshaller();
80             unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
81             unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
82                 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
83                 Object obj = context.newDynamicEntity("Relationship");
84
85                 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
86
87                 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
88                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
89
90                 String expected =
91                                 ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
92                 assertEquals(
93                                 "gremlin query should be " + expected,
94                                 expected,
95                                 query.getQueryBuilder().getQuery());
96                 assertEquals(
97                                 "parent gremlin query should be equal to normal query",
98                                 expected,
99                                 query.getQueryBuilder().getParentQuery().getQuery());
100                 assertEquals(
101                                 "result type should be pserver",
102                                 "pserver",
103                                 query.getResultType());
104
105     }
106
107         /**
108          * Child query.
109          *
110          * @throws JAXBException the JAXB exception
111          * @throws UnsupportedEncodingException the unsupported encoding exception
112          * @throws AAIException the AAI exception
113          */
114         @Ignore
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                 Unmarshaller unmarshaller = context.createUnmarshaller();
130             unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
131             unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
132                 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
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                 String expected =
140                                 ".has('hostname', 'key1').has('aai-node-type', 'pserver').out('hasLAGInterface').has('aai-node-type', 'lag-interface')"
141                                 + ".has('interface-name', 'key2')";
142                 String parentExpected =
143                                 ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
144                 assertEquals(
145                                 "gremlin query should be for node",
146                                 expected,
147                                 query.getQueryBuilder().getQuery());
148                 assertEquals(
149                                 "parent gremlin query should be for parent",
150                                 parentExpected,
151                                 query.getQueryBuilder().getParentQuery().getQuery());
152                 assertEquals(
153                                 "result type should be lag-interface",
154                                 "lag-interface",
155                                 query.getResultType());
156     }
157
158         /**
159          * Naming exceptions.
160          *
161          * @throws JAXBException the JAXB exception
162          * @throws UnsupportedEncodingException the unsupported encoding exception
163          * @throws AAIException the AAI exception
164          */
165         @Ignore
166         @Test
167     public void namingExceptions() throws JAXBException, UnsupportedEncodingException, AAIException {
168                 String content =
169                                 "{"
170                                 + "\"related-to\" : \"cvlan-tag\","
171                                 + "\"relationship-data\" : [{"
172                                 + "\"relationship-key\" : \"vce.vnf-id\","
173                                 + "\"relationship-value\" : \"key1\""
174                                 + "}, {"
175                                 + "\"relationship-key\" : \"port-group.interface-id\","
176                                 + "\"relationship-value\" : \"key2\""
177                                 + "},{"
178                                 + "\"relationship-key\" : \"cvlan-tag.cvlan-tag\","
179                                 + "\"relationship-value\" : \"655\""
180                                 + "}]"
181                                 + "}";
182
183                 Unmarshaller unmarshaller = context.createUnmarshaller();
184             unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
185             unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
186                 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
187                 Object obj = context.newDynamicEntity("Relationship");
188
189                 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
190
191                 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
192                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
193                 String expected =
194                                 ".has('vnf-id', 'key1').has('aai-node-type', 'vce').in('org.onap.relationships.inventory.BelongsTo')"
195                                 + ".has('aai-node-type', 'port-group').has('interface-id', 'key2').in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'cvlan-tag')"
196                                 + ".has('cvlan-tag', 655)";
197                 String expectedParent =
198                                                 ".has('vnf-id', 'key1').has('aai-node-type', 'vce').in('org.onap.relationships.inventory.BelongsTo')"
199                                                 + ".has('aai-node-type', 'port-group').has('interface-id', 'key2')";
200
201                 assertEquals(
202                                 "gremlin query should be " + expected,
203                                 expected,
204                                 query.getQueryBuilder().getQuery());
205                 assertEquals(
206                                 "parent gremlin query should be equal the query for port group",
207                                 expectedParent,
208                                 query.getQueryBuilder().getParentQuery().getQuery());
209                 assertEquals(
210                                 "result type should be cvlan-tag",
211                                 "cvlan-tag",
212                                 query.getResultType());
213
214     }
215
216         /**
217          * Double key.
218          *
219          * @throws JAXBException the JAXB exception
220          * @throws UnsupportedEncodingException the unsupported encoding exception
221          * @throws AAIException the AAI exception
222          */
223         @Ignore
224         @Test
225     public void doubleKey() throws JAXBException, UnsupportedEncodingException, AAIException {
226                 String content =
227                                 "{"
228                                 + "\"related-to\" : \"ctag-pool\","
229                                 + "\"relationship-data\" : [{"
230                                 + "\"relationship-key\" : \"complex.physical-location-id\","
231                                 + "\"relationship-value\" : \"key1\""
232                                 + " }, { "
233                                 + "\"relationship-key\" : \"ctag-pool.target-pe\","
234                                 + " \"relationship-value\" : \"key2\""
235                                 + " },{"
236                                 + "\"relationship-key\" : \"ctag-pool.availability-zone-name\","
237                                 + "\"relationship-value\" : \"key3\""
238                                 + "}]"
239                                 + "}";
240
241
242                 Unmarshaller unmarshaller = context.createUnmarshaller();
243             unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
244             unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
245                 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
246                 Object obj = context.newDynamicEntity("Relationship");
247
248                 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
249
250                 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
251                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
252
253                 String expected =
254                                 ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')"
255                                 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'ctag-pool')"
256                                 + ".has('target-pe', 'key2')"
257                                 + ".has('availability-zone-name', 'key3')";
258                 String expectedParent =
259                                 ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')";
260
261                 assertEquals(
262                                 "gremlin query should be " + expected,
263                                 expected,
264                                 query.getQueryBuilder().getQuery());
265                 assertEquals(
266                                 "parent gremlin query should be equal the query for port group",
267                                 expectedParent,
268                                 query.getQueryBuilder().getParentQuery().getQuery());
269                 assertEquals(
270                                 "result type should be ctag-pool",
271                                 "ctag-pool",
272                                 query.getResultType());
273
274     }
275
276 }