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