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