5041c7d184a0493434e7cb8dba8edb6fd891cc68
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / serialization / db / DbSerializerTest.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.db;
21
22 import org.janusgraph.core.JanusGraphFactory;
23 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
24 import org.apache.tinkerpop.gremlin.structure.*;
25 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
26 import org.junit.*;
27 import org.junit.rules.ExpectedException;
28 import org.junit.runner.RunWith;
29 import org.junit.runners.Parameterized;
30 import org.onap.aai.AAISetup;
31 import org.onap.aai.db.props.AAIProperties;
32 import org.onap.aai.dbmap.DBConnectionType;
33 import org.onap.aai.edges.EdgeIngestor;
34 import org.onap.aai.edges.enums.EdgeType;
35 import org.onap.aai.exceptions.AAIException;
36 import org.onap.aai.introspection.*;
37 import org.onap.aai.parsers.query.QueryParser;
38 import org.onap.aai.serialization.engines.QueryStyle;
39 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
40 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
41 import org.onap.aai.setup.SchemaVersion;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.test.annotation.DirtiesContext;
44
45 import java.io.UnsupportedEncodingException;
46 import java.net.URI;
47 import java.net.URISyntaxException;
48 import java.util.*;
49
50 import static org.junit.Assert.*;
51 import static org.mockito.Mockito.spy;
52 import static org.mockito.Mockito.when;
53
54 @RunWith(value = Parameterized.class)
55 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
56 public class DbSerializerTest extends AAISetup {
57
58         //to use, set thrown.expect to whatever your test needs
59         //this line establishes default of expecting no exception to be thrown
60         @Rule
61         public ExpectedException thrown = ExpectedException.none();
62
63         protected static Graph graph;
64
65         @Autowired
66         protected EdgeSerializer edgeSer;
67         @Autowired
68         protected EdgeIngestor ei;
69
70         private SchemaVersion version;
71         private final ModelType introspectorFactoryType = ModelType.MOXY;
72         private final DBConnectionType type = DBConnectionType.REALTIME;
73         private Loader loader;
74         private TransactionalGraphEngine dbEngine;
75         private TransactionalGraphEngine engine; //for tests that aren't mocking the engine
76         private DBSerializer dbser;
77         TransactionalGraphEngine spy;
78         TransactionalGraphEngine.Admin adminSpy;
79
80         @Parameterized.Parameter(value = 0)
81         public QueryStyle queryStyle;
82
83         @Parameterized.Parameters(name = "QueryStyle.{0}")
84         public static Collection<Object[]> data() {
85                 return Arrays.asList(new Object[][]{
86                                 {QueryStyle.TRAVERSAL},
87                                 {QueryStyle.TRAVERSAL_URI}
88                 });
89         }
90
91         @BeforeClass
92         public static void init() throws Exception {
93                 graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
94
95         }
96
97         @Before
98         public void setup() throws Exception {
99                 //createGraph();
100         version = schemaVersions.getDefaultVersion();
101                 loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
102                 dbEngine = new JanusGraphDBEngine(queryStyle, type, loader);
103                 spy = spy(dbEngine);
104                 adminSpy = spy(dbEngine.asAdmin());
105
106
107                 engine = new JanusGraphDBEngine(queryStyle, type, loader);
108                 dbser = new DBSerializer(version, engine, introspectorFactoryType, "AAI-TEST");
109         }
110
111         @Test
112         public void testFindDeletableDoesNotReturnDuplicates() throws AAIException {
113
114                 Vertex genericVnf1 = graph.addVertex("aai-node-type", "generic-vnf", "vnf-id", "vnf1", "vnf-name", "vnfName1");
115
116                 Vertex lInterface1 = graph.addVertex("aai-node-type", "l-interface", "interface-name", "lInterface1");
117                 Vertex lInterface2 = graph.addVertex("aai-node-type", "l-interface", "interface-name", "lInterface2");
118
119                 Vertex logicalLink1 = graph.addVertex("aai-node-type", "logical-link", "link-name", "logicalLink1");
120                 Vertex logicalLink2 = graph.addVertex("aai-node-type", "logical-link", "link-name", "logicalLink2");
121
122                 GraphTraversalSource g = graph.traversal();
123
124                 edgeSer.addTreeEdge(g, genericVnf1, lInterface1);
125                 edgeSer.addTreeEdge(g, genericVnf1, lInterface2);
126                 edgeSer.addEdge(g, lInterface1, logicalLink1);
127                 edgeSer.addEdge(g, lInterface1, logicalLink2);
128                 // This line will cause the logical link2 to be found twice under linterface 1
129                 // and also under the linterface 2 and since in the past deletable returned
130                 // duplicates this test checks that it shouldn't return duplicates
131                 edgeSer.addEdge(g, lInterface2, logicalLink2);
132
133                 when(spy.asAdmin()).thenReturn(adminSpy);
134                 when(adminSpy.getTraversalSource()).thenReturn(g);
135                 when(adminSpy.getReadOnlyTraversalSource()).thenReturn(g);
136
137                 List<Vertex> deletableVertexes = spy.getQueryEngine().findDeletable(genericVnf1);
138                 Set<Vertex> vertexSet = new HashSet<>();
139
140                 for (Vertex deletableVertex : deletableVertexes) {
141                     if(!vertexSet.contains(deletableVertex)){
142                         vertexSet.add(deletableVertex);
143                     } else {
144                         fail("Find deletable is returning a list of duplicate vertexes");
145                     }
146                 }
147         }
148
149         @After
150         public void tearDown() throws Exception {
151             engine.rollback();
152         }
153
154         @AfterClass
155         public static void destroy() throws Exception {
156                 graph.close();
157         }
158
159
160         public void subnetSetup() throws AAIException {
161                 /*
162                  * This setus up the test graph, For future junits , add more vertices
163                  * and edges
164                  */
165
166                 Vertex l3interipv4addresslist_1 = graph.traversal().addV("aai-node-type", "l3-interface-ipv4-address-list",
167                                 "l3-interface-ipv4-address", "l3-interface-ipv4-address-1").next();
168                 Vertex subnet_2 = graph.traversal().addV("aai-node-type", "subnet", "subnet-id", "subnet-id-2").next();
169                 Vertex l3interipv6addresslist_3 = graph.traversal().addV("aai-node-type", "l3-interface-ipv6-address-list",
170                                 "l3-interface-ipv6-address", "l3-interface-ipv6-address-3").next();
171                 Vertex subnet_4 = graph.traversal().addV("aai-node-type", "subnet", "subnet-id", "subnet-id-4").next();
172                 Vertex subnet_5 = graph.traversal().addV("aai-node-type", "subnet", "subnet-id", "subnet-id-5").next();
173                 Vertex l3network_6 = graph.traversal()
174                                 .addV("aai-node-type", "l3-network", "network-id", "network-id-6", "network-name", "network-name-6")
175                                 .next();
176
177                 GraphTraversalSource g = graph.traversal();
178                 edgeSer.addEdge(g, l3interipv4addresslist_1, subnet_2);
179                 edgeSer.addEdge(g, l3interipv6addresslist_3, subnet_4);
180                 edgeSer.addTreeEdge(g, subnet_5, l3network_6);
181         }
182
183
184         public void l3NetworkSetup() throws AAIException {
185                 /*
186                  * This setus up the test graph, For future junits , add more vertices
187                  * and edges
188                  */
189
190                 Vertex l3network1 = graph.addVertex("aai-node-type", "l3-network", "network-id", "network-id-v1", "network-name", "network-name-v1");
191                 Vertex l3network2 = graph.addVertex("aai-node-type", "l3-network", "network-id", "network-id-v2", "network-name", "network-name-v2");
192                 Vertex subnet1 = graph.addVertex("aai-node-type", "subnet", "subnet-id", "subnet-id-v1");
193                 Vertex subnet2 = graph.addVertex("aai-node-type", "subnet", "subnet-id", "subnet-id-v2");
194
195                 Vertex l3interipv4addresslist_1 = graph.addVertex("aai-node-type", "l3-interface-ipv4-address-list",
196                                 "l3-interface-ipv4-address", "l3-intr-v1");
197                 Vertex l3interipv6addresslist_1 =  graph.addVertex("aai-node-type", "l3-interface-ipv6-address-list",
198                                 "l3-interface-ipv6-address", "l3-interface-ipv6-v1");
199
200
201
202
203
204                 GraphTraversalSource g = graph.traversal();
205                 edgeSer.addTreeEdge(g, subnet1, l3network1);
206                 edgeSer.addEdge(g, l3interipv4addresslist_1, subnet1);
207                 edgeSer.addEdge(g, l3interipv6addresslist_1, subnet1);
208
209                 edgeSer.addTreeEdge(g, subnet2, l3network2);
210
211
212
213         }
214
215         public void vserverSetup() throws AAIException {
216                 /*
217                  * This setus up the test graph, For future junits , add more vertices
218                  * and edges
219                  */
220
221                 Vertex vserver1 = graph.addVertex("aai-node-type", "vserver", "vserver-id", "vss1",
222                                 AAIProperties.AAI_URI.toString(),
223                                         "/cloud-infrastructure/cloud-regions/cloud-region/me/123/tenants/tenant/453/vservers/vserver/vss1");
224
225                 Vertex lInterface1 = graph.addVertex("aai-node-type", "l-interface", "interface-name", "lIntr1");
226                 Vertex lInterface2 = graph.addVertex("aai-node-type", "l-interface", "interface-name", "lIntr2");
227
228                 Vertex logicalLink1 = graph.addVertex("aai-node-type", "logical-link", "link-name", "logLink1");
229                 Vertex logicalLink2 = graph.addVertex("aai-node-type", "logical-link", "link-name", "logLink2");
230
231                 Vertex l3interipv4addresslist_1 = graph.addVertex("aai-node-type", "l3-interface-ipv4-address-list",
232                                 "l3-interface-ipv4-address", "l3-intr-ipv4-address-1");
233                 Vertex l3interipv6addresslist_2 = graph.addVertex("aai-node-type", "l3-interface-ipv6-address-list",
234                                 "l3-interface-ipv4-address", "l3-intr-ipv6-address-1");
235
236                 GraphTraversalSource g = graph.traversal();
237
238                 edgeSer.addTreeEdge(g, lInterface1, vserver1);
239                 edgeSer.addTreeEdge(g, lInterface2, vserver1);
240                 edgeSer.addTreeEdge(g, l3interipv4addresslist_1, lInterface1);
241                 edgeSer.addTreeEdge(g, l3interipv6addresslist_2, lInterface2);
242
243                 edgeSer.addEdge(g, lInterface1, logicalLink1);
244                 edgeSer.addEdge(g, lInterface2, logicalLink2);
245         }
246
247         @Test
248         public void subnetDelWithInEdgesIpv4Test() throws AAIException {
249                 subnetSetup();
250                 String expected_message = "Object is being reference by additional objects preventing it from being deleted. Please clean up references from the following types [l3-interface-ipv4-address-list]";
251
252                 /*
253                  * This subnet has in-edges with l3-ipv4 and NOT ok to delete
254                  */
255                 Vertex subnet = graph.traversal().V().has("aai-node-type", "subnet").has("subnet-id", "subnet-id-2").next();
256
257                 String exceptionMessage = testCascadeDelete(subnet);
258                 assertEquals(expected_message, exceptionMessage);
259
260         }
261
262         @Test
263         public void subnetDelWithInEdgesIpv6Test() throws AAIException {
264                 subnetSetup();
265                 String expected_message = "Object is being reference by additional objects preventing it from being deleted. Please clean up references from the following types [l3-interface-ipv6-address-list]";
266
267                 /*
268                  * This subnet has in-edges with l3-ipv6 and NOT ok to delete
269                  */
270                 Vertex subnet = graph.traversal().V().has("aai-node-type", "subnet").has("subnet-id", "subnet-id-4").next();
271                 String exceptionMessage = testCascadeDelete(subnet);
272                 assertEquals(expected_message, exceptionMessage);
273
274         }
275
276         @Test
277         public void subnetDelWithInEdgesL3network() throws AAIException {
278                 subnetSetup();
279                 String expected_message = "";
280
281                 /*
282                  * This subnet has in-edges with l3-network and ok to delete
283                  */
284                 Vertex subnet = graph.traversal().V().has("aai-node-type", "subnet").has("subnet-id", "subnet-id-5").next();
285
286                 String exceptionMessage = testCascadeDelete(subnet);
287                 assertEquals(expected_message, exceptionMessage);
288
289         }
290
291         public String testCascadeDelete(Vertex v) throws AAIException {
292
293                 GraphTraversalSource traversal = graph.traversal();
294                 when(spy.asAdmin()).thenReturn(adminSpy);
295                 when(adminSpy.getTraversalSource()).thenReturn(traversal);
296                 when(adminSpy.getReadOnlyTraversalSource()).thenReturn(traversal);
297
298                 String exceptionMessage = "";
299                 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
300                 List<Vertex> deletableVertices = spy.getQueryEngine().findDeletable(v);
301
302                 try {
303                         serializer.delete(v, deletableVertices, "resourceVersion", false);
304                 } catch (AAIException exception) {
305                         exceptionMessage = exception.getMessage();
306                 }
307                 return exceptionMessage;
308
309         }
310
311         public String testDelete(Vertex v) throws AAIException {
312
313                 GraphTraversalSource traversal = graph.traversal();
314                 when(spy.asAdmin()).thenReturn(adminSpy);
315                 when(adminSpy.getTraversalSource()).thenReturn(traversal);
316                 when(adminSpy.getReadOnlyTraversalSource()).thenReturn(traversal);
317
318                 String exceptionMessage = "";
319                 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
320
321                 try {
322                         serializer.delete(v, "resourceVersion", false);
323                 } catch (AAIException exception) {
324                         exceptionMessage = exception.getMessage();
325                 }
326                 return exceptionMessage;
327
328         }
329
330         @Test
331         public void createNewVertexTest() throws AAIException {
332                 engine.startTransaction();
333
334                 Introspector testObj = loader.introspectorFromName("generic-vnf");
335
336                 Vertex testVertex = dbser.createNewVertex(testObj);
337                 Vertex fromGraph = engine.tx().traversal().V().has("aai-node-type","generic-vnf").toList().get(0);
338                 assertEquals(testVertex.id(), fromGraph.id());
339                 assertEquals("AAI-TEST", fromGraph.property(AAIProperties.SOURCE_OF_TRUTH.toString()).value());
340
341         }
342
343     @Test
344         public void touchStandardVertexPropertiesTest() throws AAIException, InterruptedException {
345                 engine.startTransaction();
346
347         //if this test runs through too fast the value may not change, causing the test to fail. sleeping ensures a different value
348         Thread.sleep(2);
349                 DBSerializer dbser2 = new DBSerializer(version, engine, introspectorFactoryType, "AAI-TEST-2");
350                 Vertex vert = graph.addVertex("aai-node-type", "generic-vnf");
351
352                 // Upon first creation of the Vertex and the DBSerializer
353         // the source of truth and created-ts should be the same as their modified counterparts
354         dbser2.touchStandardVertexProperties(vert, true);
355         String createTS = (String)vert.property(AAIProperties.CREATED_TS).value();
356         String modTS = (String)vert.property(AAIProperties.LAST_MOD_TS).value();
357         String sot = (String)vert.property(AAIProperties.SOURCE_OF_TRUTH).value();
358         String lastModSOT = (String)vert.property(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH).value();
359         assertTrue(createTS.equals(modTS));
360         assertTrue(sot.equals(lastModSOT));
361
362         //if this test runs through too fast the value may not change, causing the test to fail. sleeping ensures a different value
363         Thread.sleep(2);
364
365         // Not new vertex && new DBSerializer (A new serializer since a new one will be created per transaction)
366         // Here the vertex will be modified by a different source of truth
367         DBSerializer dbser3 = new DBSerializer(version, engine, introspectorFactoryType, "AAI-TEST-3");
368         dbser3.touchStandardVertexProperties(vert, false);
369         createTS = (String)vert.property(AAIProperties.CREATED_TS).value();
370         modTS = (String)vert.property(AAIProperties.LAST_MOD_TS).value();
371         sot = (String)vert.property(AAIProperties.SOURCE_OF_TRUTH).value();
372         lastModSOT = (String)vert.property(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH).value();
373         assertFalse(createTS.equals(modTS));
374         assertFalse(sot.equals(lastModSOT));
375
376         //if this test runs through too fast the value may not change, causing the test to fail. sleeping ensures a different value
377         Thread.sleep(2);
378
379         // The currentTimeMillis used for the created-ts and modified-ts is created at DBSerializer instantiation
380         // Every REST transaction should create a new DBSerializer - thus a new currentTimeMillis is used at the time of transaction.
381         // Using an existing vertex, but treating it as new && using an older DBSerializer
382                 dbser.touchStandardVertexProperties(vert, true);
383                 String resverStart = (String)vert.property(AAIProperties.RESOURCE_VERSION).value();
384         String lastModTimeStart = (String)vert.property(AAIProperties.LAST_MOD_TS).value();
385         createTS = (String)vert.property(AAIProperties.CREATED_TS).value();
386         modTS = (String)vert.property(AAIProperties.LAST_MOD_TS).value();
387         assertTrue(createTS.equals(modTS));
388         assertEquals("AAI-TEST", vert.property(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH).value());
389
390         //if this test runs through too fast the value may not change, causing the test to fail. sleeping ensures a different value
391         Thread.sleep(2);
392
393                 dbser2.touchStandardVertexProperties(vert, false);
394                 String resourceVer = (String)vert.property(AAIProperties.RESOURCE_VERSION).value();
395                 String lastModTs = (String)vert.property(AAIProperties.LAST_MOD_TS).value();
396                 String lastModSoT = (String)vert.property(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH).value();
397
398                 assertFalse(resverStart.equals(resourceVer));
399                 assertFalse(lastModTimeStart.equals(lastModTs));
400                 assertEquals("AAI-TEST-2", lastModSoT);
401         }
402
403         @Test
404         public void touchStandardVertexPropertiesAAIUUIDTest() throws AAIException, InterruptedException {
405                 engine.startTransaction();
406
407                 Graph graph = TinkerGraph.open();
408                 Vertex v = graph.addVertex("aai-node-type", "generic-vnf");
409
410                 dbser.touchStandardVertexProperties(v, true);
411
412                 assertTrue(v.property(AAIProperties.AAI_UUID).isPresent());
413                 try {
414                         UUID.fromString((String)v.property(AAIProperties.AAI_UUID).value());
415                 } catch (IllegalArgumentException e) {
416                         fail("Vertex uuid is not valid uuid");
417                 }
418         }
419
420         @Test
421         public void verifyResourceVersion_SunnyDayTest() throws AAIException {
422                 engine.startTransaction();
423
424                 assertTrue(dbser.verifyResourceVersion("delete", "vnfc", "abc", "abc", "vnfcs/vnfc/vnfcId"));
425
426         }
427
428         @Test
429         public void verifyResourceVersion_CreateWithRVTest() throws AAIException {
430                 engine.startTransaction();
431
432                 thrown.expect(AAIException.class);
433                 thrown.expectMessage("resource-version passed for create of generic-vnfs/generic-vnf/myid");
434                 dbser.verifyResourceVersion("create", "generic-vnf", null, "old-res-ver", "generic-vnfs/generic-vnf/myid");
435
436         }
437
438         @Test
439         public void verifyResourceVersion_MissingRVTest() throws AAIException {
440                 engine.startTransaction();
441
442                 thrown.expect(AAIException.class);
443                 thrown.expectMessage("resource-version not passed for update of generic-vnfs/generic-vnf/myid");
444                 dbser.verifyResourceVersion("update", "generic-vnf", "current-res-ver", null, "generic-vnfs/generic-vnf/myid");
445
446         }
447
448         @Test
449         public void verifyResourceVersion_MismatchRVTest() throws AAIException {
450                 engine.startTransaction();
451
452                 thrown.expect(AAIException.class);
453                 thrown.expectMessage("resource-version MISMATCH for update of generic-vnfs/generic-vnf/myid");
454                 dbser.verifyResourceVersion("update", "generic-vnf", "current-res-ver", "old-res-ver", "generic-vnfs/generic-vnf/myid");
455
456         }
457
458         @Test
459         public void trimClassNameTest() throws AAIException {
460                 assertEquals("GenericVnf", dbser.trimClassName("GenericVnf"));
461                 assertEquals("GenericVnf", dbser.trimClassName("org.onap.aai.GenericVnf"));
462         }
463
464         @Test
465         public void getURIForVertexTest() throws AAIException, URISyntaxException, UnsupportedEncodingException {
466                 engine.startTransaction();
467
468                 Vertex cr = engine.tx().addVertex("aai-node-type", "cloud-region", "cloud-owner", "me", "cloud-region-id", "123", "aai-uri", "/cloud-infrastructure/cloud-regions/cloud-region/me/123");
469                 Vertex ten = engine.tx().addVertex("aai-node-type", "tenant", "tenant-id", "453");
470
471                 edgeSer.addTreeEdge(engine.tx().traversal(), cr, ten);
472
473                 ten.property("aai-uri", "/cloud-infrastructure/cloud-regions/cloud-region/me/123/tenants/tenant/453");
474
475                 URI compare = new URI("/cloud-infrastructure/cloud-regions/cloud-region/me/123/tenants/tenant/453");
476                 assertEquals(compare, dbser.getURIForVertex(ten));
477
478                 URI compareFailure = new URI("/unknown-uri");
479                 ten.property("aai-uri").remove();
480                 assertEquals(compareFailure, dbser.getURIForVertex(ten));
481
482         }
483
484         @Test
485         public void getVertexPropertiesTest() throws AAIException, UnsupportedEncodingException {
486                 engine.startTransaction();
487
488                 Vertex cr = engine.tx().addVertex("aai-node-type", "cloud-region", "cloud-owner", "me", "cloud-region-id", "123");
489
490                 Introspector crIntro = dbser.getVertexProperties(cr);
491                 assertEquals("cloud-region", crIntro.getDbName());
492                 assertEquals("me", crIntro.getValue("cloud-owner"));
493                 assertEquals("123", crIntro.getValue("cloud-region-id"));
494
495         }
496
497         @Test
498         public void getEdgeBetweenTest() throws AAIException {
499                 engine.startTransaction();
500
501                 Vertex cr = engine.tx().addVertex("aai-node-type", "cloud-region", "cloud-owner", "me", "cloud-region-id", "123");
502                 Vertex ten = engine.tx().addVertex("aai-node-type", "tenant", "tenant-id", "453");
503
504                 edgeSer.addTreeEdge(engine.tx().traversal(), cr, ten);
505
506                 Edge e = dbser.getEdgeBetween(EdgeType.TREE, ten, cr, null);
507                 assertEquals("org.onap.relationships.inventory.BelongsTo", e.label());
508
509         }
510
511         @Test
512         public void deleteEdgeTest() throws AAIException, UnsupportedEncodingException {
513                 engine.startTransaction();
514
515                 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","myvnf", "aai-uri", "/network/generic-vnfs/generic-vnf/myvnf");
516                 Vertex vnfc = engine.tx().addVertex("aai-node-type","vnfc","vnfc-name","a-name", "aai-uri", "/network/vnfcs/vnfc/a-name");
517
518                 edgeSer.addEdge(engine.tx().traversal(), gvnf, vnfc);
519
520                 Introspector relData = loader.introspectorFromName("relationship-data");
521                 relData.setValue("relationship-key", "vnfc.vnfc-name");
522                 relData.setValue("relationship-value", "a-name");
523                 Introspector relationship = loader.introspectorFromName("relationship");
524                 relationship.setValue("related-to", "vnfc");
525                 relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
526                 relationship.setValue("relationship-data",relData);
527
528                 assertTrue(dbser.deleteEdge(relationship, gvnf));
529
530                 assertFalse(engine.tx().traversal().V(gvnf).both("uses").hasNext());
531                 assertFalse(engine.tx().traversal().V(vnfc).both("uses").hasNext());
532
533         }
534
535         @Test
536         public void createEdgeTest() throws AAIException, UnsupportedEncodingException {
537                 engine.startTransaction();
538
539                 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","myvnf", "aai-uri", "/network/generic-vnfs/generic-vnf/myvnf");
540                 Vertex vnfc = engine.tx().addVertex("aai-node-type","vnfc","vnfc-name","a-name", "aai-uri", "/network/vnfcs/vnfc/a-name");
541
542                 //sunny day case
543                 Introspector relData = loader.introspectorFromName("relationship-data");
544                 relData.setValue("relationship-key", "vnfc.vnfc-name");
545                 relData.setValue("relationship-value", "a-name");
546                 Introspector relationship = loader.introspectorFromName("relationship");
547                 relationship.setValue("related-to", "vnfc");
548                 relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
549                 relationship.setValue("relationship-data",relData);
550
551                 assertTrue(dbser.createEdge(relationship, gvnf));
552                 assertTrue(engine.tx().traversal().V(gvnf).both("org.onap.relationships.inventory.BelongsTo").hasNext());
553                 assertTrue(engine.tx().traversal().V(vnfc).both("org.onap.relationships.inventory.BelongsTo").hasNext());
554
555         }
556
557         @Test
558         public void createCousinEdgeThatShouldBeTreeTest() throws AAIException, UnsupportedEncodingException, URISyntaxException {
559                 engine.startTransaction();
560
561                 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","myvnf", "aai-uri", "/network/generic-vnfs/generic-vnf/myvnf");
562                 Vertex vf = engine.tx().addVertex("aai-node-type","vf-module","vf-module-id","vf-id", "aai-uri", "/network/generic-vnfs/generic-vnf/myvnf/vf-modules/vf-module/vf-id");
563
564                 edgeSer.addTreeEdge(engine.tx().traversal(), gvnf, vf);
565
566                 Introspector relationship = loader.introspectorFromName("relationship");
567                 relationship.setValue("related-to", "vf-module");
568                 relationship.setValue("related-link", dbser.getURIForVertex(vf).toString());
569                 Introspector relationshipList = loader.introspectorFromName("relationship-list");
570                 relationshipList.setValue("relationship", Collections.singletonList(relationship.getUnderlyingObject()));
571
572                 Introspector gvnfObj = loader.introspectorFromName("generic-vnf");
573                 Vertex gvnf2 = dbser.createNewVertex(gvnfObj);
574                 gvnfObj.setValue("relationship-list", relationshipList.getUnderlyingObject());
575                 gvnfObj.setValue("vnf-id", "myvnf-1");
576
577                 QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(new URI("/network/generic-vnfs/generic-vnf/myvnf-1"));
578
579                 try {
580                         dbser.serializeToDb(gvnfObj, gvnf2, uriQuery, null, "test");
581                 } catch (AAIException e) {
582                         assertEquals("AAI_6145", e.getCode());
583                 }
584         }
585
586         @Test
587         public void createEdgeNodeDoesNotExistExceptionTest() throws AAIException, UnsupportedEncodingException {
588                 engine.startTransaction();
589
590                 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","myvnf", "aai-uri", "/network/generic-vnfs/generic-vnf/myvnf");
591
592                 //rainy day case, edge to non-existent object
593                 Introspector relData = loader.introspectorFromName("relationship-data");
594                 relData.setValue("relationship-key", "vnfc.vnfc-name");
595                 relData.setValue("relationship-value", "b-name");
596                 Introspector relationship = loader.introspectorFromName("relationship");
597                 relationship.setValue("related-to", "vnfc");
598                 relationship.setValue("related-link", "/network/vnfcs/vnfc/b-name");
599                 relationship.setValue("relationship-data",relData);
600
601                 thrown.expect(AAIException.class);
602                 thrown.expectMessage("Node of type vnfc. Could not find object at: /network/vnfcs/vnfc/b-name");
603                 dbser.createEdge(relationship, gvnf);
604
605         }
606
607         @Test
608         public void serializeSingleVertexTopLevelTest() throws AAIException, UnsupportedEncodingException {
609                 engine.startTransaction();
610
611                 Introspector gvnf = loader.introspectorFromName("generic-vnf");
612                 Vertex gvnfVert = dbser.createNewVertex(gvnf);
613
614                 gvnf.setValue("vnf-id", "myvnf");
615                 dbser.serializeSingleVertex(gvnfVert, gvnf, "test");
616                 assertTrue(engine.tx().traversal().V().has("aai-node-type","generic-vnf").has("vnf-id","myvnf").hasNext());
617
618         }
619
620         @Test
621         public void serializeSingleVertexChildTest() throws AAIException, UnsupportedEncodingException {
622                 engine.startTransaction();
623
624                 Vertex cr = engine.tx().addVertex("aai-node-type", "cloud-region", "cloud-owner", "me", "cloud-region-id", "123", "aai-uri", "/cloud-infrastructure/cloud-regions/cloud-region/me/123");
625                 Introspector tenIn = loader.introspectorFromName("tenant");
626                 Vertex ten = dbser.createNewVertex(tenIn);
627                 ten.property("aai-uri", cr.property("aai-uri").value().toString() + "/tenants/tenant/453");
628
629                 edgeSer.addTreeEdge(engine.tx().traversal(), cr, ten);
630
631                 tenIn.setValue("tenant-id", "453");
632                 tenIn.setValue("tenant-name", "mytenant");
633
634                 dbser.serializeSingleVertex(ten, tenIn, "test");
635
636                 assertTrue(engine.tx().traversal().V().has("aai-node-type","tenant").has("tenant-id","453").has("tenant-name","mytenant").hasNext());
637
638         }
639
640
641         @Test
642         public void getVertexPropertiesRelationshipHasLabelTest() throws AAIException, UnsupportedEncodingException {
643                 engine.startTransaction();
644
645                 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","vnf-123","aai-uri", "/network/generic-vnfs/generic-vnf/vnf-123");
646                 Vertex vnfc = engine.tx().addVertex("aai-node-type","vnfc","vnfc-name","vnfc-123","aai-uri", "/network/vnfcs/vnfc/vnfc-123");
647
648                 edgeSer.addEdge(engine.tx().traversal(), gvnf, vnfc);
649
650                 Introspector obj = loader.introspectorFromName("generic-vnf");
651                 obj = this.dbser.dbToObject(Arrays.asList(gvnf), obj, AAIProperties.MAXIMUM_DEPTH, false, "false");
652
653
654                 MarshallerProperties properties = new MarshallerProperties
655                                 .Builder(org.onap.aai.restcore.MediaType.getEnum("application/json")).formatted(true).build();
656                 System.out.println(obj.marshal(properties));
657
658                 assertEquals("edge label between generic-vnf and vnfs is uses",
659                                 "org.onap.relationships.inventory.BelongsTo",
660                                 obj.getWrappedValue("relationship-list")
661                                         .getWrappedListValue("relationship")
662                                         .get(0)
663                                         .getValue("relationship-label")
664                 );
665
666
667         }
668
669         @Test
670         public void getVertexPropertiesRelationshipOldVersionNoEdgeLabelTest() throws AAIException, UnsupportedEncodingException {
671
672                 SchemaVersion version = schemaVersions.getAppRootVersion();
673                 DBSerializer dbser = new DBSerializer(version, engine, introspectorFactoryType, "AAI-TEST");
674                 Loader loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
675
676                 engine.startTransaction();
677
678                 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","vnf-123", "aai-uri", "/network/generic-vnfs/generic-vnf/vnf-123");
679                 Vertex vnfc = engine.tx().addVertex("aai-node-type","vnfc","vnfc-name","vnfc-123", "aai-uri", "/network/vnfcs/vnfc/vnfc-123");
680
681                 edgeSer.addEdge(engine.tx().traversal(), gvnf, vnfc);
682
683                 Introspector obj = loader.introspectorFromName("generic-vnf");
684                 obj = dbser.dbToObject(Arrays.asList(gvnf), obj, AAIProperties.MAXIMUM_DEPTH, false, "false");
685
686                 assertEquals("Relationship does not contain edge-property", false, obj.getWrappedValue("relationship-list").getWrappedListValue("relationship").get(0).hasProperty("relationship-label"));
687
688
689         }
690
691         @Test
692         public void createEdgeWithInvalidLabelTest() throws AAIException, UnsupportedEncodingException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
693
694                 engine.startTransaction();
695
696                 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","myvnf", "aai-uri", "/network/generic-vnfs/generic-vnf/myvnf");
697                 engine.tx().addVertex("aai-node-type","vnfc","vnfc-name","a-name", "aai-uri", "/network/vnfcs/vnfc/a-name");
698
699                 Introspector relData = loader.introspectorFromName("relationship-data");
700                 relData.setValue("relationship-key", "vnfc.vnfc-name");
701                 relData.setValue("relationship-value", "a-name");
702                 Introspector relationship = loader.introspectorFromName("relationship");
703                 relationship.setValue("related-to", "vnfc");
704                 relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
705                 relationship.setValue("relationship-data",relData);
706                 relationship.setValue("relationship-label", "NA");
707
708                 thrown.expect(AAIException.class);
709                 thrown.expectMessage("No rule found");
710                 thrown.expectMessage("node type: generic-vnf, node type: vnfc, label: NA, type: COUSIN");
711                 dbser.createEdge(relationship, gvnf);
712
713         }
714
715         @Test
716         public void addRelatedToPropertyTest() throws AAIException {
717                 Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getAppRootVersion());
718                 Introspector gv = loader.introspectorFromName("generic-vnf");
719                 gv.setValue("vnf-name", "myname");
720                 Introspector rel = loader.introspectorFromName("relationship");
721                 DBSerializer dbser = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine,
722                                                                                                 ModelType.MOXY, "AAI-TEST");
723                 dbser.addRelatedToProperty(rel, gv);
724                 List<Introspector> relToProps = rel.getWrappedListValue("related-to-property");
725                 assertTrue(relToProps.size() == 1);
726                 Introspector relToProp = relToProps.get(0);
727                 assertTrue("generic-vnf.vnf-name".equals(relToProp.getValue("property-key")));
728                 assertTrue("myname".equals(relToProp.getValue("property-value")));
729         }
730
731         @Test
732         public void dbToObjectContainerMismatchTest() throws AAIException, UnsupportedEncodingException {
733                 DBSerializer dbser = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine,
734                                 ModelType.MOXY, "AAI-TEST");
735                 Graph vertexMaker = TinkerGraph.open();
736                 Vertex a = vertexMaker.addVertex(T.id, "0");
737                 Vertex b = vertexMaker.addVertex(T.id, "1");
738                 List<Vertex> vertices = Arrays.asList(a,b);
739
740                 Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getAppRootVersion());
741                 Introspector intro = loader.introspectorFromName("image"); //just need any non-container object
742
743                 thrown.expect(AAIException.class);
744                 thrown.expectMessage("query object mismatch: this object cannot hold multiple items.");
745
746                 dbser.dbToObject(vertices, intro, Integer.MAX_VALUE, true, "doesn't matter");
747         }
748
749         @Test
750         public void dbToObjectTest() throws AAIException, UnsupportedEncodingException {
751                 engine.startTransaction();
752
753                 DBSerializer dbser = new DBSerializer(version, engine,
754                                 ModelType.MOXY, "AAI-TEST");
755                 Vertex gv1 = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "id1");
756                 Vertex gv2 = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "id2");
757                 List<Vertex> vertices = Arrays.asList(gv1, gv2);
758
759                 Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, version);
760                 Introspector gvContainer = loader.introspectorFromName("generic-vnfs");
761
762                 Introspector res = dbser.dbToObject(vertices, gvContainer, 0, true, "true");
763                 List<Introspector> gvs = res.getWrappedListValue("generic-vnf");
764                 assertTrue(gvs.size() == 2);
765                 for (Introspector i : gvs) {
766                         String vnfId = i.getValue("vnf-id");
767                         assertTrue("id1".equals(vnfId) || "id2".equals(vnfId));
768                 }
769
770
771         }
772
773         @Test
774         public void getEdgeBetweenNoLabelTest() throws AAIException {
775                 DBSerializer dbser = new DBSerializer(version, engine,
776                                 ModelType.MOXY, "AAI-TEST");
777                 engine.startTransaction();
778                 Vertex gv = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "id1");
779                 Vertex lint = engine.tx().addVertex("aai-node-type", "l-interface", "interface-name", "name1");
780                 edgeSer.addTreeEdge(engine.tx().traversal(), gv, lint);
781
782                 Edge res = dbser.getEdgeBetween(EdgeType.TREE, gv, lint);
783                 assertEquals("org.onap.relationships.inventory.BelongsTo", res.label());
784
785         }
786
787         @Test
788         public void deleteItemsWithTraversal() throws AAIException {
789                 DBSerializer dbser = new DBSerializer(version, engine,
790                                 ModelType.MOXY, "AAI-TEST");
791                 engine.startTransaction();
792                 Vertex gv = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "id1");
793                 Vertex lint = engine.tx().addVertex("aai-node-type", "l-interface", "interface-name", "name1");
794
795                 assertTrue(engine.tx().traversal().V().has("vnf-id", "id1").hasNext());
796                 assertTrue(engine.tx().traversal().V().has("interface-name", "name1").hasNext());
797
798                 dbser.deleteItemsWithTraversal(Arrays.asList(gv, lint));
799
800                 assertTrue(!engine.tx().traversal().V().has("vnf-id", "id1").hasNext());
801                 assertTrue(!engine.tx().traversal().V().has("interface-name", "name1").hasNext());
802
803
804         }
805
806         @Test
807         public void serializeToDbWithParentTest() throws AAIException, UnsupportedEncodingException, URISyntaxException {
808                 DBSerializer dbser = new DBSerializer(version, engine,
809                                 ModelType.MOXY, "AAI-TEST");
810                 engine.startTransaction();
811                 Vertex gv = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "id1", "aai-uri", "/network/generic-vnfs/generic-vnf/id1");
812                 Vertex lint = engine.tx().addVertex("aai-node-type", "l-interface");
813                 edgeSer.addTreeEdge(engine.tx().traversal(), gv, lint);
814
815                 Introspector lintIntro = loader.introspectorFromName("l-interface");
816                 lintIntro.setValue("interface-name", "name1");
817                 lintIntro.setValue("interface-role", "actor");
818                 URI lintURI = new URI("/network/generic-vnfs/generic-vnf/id1/l-interfaces/l-interface/name1");
819                 QueryParser uriQuery = engine.getQueryBuilder(gv).createQueryFromURI(lintURI);
820                 dbser.serializeToDb(lintIntro, lint, uriQuery, "test-identifier", "AAI-TEST");
821
822                 assertTrue(engine.tx().traversal().V(lint).has("interface-role", "actor").hasNext());
823
824
825         }
826
827         @Test
828         public void getLatestVersionViewTest() throws AAIException, UnsupportedEncodingException {
829                 DBSerializer dbser = new DBSerializer(version, engine,
830                                 ModelType.MOXY, "AAI-TEST");
831                 engine.startTransaction();
832                 Vertex phys = engine.tx().addVertex("aai-node-type", "physical-link", "link-name", "zaldo",
833                                                                                                 "speed-value", "very-fast", "service-provider-bandwidth-up-units", "things");
834
835                 Introspector res = dbser.getLatestVersionView(phys);
836                 assertTrue("zaldo".equals(res.getValue("link-name")));
837                 assertTrue("very-fast".equals(res.getValue("speed-value")));
838                 assertTrue("things".equals(res.getValue("service-provider-bandwidth-up-units")));
839         }
840
841         @Test
842         public void cascadeVserverDeleteTest() throws AAIException {
843                 vserverSetup();
844                 String expected_message = "";
845
846                 /* vserver-->l-interface -->logical-link
847                  *                       -->l3-ipvx-list
848                  */
849                 Vertex vserver = graph.traversal().V().has("aai-node-type", "vserver").has("vserver-id", "vss1").next();
850
851                 String exceptionMessage = testCascadeDelete(vserver);
852                 assertEquals(expected_message, exceptionMessage);
853
854         }
855
856         @Test
857         public void cascadeL3NetworkPreventDeleteTest() throws AAIException {
858                 l3NetworkSetup();
859                 ArrayList  expected_messages = new ArrayList<String>();
860                 expected_messages.add("Object is being reference by additional objects preventing it from being deleted. Please clean up references from the following types [l3-interface-ipv4-address-list, l3-interface-ipv6-address-list]");
861                 expected_messages.add("Object is being reference by additional objects preventing it from being deleted. Please clean up references from the following types [l3-interface-ipv6-address-list, l3-interface-ipv4-address-list]");
862
863                 /* vserver-->l-interface -->logical-link
864                  *                       -->l3-ipvx-list
865                  */
866                 Vertex l3network = graph.traversal().V().has("aai-node-type", "l3-network").has("network-id", "network-id-v1").next();
867
868                 String exceptionMessage = testCascadeDelete(l3network);
869                 assertTrue(expected_messages.contains(exceptionMessage));
870
871         }
872
873         @Test
874         public void cascadeL3NetworkDeleteTest() throws AAIException {
875                 l3NetworkSetup();
876                 String expected_message = "";
877
878                 /* vserver-->l-interface -->logical-link
879                  *                       -->l3-ipvx-list
880                  */
881                 Vertex l3network = graph.traversal().V().has("aai-node-type", "l3-network").has("network-id", "network-id-v2").next();
882
883                 String exceptionMessage = testCascadeDelete(l3network);
884                 assertEquals(expected_message, exceptionMessage);
885
886         }
887 }