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