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