2 * ============LICENSE_START=======================================================
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
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=========================================================
20 package org.onap.aai.migration.v12;
22 import static org.junit.Assert.assertArrayEquals;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.when;
28 import java.util.Optional;
30 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
31 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.onap.aai.AAISetup;
37 import org.onap.aai.dbmap.DBConnectionType;
38 import org.onap.aai.introspection.Loader;
39 import org.onap.aai.introspection.LoaderFactory;
40 import org.onap.aai.introspection.ModelType;
41 import org.onap.aai.introspection.Version;
42 import org.onap.aai.serialization.db.EdgeRules;
43 import org.onap.aai.serialization.engines.QueryStyle;
44 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
45 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
47 import org.janusgraph.core.JanusGraphFactory;
48 import org.janusgraph.core.JanusGraph;
49 import org.janusgraph.core.JanusGraphTransaction;
51 public class MigrateServiceInstanceToConfigurationTestPreMigrationMock extends AAISetup {
53 private final static Version version = Version.v12;
54 private final static ModelType introspectorFactoryType = ModelType.MOXY;
55 private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL;
56 private final static DBConnectionType type = DBConnectionType.REALTIME;
58 private static Loader loader;
59 private static TransactionalGraphEngine dbEngine;
60 private static JanusGraph graph;
61 private static MigrateServiceInstanceToConfiguration migration;
62 private static JanusGraphTransaction tx;
63 private static GraphTraversalSource g;
64 private static EdgeRules rules;
67 public static void setUp() throws Exception {
68 graph = JanusGraphFactory.build().set("storage.backend","inmemory").open();
69 tx = graph.newTransaction();
71 loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version);
72 dbEngine = new JanusGraphDBEngine(
76 rules = EdgeRules.getInstance();
78 Vertex customer = g.addV()
79 .property("aai-node-type", "customer")
80 .property("global-customer-id", "customer-9972-BandwidthMigration")
81 .property("subscriber-type", "CUST")
84 Vertex servSubSDNEI = g.addV()
85 .property("aai-node-type", "service-subscription")
86 .property("service-type", "SDN-ETHERNET-INTERNET")
89 Vertex servInstance22 = g.addV()
90 .property("aai-node-type", "service-instance")
91 .property("service-instance-id", "servInstance-9972-22-BandwidthMigration")
92 .property("operational-status", "activated")
93 .property("bandwidth-total", "bandwidth-total-22-BandwidthMigration")
96 Vertex servInstance11 = g.addV()
97 .property("aai-node-type", "service-instance")
98 .property("service-instance-id", "servInstance-9972-11-BandwidthMigration")
99 .property("operational-status", "activated")
100 .property("bandwidth-total", "bandwidth-total-11-BandwidthMigration")
103 Vertex servSubDHV = g.addV()
104 .property("aai-node-type", "service-subscription")
105 .property("service-type", "DHV")
108 Vertex servInstance4 = g.addV()
109 .property("aai-node-type", "service-instance")
110 .property("service-instance-id", "servInstance-9972-4-BandwidthMigration")
111 .property("operational-status", "activated")
112 .property("bandwidth-total", "bandwidth-total-4-BandwidthMigration")
115 Vertex servInstance1 = g.addV()
116 .property("aai-node-type", "service-instance")
117 .property("service-instance-id", "ServInstance-9972-1-BandwidthMigration")
118 .property("operational-status", "activated")
119 .property("bandwidth-total", "2380")
122 Vertex servInstance3 = g.addV()
123 .property("aai-node-type", "service-instance")
124 .property("service-instance-id", "servInstance-9972-3-BandwidthMigration")
125 .property("operational-status", "activated")
126 .property("bandwidth-total", "bandwidth-total-3-BandwidthMigration")
129 Vertex servInstance2 = g.addV()
130 .property("aai-node-type", "service-instance")
131 .property("service-instance-id", "servInstance-9972-2-BandwidthMigration")
132 .property("operational-status", "activated")
133 .property("bandwidth-total", "bandwidth-total-2-BandwidthMigration")
136 Vertex config1 = g.addV()
137 .property("aai-node-type", "configuration")
138 .property("configuration-id", "9972-config-LB1113")
139 .property("configuration-type", "DHV")
140 .property("tunnel-bandwidth", "12")
143 Vertex config2 = g.addV()
144 .property("aai-node-type", "configuration")
145 .property("configuration-id", "9972-1config-LB1113")
146 .property("configuration-type", "configuration-type1-9972")
149 Vertex allottedResource = g.addV()
150 .property("aai-node-type", "allotted-resource")
151 .property("id", "allResource-9972-BandwidthMigration")
154 rules.addTreeEdge(g, customer, servSubSDNEI);
155 rules.addTreeEdge(g, customer, servSubDHV);
156 rules.addTreeEdge(g, servSubSDNEI, servInstance22);
157 rules.addTreeEdge(g, servSubSDNEI, servInstance11);
158 rules.addTreeEdge(g, servSubDHV, servInstance4);
159 rules.addTreeEdge(g, servSubDHV, servInstance1);
160 rules.addTreeEdge(g, servSubDHV, servInstance3);
161 rules.addTreeEdge(g, servSubDHV, servInstance2);
162 rules.addEdge(g, servInstance1, allottedResource);
163 rules.addEdge(g, servInstance1, config1);
164 rules.addEdge(g, servInstance2, config2);
166 TransactionalGraphEngine spy = spy(dbEngine);
167 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
169 GraphTraversalSource traversal = g;
170 GraphTraversalSource readOnly = tx.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance()));
171 when (spy.tx()).thenReturn(tx);
172 when(spy.asAdmin()).thenReturn(adminSpy);
173 when(adminSpy.getTraversalSource()).thenReturn(traversal);
174 when(adminSpy.getReadOnlyTraversalSource()).thenReturn(readOnly);
176 migration = new MigrateServiceInstanceToConfiguration(spy);
181 public static void cleanUp() {
187 public void testRun() throws Exception {
188 // check if graph nodes exist
189 assertEquals("customer node exists", true,
190 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
193 assertEquals("service subscription node, service-type=SDN-ETHERNET-INTERNET", true,
194 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
195 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "SDN-ETHERNET-INTERNET")
198 assertEquals("service instance node, bandwidth-total=bandwidth-total-22-BandwidthMigration", true,
199 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
200 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "SDN-ETHERNET-INTERNET")
201 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-22-BandwidthMigration")
202 .has("bandwidth-total", "bandwidth-total-22-BandwidthMigration")
205 assertEquals("service instance node, bandwidth-total=bandwidth-total-11-BandwidthMigration", true,
206 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
207 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "SDN-ETHERNET-INTERNET")
208 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-11-BandwidthMigration")
209 .has("bandwidth-total", "bandwidth-total-11-BandwidthMigration")
212 assertEquals("service subscription node, service-type=DHV", true,
213 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
214 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
217 assertEquals("service instance node, bandwidth-total=servInstance-9972-4-BandwidthMigration", true,
218 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
219 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
220 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-4-BandwidthMigration")
221 .has("bandwidth-total", "bandwidth-total-4-BandwidthMigration")
224 assertEquals("service instance node, bandwidth-total=ServInstance-9972-1-BandwidthMigration", true,
225 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
226 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
227 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "ServInstance-9972-1-BandwidthMigration")
228 .has("bandwidth-total", "2380")
231 assertEquals("service instance node, bandwidth-total=servInstance-9972-3-BandwidthMigration", true,
232 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
233 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
234 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-3-BandwidthMigration")
235 .has("bandwidth-total", "bandwidth-total-3-BandwidthMigration")
238 assertEquals("service instance node, bandwidth-total=servInstance-9972-2-BandwidthMigration", true,
239 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
240 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
241 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-2-BandwidthMigration")
242 .has("bandwidth-total", "bandwidth-total-2-BandwidthMigration")
245 assertEquals("configuration node with type=configuration-type1-9972, tunnel-bandwidth does not exist", true,
246 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
247 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
248 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-2-BandwidthMigration")
249 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration")
250 .has("configuration-type", "configuration-type1-9972")
253 // check if configuration node gets created for 2, 3, 4
254 assertEquals("configuration node created with type=DHV, tunnel-bandwidth=servInstance-9972-4-BandwidthMigration", true,
255 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
256 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
257 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-4-BandwidthMigration")
258 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration")
259 .has("configuration-type", "DHV").has("tunnel-bandwidth", "bandwidth-total-4-BandwidthMigration")
262 assertEquals("configuration node created with type=DHV, tunnel-bandwidth=servInstance-9972-3-BandwidthMigration", true,
263 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
264 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
265 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-3-BandwidthMigration")
266 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration")
267 .has("configuration-type", "DHV").has("tunnel-bandwidth", "bandwidth-total-3-BandwidthMigration")
270 assertEquals("configuration node created with type=DHV, tunnel-bandwidth=servInstance-9972-2-BandwidthMigration", true,
271 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
272 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
273 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-2-BandwidthMigration")
274 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration")
275 .has("configuration-type", "DHV").has("tunnel-bandwidth", "bandwidth-total-2-BandwidthMigration")
278 // configuration modified for ServInstance-9972-1-BandwidthMigration
279 assertEquals("configuration node modified for ServInstance-9972-1-BandwidthMigration, tunnel-bandwidth=2380", true,
280 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
281 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
282 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "ServInstance-9972-1-BandwidthMigration")
283 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration")
284 .has("configuration-type", "DHV").has("tunnel-bandwidth", "2380")
289 public void testGetAffectedNodeTypes() {
290 Optional<String[]> types = migration.getAffectedNodeTypes();
291 Optional<String[]> expected = Optional.of(new String[]{"service-instance"});
293 assertNotNull(types);
294 assertArrayEquals(expected.get(), types.get());
298 public void testGetMigrationName() {
299 String migrationName = migration.getMigrationName();
301 assertNotNull(migrationName);
302 assertEquals("service-instance-to-configuration", migrationName);