EdgeRules throws descriptive error on invalid rule
[aai/aai-common.git] / aai-core / src / test / java / org / openecomp / aai / serialization / db / DbSerializerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
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
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.openecomp.aai.serialization.db;
22
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;
40
41 import static org.junit.Assert.assertEquals;
42 import static org.mockito.Mockito.spy;
43 import static org.mockito.Mockito.when;
44
45 public class DbSerializerTest extends AAISetup {
46
47         protected Graph graph;
48         protected final EdgeRules rules = EdgeRules.getInstance();
49
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;
58
59         @Before
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);
64                 spy = spy(dbEngine);
65                 adminSpy = spy(dbEngine.asAdmin());
66
67                 createGraph();
68         }
69
70         public void createGraph() throws AAIException {
71                 /*
72                  * This setus up the test graph, For future junits , add more vertices
73                  * and edges
74                  */
75
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")
85                                 .next();
86
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);
91
92         }
93
94         @After
95         public void tearDown() throws Exception {
96                 graph.close();
97         }
98
99         @Test
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]";
102
103                 /*
104                  * This subnet has in-edges with l3-ipv4 and NOT ok to delete
105                  */
106                 Vertex subnet = graph.traversal().V().has("aai-node-type", "subnet").has("subnet-id", "subnet-id-2").next();
107
108                 String exceptionMessage = testDelete(subnet);
109                 assertEquals(expected_message, exceptionMessage);
110
111         }
112
113         @Test
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]";
116
117                 /*
118                  * This subnet has in-edges with l3-ipv6 and NOT ok to delete
119                  */
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);
123
124         }
125
126         @Test
127         public void subnetDelwithInEdgesL3network() throws AAIException {
128                 String expected_message = "";
129
130                 /*
131                  * This subnet has in-edges with l3-network and ok to delete
132                  */
133                 Vertex subnet = graph.traversal().V().has("aai-node-type", "subnet").has("subnet-id", "subnet-id-5").next();
134
135                 String exceptionMessage = testDelete(subnet);
136                 assertEquals(expected_message, exceptionMessage);
137
138         }
139
140         public String testDelete(Vertex v) throws AAIException {
141
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);
147
148                 String exceptionMessage = "";
149                 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
150                 try {
151                         serializer.delete(v, "resourceVersion", false);
152                 } catch (AAIException exception) {
153                         exceptionMessage = exception.getMessage();
154
155                 }
156                 return exceptionMessage;
157
158         }
159
160 }