Merge "Update shema for VFC"
[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 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.eclipse.persistence.dynamic.DynamicEntity;
25 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
26 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
27 import org.junit.Ignore;
28 import org.junit.Test;
29 import org.onap.aai.AAISetup;
30 import org.onap.aai.exceptions.AAIException;
31 import org.onap.aai.introspection.*;
32 import org.onap.aai.serialization.engines.QueryStyle;
33 import org.onap.aai.serialization.engines.TitanDBEngine;
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         private ModelInjestor injestor = ModelInjestor.getInstance();
48
49         private TransactionalGraphEngine dbEngine = 
50                         new TitanDBEngine(QueryStyle.GREMLIN_TRAVERSAL, 
51                                 LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v8),
52                                 false);
53         private final Version version = Version.v8;
54         private DynamicJAXBContext context = injestor.getContextForVersion(version);
55
56         /**
57          * Parent query.
58          *
59          * @throws JAXBException the JAXB exception
60          * @throws UnsupportedEncodingException the unsupported encoding exception
61          * @throws AAIException the AAI exception
62          */
63         @Test
64     public void parentQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
65                 
66                 String content =
67                                 "{"
68                                 + "\"related-to\" : \"pserver\","
69                                 + "\"relationship-data\" : [{"
70                                 + "\"relationship-key\" : \"pserver.hostname\","
71                                 + "\"relationship-value\" : \"key1\""
72                                 + "}]"
73                                 + "}";
74                                                 
75                 Unmarshaller 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                 Object obj = context.newDynamicEntity("Relationship");
80
81                 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
82                         
83                 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
84                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
85
86                 String expected = 
87                                 ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
88                 assertEquals(
89                                 "gremlin query should be " + expected,
90                                 expected,
91                                 query.getQueryBuilder().getQuery());
92                 assertEquals(
93                                 "parent gremlin query should be equal to normal query",
94                                 expected,
95                                 query.getQueryBuilder().getParentQuery().getQuery());
96                 assertEquals(
97                                 "result type should be pserver",
98                                 "pserver",
99                                 query.getResultType());
100                 
101     }
102
103         /**
104          * Child query.
105          *
106          * @throws JAXBException the JAXB exception
107          * @throws UnsupportedEncodingException the unsupported encoding exception
108          * @throws AAIException the AAI exception
109          */
110         @Ignore
111         @Test
112     public void childQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
113                 String content =
114                                 "{"
115                                 + "\"related-to\" : \"lag-interface\","
116                                 + "\"relationship-data\" : [{"
117                                 + "\"relationship-key\" : \"pserver.hostname\","
118                                 + "\"relationship-value\" : \"key1\""
119                                 + "}, {"
120                                 + "\"relationship-key\" : \"lag-interface.interface-name\","
121                                 + "\"relationship-value\" : \"key2\""
122                                 + "}]"
123                                 + "}";
124                                                 
125                 Unmarshaller unmarshaller = context.createUnmarshaller();
126             unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
127             unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
128                 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
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                 String expected =
136                                 ".has('hostname', 'key1').has('aai-node-type', 'pserver').out('hasLAGInterface').has('aai-node-type', 'lag-interface')"
137                                 + ".has('interface-name', 'key2')";
138                 String parentExpected = 
139                                 ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
140                 assertEquals(
141                                 "gremlin query should be for node",
142                                 expected,
143                                 query.getQueryBuilder().getQuery());
144                 assertEquals(
145                                 "parent gremlin query should be for parent",
146                                 parentExpected,
147                                 query.getQueryBuilder().getParentQuery().getQuery());
148                 assertEquals(
149                                 "result type should be lag-interface",
150                                 "lag-interface",
151                                 query.getResultType());
152     }
153         
154         /**
155          * Naming exceptions.
156          *
157          * @throws JAXBException the JAXB exception
158          * @throws UnsupportedEncodingException the unsupported encoding exception
159          * @throws AAIException the AAI exception
160          */
161         @Ignore
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                 Unmarshaller unmarshaller = context.createUnmarshaller();
180             unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
181             unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
182                 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
183                 Object obj = context.newDynamicEntity("Relationship");
184
185                 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
186                         
187                 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
188                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
189                 String expected = 
190                                 ".has('vnf-id', 'key1').has('aai-node-type', 'vce').in('org.onap.relationships.inventory.BelongsTo')"
191                                 + ".has('aai-node-type', 'port-group').has('interface-id', 'key2').in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'cvlan-tag')"
192                                 + ".has('cvlan-tag', 655)";
193                 String expectedParent = 
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')";
196                 
197                 assertEquals(
198                                 "gremlin query should be " + expected,
199                                 expected,
200                                 query.getQueryBuilder().getQuery());
201                 assertEquals(
202                                 "parent gremlin query should be equal the query for port group",
203                                 expectedParent,
204                                 query.getQueryBuilder().getParentQuery().getQuery());
205                 assertEquals(
206                                 "result type should be cvlan-tag",
207                                 "cvlan-tag",
208                                 query.getResultType());
209                 
210     }
211         
212         /**
213          * Double key.
214          *
215          * @throws JAXBException the JAXB exception
216          * @throws UnsupportedEncodingException the unsupported encoding exception
217          * @throws AAIException the AAI exception
218          */
219         @Ignore
220         @Test
221     public void doubleKey() throws JAXBException, UnsupportedEncodingException, AAIException {
222                 String content =
223                                 "{"
224                                 + "\"related-to\" : \"ctag-pool\","
225                                 + "\"relationship-data\" : [{"
226                                 + "\"relationship-key\" : \"complex.physical-location-id\","
227                                 + "\"relationship-value\" : \"key1\""
228                                 + " }, { "
229                                 + "\"relationship-key\" : \"ctag-pool.target-pe\","
230                                 + " \"relationship-value\" : \"key2\""
231                                 + " },{"
232                                 + "\"relationship-key\" : \"ctag-pool.availability-zone-name\","
233                                 + "\"relationship-value\" : \"key3\""
234                                 + "}]"
235                                 + "}";
236                                                 
237                                                 
238                 Unmarshaller unmarshaller = context.createUnmarshaller();
239             unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
240             unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
241                 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
242                 Object obj = context.newDynamicEntity("Relationship");
243
244                 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
245                         
246                 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
247                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
248
249                 String expected = 
250                                 ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')"
251                                 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'ctag-pool')"
252                                 + ".has('target-pe', 'key2')"
253                                 + ".has('availability-zone-name', 'key3')";
254                 String expectedParent = 
255                                 ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')";
256
257                 assertEquals(
258                                 "gremlin query should be " + expected,
259                                 expected,
260                                 query.getQueryBuilder().getQuery());
261                 assertEquals(
262                                 "parent gremlin query should be equal the query for port group",
263                                 expectedParent,
264                                 query.getQueryBuilder().getParentQuery().getQuery());
265                 assertEquals(
266                                 "result type should be ctag-pool",
267                                 "ctag-pool",
268                                 query.getResultType());
269                 
270     }
271         
272 }