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