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