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