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