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