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