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 MigrateServiceInstanceToConfigurationTest 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 customer1 = g.addV()
79 .property("aai-node-type", "customer")
80 .property("global-customer-id", "customer-id-1")
81 .property("subscriber-type", "CUST")
84 Vertex customer2 = g.addV()
85 .property("aai-node-type", "customer")
86 .property("global-customer-id", "customer-id-2")
87 .property("subscriber-type", "CUST")
90 Vertex customer3 = g.addV()
91 .property("aai-node-type", "customer")
92 .property("global-customer-id", "customer-id-3")
93 .property("subscriber-type", "CUST")
96 Vertex customer4 = g.addV()
97 .property("aai-node-type", "customer")
98 .property("global-customer-id", "customer-id-4")
99 .property("subscriber-type", "CUST")
102 Vertex servSub1 = g.addV()
103 .property("aai-node-type", "service-subscription")
104 .property("service-type", "DHV")
107 Vertex servSub2 = g.addV()
108 .property("aai-node-type", "service-subscription")
109 .property("service-type", "OTHER")
112 Vertex servSub3 = g.addV()
113 .property("aai-node-type", "service-subscription")
114 .property("service-type", "DHV")
117 Vertex servSub4 = g.addV()
118 .property("aai-node-type", "service-subscription")
119 .property("service-type", "DHV")
122 Vertex servSub5 = g.addV()
123 .property("aai-node-type", "service-subscription")
124 .property("service-type", "DHV")
127 Vertex servInstance1 = g.addV()
128 .property("aai-node-type", "service-instance")
129 .property("service-instance-id", "service-inst-1")
130 .property("operational-status", "activated")
131 .property("bandwidth-total", "5")
134 Vertex servInstance2 = g.addV()
135 .property("aai-node-type", "service-instance")
136 .property("service-instance-id", "service-inst-2")
137 .property("operational-status", "activated")
138 .property("bandwidth-total", "8")
141 Vertex servInstance3 = g.addV()
142 .property("aai-node-type", "service-instance")
143 .property("service-instance-id", "service-inst-3")
144 .property("operational-status", "activated")
145 .property("bandwidth-total", "10")
148 Vertex servInstance4 = g.addV()
149 .property("aai-node-type", "service-instance")
150 .property("service-instance-id", "service-inst-4")
151 .property("operational-status", "activated")
152 .property("bandwidth-total", "15")
155 Vertex config1 = g.addV()
156 .property("aai-node-type", "configuration")
157 .property("configuration-id", "configuration-1")
158 .property("configuration-type", "DHV")
159 .property("tunnel-bandwidth", "7")
162 Vertex config2 = g.addV()
163 .property("aai-node-type", "configuration")
164 .property("configuration-id", "configuration-2")
165 .property("configuration-type", "OTHER")
166 .property("tunnel-bandwidth", "3")
169 Vertex config3 = g.addV()
170 .property("aai-node-type", "configuration")
171 .property("configuration-id", "configuration-3")
172 .property("configuration-type", "OTHER")
173 .property("tunnel-bandwidth", "2")
176 Vertex config4 = g.addV()
177 .property("aai-node-type", "configuration")
178 .property("configuration-id", "configuration-4")
179 .property("configuration-type", "OTHER")
180 .property("tunnel-bandwidth", "4")
184 rules.addTreeEdge(g, customer1, servSub1);
185 rules.addTreeEdge(g, customer1, servSub2);
186 rules.addTreeEdge(g, servSub1, servInstance1);
187 rules.addTreeEdge(g, servSub2, servInstance2);
190 rules.addTreeEdge(g, customer2, servSub3);
193 rules.addTreeEdge(g, customer3, servSub4);
194 rules.addTreeEdge(g, servSub4, servInstance3);
195 rules.addEdge(g, servInstance3, config1);
196 rules.addEdge(g, servInstance3, config2);
199 rules.addTreeEdge(g, customer4, servSub5);
200 rules.addTreeEdge(g, servSub5, servInstance4);
201 rules.addEdge(g, servInstance4, config3);
202 rules.addEdge(g, servInstance4, config4);
204 TransactionalGraphEngine spy = spy(dbEngine);
205 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
207 GraphTraversalSource traversal = g;
208 GraphTraversalSource readOnly = tx.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance()));
209 when (spy.tx()).thenReturn(tx);
210 when(spy.asAdmin()).thenReturn(adminSpy);
211 when(adminSpy.getTraversalSource()).thenReturn(traversal);
212 when(adminSpy.getReadOnlyTraversalSource()).thenReturn(readOnly);
214 migration = new MigrateServiceInstanceToConfiguration(spy);
219 public static void cleanUp() {
225 public void testRun_createConfigNode() throws Exception {
226 // check if graph nodes exist
227 assertEquals("customer node exists", true,
228 g.V().has("global-customer-id", "customer-id-1")
231 assertEquals("service subscription node, service-type=DHV", true,
232 g.V().has("global-customer-id", "customer-id-1")
233 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
236 assertEquals("service instance node, bandwidth-total=5", true,
237 g.V().has("global-customer-id", "customer-id-1")
238 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
239 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-1").has("bandwidth-total", "5")
242 // check if configuration node gets created
243 assertEquals("configuration node exists", true,
244 g.V().has("global-customer-id", "customer-id-1")
245 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
246 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-1")
247 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration")
250 // check configuration type
251 assertEquals("configuration node, configuration-type=DHV", true,
252 g.V().has("global-customer-id", "customer-id-1")
253 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
254 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-1")
255 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("configuration-type", "DHV")
258 // check configuration tunnel-bandwidth
259 assertEquals("configuration node, tunnel-bandwidth=5", true,
260 g.V().has("global-customer-id", "customer-id-1")
261 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
262 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-1")
263 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("tunnel-bandwidth", "5")
268 public void testRun_configNodeNotCreated() throws Exception {
269 // check if graph nodes exist
270 assertEquals("customer node exists", true,
271 g.V().has("global-customer-id", "customer-id-1")
274 assertEquals("service subscription node, service-type=OTHER", true,
275 g.V().has("global-customer-id", "customer-id-1")
276 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "OTHER")
279 assertEquals("service instance node, bandwidth-total=8", true,
280 g.V().has("global-customer-id", "customer-id-1")
281 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "OTHER")
282 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-2").has("bandwidth-total", "8")
285 // configuration node should not be created
286 assertEquals("configuration node does not exist", false,
287 g.V().has("global-customer-id", "customer-id-1")
288 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "OTHER")
289 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-2")
290 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration")
293 // edge between service instance and configuration should not be created
294 assertEquals("configuration node does not exist", false,
295 g.V().has("global-customer-id", "customer-id-1")
296 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "OTHER")
297 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-2")
298 .out("org.onap.relationships.inventory.Uses").hasNext());
302 public void testRun_noServiceInstance() throws Exception {
303 // check if graph nodes exist
304 assertEquals("customer node exists", true,
305 g.V().has("global-customer-id", "customer-id-2")
308 assertEquals("service subscription node, service-type=DHV", true,
309 g.V().has("global-customer-id", "customer-id-2")
310 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
313 // no service instance nodes
314 assertEquals("no service instance nodes", false,
315 g.V().has("global-customer-id", "customer-id-2")
316 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
317 .in("org.onap.relationships.inventory.BelongsTo").has("aai-node-type", "service-instance")
322 public void testRun_existingConfig() throws Exception {
323 // check if graph nodes exist
324 assertEquals("customer node exists", true,
325 g.V().has("global-customer-id", "customer-id-3")
328 assertEquals("service subscription node, service-type=DHV", true,
329 g.V().has("global-customer-id", "customer-id-3")
330 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
333 assertEquals("service instance node, bandwidth-total=10", true,
334 g.V().has("global-customer-id", "customer-id-3")
335 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
336 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-3").has("bandwidth-total", "10")
339 assertEquals("configuration node with type DHV, tunnel-bandwidth changed to 10", true,
340 g.V().has("global-customer-id", "customer-id-3")
341 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
342 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-3")
343 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("tunnel-bandwidth", "10")
346 assertEquals("configuration node with type OTHER, tunnel-bandwidth remains same", true,
347 g.V().has("global-customer-id", "customer-id-3")
348 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
349 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-3")
350 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("tunnel-bandwidth", "3")
355 public void testRun_existingConfigNotDHV() throws Exception {
356 // check if graph nodes exist
357 assertEquals("customer node exists", true,
358 g.V().has("global-customer-id", "customer-id-4")
361 assertEquals("service subscription node, service-type=DHV", true,
362 g.V().has("global-customer-id", "customer-id-4")
363 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
366 assertEquals("service instance node, bandwidth-total=15", true,
367 g.V().has("global-customer-id", "customer-id-4")
368 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
369 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-4").has("bandwidth-total", "15")
372 assertEquals("first configuration node with type OTHER, tunnel-bandwidth remains same", true,
373 g.V().has("global-customer-id", "customer-id-4")
374 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
375 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-4")
376 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("tunnel-bandwidth", "2")
379 assertEquals("second configuration node with type OTHER, tunnel-bandwidth remains same", true,
380 g.V().has("global-customer-id", "customer-id-4")
381 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
382 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-4")
383 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("tunnel-bandwidth", "4")
386 assertEquals("new configuration node created with type DHV, tunnel-bandwidth=15", true,
387 g.V().has("global-customer-id", "customer-id-4")
388 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
389 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-4")
390 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("tunnel-bandwidth", "15")
395 public void testGetAffectedNodeTypes() {
396 Optional<String[]> types = migration.getAffectedNodeTypes();
397 Optional<String[]> expected = Optional.of(new String[]{"service-instance"});
399 assertNotNull(types);
400 assertArrayEquals(expected.get(), types.get());
404 public void testGetMigrationName() {
405 String migrationName = migration.getMigrationName();
407 assertNotNull(migrationName);
408 assertEquals("service-instance-to-configuration", migrationName);