808dd631aaded600b0f6ff97eda986e9b8dd9018
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / serialization / db / DbSerializer_needsFakeRulesTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-18 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.junit.Assert.*;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.when;
26
27 import java.io.UnsupportedEncodingException;
28 import java.net.URI;
29 import java.net.URISyntaxException;
30 import java.util.*;
31
32 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
33 import org.apache.tinkerpop.gremlin.structure.*;
34 import org.janusgraph.core.JanusGraphFactory;
35 import org.junit.*;
36 import org.junit.rules.ExpectedException;
37 import org.junit.runner.RunWith;
38 import org.onap.aai.config.ConfigConfiguration;
39 import org.onap.aai.config.IntrospectionConfig;
40 import org.onap.aai.config.SpringContextAware;
41 import org.onap.aai.db.props.AAIProperties;
42 import org.onap.aai.dbmap.DBConnectionType;
43 import org.onap.aai.edges.EdgeIngestor;
44 import org.onap.aai.exceptions.AAIException;
45 import org.onap.aai.introspection.*;
46 import org.onap.aai.nodes.NodeIngestor;
47 import org.onap.aai.parsers.query.QueryParser;
48 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
49 import org.onap.aai.serialization.engines.QueryStyle;
50 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
51 import org.onap.aai.serialization.queryformats.QueryFormatTestHelper;
52 import org.onap.aai.setup.SchemaLocationsBean;
53 import org.onap.aai.setup.SchemaVersion;
54 import org.onap.aai.setup.SchemaVersions;
55 import org.onap.aai.util.AAIConstants;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.test.context.ContextConfiguration;
58 import org.springframework.test.context.TestPropertySource;
59 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
60
61 //@RunWith(value = Parameterized.class) TODO replace this functionality
62 @RunWith(SpringJUnit4ClassRunner.class)
63 @ContextConfiguration(
64         classes = {ConfigConfiguration.class, AAICoreFakeEdgesConfigTranslator.class, NodeIngestor.class,
65                 EdgeIngestor.class, EdgeSerializer.class, SpringContextAware.class, IntrospectionConfig.class})
66 @TestPropertySource(
67         properties = {"schema.translator.list = config", "schema.nodes.location=src/test/resources/onap/oxm",
68                 "schema.edges.location=src/test/resources/onap/dbedgerules"})
69 public class DbSerializer_needsFakeRulesTest {
70
71     // to use, set thrown.expect to whatever your test needs
72     // this line establishes default of expecting no exception to be thrown
73     @Rule
74     public ExpectedException thrown = ExpectedException.none();
75
76     protected static Graph graph;
77
78     @Autowired
79     protected EdgeSerializer edgeSer;
80     @Autowired
81     protected EdgeIngestor ei;
82     @Autowired
83     protected SchemaVersions schemaVersions;
84
85     private SchemaVersion version;
86     private final ModelType introspectorFactoryType = ModelType.MOXY;
87     private final DBConnectionType type = DBConnectionType.REALTIME;
88     private Loader loader;
89     private TransactionalGraphEngine dbEngine;
90     private TransactionalGraphEngine engine; // for tests that aren't mocking the engine
91     private DBSerializer dbser;
92     TransactionalGraphEngine spy;
93     TransactionalGraphEngine.Admin adminSpy;
94
95     // @Parameterized.Parameter(value = 0)
96     public QueryStyle queryStyle = QueryStyle.TRAVERSAL;
97
98     /*
99      * @Parameterized.Parameters(name = "QueryStyle.{0}")
100      * public static Collection<Object[]> data() {
101      * return Arrays.asList(new Object[][]{
102      * {QueryStyle.TRAVERSAL},
103      * {QueryStyle.TRAVERSAL_URI}
104      * });
105      * }
106      */
107
108     @BeforeClass
109     public static void init() throws Exception {
110         graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
111         System.setProperty("AJSC_HOME", ".");
112         System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
113         QueryFormatTestHelper.setFinalStatic(AAIConstants.class.getField("AAI_HOME_ETC_OXM"),
114                 "src/test/resources/bundleconfig-local/etc/oxm/");
115
116     }
117
118     @Before
119     public void setup() throws Exception {
120         // createGraph();
121         version = schemaVersions.getDefaultVersion();
122         loader = SpringContextAware.getBean(LoaderFactory.class).createLoaderForVersion(introspectorFactoryType,
123                 version);
124         dbEngine = new JanusGraphDBEngine(queryStyle, type, loader);
125         spy = spy(dbEngine);
126         adminSpy = spy(dbEngine.asAdmin());
127
128         engine = new JanusGraphDBEngine(queryStyle, type, loader);
129         dbser = new DBSerializer(version, engine, introspectorFactoryType, "AAI-TEST");
130     }
131
132     @After
133     public void tearDown() throws Exception {
134         engine.rollback();
135     }
136
137     @AfterClass
138     public static void destroy() throws Exception {
139         graph.close();
140     }
141
142     public void subnetSetup() throws AAIException {
143         /*
144          * This setus up the test graph, For future junits , add more vertices
145          * and edges
146          */
147
148         Vertex l3interipv4addresslist_1 = graph.traversal().addV("aai-node-type", "l3-interface-ipv4-address-list",
149                 "l3-interface-ipv4-address", "l3-interface-ipv4-address-1").next();
150         Vertex subnet_2 = graph.traversal().addV("aai-node-type", "subnet", "subnet-id", "subnet-id-2").next();
151         Vertex l3interipv6addresslist_3 = graph.traversal().addV("aai-node-type", "l3-interface-ipv6-address-list",
152                 "l3-interface-ipv6-address", "l3-interface-ipv6-address-3").next();
153         Vertex subnet_4 = graph.traversal().addV("aai-node-type", "subnet", "subnet-id", "subnet-id-4").next();
154         Vertex subnet_5 = graph.traversal().addV("aai-node-type", "subnet", "subnet-id", "subnet-id-5").next();
155         Vertex l3network_6 = graph.traversal()
156                 .addV("aai-node-type", "l3-network", "network-id", "network-id-6", "network-name", "network-name-6")
157                 .next();
158
159         GraphTraversalSource g = graph.traversal();
160         edgeSer.addEdge(g, l3interipv4addresslist_1, subnet_2);
161         edgeSer.addEdge(g, l3interipv6addresslist_3, subnet_4);
162         edgeSer.addTreeEdge(g, subnet_5, l3network_6);
163     }
164
165     public String testDelete(Vertex v) throws AAIException {
166
167         GraphTraversalSource traversal = graph.traversal();
168         when(spy.asAdmin()).thenReturn(adminSpy);
169         when(adminSpy.getTraversalSource()).thenReturn(traversal);
170         when(adminSpy.getReadOnlyTraversalSource()).thenReturn(traversal);
171
172         String exceptionMessage = "";
173         DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
174         try {
175             serializer.delete(v, "resourceVersion", false);
176         } catch (AAIException exception) {
177             exceptionMessage = exception.getMessage();
178         }
179         return exceptionMessage;
180
181     }
182
183     @Test
184     public void serializeToDbNewVertexAndEdgeAAIUUIDTest()
185             throws AAIException, UnsupportedEncodingException, NoSuchFieldException, SecurityException,
186             IllegalArgumentException, IllegalAccessException, URISyntaxException {
187         String testName = new Object() {}.getClass().getEnclosingMethod().getName();
188         DBSerializer localDbser = getDBSerializerWithSpecificEdgeRules();
189
190         engine.startTransaction();
191
192         engine.tx().addVertex("aai-node-type", "vnfc", "vnfc-name", "vnfc-" + testName, AAIProperties.AAI_URI,
193                 "/network/vnfcs/vnfc/vnfc-" + testName);
194
195         Introspector relationship = loader.introspectorFromName("relationship");
196         relationship.setValue("related-to", "vnfc");
197         relationship.setValue("related-link", "/network/vnfcs/vnfc/vnfc-" + testName);
198
199         Introspector relationshipList = loader.introspectorFromName("relationship-list");
200         relationshipList.setValue("relationship", Collections.singletonList(relationship.getUnderlyingObject()));
201
202         Introspector gvnfObj = loader.introspectorFromName("generic-vnf");
203         Vertex gvnf = localDbser.createNewVertex(gvnfObj);
204         gvnfObj.setValue("relationship-list", relationshipList.getUnderlyingObject());
205         gvnfObj.setValue("vnf-id", "vnf-" + testName);
206
207         QueryParser uriQuery = dbEngine.getQueryBuilder()
208                 .createQueryFromURI(new URI("/network/generic-vnfs/generic-vnf/vnf-" + testName));
209
210         localDbser.serializeToDb(gvnfObj, gvnf, uriQuery, null, "test");
211         assertTrue("Generic-vnf has uuid ", gvnf.property(AAIProperties.AAI_UUID).isPresent());
212         assertTrue("Edge has uuid ", gvnf.edges(Direction.BOTH).next().property(AAIProperties.AAI_UUID).isPresent());
213
214     }
215
216     @Test
217     public void createEdgeWithValidLabelTest() throws AAIException, UnsupportedEncodingException, NoSuchFieldException,
218             SecurityException, IllegalArgumentException, IllegalAccessException {
219
220         DBSerializer localDbser = getDBSerializerWithSpecificEdgeRules();
221
222         engine.startTransaction();
223
224         Vertex gvnf = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "myvnf", "aai-uri",
225                 "/network/generic-vnfs/generic-vnf/myvnf");
226         Vertex vnfc = engine.tx().addVertex("aai-node-type", "vnfc", "vnfc-name", "a-name", "aai-uri",
227                 "/network/vnfcs/vnfc/a-name");
228
229         // sunny day case
230         Introspector relData = loader.introspectorFromName("relationship-data");
231         relData.setValue("relationship-key", "vnfc.vnfc-name");
232         relData.setValue("relationship-value", "a-name");
233         Introspector relationship = loader.introspectorFromName("relationship");
234         relationship.setValue("related-to", "vnfc");
235         relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
236         relationship.setValue("relationship-data", relData);
237         relationship.setValue("relationship-label", "over-uses");
238
239         assertTrue(localDbser.createEdge(relationship, gvnf));
240         assertTrue(engine.tx().traversal().V(gvnf).both("over-uses").hasNext());
241         assertTrue(engine.tx().traversal().V(vnfc).both("over-uses").hasNext());
242
243     }
244
245     @Test
246     public void createEdgeWithValidLabelWhenSameEdgeExistsTest() throws AAIException, UnsupportedEncodingException,
247             NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
248
249         DBSerializer localDbser = getDBSerializerWithSpecificEdgeRules();
250         engine.startTransaction();
251
252         Vertex gvnf = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "myvnf", "aai-uri",
253                 "/network/generic-vnfs/generic-vnf/myvnf");
254         Vertex vnfc = engine.tx().addVertex("aai-node-type", "vnfc", "vnfc-name", "a-name", "aai-uri",
255                 "/network/vnfcs/vnfc/a-name");
256         edgeSer.addEdge(graph.traversal(), gvnf, vnfc, "re-uses");
257
258         Introspector relData = loader.introspectorFromName("relationship-data");
259         relData.setValue("relationship-key", "vnfc.vnfc-name");
260         relData.setValue("relationship-value", "a-name");
261         Introspector relationship = loader.introspectorFromName("relationship");
262         relationship.setValue("related-to", "vnfc");
263         relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
264         relationship.setValue("relationship-data", relData);
265         relationship.setValue("relationship-label", "re-uses");
266
267         assertTrue(localDbser.createEdge(relationship, gvnf));
268         assertTrue(engine.tx().traversal().V(gvnf).both("re-uses").hasNext());
269         assertTrue(engine.tx().traversal().V(vnfc).both("re-uses").hasNext());
270         assertEquals("Number of edges between vertexes is 1", Long.valueOf(1),
271                 engine.tx().traversal().V(vnfc).both().count().next());
272
273     }
274
275     @Test
276     public void createEdgeWithValidLabelWhenDiffEdgeExistsTest() throws AAIException, UnsupportedEncodingException,
277             NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
278
279         DBSerializer localDbser = getDBSerializerWithSpecificEdgeRules();
280         engine.startTransaction();
281
282         Vertex gvnf = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "myvnf", "aai-uri",
283                 "/network/generic-vnfs/generic-vnf/myvnf");
284         Vertex vnfc = engine.tx().addVertex("aai-node-type", "vnfc", "vnfc-name", "a-name", "aai-uri",
285                 "/network/vnfcs/vnfc/a-name");
286         edgeSer.addEdge(graph.traversal(), gvnf, vnfc, "uses");
287
288         Introspector relData = loader.introspectorFromName("relationship-data");
289         relData.setValue("relationship-key", "vnfc.vnfc-name");
290         relData.setValue("relationship-value", "a-name");
291         Introspector relationship = loader.introspectorFromName("relationship");
292         relationship.setValue("related-to", "vnfc");
293         relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
294         relationship.setValue("relationship-data", relData);
295         relationship.setValue("relationship-label", "uses");
296         localDbser.createEdge(relationship, gvnf);
297
298         relationship.setValue("relationship-label", "re-uses");
299
300         assertTrue(localDbser.createEdge(relationship, gvnf));
301         assertTrue(engine.tx().traversal().V(gvnf).both("re-uses").hasNext());
302         assertTrue(engine.tx().traversal().V(vnfc).both("re-uses").hasNext());
303         assertTrue(engine.tx().traversal().V(gvnf).both("uses").hasNext());
304         assertTrue(engine.tx().traversal().V(vnfc).both("uses").hasNext());
305         assertEquals("Number of edges between vertexes is 2", Long.valueOf(2),
306                 engine.tx().traversal().V(vnfc).both().count().next());
307         assertEquals("Number of edges between vertexes is 2", Long.valueOf(2),
308                 engine.tx().traversal().V(gvnf).both().count().next());
309
310     }
311
312     @Test
313     public void createEdgeWithNoLabelTest() throws AAIException, UnsupportedEncodingException, NoSuchFieldException,
314             SecurityException, IllegalArgumentException, IllegalAccessException {
315
316         DBSerializer localDbser = getDBSerializerWithSpecificEdgeRules();
317         engine.startTransaction();
318
319         Vertex gvnf = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "myvnf", "aai-uri",
320                 "/network/generic-vnfs/generic-vnf/myvnf");
321         Vertex vnfc = engine.tx().addVertex("aai-node-type", "vnfc", "vnfc-name", "a-name", "aai-uri",
322                 "/network/vnfcs/vnfc/a-name");
323
324         Introspector relData = loader.introspectorFromName("relationship-data");
325         relData.setValue("relationship-key", "vnfc.vnfc-name");
326         relData.setValue("relationship-value", "a-name");
327         Introspector relationship = loader.introspectorFromName("relationship");
328         relationship.setValue("related-to", "vnfc");
329         relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
330         relationship.setValue("relationship-data", relData);
331         localDbser.createEdge(relationship, gvnf);
332
333         assertTrue(localDbser.createEdge(relationship, gvnf));
334         assertTrue(engine.tx().traversal().V(gvnf).both("uses").hasNext());
335         assertTrue(engine.tx().traversal().V(vnfc).both("uses").hasNext());
336         assertEquals("Number of edges between vertexes is 1", Long.valueOf(1),
337                 engine.tx().traversal().V(vnfc).both().count().next());
338         assertEquals("Number of edges between vertexes is 1", Long.valueOf(1),
339                 engine.tx().traversal().V(gvnf).both().count().next());
340
341     }
342
343     @Test
344     public void deleteEdgeWithNoLabelWhenMultipleExistsTest() throws AAIException, UnsupportedEncodingException,
345             NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
346
347         DBSerializer localDbser = getDBSerializerWithSpecificEdgeRules();
348         engine.startTransaction();
349
350         Vertex gvnf = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "myvnf", "aai-uri",
351                 "/network/generic-vnfs/generic-vnf/myvnf");
352         Vertex vnfc = engine.tx().addVertex("aai-node-type", "vnfc", "vnfc-name", "a-name", "aai-uri",
353                 "/network/vnfcs/vnfc/a-name");
354         edgeSer.addEdge(graph.traversal(), gvnf, vnfc, "uses");
355         edgeSer.addEdge(graph.traversal(), gvnf, vnfc, "re-uses");
356         edgeSer.addEdge(graph.traversal(), gvnf, vnfc, "over-uses");
357
358         Introspector relData = loader.introspectorFromName("relationship-data");
359         relData.setValue("relationship-key", "vnfc.vnfc-name");
360         relData.setValue("relationship-value", "a-name");
361         Introspector relationship = loader.introspectorFromName("relationship");
362         relationship.setValue("related-to", "vnfc");
363         relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
364         relationship.setValue("relationship-data", relData);
365
366         assertTrue(localDbser.deleteEdge(relationship, gvnf));
367         assertFalse("generic-vnf has no edge uses", engine.tx().traversal().V(gvnf).both("uses").hasNext());
368         assertFalse("vnfc has no edge uses", engine.tx().traversal().V(vnfc).both("uses").hasNext());
369         assertTrue("generic-vnf has edge re-uses", engine.tx().traversal().V(gvnf).both("re-uses").hasNext());
370         assertTrue("vnfc has edge re-uses", engine.tx().traversal().V(vnfc).both("re-uses").hasNext());
371         assertTrue("generic-vnf has edge re-uses", engine.tx().traversal().V(gvnf).both("over-uses").hasNext());
372         assertTrue("vnfc has edge re-uses", engine.tx().traversal().V(vnfc).both("over-uses").hasNext());
373         assertEquals("Number of edges between vertexes is 2", Long.valueOf(2),
374                 engine.tx().traversal().V(vnfc).both().count().next());
375         assertEquals("Number of edges between vertexes is 2", Long.valueOf(2),
376                 engine.tx().traversal().V(gvnf).both().count().next());
377
378     }
379
380     @Test
381     public void deleteEdgeWithValidLabelWhenMultipleExistsTest() throws AAIException, UnsupportedEncodingException,
382             NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
383
384         DBSerializer localDbser = getDBSerializerWithSpecificEdgeRules();
385
386         engine.startTransaction();
387
388         Vertex gvnf = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "myvnf", "aai-uri",
389                 "/network/generic-vnfs/generic-vnf/myvnf");
390         Vertex vnfc = engine.tx().addVertex("aai-node-type", "vnfc", "vnfc-name", "a-name", "aai-uri",
391                 "/network/vnfcs/vnfc/a-name");
392         edgeSer.addEdge(graph.traversal(), gvnf, vnfc, "uses");
393         edgeSer.addEdge(graph.traversal(), gvnf, vnfc, "re-uses");
394         edgeSer.addEdge(graph.traversal(), gvnf, vnfc, "over-uses");
395
396         Introspector relData = loader.introspectorFromName("relationship-data");
397         relData.setValue("relationship-key", "vnfc.vnfc-name");
398         relData.setValue("relationship-value", "a-name");
399         Introspector relationship = loader.introspectorFromName("relationship");
400         relationship.setValue("related-to", "vnfc");
401         relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
402         relationship.setValue("relationship-data", relData);
403         relationship.setValue("relationship-label", "re-uses");
404
405         assertTrue(localDbser.deleteEdge(relationship, gvnf));
406         assertTrue("generic-vnf has edge uses", engine.tx().traversal().V(gvnf).both("uses").hasNext());
407         assertTrue("vnfc has edge uses", engine.tx().traversal().V(vnfc).both("uses").hasNext());
408         assertFalse("generic-vnf has no edge re-uses", engine.tx().traversal().V(gvnf).both("re-uses").hasNext());
409         assertFalse("vnfc has no edge re-uses", engine.tx().traversal().V(vnfc).both("re-uses").hasNext());
410         assertTrue("generic-vnf has edge re-uses", engine.tx().traversal().V(gvnf).both("over-uses").hasNext());
411         assertTrue("vnfc has edge re-uses", engine.tx().traversal().V(vnfc).both("over-uses").hasNext());
412         assertEquals("Number of edges between vertexes is 2", Long.valueOf(2),
413                 engine.tx().traversal().V(vnfc).both().count().next());
414         assertEquals("Number of edges between vertexes is 2", Long.valueOf(2),
415                 engine.tx().traversal().V(gvnf).both().count().next());
416
417     }
418
419     @Test
420     public void deleteEdgeWithValidInvalidLabelWhenMultipleExistsTest()
421             throws AAIException, UnsupportedEncodingException, NoSuchFieldException, SecurityException,
422             IllegalArgumentException, IllegalAccessException {
423
424         DBSerializer localDbser = getDBSerializerWithSpecificEdgeRules();
425
426         engine.startTransaction();
427
428         Vertex gvnf = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "myvnf", "aai-uri",
429                 "/network/generic-vnfs/generic-vnf/myvnf");
430         Vertex vnfc = engine.tx().addVertex("aai-node-type", "vnfc", "vnfc-name", "a-name", "aai-uri",
431                 "/network/vnfcs/vnfc/a-name");
432         edgeSer.addEdge(graph.traversal(), gvnf, vnfc, "uses");
433         edgeSer.addEdge(graph.traversal(), gvnf, vnfc, "re-uses");
434         edgeSer.addEdge(graph.traversal(), gvnf, vnfc, "over-uses");
435
436         Introspector relData = loader.introspectorFromName("relationship-data");
437         relData.setValue("relationship-key", "vnfc.vnfc-name");
438         relData.setValue("relationship-value", "a-name");
439         Introspector relationship = loader.introspectorFromName("relationship");
440         relationship.setValue("related-to", "vnfc");
441         relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
442         relationship.setValue("relationship-data", relData);
443         relationship.setValue("relationship-label", "NA");
444
445         thrown.expect(AAIException.class);
446         thrown.expectMessage("No rule found");
447         thrown.expectMessage("node type: generic-vnf, node type: vnfc, label: NA, type: COUSIN");
448         localDbser.deleteEdge(relationship, gvnf);
449     }
450
451     @Test
452     public void serializeToDbWithLabelTest() throws AAIException, UnsupportedEncodingException, NoSuchFieldException,
453             SecurityException, IllegalArgumentException, IllegalAccessException, URISyntaxException {
454
455         DBSerializer localDbser = getDBSerializerWithSpecificEdgeRules();
456
457         engine.startTransaction();
458
459         engine.tx().addVertex("aai-node-type", "vnfc", "vnfc-name", "a-name", "aai-uri", "/network/vnfcs/vnfc/a-name");
460
461         Introspector relationship = loader.introspectorFromName("relationship");
462         relationship.setValue("related-to", "vnfc");
463         relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
464         relationship.setValue("relationship-label", "re-uses");
465         Introspector relationshipList = loader.introspectorFromName("relationship-list");
466         relationshipList.setValue("relationship", Collections.singletonList(relationship.getUnderlyingObject()));
467
468         Introspector gvnfObj = loader.introspectorFromName("generic-vnf");
469         Vertex gvnf = localDbser.createNewVertex(gvnfObj);
470         gvnfObj.setValue("relationship-list", relationshipList.getUnderlyingObject());
471         gvnfObj.setValue("vnf-id", "myvnf");
472
473         QueryParser uriQuery =
474                 dbEngine.getQueryBuilder().createQueryFromURI(new URI("/network/generic-vnfs/generic-vnf/myvnf"));
475
476         localDbser.serializeToDb(gvnfObj, gvnf, uriQuery, null, "test");
477
478         assertTrue("vertex with vnf-id myvnf exists", engine.tx().traversal().V().has("vnf-id", "myvnf").hasNext());
479         assertTrue("vertex with vnfc-name a-name exists",
480                 engine.tx().traversal().V().has("vnfc-name", "a-name").hasNext());
481         assertFalse("generic-vnf has no edge re-uses",
482                 engine.tx().traversal().V().has("vnf-id", "myvnf").both("uses").hasNext());
483         assertFalse("vnfc has no edge re-uses",
484                 engine.tx().traversal().V().has("vnfc-name", "a-name").both("uses").hasNext());
485         assertTrue("generic-vnf has edge re-uses",
486                 engine.tx().traversal().V().has("vnf-id", "myvnf").both("re-uses").hasNext());
487         assertTrue("vnfc has edge re-uses",
488                 engine.tx().traversal().V().has("vnfc-name", "a-name").both("re-uses").hasNext());
489         assertFalse("generic-vnf has no edge re-uses",
490                 engine.tx().traversal().V().has("vnf-id", "myvnf").both("over-uses").hasNext());
491         assertFalse("vnfc has no edge re-uses",
492                 engine.tx().traversal().V().has("vnfc-name", "a-name").both("over-uses").hasNext());
493         assertEquals("Number of edges between vertexes is 1", Long.valueOf(1),
494                 engine.tx().traversal().V().has("vnfc-name", "a-name").both().count().next());
495         assertEquals("Number of edges between vertexes is 1", Long.valueOf(1),
496                 engine.tx().traversal().V().has("vnf-id", "myvnf").both().count().next());
497
498     }
499
500     @Test
501     public void serializeToDbWithoutLabelTest() throws AAIException, UnsupportedEncodingException, NoSuchFieldException,
502             SecurityException, IllegalArgumentException, IllegalAccessException, URISyntaxException {
503
504         DBSerializer localDbser = getDBSerializerWithSpecificEdgeRules();
505
506         engine.startTransaction();
507
508         engine.tx().addVertex("aai-node-type", "vnfc", "vnfc-name", "a-name", "aai-uri", "/network/vnfcs/vnfc/a-name");
509
510         Introspector relationship = loader.introspectorFromName("relationship");
511         relationship.setValue("related-to", "vnfc");
512         relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
513
514         Introspector relationshipList = loader.introspectorFromName("relationship-list");
515         relationshipList.setValue("relationship", Collections.singletonList(relationship.getUnderlyingObject()));
516
517         Introspector gvnfObj = loader.introspectorFromName("generic-vnf");
518         Vertex gvnf = localDbser.createNewVertex(gvnfObj);
519         gvnfObj.setValue("relationship-list", relationshipList.getUnderlyingObject());
520         gvnfObj.setValue("vnf-id", "myvnf");
521
522         QueryParser uriQuery =
523                 dbEngine.getQueryBuilder().createQueryFromURI(new URI("/network/generic-vnfs/generic-vnf/myvnf"));
524
525         localDbser.serializeToDb(gvnfObj, gvnf, uriQuery, null, "test");
526
527         assertTrue("vertex with vnf-id myvnf exists", engine.tx().traversal().V().has("vnf-id", "myvnf").hasNext());
528         assertTrue("vertex with vnfc-name a-name exists",
529                 engine.tx().traversal().V().has("vnfc-name", "a-name").hasNext());
530         assertTrue("generic-vnf has edge uses",
531                 engine.tx().traversal().V().has("vnf-id", "myvnf").both("uses").hasNext());
532         assertTrue("vnfc has edge uses", engine.tx().traversal().V().has("vnfc-name", "a-name").both("uses").hasNext());
533         assertFalse("generic-vnf has no edge re-uses",
534                 engine.tx().traversal().V().has("vnf-id", "myvnf").both("re-uses").hasNext());
535         assertFalse("vnfc has no edge re-uses",
536                 engine.tx().traversal().V().has("vnfc-name", "a-name").both("re-uses").hasNext());
537         assertFalse("generic-vnf has no edge over-uses",
538                 engine.tx().traversal().V().has("vnf-id", "myvnf").both("over-uses").hasNext());
539         assertFalse("vnfc has no edge over-uses",
540                 engine.tx().traversal().V().has("vnfc-name", "a-name").both("over-uses").hasNext());
541         assertEquals("Number of edges between vertexes is 1", Long.valueOf(1),
542                 engine.tx().traversal().V().has("vnfc-name", "a-name").both().count().next());
543         assertEquals("Number of edges between vertexes is 1", Long.valueOf(1),
544                 engine.tx().traversal().V().has("vnf-id", "myvnf").both().count().next());
545
546     }
547
548     @Test
549     public void serializeToDbWithInvalidLabelTest()
550             throws AAIException, UnsupportedEncodingException, NoSuchFieldException, SecurityException,
551             IllegalArgumentException, IllegalAccessException, URISyntaxException {
552
553         DBSerializer localDbser = getDBSerializerWithSpecificEdgeRules();
554
555         engine.startTransaction();
556
557         engine.tx().addVertex("aai-node-type", "vnfc", "vnfc-name", "a-name", "aai-uri", "/network/vnfcs/vnfc/a-name");
558
559         Introspector relationship = loader.introspectorFromName("relationship");
560         relationship.setValue("related-to", "vnfc");
561         relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
562         relationship.setValue("relationship-label", "NA");
563         Introspector relationshipList = loader.introspectorFromName("relationship-list");
564         relationshipList.setValue("relationship", Collections.singletonList(relationship.getUnderlyingObject()));
565
566         Introspector gvnfObj = loader.introspectorFromName("generic-vnf");
567         Vertex gvnf = localDbser.createNewVertex(gvnfObj);
568         gvnfObj.setValue("relationship-list", relationshipList.getUnderlyingObject());
569         gvnfObj.setValue("vnf-id", "myvnf");
570
571         QueryParser uriQuery =
572                 dbEngine.getQueryBuilder().createQueryFromURI(new URI("/network/generic-vnfs/generic-vnf/myvnf"));
573
574         thrown.expect(AAIException.class);
575         thrown.expectMessage("No EdgeRule found for passed nodeTypes: generic-vnf, vnfc with label NA.");
576         localDbser.serializeToDb(gvnfObj, gvnf, uriQuery, null, "test");
577
578     }
579
580     @Test
581     public void serializeToDbWithLabelAndEdgeExistsTest()
582             throws AAIException, UnsupportedEncodingException, NoSuchFieldException, SecurityException,
583             IllegalArgumentException, IllegalAccessException, URISyntaxException {
584
585         DBSerializer localDbser = getDBSerializerWithSpecificEdgeRules();
586
587         engine.startTransaction();
588         engine.tx().addVertex("aai-node-type", "vnfc", "vnfc-name", "a-name", "aai-uri", "/network/vnfcs/vnfc/a-name");
589
590         Introspector relationship;
591         Introspector relationshipList;
592         List<Object> relList = new ArrayList<>();
593
594         // create generic-vnf
595         Introspector gvnfObj = loader.introspectorFromName("generic-vnf");
596         Vertex gvnf = localDbser.createNewVertex(gvnfObj);
597         gvnfObj.setValue("vnf-id", "myvnf");
598         QueryParser uriQuery =
599                 dbEngine.getQueryBuilder().createQueryFromURI(new URI("/network/generic-vnfs/generic-vnf/myvnf"));
600
601         // create relationship to vnfc
602         relationship = loader.introspectorFromName("relationship");
603         relationship.setValue("related-to", "vnfc");
604         relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
605         relList.add(relationship.getUnderlyingObject());
606         relationshipList = loader.introspectorFromName("relationship-list");
607         relationshipList.setValue("relationship", relList);
608         gvnfObj.setValue("relationship-list", relationshipList.getUnderlyingObject());
609
610         // add gvnf to graph
611         localDbser.serializeToDb(gvnfObj, gvnf, uriQuery, null, "test");
612
613         // add second relationship
614         relationship = loader.introspectorFromName("relationship");
615         relationship.setValue("related-to", "vnfc");
616         relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
617         relationship.setValue("relationship-label", "re-uses");
618         relList.add(relationship.getUnderlyingObject());
619         relationshipList = loader.introspectorFromName("relationship-list");
620         relationshipList.setValue("relationship", relList);
621         gvnfObj.setValue("relationship-list", relationshipList.getUnderlyingObject());
622
623         localDbser.serializeToDb(gvnfObj, gvnf, uriQuery, null, "test");
624
625         assertTrue("vertex with vnf-id myvnf exists", engine.tx().traversal().V().has("vnf-id", "myvnf").hasNext());
626         assertTrue("vertex with vnfc-name a-name exists",
627                 engine.tx().traversal().V().has("vnfc-name", "a-name").hasNext());
628         assertTrue("generic-vnf has  edge uses",
629                 engine.tx().traversal().V().has("vnf-id", "myvnf").both("uses").hasNext());
630         assertTrue("vnfc has  edge uses",
631                 engine.tx().traversal().V().has("vnfc-name", "a-name").both("uses").hasNext());
632         assertTrue("generic-vnf has edge re-uses",
633                 engine.tx().traversal().V().has("vnf-id", "myvnf").both("re-uses").hasNext());
634         assertTrue("vnfc has edge re-uses",
635                 engine.tx().traversal().V().has("vnfc-name", "a-name").both("re-uses").hasNext());
636         assertFalse("generic-vnf has no edge over-uses",
637                 engine.tx().traversal().V().has("vnf-id", "myvnf").both("over-uses").hasNext());
638         assertFalse("vnfc has no edge over-uses",
639                 engine.tx().traversal().V().has("vnfc-name", "a-name").both("over-uses").hasNext());
640         assertEquals("Number of edges between vertexes is 2", Long.valueOf(2),
641                 engine.tx().traversal().V().has("vnfc-name", "a-name").both().count().next());
642         assertEquals("Number of edges between vertexes is 2", Long.valueOf(2),
643                 engine.tx().traversal().V().has("vnf-id", "myvnf").both().count().next());
644
645     }
646
647     @Test
648     public void serializeToDbWithLabelDroppingRelationshipTest()
649             throws AAIException, UnsupportedEncodingException, NoSuchFieldException, SecurityException,
650             IllegalArgumentException, IllegalAccessException, URISyntaxException {
651
652         DBSerializer localDbser = getDBSerializerWithSpecificEdgeRules();
653
654         engine.startTransaction();
655         engine.tx().addVertex("aai-node-type", "vnfc", "vnfc-name", "a-name", "aai-uri", "/network/vnfcs/vnfc/a-name");
656
657         Introspector relationship;
658         Introspector relationshipList;
659         List<Object> relList = new ArrayList<>();
660
661         // create generic-vnf
662         Introspector gvnfObj = loader.introspectorFromName("generic-vnf");
663         Vertex gvnf = localDbser.createNewVertex(gvnfObj);
664         gvnfObj.setValue("vnf-id", "myvnf");
665         QueryParser uriQuery =
666                 dbEngine.getQueryBuilder().createQueryFromURI(new URI("/network/generic-vnfs/generic-vnf/myvnf"));
667
668         // create relationship to vnfc
669         relationship = loader.introspectorFromName("relationship");
670         relationship.setValue("related-to", "vnfc");
671         relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
672         relList.add(relationship.getUnderlyingObject());
673         // add second relationship
674         relationship = loader.introspectorFromName("relationship");
675         relationship.setValue("related-to", "vnfc");
676         relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
677         relationship.setValue("relationship-label", "re-uses");
678         relList.add(relationship.getUnderlyingObject());
679         relationshipList = loader.introspectorFromName("relationship-list");
680         relationshipList.setValue("relationship", relList);
681         gvnfObj.setValue("relationship-list", relationshipList.getUnderlyingObject());
682
683         // add gvnf to graph
684         localDbser.serializeToDb(gvnfObj, gvnf, uriQuery, null, "test");
685
686         // drop second relationship
687         relList.remove(1);
688         relationshipList = loader.introspectorFromName("relationship-list");
689         relationshipList.setValue("relationship", relList);
690         gvnfObj.setValue("relationship-list", relationshipList.getUnderlyingObject());
691
692         localDbser.serializeToDb(gvnfObj, gvnf, uriQuery, null, "test");
693
694         assertTrue("vertex with vnf-id myvnf exists", engine.tx().traversal().V().has("vnf-id", "myvnf").hasNext());
695         assertTrue("vertex with vnfc-name a-name exists",
696                 engine.tx().traversal().V().has("vnfc-name", "a-name").hasNext());
697         assertTrue("generic-vnf has  edge uses",
698                 engine.tx().traversal().V().has("vnf-id", "myvnf").both("uses").hasNext());
699         assertTrue("vnfc has  edge uses",
700                 engine.tx().traversal().V().has("vnfc-name", "a-name").both("uses").hasNext());
701         assertFalse("generic-vnf no longer has edge re-uses",
702                 engine.tx().traversal().V().has("vnf-id", "myvnf").both("re-uses").hasNext());
703         assertFalse("vnfc no longer has edge re-uses",
704                 engine.tx().traversal().V().has("vnfc-name", "a-name").both("re-uses").hasNext());
705         assertFalse("generic-vnf has no edge over-uses",
706                 engine.tx().traversal().V().has("vnf-id", "myvnf").both("over-uses").hasNext());
707         assertFalse("vnfc has no edge over-uses",
708                 engine.tx().traversal().V().has("vnfc-name", "a-name").both("over-uses").hasNext());
709         assertEquals("Number of edges between vertexes is 1", Long.valueOf(1),
710                 engine.tx().traversal().V().has("vnfc-name", "a-name").both().count().next());
711         assertEquals("Number of edges between vertexes is 1", Long.valueOf(1),
712                 engine.tx().traversal().V().has("vnf-id", "myvnf").both().count().next());
713
714     }
715
716     private DBSerializer getDBSerializerWithSpecificEdgeRules()
717             throws NoSuchFieldException, AAIException, IllegalAccessException {
718
719         DBSerializer localDbser = new DBSerializer(version, engine, introspectorFactoryType, "AAI-TEST");
720         return localDbser;
721     }
722 }