1 package org.onap.aai.migration.v12;
3 import static org.junit.Assert.assertArrayEquals;
4 import static org.junit.Assert.assertEquals;
5 import static org.junit.Assert.assertNotNull;
6 import static org.mockito.Mockito.spy;
7 import static org.mockito.Mockito.when;
9 import java.util.Optional;
11 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
12 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
13 import org.apache.tinkerpop.gremlin.structure.Vertex;
14 import org.junit.AfterClass;
15 import org.junit.BeforeClass;
16 import org.junit.Test;
17 import org.onap.aai.AAISetup;
18 import org.onap.aai.dbmap.DBConnectionType;
19 import org.onap.aai.introspection.Loader;
20 import org.onap.aai.introspection.LoaderFactory;
21 import org.onap.aai.introspection.ModelType;
22 import org.onap.aai.introspection.Version;
23 import org.onap.aai.serialization.db.EdgeRules;
24 import org.onap.aai.serialization.engines.QueryStyle;
25 import org.onap.aai.serialization.engines.TitanDBEngine;
26 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
28 import com.thinkaurelius.titan.core.TitanFactory;
29 import com.thinkaurelius.titan.core.TitanGraph;
30 import com.thinkaurelius.titan.core.TitanTransaction;
32 public class MigrateServiceInstanceToConfigurationTest extends AAISetup {
34 private final static Version version = Version.v12;
35 private final static ModelType introspectorFactoryType = ModelType.MOXY;
36 private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL;
37 private final static DBConnectionType type = DBConnectionType.REALTIME;
39 private static Loader loader;
40 private static TransactionalGraphEngine dbEngine;
41 private static TitanGraph graph;
42 private static MigrateServiceInstanceToConfiguration migration;
43 private static TitanTransaction tx;
44 private static GraphTraversalSource g;
45 private static EdgeRules rules;
48 public static void setUp() throws Exception {
49 graph = TitanFactory.build().set("storage.backend","inmemory").open();
50 tx = graph.newTransaction();
52 loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version);
53 dbEngine = new TitanDBEngine(
57 rules = EdgeRules.getInstance();
59 Vertex customer1 = g.addV()
60 .property("aai-node-type", "customer")
61 .property("global-customer-id", "customer-id-1")
62 .property("subscriber-type", "CUST")
65 Vertex customer2 = g.addV()
66 .property("aai-node-type", "customer")
67 .property("global-customer-id", "customer-id-2")
68 .property("subscriber-type", "CUST")
71 Vertex customer3 = g.addV()
72 .property("aai-node-type", "customer")
73 .property("global-customer-id", "customer-id-3")
74 .property("subscriber-type", "CUST")
77 Vertex customer4 = g.addV()
78 .property("aai-node-type", "customer")
79 .property("global-customer-id", "customer-id-4")
80 .property("subscriber-type", "CUST")
83 Vertex servSub1 = g.addV()
84 .property("aai-node-type", "service-subscription")
85 .property("service-type", "DHV")
88 Vertex servSub2 = g.addV()
89 .property("aai-node-type", "service-subscription")
90 .property("service-type", "OTHER")
93 Vertex servSub3 = g.addV()
94 .property("aai-node-type", "service-subscription")
95 .property("service-type", "DHV")
98 Vertex servSub4 = g.addV()
99 .property("aai-node-type", "service-subscription")
100 .property("service-type", "DHV")
103 Vertex servSub5 = g.addV()
104 .property("aai-node-type", "service-subscription")
105 .property("service-type", "DHV")
108 Vertex servInstance1 = g.addV()
109 .property("aai-node-type", "service-instance")
110 .property("service-instance-id", "service-inst-1")
111 .property("operational-status", "activated")
112 .property("bandwidth-total", "5")
115 Vertex servInstance2 = g.addV()
116 .property("aai-node-type", "service-instance")
117 .property("service-instance-id", "service-inst-2")
118 .property("operational-status", "activated")
119 .property("bandwidth-total", "8")
122 Vertex servInstance3 = g.addV()
123 .property("aai-node-type", "service-instance")
124 .property("service-instance-id", "service-inst-3")
125 .property("operational-status", "activated")
126 .property("bandwidth-total", "10")
129 Vertex servInstance4 = g.addV()
130 .property("aai-node-type", "service-instance")
131 .property("service-instance-id", "service-inst-4")
132 .property("operational-status", "activated")
133 .property("bandwidth-total", "15")
136 Vertex config1 = g.addV()
137 .property("aai-node-type", "configuration")
138 .property("configuration-id", "configuration-1")
139 .property("configuration-type", "DHV")
140 .property("tunnel-bandwidth", "7")
143 Vertex config2 = g.addV()
144 .property("aai-node-type", "configuration")
145 .property("configuration-id", "configuration-2")
146 .property("configuration-type", "OTHER")
147 .property("tunnel-bandwidth", "3")
150 Vertex config3 = g.addV()
151 .property("aai-node-type", "configuration")
152 .property("configuration-id", "configuration-3")
153 .property("configuration-type", "OTHER")
154 .property("tunnel-bandwidth", "2")
157 Vertex config4 = g.addV()
158 .property("aai-node-type", "configuration")
159 .property("configuration-id", "configuration-4")
160 .property("configuration-type", "OTHER")
161 .property("tunnel-bandwidth", "4")
165 rules.addTreeEdge(g, customer1, servSub1);
166 rules.addTreeEdge(g, customer1, servSub2);
167 rules.addTreeEdge(g, servSub1, servInstance1);
168 rules.addTreeEdge(g, servSub2, servInstance2);
171 rules.addTreeEdge(g, customer2, servSub3);
174 rules.addTreeEdge(g, customer3, servSub4);
175 rules.addTreeEdge(g, servSub4, servInstance3);
176 rules.addEdge(g, servInstance3, config1);
177 rules.addEdge(g, servInstance3, config2);
180 rules.addTreeEdge(g, customer4, servSub5);
181 rules.addTreeEdge(g, servSub5, servInstance4);
182 rules.addEdge(g, servInstance4, config3);
183 rules.addEdge(g, servInstance4, config4);
185 TransactionalGraphEngine spy = spy(dbEngine);
186 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
188 GraphTraversalSource traversal = g;
189 GraphTraversalSource readOnly = tx.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance()));
190 when (spy.tx()).thenReturn(tx);
191 when(spy.asAdmin()).thenReturn(adminSpy);
192 when(adminSpy.getTraversalSource()).thenReturn(traversal);
193 when(adminSpy.getReadOnlyTraversalSource()).thenReturn(readOnly);
195 migration = new MigrateServiceInstanceToConfiguration(spy);
200 public static void cleanUp() {
206 public void testRun_createConfigNode() throws Exception {
207 // check if graph nodes exist
208 assertEquals("customer node exists", true,
209 g.V().has("global-customer-id", "customer-id-1")
212 assertEquals("service subscription node, service-type=DHV", true,
213 g.V().has("global-customer-id", "customer-id-1")
214 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
217 assertEquals("service instance node, bandwidth-total=5", true,
218 g.V().has("global-customer-id", "customer-id-1")
219 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
220 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-1").has("bandwidth-total", "5")
223 // check if configuration node gets created
224 assertEquals("configuration node exists", true,
225 g.V().has("global-customer-id", "customer-id-1")
226 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
227 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-1")
228 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration")
231 // check configuration type
232 assertEquals("configuration node, configuration-type=DHV", true,
233 g.V().has("global-customer-id", "customer-id-1")
234 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
235 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-1")
236 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("configuration-type", "DHV")
239 // check configuration tunnel-bandwidth
240 assertEquals("configuration node, tunnel-bandwidth=5", true,
241 g.V().has("global-customer-id", "customer-id-1")
242 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
243 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-1")
244 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("tunnel-bandwidth", "5")
249 public void testRun_configNodeNotCreated() throws Exception {
250 // check if graph nodes exist
251 assertEquals("customer node exists", true,
252 g.V().has("global-customer-id", "customer-id-1")
255 assertEquals("service subscription node, service-type=OTHER", true,
256 g.V().has("global-customer-id", "customer-id-1")
257 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "OTHER")
260 assertEquals("service instance node, bandwidth-total=8", true,
261 g.V().has("global-customer-id", "customer-id-1")
262 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "OTHER")
263 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-2").has("bandwidth-total", "8")
266 // configuration node should not be created
267 assertEquals("configuration node does not exist", false,
268 g.V().has("global-customer-id", "customer-id-1")
269 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "OTHER")
270 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-2")
271 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration")
274 // edge between service instance and configuration should not be created
275 assertEquals("configuration node does not exist", false,
276 g.V().has("global-customer-id", "customer-id-1")
277 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "OTHER")
278 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-2")
279 .out("org.onap.relationships.inventory.Uses").hasNext());
283 public void testRun_noServiceInstance() throws Exception {
284 // check if graph nodes exist
285 assertEquals("customer node exists", true,
286 g.V().has("global-customer-id", "customer-id-2")
289 assertEquals("service subscription node, service-type=DHV", true,
290 g.V().has("global-customer-id", "customer-id-2")
291 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
294 // no service instance nodes
295 assertEquals("no service instance nodes", false,
296 g.V().has("global-customer-id", "customer-id-2")
297 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
298 .in("org.onap.relationships.inventory.BelongsTo").has("aai-node-type", "service-instance")
303 public void testRun_existingConfig() throws Exception {
304 // check if graph nodes exist
305 assertEquals("customer node exists", true,
306 g.V().has("global-customer-id", "customer-id-3")
309 assertEquals("service subscription node, service-type=DHV", true,
310 g.V().has("global-customer-id", "customer-id-3")
311 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
314 assertEquals("service instance node, bandwidth-total=10", true,
315 g.V().has("global-customer-id", "customer-id-3")
316 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
317 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-3").has("bandwidth-total", "10")
320 assertEquals("configuration node with type DHV, tunnel-bandwidth changed to 10", true,
321 g.V().has("global-customer-id", "customer-id-3")
322 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
323 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-3")
324 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("tunnel-bandwidth", "10")
327 assertEquals("configuration node with type OTHER, tunnel-bandwidth remains same", true,
328 g.V().has("global-customer-id", "customer-id-3")
329 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
330 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-3")
331 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("tunnel-bandwidth", "3")
336 public void testRun_existingConfigNotDHV() throws Exception {
337 // check if graph nodes exist
338 assertEquals("customer node exists", true,
339 g.V().has("global-customer-id", "customer-id-4")
342 assertEquals("service subscription node, service-type=DHV", true,
343 g.V().has("global-customer-id", "customer-id-4")
344 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
347 assertEquals("service instance node, bandwidth-total=15", true,
348 g.V().has("global-customer-id", "customer-id-4")
349 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
350 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-4").has("bandwidth-total", "15")
353 assertEquals("first configuration node with type OTHER, tunnel-bandwidth remains same", true,
354 g.V().has("global-customer-id", "customer-id-4")
355 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
356 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-4")
357 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("tunnel-bandwidth", "2")
360 assertEquals("second configuration node with type OTHER, tunnel-bandwidth remains same", true,
361 g.V().has("global-customer-id", "customer-id-4")
362 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
363 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-4")
364 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("tunnel-bandwidth", "4")
367 assertEquals("new configuration node created with type DHV, tunnel-bandwidth=15", true,
368 g.V().has("global-customer-id", "customer-id-4")
369 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
370 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-4")
371 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("tunnel-bandwidth", "15")
376 public void testGetAffectedNodeTypes() {
377 Optional<String[]> types = migration.getAffectedNodeTypes();
378 Optional<String[]> expected = Optional.of(new String[]{"service-instance"});
380 assertNotNull(types);
381 assertArrayEquals(expected.get(), types.get());
385 public void testGetMigrationName() {
386 String migrationName = migration.getMigrationName();
388 assertNotNull(migrationName);
389 assertEquals("service-instance-to-configuration", migrationName);