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