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