2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.aai.serialization.db;
23 import com.thinkaurelius.titan.core.TitanFactory;
24 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
25 import org.apache.tinkerpop.gremlin.structure.Graph;
26 import org.apache.tinkerpop.gremlin.structure.Vertex;
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.openecomp.aai.AAISetup;
31 import org.openecomp.aai.dbmap.DBConnectionType;
32 import org.openecomp.aai.exceptions.AAIException;
33 import org.openecomp.aai.introspection.Loader;
34 import org.openecomp.aai.introspection.LoaderFactory;
35 import org.openecomp.aai.introspection.ModelType;
36 import org.openecomp.aai.introspection.Version;
37 import org.openecomp.aai.serialization.engines.QueryStyle;
38 import org.openecomp.aai.serialization.engines.TitanDBEngine;
39 import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
41 import static org.junit.Assert.assertEquals;
42 import static org.mockito.Mockito.spy;
43 import static org.mockito.Mockito.when;
45 public class DbSerializerTest extends AAISetup {
47 protected Graph graph;
48 protected final EdgeRules rules = EdgeRules.getInstance();
50 private final Version version = Version.getLatest();
51 private final ModelType introspectorFactoryType = ModelType.MOXY;
52 private final QueryStyle queryStyle = QueryStyle.TRAVERSAL;
53 private final DBConnectionType type = DBConnectionType.REALTIME;
54 private Loader loader;
55 private TransactionalGraphEngine dbEngine;
56 TransactionalGraphEngine spy;
57 TransactionalGraphEngine.Admin adminSpy;
60 public void setup() throws Exception {
61 graph = TitanFactory.build().set("storage.backend", "inmemory").open();
62 loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version);
63 dbEngine = new TitanDBEngine(queryStyle, type, loader);
65 adminSpy = spy(dbEngine.asAdmin());
70 public void createGraph() throws AAIException {
72 * This setus up the test graph, For future junits , add more vertices
76 Vertex l3interipv4addresslist_1 = graph.traversal().addV("aai-node-type", "l3-interface-ipv4-address-list",
77 "l3-interface-ipv4-address", "l3-interface-ipv4-address-1").next();
78 Vertex subnet_2 = graph.traversal().addV("aai-node-type", "subnet", "subnet-id", "subnet-id-2").next();
79 Vertex l3interipv6addresslist_3 = graph.traversal().addV("aai-node-type", "l3-interface-ipv6-address-list",
80 "l3-interface-ipv6-address", "l3-interface-ipv6-address-3").next();
81 Vertex subnet_4 = graph.traversal().addV("aai-node-type", "subnet", "subnet-id", "subnet-id-4").next();
82 Vertex subnet_5 = graph.traversal().addV("aai-node-type", "subnet", "subnet-id", "subnet-id-5").next();
83 Vertex l3network_6 = graph.traversal()
84 .addV("aai-node-type", "l3-network", "network-id", "network-id-6", "network-name", "network-name-6")
87 GraphTraversalSource g = graph.traversal();
88 rules.addEdge(g, l3interipv4addresslist_1, subnet_2);
89 rules.addEdge(g, l3interipv6addresslist_3, subnet_4);
90 rules.addTreeEdge(g, subnet_5, l3network_6);
95 public void tearDown() throws Exception {
100 public void subnetDelwithInEdgesIpv4Test() throws AAIException {
101 String expected_message = "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]";
104 * This subnet has in-edges with l3-ipv4 and NOT ok to delete
106 Vertex subnet = graph.traversal().V().has("aai-node-type", "subnet").has("subnet-id", "subnet-id-2").next();
108 String exceptionMessage = testDelete(subnet);
109 assertEquals(expected_message, exceptionMessage);
114 public void subnetDelwithInEdgesIpv6Test() throws AAIException {
115 String expected_message = "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]";
118 * This subnet has in-edges with l3-ipv6 and NOT ok to delete
120 Vertex subnet = graph.traversal().V().has("aai-node-type", "subnet").has("subnet-id", "subnet-id-4").next();
121 String exceptionMessage = testDelete(subnet);
122 assertEquals(expected_message, exceptionMessage);
127 public void subnetDelwithInEdgesL3network() throws AAIException {
128 String expected_message = "";
131 * This subnet has in-edges with l3-network and ok to delete
133 Vertex subnet = graph.traversal().V().has("aai-node-type", "subnet").has("subnet-id", "subnet-id-5").next();
135 String exceptionMessage = testDelete(subnet);
136 assertEquals(expected_message, exceptionMessage);
140 public String testDelete(Vertex v) throws AAIException {
142 // Graph g_tx = graph.newTransaction();
143 GraphTraversalSource traversal = graph.traversal();
144 when(spy.asAdmin()).thenReturn(adminSpy);
145 when(adminSpy.getTraversalSource()).thenReturn(traversal);
146 when(adminSpy.getReadOnlyTraversalSource()).thenReturn(traversal);
148 String exceptionMessage = "";
149 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
151 serializer.delete(v, "resourceVersion", false);
152 } catch (AAIException exception) {
153 exceptionMessage = exception.getMessage();
156 return exceptionMessage;