847a45ed8f4cf4e4c20a97a0a530130903ca2f21
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / serialization / queryformats / GraphSONTest.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
21 package org.onap.aai.serialization.queryformats;
22
23 import static org.hamcrest.CoreMatchers.containsString;
24 import static org.hamcrest.MatcherAssert.assertThat;
25 import static org.hamcrest.core.IsNot.not;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFManager;
31 import com.google.gson.JsonArray;
32 import com.google.gson.JsonObject;
33
34 import java.util.Optional;
35
36 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
37 import org.apache.tinkerpop.gremlin.structure.Graph;
38 import org.apache.tinkerpop.gremlin.structure.T;
39 import org.apache.tinkerpop.gremlin.structure.Vertex;
40 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.onap.aai.config.ConfigConfiguration;
45 import org.onap.aai.config.SpringContextAware;
46 import org.onap.aai.edges.EdgeIngestor;
47 import org.onap.aai.edges.exceptions.AmbiguousRuleChoiceException;
48 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
49 import org.onap.aai.exceptions.AAIException;
50 import org.onap.aai.serialization.db.AAICorePrivateEdgeTestConfigTranslator;
51 import org.onap.aai.serialization.db.EdgeSerializer;
52 import org.onap.aai.setup.SchemaLocationsBean;
53 import org.onap.aai.setup.SchemaVersions;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.test.annotation.DirtiesContext;
56 import org.springframework.test.context.ContextConfiguration;
57 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
58
59 @RunWith(SpringJUnit4ClassRunner.class)
60 @ContextConfiguration(
61         classes = {ConfigConfiguration.class, AAICorePrivateEdgeTestConfigTranslator.class, EdgeIngestor.class,
62                 EdgeSerializer.class, SpringContextAware.class})
63 @DirtiesContext
64 public class GraphSONTest {
65
66     private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(GraphSONTest.class);
67
68     private Graph graph;
69     private Vertex v1;
70
71     @Autowired
72     protected EdgeSerializer edgeSer;
73
74     @Autowired
75     protected EdgeIngestor rules;
76
77     private JsonObject jsonObj = new JsonObject();
78     private JsonObject properties = new JsonObject();
79     private JsonArray name = new JsonArray();
80     private JsonObject idVal = new JsonObject();
81
82     private GraphSON graphSON;
83
84     @Before
85     public void setUp() {
86
87         jsonObj.addProperty("id", 0);
88         jsonObj.addProperty("label", "vertex");
89
90         idVal.addProperty("id", 1);
91         idVal.addProperty("value", "Sam");
92
93         name.add(idVal);
94         properties.add("name", name);
95         jsonObj.add("properties", properties);
96
97         graph = TinkerGraph.open();
98         v1 = graph.addVertex("name", "Sam");
99
100         graphSON = new GraphSON();
101     }
102
103     @Test
104     public void classGraphSONTestWithVertex() {
105
106         GraphSON graphSonObj1 = new GraphSON();
107         JsonObject obj = graphSonObj1.formatObject(v1).get();
108
109         assertEquals(jsonObj, obj);
110     }
111
112     /**
113      * Case where there is only one private edge
114      * 
115      * <pre>
116      *     {
117      *         "id": 21,
118      *         "inE": {
119      *             "org.onap.relationships.inventory.isA": [
120      *                          {
121      *                                  "id": 10,
122      *                                  "properties": {
123      *                                    "aai-uuid": "oafjdsiofjs",
124      *                                    "private": true
125      *                                  }
126      *                          }
127      *             ]
128      *         }
129      *         "label": "model-ver",
130      *         "properties": {
131      *             "aai-node-type": [
132      *                          {
133      *                                  "id": 5,
134      *                                  "value": "model-ver"
135      *                          }
136      *             ]
137      *         }
138      *     }
139      * </pre>
140      *
141      * @throws AAIException
142      */
143     @Test
144     public void testGraphWithVertexHavingPrivateEdges()
145             throws AAIException, EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
146
147         Vertex genericVnf = graph.addVertex(T.label, "generic-vnf", T.id, "20", "aai-node-type", "generic-vnf",
148                 "vnf-id", "vnf-id-1", "vnf-name", "vnf-name-1");
149
150         Vertex modelVer = graph.addVertex(T.label, "model-ver", T.id, "21", "aai-node-type", "model-ver",
151                 "model-version-id", "modelVer1", "model-name", "modelName1");
152
153         GraphTraversalSource source = graph.traversal();
154         edgeSer.addPrivateEdge(source, genericVnf, modelVer, null);
155
156         Optional<JsonObject> jsonObjectOptional = graphSON.formatObject(genericVnf);
157         JsonObject obj = jsonObjectOptional.get();
158         LOGGER.info(obj.toString());
159         assertNotNull(obj);
160         String value = obj.toString();
161
162         assertThat(value, not(containsString("private")));
163         assertThat(value, not(containsString("inE")));
164         assertThat(value, not(containsString("outE")));
165     }
166
167     /**
168      * Case where there is one private edge and regular edge
169      * with the same edge label name
170      * 
171      * <pre>
172      *     {
173      *         "id": 21,
174      *         "inE": {
175      *             "org.onap.relationships.inventory.isA": [
176      *                          {
177      *                                  "id": 10,
178      *                                  "properties": {
179      *                                    "aai-uuid": "oafjdsiofjs",
180      *                                    "private": true
181      *                                  }
182      *                          }
183      *                          {
184      *                                  "id": 11,
185      *                                  "properties": {
186      *                                    "aai-uuid": "oafjdsiofjs",
187      *                                    "private": false
188      *                                  }
189      *                          }
190      *             ]
191      *         }
192      *         "label": "model-ver",
193      *         "properties": {
194      *             "aai-node-type": [
195      *                          {
196      *                                  "id": 5,
197      *                                  "value": "model-ver"
198      *                          }
199      *             ]
200      *         }
201      *     }
202      * </pre>
203      *
204      * @throws AAIException
205      */
206     @Test
207     public void testGraphWithSameLabelWithPrivateEdgeAndRegularEdge()
208             throws AAIException, EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
209
210         Vertex genericVnf = graph.addVertex(T.label, "generic-vnf", T.id, "20", "aai-node-type", "generic-vnf",
211                 "vnf-id", "vnf-id-1", "vnf-name", "vnf-name-1");
212
213         Vertex modelVer = graph.addVertex(T.label, "model-ver", T.id, "21", "aai-node-type", "model-ver",
214                 "model-version-id", "modelVer1", "model-name", "modelName1");
215
216         Vertex modelElement = graph.addVertex(T.label, "model-element", T.id, "22", "aai-node-type", "model-element");
217
218         GraphTraversalSource source = graph.traversal();
219         edgeSer.addPrivateEdge(source, genericVnf, modelVer, null);
220         edgeSer.addEdge(source, modelVer, modelElement, null);
221
222         Optional<JsonObject> jsonObjectOptional = graphSON.formatObject(modelVer);
223         JsonObject obj = jsonObjectOptional.get();
224         LOGGER.info(obj.toString());
225         assertNotNull(obj);
226         String value = obj.toString();
227         assertThat(value, not(containsString("\"private\":true")));
228     }
229
230     /**
231      * Case where there is one private edge and regular edge to same label
232      * And another regular edge to a different label
233      * 
234      * <pre>
235      *     {
236      *         "id": 21,
237      *         "inE": {
238      *             "org.onap.relationships.inventory.isA": [
239      *                          {
240      *                                  "id": 10,
241      *                                  "properties": {
242      *                                    "aai-uuid": "oafjdsiofjs",
243      *                                    "private": true
244      *                                  }
245      *                          },
246      *                          {
247      *                                  "id": 11,
248      *                                  "properties": {
249      *                                    "aai-uuid": "oafjdsiofjs",
250      *                                    "private": false
251      *                                  }
252      *                          }
253      *             ],
254      *             "org.onap.relationships.inventory.BelongsTo": [
255      *                          {
256      *                                  "id": 13,
257      *                                  "properties": {
258      *                                    "aai-uuid": "oafjdsiofjs",
259      *                                    "private": false
260      *                                  }
261      *                          }
262      *             ]
263      *         }
264      *         "label": "model-ver",
265      *         "properties": {
266      *             "aai-node-type": [
267      *                          {
268      *                                  "id": 5,
269      *                                  "value": "model-ver"
270      *                          }
271      *             ]
272      *         }
273      *     }
274      * </pre>
275      *
276      * @throws AAIException
277      */
278     @Test
279     public void testGraphWithMultipleLabelWithOneLabelWithPrivateEdgeAndRegularEdgeAndAnotherLabelWithRegularEdge()
280             throws AAIException, EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
281
282         Vertex genericVnf = graph.addVertex(T.label, "generic-vnf", T.id, "20", "aai-node-type", "generic-vnf",
283                 "vnf-id", "vnf-id-1", "vnf-name", "vnf-name-1");
284
285         Vertex modelVer = graph.addVertex(T.label, "model-ver", T.id, "21", "aai-node-type", "model-ver",
286                 "model-version-id", "modelVer1", "model-name", "modelName1");
287
288         Vertex modelElement = graph.addVertex(T.label, "model-element", T.id, "22", "aai-node-type", "model-element");
289
290         Vertex metadatum = graph.addVertex(T.label, "metadatum", T.id, "23", "aai-node-type", "metadatum");
291
292         GraphTraversalSource source = graph.traversal();
293         edgeSer.addPrivateEdge(source, genericVnf, modelVer, null);
294         edgeSer.addEdge(source, modelVer, modelElement, null);
295         edgeSer.addTreeEdge(source, modelVer, metadatum);
296
297         Optional<JsonObject> jsonObjectOptional = graphSON.formatObject(modelVer);
298         JsonObject obj = jsonObjectOptional.get();
299         LOGGER.info(obj.toString());
300         assertNotNull(obj);
301         String value = obj.toString();
302         assertThat(value, not(containsString("\"private\":true")));
303     }
304
305     @Test
306     public void testGraphCreateRegularOutAndInEdges() throws AAIException {
307
308         Vertex complex1 = graph.addVertex(T.label, "complex", T.id, "20", "aai-node-type", "complex");
309
310         Vertex pserver1 = graph.addVertex(T.label, "pserver", T.id, "22", "aai-node-type", "pserver", "hostname",
311                 "test-pserver1");
312
313         Vertex pserver2 = graph.addVertex(T.label, "pserver", T.id, "23", "aai-node-type", "pserver", "hostname",
314                 "test-pserver2");
315
316         GraphTraversalSource source = graph.traversal();
317         edgeSer.addEdge(source, pserver1, complex1, null);
318         edgeSer.addEdge(source, pserver2, complex1, null);
319
320         Optional<JsonObject> jsonObjectOptional = graphSON.formatObject(complex1);
321         JsonObject obj = jsonObjectOptional.get();
322         LOGGER.info(obj.toString());
323         assertNotNull(obj);
324         assertThat(obj.toString(), not(containsString("\"private\":true")));
325         assertThat(obj.toString(), containsString("inE"));
326     }
327
328     /**
329      * Case where there is one private edge and regular edge to same label
330      * And another regular edge to a different label
331      * 
332      * <pre>
333      *     {
334      *         "id": 21,
335      *         "inE": {
336      *             "org.onap.relationships.inventory.isA": [
337      *                          {
338      *                                  "id": 10,
339      *                                  "properties": {
340      *                                    "aai-uuid": "oafjdsiofjs",
341      *                                    "private": true
342      *                                  }
343      *                          }
344      *             ],
345      *             "org.onap.relationships.inventory.BelongsTo": [
346      *                          {
347      *                                  "id": 13,
348      *                                  "properties": {
349      *                                    "aai-uuid": "oafjdsiofjs",
350      *                                    "private": true
351      *                                  }
352      *                          }
353      *             ]
354      *         }
355      *         "label": "model-ver",
356      *         "properties": {
357      *             "aai-node-type": [
358      *                          {
359      *                                  "id": 5,
360      *                                  "value": "model-ver"
361      *                          }
362      *             ]
363      *         }
364      *     }
365      * </pre>
366      *
367      * @throws AAIException
368      */
369     @Test
370     public void testWhenMultipleEdgeLabelsBothOnlyHavePrivateEdges()
371             throws AAIException, EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
372
373         Vertex genericVnf = graph.addVertex(T.label, "generic-vnf", T.id, "20", "aai-node-type", "generic-vnf",
374                 "vnf-id", "vnf-id-1", "vnf-name", "vnf-name-1");
375
376         Vertex modelVer = graph.addVertex(T.label, "model-ver", T.id, "21", "aai-node-type", "model-ver",
377                 "model-version-id", "modelVer1", "model-name", "modelName1");
378
379         Vertex modelPrivate = graph.addVertex(T.label, "model-private", T.id, "22", "aai-node-type", "model-private");
380
381         GraphTraversalSource source = graph.traversal();
382         edgeSer.addPrivateEdge(source, genericVnf, modelVer, null);
383         edgeSer.addPrivateEdge(source, modelVer, modelPrivate, null);
384
385         Optional<JsonObject> jsonObjectOptional = graphSON.formatObject(modelVer);
386         JsonObject obj = jsonObjectOptional.get();
387         LOGGER.info(obj.toString());
388         assertNotNull(obj);
389         String value = obj.toString();
390         assertThat(value, not(containsString("\"private\":true")));
391         assertThat(value, not(containsString("inventory.BelongsTo")));
392         assertThat(value, not(containsString("inE")));
393     }
394
395     /**
396      * Case where there is one private edge and regular edge to same label
397      * And another regular edge to a different label
398      * 
399      * <pre>
400      *     {
401      *         "id": 21,
402      *         "inE": {
403      *             "org.onap.relationships.inventory.isA": [
404      *                          {
405      *                                  "id": 10,
406      *                                  "properties": {
407      *                                    "aai-uuid": "oafjdsiofjs",
408      *                                    "private": true
409      *                                  }
410      *                          }
411      *             ],
412      *             "org.onap.relationships.inventory.BelongsTo": [
413      *                          {
414      *                                  "id": 13,
415      *                                  "properties": {
416      *                                    "aai-uuid": "oafjdsiofjs",
417      *                                    "private": true
418      *                                  }
419      *                          },
420      *                          {
421      *                                  "id": 13,
422      *                                  "properties": {
423      *                                    "aai-uuid": "jaosjfaisj",
424      *                                    "private": false
425      *                                  }
426      *                          }
427      *             ]
428      *         }
429      *         "label": "model-ver",
430      *         "properties": {
431      *             "aai-node-type": [
432      *                          {
433      *                                  "id": 5,
434      *                                  "value": "model-ver"
435      *                          }
436      *             ]
437      *         }
438      *     }
439      * </pre>
440      *
441      * @throws AAIException
442      */
443     @Test
444     public void testWhenMultipleEdgeLabelsBothHavePrivateEdgesButOneHasTreeEdgeAndPrivateEdge()
445             throws AAIException, EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
446
447         Vertex genericVnf = graph.addVertex(T.label, "generic-vnf", T.id, "20", "aai-node-type", "generic-vnf",
448                 "vnf-id", "vnf-id-1", "vnf-name", "vnf-name-1");
449
450         Vertex modelVer = graph.addVertex(T.label, "model-ver", T.id, "21", "aai-node-type", "model-ver",
451                 "model-version-id", "modelVer1", "model-name", "modelName1");
452
453         Vertex modelPrivate = graph.addVertex(T.label, "model-private", T.id, "22", "aai-node-type", "model-private");
454
455         Vertex metadatum = graph.addVertex(T.label, "metadatum", T.id, "23", "aai-node-type", "metadatum");
456
457         GraphTraversalSource source = graph.traversal();
458         edgeSer.addPrivateEdge(source, genericVnf, modelVer, null);
459         edgeSer.addPrivateEdge(source, modelVer, modelPrivate, null);
460         edgeSer.addTreeEdge(source, modelVer, metadatum);
461
462         Optional<JsonObject> jsonObjectOptional = graphSON.formatObject(modelVer);
463         JsonObject obj = jsonObjectOptional.get();
464         LOGGER.info(obj.toString());
465         assertNotNull(obj);
466         String value = obj.toString();
467         assertThat(value, not(containsString("\"private\":true")));
468         assertThat(value, containsString("inventory.BelongsTo"));
469     }
470
471     @Test
472     public void parallelThresholdCehck() {
473
474         GraphSON graphSonObj2 = new GraphSON();
475         assertEquals(50, graphSonObj2.parallelThreshold());
476
477     }
478 }