823785772553c233d4fcb20a4eef765659579be5
[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", "./src/test/resources/");
66                 System.setProperty("BUNDLECONFIG_DIR", "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         @Test
125     public void childQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
126                 String content =
127                                 "{"
128                                 + "\"related-to\" : \"lag-interface\","
129                                 + "\"relationship-data\" : [{"
130                                 + "\"relationship-key\" : \"pserver.hostname\","
131                                 + "\"relationship-value\" : \"key1\""
132                                 + "}, {"
133                                 + "\"relationship-key\" : \"lag-interface.interface-name\","
134                                 + "\"relationship-value\" : \"key2\""
135                                 + "}]"
136                                 + "}";
137                                                 
138                 Unmarshaller unmarshaller = context.createUnmarshaller();
139             unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
140             unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
141                 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
142                 Object obj = context.newDynamicEntity("Relationship");
143
144                 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
145                         
146                 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
147                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
148                 String expected =
149                                 ".has('hostname', 'key1').has('aai-node-type', 'pserver').out('hasLAGInterface').has('aai-node-type', 'lag-interface')"
150                                 + ".has('interface-name', 'key2')";
151                 String parentExpected = 
152                                 ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
153                 assertEquals(
154                                 "gremlin query should be for node",
155                                 expected,
156                                 query.getQueryBuilder().getQuery());
157                 assertEquals(
158                                 "parent gremlin query should be for parent",
159                                 parentExpected,
160                                 query.getQueryBuilder().getParentQuery().getQuery());
161                 assertEquals(
162                                 "result type should be lag-interface",
163                                 "lag-interface",
164                                 query.getResultType());
165     }
166         
167         /**
168          * Naming exceptions.
169          *
170          * @throws JAXBException the JAXB exception
171          * @throws UnsupportedEncodingException the unsupported encoding exception
172          * @throws AAIException the AAI exception
173          */
174         @Test
175         @Ignore
176     public void namingExceptions() throws JAXBException, UnsupportedEncodingException, AAIException {
177                 String content =
178                                 "{"
179                                 + "\"related-to\" : \"cvlan-tag\","
180                                 + "\"relationship-data\" : [{"
181                                 + "\"relationship-key\" : \"vce.vnf-id\","
182                                 + "\"relationship-value\" : \"key1\""
183                                 + "}, {"
184                                 + "\"relationship-key\" : \"port-group.interface-id\","
185                                 + "\"relationship-value\" : \"key2\""
186                                 + "},{"
187                                 + "\"relationship-key\" : \"cvlan-tag.cvlan-tag\","
188                                 + "\"relationship-value\" : \"655\""
189                                 + "}]"
190                                 + "}";
191                                                 
192                 Unmarshaller unmarshaller = context.createUnmarshaller();
193             unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
194             unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
195                 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
196                 Object obj = context.newDynamicEntity("Relationship");
197
198                 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
199                         
200                 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
201                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
202                 String expected = 
203                                 ".has('vnf-id', 'key1').has('aai-node-type', 'vce').in('org.onap.relationships.inventory.BelongsTo')"
204                                 + ".has('aai-node-type', 'port-group').has('interface-id', 'key2').in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'cvlan-tag')"
205                                 + ".has('cvlan-tag', 655)";
206                 String expectedParent = 
207                                                 ".has('vnf-id', 'key1').has('aai-node-type', 'vce').in('org.onap.relationships.inventory.BelongsTo')"
208                                                 + ".has('aai-node-type', 'port-group').has('interface-id', 'key2')";
209                 
210                 assertEquals(
211                                 "gremlin query should be " + expected,
212                                 expected,
213                                 query.getQueryBuilder().getQuery());
214                 assertEquals(
215                                 "parent gremlin query should be equal the query for port group",
216                                 expectedParent,
217                                 query.getQueryBuilder().getParentQuery().getQuery());
218                 assertEquals(
219                                 "result type should be cvlan-tag",
220                                 "cvlan-tag",
221                                 query.getResultType());
222                 
223     }
224         
225         /**
226          * Double key.
227          *
228          * @throws JAXBException the JAXB exception
229          * @throws UnsupportedEncodingException the unsupported encoding exception
230          * @throws AAIException the AAI exception
231          */
232         @Ignore
233         @Test
234     public void doubleKey() throws JAXBException, UnsupportedEncodingException, AAIException {
235                 String content =
236                                 "{"
237                                 + "\"related-to\" : \"ctag-pool\","
238                                 + "\"relationship-data\" : [{"
239                                 + "\"relationship-key\" : \"complex.physical-location-id\","
240                                 + "\"relationship-value\" : \"key1\""
241                                 + " }, { "
242                                 + "\"relationship-key\" : \"ctag-pool.target-pe\","
243                                 + " \"relationship-value\" : \"key2\""
244                                 + " },{"
245                                 + "\"relationship-key\" : \"ctag-pool.availability-zone-name\","
246                                 + "\"relationship-value\" : \"key3\""
247                                 + "}]"
248                                 + "}";
249                                                 
250                                                 
251                 Unmarshaller unmarshaller = context.createUnmarshaller();
252             unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
253             unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
254                 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
255                 Object obj = context.newDynamicEntity("Relationship");
256
257                 DynamicEntity entity = (DynamicEntity)unmarshaller.unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
258                         
259                 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
260                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
261
262                 String expected = 
263                                 ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')"
264                                 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'ctag-pool')"
265                                 + ".has('target-pe', 'key2')"
266                                 + ".has('availability-zone-name', 'key3')";
267                 String expectedParent = 
268                                 ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')";
269
270                 assertEquals(
271                                 "gremlin query should be " + expected,
272                                 expected,
273                                 query.getQueryBuilder().getQuery());
274                 assertEquals(
275                                 "parent gremlin query should be equal the query for port group",
276                                 expectedParent,
277                                 query.getQueryBuilder().getParentQuery().getQuery());
278                 assertEquals(
279                                 "result type should be ctag-pool",
280                                 "ctag-pool",
281                                 query.getResultType());
282                 
283     }
284         
285 }