6e153eae9193f8f365d723d2fea5e4dd8fc09f81
[aai/resources.git] /
1 package org.onap.aai.migration.v12;
2
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;
8
9 import java.util.Optional;
10
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;
27
28 import com.thinkaurelius.titan.core.TitanFactory;
29 import com.thinkaurelius.titan.core.TitanGraph;
30 import com.thinkaurelius.titan.core.TitanTransaction;
31
32 public class MigrateServiceInstanceToConfigurationTest extends AAISetup {
33
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;
38
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;
46
47         @BeforeClass
48         public static void setUp() throws Exception {
49                 graph = TitanFactory.build().set("storage.backend","inmemory").open();
50                 tx = graph.newTransaction();
51                 g = tx.traversal();
52                 loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version);
53                 dbEngine = new TitanDBEngine(
54                                 queryStyle,
55                                 type,
56                                 loader);
57                 rules = EdgeRules.getInstance();
58
59                 Vertex customer1 = g.addV()
60                                 .property("aai-node-type", "customer")
61                                 .property("global-customer-id", "customer-id-1")
62                                 .property("subscriber-type", "CUST")
63                                 .next();
64                 
65                 Vertex customer2 = g.addV()
66                                 .property("aai-node-type", "customer")
67                                 .property("global-customer-id", "customer-id-2")
68                                 .property("subscriber-type", "CUST")
69                                 .next();
70                 
71                 Vertex customer3 = g.addV()
72                                 .property("aai-node-type", "customer")
73                                 .property("global-customer-id", "customer-id-3")
74                                 .property("subscriber-type", "CUST")
75                                 .next();
76                 
77                 Vertex customer4 = g.addV()
78                                 .property("aai-node-type", "customer")
79                                 .property("global-customer-id", "customer-id-4")
80                                 .property("subscriber-type", "CUST")
81                                 .next();
82
83                 Vertex servSub1 = g.addV()
84                                 .property("aai-node-type", "service-subscription")
85                                 .property("service-type", "DHV")
86                                 .next();
87                 
88                 Vertex servSub2 = g.addV()
89                                 .property("aai-node-type", "service-subscription")
90                                 .property("service-type", "OTHER")
91                                 .next();
92                 
93                 Vertex servSub3 = g.addV()
94                                 .property("aai-node-type", "service-subscription")
95                                 .property("service-type", "DHV")
96                                 .next();
97                 
98                 Vertex servSub4 = g.addV()
99                                 .property("aai-node-type", "service-subscription")
100                                 .property("service-type", "DHV")
101                                 .next();
102                 
103                 Vertex servSub5 = g.addV()
104                                 .property("aai-node-type", "service-subscription")
105                                 .property("service-type", "DHV")
106                                 .next();
107                 
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")
113                                 .next();
114                 
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")
120                                 .next();
121                 
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")
127                                 .next();
128                 
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")
134                                 .next();
135                 
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")
141                                 .next();
142                 
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")
148                                 .next();
149                 
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")
155                                 .next();
156                 
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")
162                                 .next();
163
164                 // graph 1
165                 rules.addTreeEdge(g, customer1, servSub1);
166                 rules.addTreeEdge(g, customer1, servSub2);
167                 rules.addTreeEdge(g, servSub1, servInstance1);
168                 rules.addTreeEdge(g, servSub2, servInstance2);
169                 
170                 // graph 2
171                 rules.addTreeEdge(g, customer2, servSub3);
172                 
173                 // graph 3
174                 rules.addTreeEdge(g, customer3, servSub4);
175                 rules.addTreeEdge(g, servSub4, servInstance3);
176                 rules.addEdge(g, servInstance3, config1);
177                 rules.addEdge(g, servInstance3, config2);
178                 
179                 // graph 4
180                 rules.addTreeEdge(g, customer4, servSub5);
181                 rules.addTreeEdge(g, servSub5, servInstance4);
182                 rules.addEdge(g, servInstance4, config3);
183                 rules.addEdge(g, servInstance4, config4);
184
185                 TransactionalGraphEngine spy = spy(dbEngine);
186                 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
187
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);
194                 
195                 migration = new MigrateServiceInstanceToConfiguration(spy);
196                 migration.run();
197         }
198         
199         @AfterClass
200         public static void cleanUp() {
201                 tx.tx().rollback();
202                 graph.close();
203         }
204         
205         @Test
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")
210                                 .hasNext());
211                 
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")
215                                 .hasNext());
216                 
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")
221                                 .hasNext());
222                 
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")
229                                 .hasNext());
230                 
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")
237                                 .hasNext());
238                 
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")
245                                 .hasNext());
246         }
247
248         @Test
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")
253                                 .hasNext());
254                 
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")
258                                 .hasNext());
259                 
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")
264                                 .hasNext());
265                 
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")
272                                 .hasNext());
273                 
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());
280         }
281         
282         @Test
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")
287                                 .hasNext());
288                 
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")
292                                 .hasNext());
293                 
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")
299                                 .hasNext());
300         }
301         
302         @Test
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")
307                                 .hasNext());
308                 
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")
312                                 .hasNext());
313                 
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")
318                                 .hasNext());
319                 
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")
325                                 .hasNext());
326                 
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")
332                                 .hasNext());
333         }
334         
335         @Test
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")
340                                 .hasNext());
341                 
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")
345                                 .hasNext());
346                 
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")
351                                 .hasNext());
352                 
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")
358                                 .hasNext());
359                 
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")
365                                 .hasNext());
366                 
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")
372                                 .hasNext());
373         }
374         
375         @Test
376         public void testGetAffectedNodeTypes() {
377                 Optional<String[]> types = migration.getAffectedNodeTypes();
378                 Optional<String[]> expected = Optional.of(new String[]{"service-instance"});
379                 
380                 assertNotNull(types);
381                 assertArrayEquals(expected.get(), types.get());
382         }
383
384         @Test
385         public void testGetMigrationName() {
386                 String migrationName = migration.getMigrationName();
387
388                 assertNotNull(migrationName);
389                 assertEquals("service-instance-to-configuration", migrationName);
390         }
391 }