Refactor DBSerializer verifyResourceVersion method
[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 serializer;
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         serializer = 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                 serializer.getURIForVertex(l3interipv4addresslist_1).toString());
219         subnet_2.property(AAIProperties.AAI_URI, serializer.getURIForVertex(subnet_2).toString());
220         l3interipv6addresslist_3.property(AAIProperties.AAI_URI,
221                 serializer.getURIForVertex(l3interipv6addresslist_3).toString());
222         subnet_4.property(AAIProperties.AAI_URI, serializer.getURIForVertex(subnet_4).toString());
223         subnet_5.property(AAIProperties.AAI_URI, serializer.getURIForVertex(subnet_5).toString());
224         l3network_6.property(AAIProperties.AAI_URI, serializer.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, serializer.getURIForVertex(subnet1).toString());
269         l3interipv4addresslist_1.property(AAIProperties.AAI_URI,
270                 serializer.getURIForVertex(l3interipv4addresslist_1).toString());
271         l3network1.property(AAIProperties.AAI_URI, serializer.getURIForVertex(l3network1).toString());
272         subnet2.property(AAIProperties.AAI_URI, serializer.getURIForVertex(subnet2).toString());
273         l3network2.property(AAIProperties.AAI_URI, serializer.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, serializer.getURIForVertex(vserver1).toString());
329         lInterface1.property(AAIProperties.AAI_URI, serializer.getURIForVertex(lInterface1).toString());
330         lInterface2.property(AAIProperties.AAI_URI, serializer.getURIForVertex(lInterface2).toString());
331         l3interipv4addresslist_1.property(AAIProperties.AAI_URI,
332                 serializer.getURIForVertex(l3interipv4addresslist_1).toString());
333         l3interipv6addresslist_2.property(AAIProperties.AAI_URI,
334                 serializer.getURIForVertex(l3interipv6addresslist_2).toString());
335         logicalLink1.property(AAIProperties.AAI_URI, serializer.getURIForVertex(logicalLink1).toString());
336         logicalLink2.property(AAIProperties.AAI_URI, serializer.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 = serializer.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         serializer.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         serializer.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 thatDeleteWithMatchingResourceVersionsIsValid() throws AAIException {
503         engine.startTransaction();
504
505         assertTrue(serializer.verifyResourceVersion("delete", "vnfc", "abc", "abc", "vnfcs/vnfc/vnfcId"));
506
507     }
508
509     @Test
510     public void thatDeleteWithResourceVersionDisabledConstantUUIDIsValid() throws AAIException {
511         engine.startTransaction();
512
513         assertTrue(serializer.verifyResourceVersion("delete", "generic-vnf", "current-res-ver",
514                 AAIConstants.AAI_RESVERSION_DISABLED_UUID_DEFAULT, "generic-vnfs/generic-vnf/myid"));
515     }
516
517     @Test
518     public void thatDeleteWithMismatchingResourceVersionsIsInvalid() throws AAIException {
519         engine.startTransaction();
520
521         thrown.expect(AAIException.class);
522         thrown.expectMessage("resource-version MISMATCH for delete of vnfcs/vnfc/vnfcId");
523         assertTrue(serializer.verifyResourceVersion("delete", "vnfc", "currentResourceVersion", "mismatchingResourceVersion", "vnfcs/vnfc/vnfcId"));
524
525     }
526
527     @Test
528     public void thatCreateWithoutResourceVersionsIsValid() throws AAIException {
529         engine.startTransaction();
530
531         assertTrue(serializer.verifyResourceVersion("create", "generic-vnf", null, null, "generic-vnfs/generic-vnf/myid"));
532     }
533
534     @Test
535     public void thatCreateWithResourceVersionIsInvalid() throws AAIException {
536         engine.startTransaction();
537
538         thrown.expect(AAIException.class);
539         thrown.expectMessage("resource-version passed for create of generic-vnfs/generic-vnf/myid");
540         serializer.verifyResourceVersion("create", "generic-vnf", null, "old-res-ver", "generic-vnfs/generic-vnf/myid");
541     }
542
543     @Test
544     public void thatUpdateWithMatchingResourceVersionsIsValid() throws AAIException {
545         engine.startTransaction();
546
547         assertTrue(serializer.verifyResourceVersion("update", "generic-vnf", "current-res-ver", "current-res-ver", "generic-vnfs/generic-vnf/myid"));
548     }
549
550     @Test
551     public void thatUpdateWithoutResourceVersionIsInvalid() throws AAIException {
552         engine.startTransaction();
553
554         thrown.expect(AAIException.class);
555         thrown.expectMessage("resource-version not passed for update of generic-vnfs/generic-vnf/myid");
556         serializer.verifyResourceVersion("update", "generic-vnf", "current-res-ver", null, "generic-vnfs/generic-vnf/myid");
557
558     }
559
560     @Test
561     public void thatUpdateWithResourceVersionMismatchIsInvalid() throws AAIException {
562         engine.startTransaction();
563
564         thrown.expect(AAIException.class);
565         thrown.expectMessage("resource-version MISMATCH for update of generic-vnfs/generic-vnf/myid");
566         serializer.verifyResourceVersion("update", "generic-vnf", "current-res-ver", "old-res-ver",
567                 "generic-vnfs/generic-vnf/myid");
568
569     }
570
571     @Test
572     public void trimClassNameTest() {
573         assertEquals("GenericVnf", serializer.trimClassName("GenericVnf"));
574         assertEquals("GenericVnf", serializer.trimClassName("org.onap.aai.GenericVnf"));
575     }
576
577     @Test
578     public void getURIForVertexTest() throws AAIException, URISyntaxException, UnsupportedEncodingException {
579         engine.startTransaction();
580
581         Vertex cr = engine.tx().addVertex("aai-node-type", "cloud-region", "cloud-owner", "me", "cloud-region-id",
582                 "123", "aai-uri", "/cloud-infrastructure/cloud-regions/cloud-region/me/123");
583         Vertex ten = engine.tx().addVertex("aai-node-type", "tenant", "tenant-id", "453");
584
585         edgeSer.addTreeEdge(engine.tx().traversal(), cr, ten);
586
587         ten.property("aai-uri", "/cloud-infrastructure/cloud-regions/cloud-region/me/123/tenants/tenant/453");
588
589         URI compare = new URI("/cloud-infrastructure/cloud-regions/cloud-region/me/123/tenants/tenant/453");
590         assertEquals(compare, serializer.getURIForVertex(ten));
591
592         URI compareFailure = new URI("/unknown-uri");
593         ten.property("aai-uri").remove();
594         assertEquals(compareFailure, serializer.getURIForVertex(ten));
595
596     }
597
598     @Test
599     public void getVertexPropertiesTest() throws AAIException, UnsupportedEncodingException {
600         engine.startTransaction();
601
602         Vertex gvnf = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "myvnf", "aai-uri",
603                 "/network/generic-vnfs/generic-vnf/myvnf", AAIProperties.AAI_UUID, UUID.randomUUID().toString(),
604                 AAIProperties.CREATED_TS, 123L, AAIProperties.SOURCE_OF_TRUTH, "sot", AAIProperties.RESOURCE_VERSION,
605                 "123", AAIProperties.LAST_MOD_SOURCE_OF_TRUTH, "lmsot", AAIProperties.LAST_MOD_TS, 333L);
606         Vertex vnfc = engine.tx().addVertex("aai-node-type", "vnfc", "vnfc-name", "a-name", "aai-uri",
607                 "/network/vnfcs/vnfc/a-name", AAIProperties.AAI_UUID, UUID.randomUUID().toString(),
608                 AAIProperties.CREATED_TS, 123L, AAIProperties.SOURCE_OF_TRUTH, "sot", AAIProperties.RESOURCE_VERSION,
609                 "123", AAIProperties.LAST_MOD_SOURCE_OF_TRUTH, "lmsot", AAIProperties.LAST_MOD_TS, 333L);
610
611         edgeSer.addEdge(engine.tx().traversal(), gvnf, vnfc);
612
613         Introspector vnf = serializer.getVertexProperties(gvnf);
614         assertEquals("generic-vnf", vnf.getDbName());
615         assertEquals("myvnf", vnf.getValue("vnf-id"));
616
617         assertFalse(vnf.marshal(false).contains("relationship-list"));
618
619     }
620
621     @Test
622     public void getEdgeBetweenTest() throws AAIException {
623         engine.startTransaction();
624
625         Vertex cr =
626                 engine.tx().addVertex("aai-node-type", "cloud-region", "cloud-owner", "me", "cloud-region-id", "123");
627         Vertex ten = engine.tx().addVertex("aai-node-type", "tenant", "tenant-id", "453");
628
629         edgeSer.addTreeEdge(engine.tx().traversal(), cr, ten);
630
631         Edge e = serializer.getEdgeBetween(EdgeType.TREE, ten, cr, null);
632         assertEquals("org.onap.relationships.inventory.BelongsTo", e.label());
633
634     }
635
636     @Test
637     public void deleteEdgeTest() throws AAIException, UnsupportedEncodingException {
638         engine.startTransaction();
639
640         Vertex gvnf = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "myvnf", "aai-uri",
641                 "/network/generic-vnfs/generic-vnf/myvnf", AAIProperties.AAI_UUID, UUID.randomUUID().toString(),
642                 AAIProperties.CREATED_TS, 123L, AAIProperties.SOURCE_OF_TRUTH, "sot", AAIProperties.RESOURCE_VERSION,
643                 "123", AAIProperties.LAST_MOD_SOURCE_OF_TRUTH, "lmsot", AAIProperties.LAST_MOD_TS, 333L);
644         Vertex vnfc = engine.tx().addVertex("aai-node-type", "vnfc", "vnfc-name", "a-name", "aai-uri",
645                 "/network/vnfcs/vnfc/a-name", AAIProperties.AAI_UUID, UUID.randomUUID().toString(),
646                 AAIProperties.CREATED_TS, 123L, AAIProperties.SOURCE_OF_TRUTH, "sot", AAIProperties.RESOURCE_VERSION,
647                 "123", AAIProperties.LAST_MOD_SOURCE_OF_TRUTH, "lmsot", AAIProperties.LAST_MOD_TS, 333L);
648
649         edgeSer.addEdge(engine.tx().traversal(), gvnf, vnfc);
650
651         Introspector relData = loader.introspectorFromName("relationship-data");
652         relData.setValue("relationship-key", "vnfc.vnfc-name");
653         relData.setValue("relationship-value", "a-name");
654         Introspector relationship = loader.introspectorFromName("relationship");
655         relationship.setValue("related-to", "vnfc");
656         relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
657         relationship.setValue("relationship-data", relData);
658
659         assertTrue(serializer.deleteEdge(relationship, gvnf).isPresent());
660
661         assertFalse(engine.tx().traversal().V(gvnf).both("uses").hasNext());
662         assertFalse(engine.tx().traversal().V(vnfc).both("uses").hasNext());
663
664     }
665
666     @Test
667     public void createEdgeTest() throws AAIException, UnsupportedEncodingException {
668         engine.startTransaction();
669
670         Vertex gvnf = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "myvnf", "aai-uri",
671                 "/network/generic-vnfs/generic-vnf/myvnf", "aai-uuid", "a");
672         Vertex vnfc = engine.tx().addVertex("aai-node-type", "vnfc", "vnfc-name", "a-name", "aai-uri",
673                 "/network/vnfcs/vnfc/a-name", "aai-uuid", "b");
674
675         // sunny day case
676         Introspector relData = loader.introspectorFromName("relationship-data");
677         relData.setValue("relationship-key", "vnfc.vnfc-name");
678         relData.setValue("relationship-value", "a-name");
679         Introspector relationship = loader.introspectorFromName("relationship");
680         relationship.setValue("related-to", "vnfc");
681         relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
682         relationship.setValue("relationship-data", relData);
683
684         assertNotNull(serializer.createEdge(relationship, gvnf));
685         assertTrue(engine.tx().traversal().V(gvnf).both("org.onap.relationships.inventory.BelongsTo").hasNext());
686         assertTrue(engine.tx().traversal().V(vnfc).both("org.onap.relationships.inventory.BelongsTo").hasNext());
687
688     }
689
690     @Test
691     public void createCousinEdgeThatShouldBeTreeTest()
692             throws AAIException, UnsupportedEncodingException, URISyntaxException {
693         engine.startTransaction();
694
695         Vertex gvnf = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "myvnf", "aai-uri",
696                 "/network/generic-vnfs/generic-vnf/myvnf");
697         Vertex vf = engine.tx().addVertex("aai-node-type", "vf-module", "vf-module-id", "vf-id", "aai-uri",
698                 "/network/generic-vnfs/generic-vnf/myvnf/vf-modules/vf-module/vf-id");
699
700         edgeSer.addTreeEdge(engine.tx().traversal(), gvnf, vf);
701
702         Introspector relationship = loader.introspectorFromName("relationship");
703         relationship.setValue("related-to", "vf-module");
704         relationship.setValue("related-link", serializer.getURIForVertex(vf).toString());
705         Introspector relationshipList = loader.introspectorFromName("relationship-list");
706         relationshipList.setValue("relationship", Collections.singletonList(relationship.getUnderlyingObject()));
707
708         Introspector gvnfObj = loader.introspectorFromName("generic-vnf");
709         Vertex gvnf2 = serializer.createNewVertex(gvnfObj);
710         gvnfObj.setValue("relationship-list", relationshipList.getUnderlyingObject());
711         gvnfObj.setValue("vnf-id", "myvnf-1");
712
713         QueryParser uriQuery =
714                 dbEngine.getQueryBuilder().createQueryFromURI(new URI("/network/generic-vnfs/generic-vnf/myvnf-1"));
715
716         try {
717             serializer.serializeToDb(gvnfObj, gvnf2, uriQuery, null, "test");
718         } catch (AAIException e) {
719             assertEquals("AAI_6145", e.getCode());
720         }
721     }
722
723     @Test
724     public void createEdgeNodeDoesNotExistExceptionTest() throws AAIException, UnsupportedEncodingException {
725         engine.startTransaction();
726
727         Vertex gvnf = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "myvnf", "aai-uri",
728                 "/network/generic-vnfs/generic-vnf/myvnf");
729
730         // rainy day case, edge to non-existent object
731         Introspector relData = loader.introspectorFromName("relationship-data");
732         relData.setValue("relationship-key", "vnfc.vnfc-name");
733         relData.setValue("relationship-value", "b-name");
734         Introspector relationship = loader.introspectorFromName("relationship");
735         relationship.setValue("related-to", "vnfc");
736         relationship.setValue("related-link", "/network/vnfcs/vnfc/b-name");
737         relationship.setValue("relationship-data", relData);
738
739         thrown.expect(AAIException.class);
740         thrown.expectMessage("Node of type vnfc. Could not find object at: /network/vnfcs/vnfc/b-name");
741         serializer.createEdge(relationship, gvnf);
742
743     }
744
745     @Test
746     public void serializeSingleVertexTopLevelTest() throws AAIException, UnsupportedEncodingException {
747         engine.startTransaction();
748
749         Introspector gvnf = loader.introspectorFromName("generic-vnf");
750         Vertex gvnfVert = serializer.createNewVertex(gvnf);
751
752         gvnf.setValue("vnf-id", "myvnf");
753         gvnf.setValue("vnf-type", "typo");
754         serializer.serializeSingleVertex(gvnfVert, gvnf, "test");
755         assertTrue(engine.tx().traversal().V().has("aai-node-type", "generic-vnf").has("vnf-id", "myvnf").hasNext());
756     }
757
758     @Test
759     public void serializeSingleVertexChildTest() throws AAIException, UnsupportedEncodingException {
760         engine.startTransaction();
761
762         Vertex cr = engine.tx().addVertex("aai-node-type", "cloud-region", "cloud-owner", "me", "cloud-region-id",
763                 "123", "aai-uri", "/cloud-infrastructure/cloud-regions/cloud-region/me/123");
764         Introspector tenIn = loader.introspectorFromName("tenant");
765         Vertex ten = serializer.createNewVertex(tenIn);
766         ten.property("aai-uri", cr.property("aai-uri").value().toString() + "/tenants/tenant/453");
767
768         edgeSer.addTreeEdge(engine.tx().traversal(), cr, ten);
769
770         tenIn.setValue("tenant-id", "453");
771         tenIn.setValue("tenant-name", "mytenant");
772
773         serializer.serializeSingleVertex(ten, tenIn, "test");
774
775         assertTrue(engine.tx().traversal().V().has("aai-node-type", "tenant").has("tenant-id", "453")
776                 .has("tenant-name", "mytenant").hasNext());
777
778     }
779
780     @Test
781     public void getVertexPropertiesRelationshipHasLabelTest() throws AAIException, UnsupportedEncodingException {
782         engine.startTransaction();
783
784         Vertex gvnf = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "vnf-123", "aai-uri",
785                 "/network/generic-vnfs/generic-vnf/vnf-123", "aai-uuid", "a");
786         Vertex vnfc = engine.tx().addVertex("aai-node-type", "vnfc", "vnfc-name", "vnfc-123", "aai-uri",
787                 "/network/vnfcs/vnfc/vnfc-123", "aai-uuid", "b");
788
789         edgeSer.addEdge(engine.tx().traversal(), gvnf, vnfc);
790
791         Introspector obj = loader.introspectorFromName("generic-vnf");
792         obj = this.serializer.dbToObject(Collections.singletonList(gvnf), obj, AAIProperties.MAXIMUM_DEPTH, false, "false");
793
794         assertEquals("edge label between generic-vnf and vnfs is uses", "org.onap.relationships.inventory.BelongsTo",
795                 obj.getWrappedValue("relationship-list").getWrappedListValue("relationship").get(0)
796                         .getValue("relationship-label"));
797
798     }
799
800     @Test
801     public void getVertexPropertiesRelationshipOldVersionNoEdgeLabelTest()
802             throws AAIException, UnsupportedEncodingException {
803
804         SchemaVersion version = schemaVersions.getAppRootVersion();
805         DBSerializer dbser = new DBSerializer(version, engine, introspectorFactoryType, "AAI-TEST");
806         Loader loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
807
808         engine.startTransaction();
809
810         Vertex gvnf = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "vnf-123", "aai-uri",
811                 "/network/generic-vnfs/generic-vnf/vnf-123");
812         Vertex vnfc = engine.tx().addVertex("aai-node-type", "vnfc", "vnfc-name", "vnfc-123", "aai-uri",
813                 "/network/vnfcs/vnfc/vnfc-123");
814
815         edgeSer.addEdge(engine.tx().traversal(), gvnf, vnfc);
816
817         Introspector obj = loader.introspectorFromName("generic-vnf");
818         obj = dbser.dbToObject(Collections.singletonList(gvnf), obj, AAIProperties.MAXIMUM_DEPTH, false, "false");
819
820         assertFalse("Relationship does not contain edge-property", obj.getWrappedValue("relationship-list")
821                 .getWrappedListValue("relationship").get(0).hasProperty("relationship-label"));
822
823     }
824
825     @Test
826     public void createEdgeWithInvalidLabelTest()
827             throws AAIException, UnsupportedEncodingException, SecurityException, IllegalArgumentException {
828
829         engine.startTransaction();
830
831         Vertex gvnf = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "myvnf", "aai-uri",
832                 "/network/generic-vnfs/generic-vnf/myvnf", "aai-uuid", "a");
833         engine.tx().addVertex("aai-node-type", "vnfc", "vnfc-name", "a-name", "aai-uri", "/network/vnfcs/vnfc/a-name",
834                 "aai-uuid", "b");
835
836         Introspector relData = loader.introspectorFromName("relationship-data");
837         relData.setValue("relationship-key", "vnfc.vnfc-name");
838         relData.setValue("relationship-value", "a-name");
839         Introspector relationship = loader.introspectorFromName("relationship");
840         relationship.setValue("related-to", "vnfc");
841         relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
842         relationship.setValue("relationship-data", relData);
843         relationship.setValue("relationship-label", "NA");
844
845         thrown.expect(AAIException.class);
846         thrown.expectMessage("No rule found");
847         thrown.expectMessage("node type: generic-vnf, node type: vnfc, label: NA, type: COUSIN");
848         serializer.createEdge(relationship, gvnf);
849
850     }
851
852     @Test
853     public void createEdgeUsingIntrospectorTest()
854             throws AAIException, UnsupportedEncodingException, SecurityException, IllegalArgumentException {
855
856         engine.startTransaction();
857
858         Vertex gvnf = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "myvnf", "aai-uri",
859                 "/network/generic-vnfs/generic-vnf/myvnf", "aai-uuid", "a");
860         engine.tx().addVertex("aai-node-type", "vnfc", "vnfc-name", "a-name", "aai-uri", "/network/vnfcs/vnfc/a-name",
861                 "aai-uuid", "b");
862
863         Introspector relData = loader.introspectorFromName("relationship-data");
864         relData.setValue("relationship-key", "vnfc.vnfc-name");
865         relData.setValue("relationship-value", "a-name");
866         Introspector relationship = loader.introspectorFromName("relationship");
867         relationship.setValue("related-to", "vnfc");
868         relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
869         relationship.setValue("relationship-data", relData);
870
871         assertEquals("/network/vnfcs/vnfc/a-name", relationship.getValue("related-link"));
872
873         serializer.createEdge(relationship, gvnf);
874     }
875
876     @Test
877     public void addRelatedToPropertyTest() throws AAIException {
878         engine.startTransaction();
879
880         Vertex gvnf = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "myname", "vnf-name", "myname",
881                 "aai-uri", "/network/generic-vnfs/generic-vnf/myname");
882         engine.tx().addVertex("aai-node-type", "vnfc", "vnfc-name", "a-name", "aai-uri", "/network/vnfcs/vnfc/a-name");
883         Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getAppRootVersion());
884         Introspector gv = loader.introspectorFromName("generic-vnf");
885         gv.setValue("vnf-name", "myname");
886
887         Introspector rel = loader.introspectorFromName("relationship");
888         DBSerializer dbser = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine, ModelType.MOXY, "AAI-TEST");
889         dbser.addRelatedToProperty(rel, gvnf, "generic-vnf");
890         List<Introspector> relToProps = rel.getWrappedListValue("related-to-property");
891         assertThat(relToProps.size(), is(1));
892         Introspector relToProp = relToProps.get(0);
893         assertThat(relToProp.getValue("property-key"), is("generic-vnf.vnf-name"));
894         assertThat(relToProp.getValue("property-value"), is("myname"));
895     }
896
897     @Test
898     public void dbToObjectContainerMismatchTest() throws AAIException, UnsupportedEncodingException {
899         DBSerializer dbser = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine, ModelType.MOXY, "AAI-TEST");
900         Graph vertexMaker = TinkerGraph.open();
901         Vertex a = vertexMaker.addVertex(T.id, "0");
902         Vertex b = vertexMaker.addVertex(T.id, "1");
903         List<Vertex> vertices = Arrays.asList(a, b);
904
905         Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getAppRootVersion());
906         Introspector intro = loader.introspectorFromName("image"); // just need any non-container object
907
908         thrown.expect(AAIException.class);
909         thrown.expectMessage("query object mismatch: this object cannot hold multiple items.");
910
911         dbser.dbToObject(vertices, intro, Integer.MAX_VALUE, true, "doesn't matter");
912     }
913
914     @Test
915     public void dbToObjectTest() throws AAIException, UnsupportedEncodingException {
916         engine.startTransaction();
917
918         DBSerializer dbser = new DBSerializer(version, engine, ModelType.MOXY, "AAI-TEST");
919         Vertex gv1 = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "id1");
920         Vertex gv2 = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "id2");
921         List<Vertex> vertices = Arrays.asList(gv1, gv2);
922
923         Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, version);
924         Introspector gvContainer = loader.introspectorFromName("generic-vnfs");
925
926         Introspector res = dbser.dbToObject(vertices, gvContainer, 0, true, "true");
927         List<Introspector> gvs = res.getWrappedListValue("generic-vnf");
928         assertEquals(2, gvs.size());
929         for (Introspector i : gvs) {
930             String vnfId = i.getValue("vnf-id");
931             assertTrue("id1".equals(vnfId) || "id2".equals(vnfId));
932         }
933
934     }
935
936     @Test
937     public void getEdgeBetweenNoLabelTest() throws AAIException {
938         DBSerializer dbser = new DBSerializer(version, engine, ModelType.MOXY, "AAI-TEST");
939         engine.startTransaction();
940         Vertex gv = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "id1");
941         Vertex lint = engine.tx().addVertex("aai-node-type", "l-interface", "interface-name", "name1");
942         edgeSer.addTreeEdge(engine.tx().traversal(), gv, lint);
943
944         Edge res = dbser.getEdgeBetween(EdgeType.TREE, gv, lint);
945         assertEquals("org.onap.relationships.inventory.BelongsTo", res.label());
946
947     }
948
949     @Test
950     public void deleteItemsWithTraversal() throws AAIException {
951         DBSerializer dbser = new DBSerializer(version, engine, ModelType.MOXY, "AAI-TEST");
952         engine.startTransaction();
953         Vertex gv = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "id1", AAIProperties.AAI_URI,
954                 "/network/generic-vnfs/generic-vnf/id1", AAIProperties.AAI_UUID, UUID.randomUUID().toString(),
955                 AAIProperties.CREATED_TS, 123L, AAIProperties.SOURCE_OF_TRUTH, "sot", AAIProperties.RESOURCE_VERSION,
956                 "123", AAIProperties.LAST_MOD_SOURCE_OF_TRUTH, "lmsot", AAIProperties.LAST_MOD_TS, 333L);
957         Vertex lint = engine.tx().addVertex("aai-node-type", "l-interface", "interface-name", "name1",
958                 AAIProperties.AAI_URI, "/network/generic-vnfs/generic-vnf/id1/l-interfaces/l-interface/name1",
959                 AAIProperties.AAI_UUID, UUID.randomUUID().toString(), AAIProperties.CREATED_TS, 123L,
960                 AAIProperties.SOURCE_OF_TRUTH, "sot", AAIProperties.RESOURCE_VERSION, "123",
961                 AAIProperties.LAST_MOD_SOURCE_OF_TRUTH, "lmsot", AAIProperties.LAST_MOD_TS, 333L);
962
963         assertTrue(engine.tx().traversal().V().has("vnf-id", "id1").hasNext());
964         assertTrue(engine.tx().traversal().V().has("interface-name", "name1").hasNext());
965
966         dbser.deleteWithTraversal(gv);
967         dbser.deleteWithTraversal(lint);
968
969         assertFalse(engine.tx().traversal().V().has("vnf-id", "id1").hasNext());
970         assertFalse(engine.tx().traversal().V().has("interface-name", "name1").hasNext());
971
972     }
973
974     @Test
975     public void serializeToDbWithParentTest() throws AAIException, UnsupportedEncodingException, URISyntaxException {
976         DBSerializer dbser = new DBSerializer(version, engine, ModelType.MOXY, "AAI-TEST");
977         engine.startTransaction();
978         Vertex gv = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "id1", "aai-uri",
979                 "/network/generic-vnfs/generic-vnf/id1", "aai-uuid", "a", AAIProperties.CREATED_TS, 123L,
980                 AAIProperties.SOURCE_OF_TRUTH, "sot", AAIProperties.RESOURCE_VERSION, "123",
981                 AAIProperties.LAST_MOD_SOURCE_OF_TRUTH, "lmsot", AAIProperties.LAST_MOD_TS, 333L);
982         Vertex lint = engine.tx().addVertex("aai-node-type", "l-interface", "aai-uri", "abc", "aai-uuid", "b",
983                 AAIProperties.CREATED_TS, 123L, AAIProperties.SOURCE_OF_TRUTH, "sot", AAIProperties.RESOURCE_VERSION,
984                 "123", AAIProperties.LAST_MOD_SOURCE_OF_TRUTH, "lmsot", AAIProperties.LAST_MOD_TS, 333L);
985         edgeSer.addTreeEdge(engine.tx().traversal(), gv, lint);
986
987         Introspector lintIntro = loader.introspectorFromName("l-interface");
988         lintIntro.setValue("interface-name", "name1");
989         lintIntro.setValue("interface-role", "actor");
990         URI lintURI = new URI("/network/generic-vnfs/generic-vnf/id1/l-interfaces/l-interface/name1");
991         QueryParser uriQuery = engine.getQueryBuilder(gv).createQueryFromURI(lintURI);
992         dbser.serializeToDb(lintIntro, lint, uriQuery, "test-identifier", "AAI-TEST");
993
994         assertTrue(engine.tx().traversal().V(lint).has("interface-role", "actor").hasNext());
995
996     }
997
998     @Test
999     public void getLatestVersionViewTest() throws AAIException, UnsupportedEncodingException {
1000         DBSerializer dbser = new DBSerializer(version, engine, ModelType.MOXY, "AAI-TEST");
1001         engine.startTransaction();
1002         Vertex phys = engine.tx().addVertex("aai-node-type", "physical-link", "link-name", "zaldo", "speed-value",
1003                 "very-fast", "service-provider-bandwidth-up-units", "things");
1004
1005         Introspector res = dbser.getLatestVersionView(phys);
1006         assertEquals("zaldo", res.getValue("link-name"));
1007         assertEquals("very-fast", res.getValue("speed-value"));
1008         assertEquals("things", res.getValue("service-provider-bandwidth-up-units"));
1009     }
1010
1011     @Test
1012     public void cascadeVserverDeleteTest() throws AAIException, UnsupportedEncodingException {
1013         vserverSetup();
1014         String expected_message = "";
1015
1016         /*
1017          * vserver-->l-interface -->logical-link
1018          * -->l3-ipvx-list
1019          */
1020         Vertex vserver = graph.traversal().V().has("aai-node-type", "vserver").has("vserver-id", "vss1").next();
1021
1022         String exceptionMessage = testCascadeDelete(vserver);
1023         assertEquals(expected_message, exceptionMessage);
1024
1025     }
1026
1027     @Test
1028     public void cascadeL3NetworkPreventDeleteTest() throws AAIException, UnsupportedEncodingException {
1029         l3NetworkSetup();
1030         ArrayList<String> expected_messages = new ArrayList<>();
1031         expected_messages.add(
1032                 "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]");
1033         expected_messages.add(
1034                 "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]");
1035
1036         /*
1037          * vserver-->l-interface -->logical-link
1038          * -->l3-ipvx-list
1039          */
1040         Vertex l3network =
1041                 graph.traversal().V().has("aai-node-type", "l3-network").has("network-id", "network-id-v1").next();
1042
1043         String exceptionMessage = testCascadeDelete(l3network);
1044         assertTrue(expected_messages.contains(exceptionMessage));
1045
1046     }
1047
1048     @Test
1049     public void cascadeL3NetworkDeleteTest() throws AAIException, UnsupportedEncodingException {
1050         l3NetworkSetup();
1051         String expected_message = "";
1052
1053         /*
1054          * vserver-->l-interface -->logical-link
1055          * -->l3-ipvx-list
1056          */
1057         Vertex l3network =
1058                 graph.traversal().V().has("aai-node-type", "l3-network").has("network-id", "network-id-v2").next();
1059
1060         String exceptionMessage = testCascadeDelete(l3network);
1061         assertEquals(expected_message, exceptionMessage);
1062
1063     }
1064
1065 }