c14781352a8f366a17d1632dd04bf13c40a4efaa
[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 MigrateServiceInstanceToConfigurationTestPreMigrationMock 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 customer = g.addV()
60                                 .property("aai-node-type", "customer")
61                                 .property("global-customer-id", "customer-9972-BandwidthMigration")
62                                 .property("subscriber-type", "CUST")
63                                 .next();
64                 
65                 Vertex servSubSDNEI = g.addV()
66                                 .property("aai-node-type", "service-subscription")
67                                 .property("service-type", "SDN-ETHERNET-INTERNET")
68                                 .next();
69                 
70                 Vertex servInstance22 = g.addV()
71                                 .property("aai-node-type", "service-instance")
72                                 .property("service-instance-id", "servInstance-9972-22-BandwidthMigration")
73                                 .property("operational-status", "activated")
74                                 .property("bandwidth-total", "bandwidth-total-22-BandwidthMigration")
75                                 .next();
76                 
77                 Vertex servInstance11 = g.addV()
78                                 .property("aai-node-type", "service-instance")
79                                 .property("service-instance-id", "servInstance-9972-11-BandwidthMigration")
80                                 .property("operational-status", "activated")
81                                 .property("bandwidth-total", "bandwidth-total-11-BandwidthMigration")
82                                 .next();
83                 
84                 Vertex servSubDHV = g.addV()
85                                 .property("aai-node-type", "service-subscription")
86                                 .property("service-type", "DHV")
87                                 .next();
88                 
89                 Vertex servInstance4 = g.addV()
90                                 .property("aai-node-type", "service-instance")
91                                 .property("service-instance-id", "servInstance-9972-4-BandwidthMigration")
92                                 .property("operational-status", "activated")
93                                 .property("bandwidth-total", "bandwidth-total-4-BandwidthMigration")
94                                 .next();
95                 
96                 Vertex servInstance1 = g.addV()
97                                 .property("aai-node-type", "service-instance")
98                                 .property("service-instance-id", "ServInstance-9972-1-BandwidthMigration")
99                                 .property("operational-status", "activated")
100                                 .property("bandwidth-total", "2380")
101                                 .next();
102                 
103                 Vertex servInstance3 = g.addV()
104                                 .property("aai-node-type", "service-instance")
105                                 .property("service-instance-id", "servInstance-9972-3-BandwidthMigration")
106                                 .property("operational-status", "activated")
107                                 .property("bandwidth-total", "bandwidth-total-3-BandwidthMigration")
108                                 .next();
109
110                 Vertex servInstance2 = g.addV()
111                                 .property("aai-node-type", "service-instance")
112                                 .property("service-instance-id", "servInstance-9972-2-BandwidthMigration")
113                                 .property("operational-status", "activated")
114                                 .property("bandwidth-total", "bandwidth-total-2-BandwidthMigration")
115                                 .next();
116                 
117                 Vertex config1 = g.addV()
118                                 .property("aai-node-type", "configuration")
119                                 .property("configuration-id", "9972-config-LB1113")
120                                 .property("configuration-type", "DHV")
121                                 .property("tunnel-bandwidth", "12")
122                                 .next();
123                 
124                 Vertex config2 = g.addV()
125                                 .property("aai-node-type", "configuration")
126                                 .property("configuration-id", "9972-1config-LB1113")
127                                 .property("configuration-type", "configuration-type1-9972")
128                                 .next();
129                 
130                 Vertex allottedResource = g.addV()
131                                 .property("aai-node-type", "allotted-resource")
132                                 .property("id", "allResource-9972-BandwidthMigration")
133                                 .next();
134
135                 rules.addTreeEdge(g, customer, servSubSDNEI);
136                 rules.addTreeEdge(g, customer, servSubDHV);
137                 rules.addTreeEdge(g, servSubSDNEI, servInstance22);
138                 rules.addTreeEdge(g, servSubSDNEI, servInstance11);
139                 rules.addTreeEdge(g, servSubDHV, servInstance4);
140                 rules.addTreeEdge(g, servSubDHV, servInstance1);
141                 rules.addTreeEdge(g, servSubDHV, servInstance3);
142                 rules.addTreeEdge(g, servSubDHV, servInstance2);
143                 rules.addEdge(g, servInstance1, allottedResource);
144                 rules.addEdge(g, servInstance1, config1);
145                 rules.addEdge(g, servInstance2, config2);
146
147                 TransactionalGraphEngine spy = spy(dbEngine);
148                 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
149
150                 GraphTraversalSource traversal = g;
151                 GraphTraversalSource readOnly = tx.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance()));
152                 when (spy.tx()).thenReturn(tx);
153                 when(spy.asAdmin()).thenReturn(adminSpy);
154                 when(adminSpy.getTraversalSource()).thenReturn(traversal);
155                 when(adminSpy.getReadOnlyTraversalSource()).thenReturn(readOnly);
156                 
157                 migration = new MigrateServiceInstanceToConfiguration(spy);
158                 migration.run();
159         }
160         
161         @AfterClass
162         public static void cleanUp() {
163                 tx.tx().rollback();
164                 graph.close();
165         }
166         
167         @Test
168         public void testRun() throws Exception {
169                 // check if graph nodes exist
170                 assertEquals("customer node exists", true, 
171                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
172                                 .hasNext());
173
174                 assertEquals("service subscription node, service-type=SDN-ETHERNET-INTERNET", true, 
175                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
176                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "SDN-ETHERNET-INTERNET")
177                                 .hasNext());
178
179                 assertEquals("service instance node, bandwidth-total=bandwidth-total-22-BandwidthMigration", true, 
180                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
181                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "SDN-ETHERNET-INTERNET")
182                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-22-BandwidthMigration")
183                                 .has("bandwidth-total", "bandwidth-total-22-BandwidthMigration")
184                                 .hasNext());
185                 
186                 assertEquals("service instance node, bandwidth-total=bandwidth-total-11-BandwidthMigration", true, 
187                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
188                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "SDN-ETHERNET-INTERNET")
189                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-11-BandwidthMigration")
190                                 .has("bandwidth-total", "bandwidth-total-11-BandwidthMigration")
191                                 .hasNext());
192                 
193                 assertEquals("service subscription node, service-type=DHV", true, 
194                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
195                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
196                                 .hasNext());
197
198                 assertEquals("service instance node, bandwidth-total=servInstance-9972-4-BandwidthMigration", true, 
199                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
200                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
201                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-4-BandwidthMigration")
202                                 .has("bandwidth-total", "bandwidth-total-4-BandwidthMigration")
203                                 .hasNext());
204                 
205                 assertEquals("service instance node, bandwidth-total=ServInstance-9972-1-BandwidthMigration", true, 
206                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
207                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
208                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "ServInstance-9972-1-BandwidthMigration")
209                                 .has("bandwidth-total", "2380")
210                                 .hasNext());
211                 
212                 assertEquals("service instance node, bandwidth-total=servInstance-9972-3-BandwidthMigration", true, 
213                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
214                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
215                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-3-BandwidthMigration")
216                                 .has("bandwidth-total", "bandwidth-total-3-BandwidthMigration")
217                                 .hasNext());
218                 
219                 assertEquals("service instance node, bandwidth-total=servInstance-9972-2-BandwidthMigration", true, 
220                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
221                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
222                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-2-BandwidthMigration")
223                                 .has("bandwidth-total", "bandwidth-total-2-BandwidthMigration")
224                                 .hasNext());
225                 
226                 assertEquals("configuration node with type=configuration-type1-9972, tunnel-bandwidth does not exist", true, 
227                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
228                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
229                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-2-BandwidthMigration")
230                                 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration")
231                                 .has("configuration-type", "configuration-type1-9972")
232                                 .hasNext());
233                 
234                 // check if configuration node gets created for 2, 3, 4
235                 assertEquals("configuration node created with type=DHV, tunnel-bandwidth=servInstance-9972-4-BandwidthMigration", true, 
236                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
237                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
238                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-4-BandwidthMigration")
239                                 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration")
240                                 .has("configuration-type", "DHV").has("tunnel-bandwidth", "bandwidth-total-4-BandwidthMigration")
241                                 .hasNext());
242                 
243                 assertEquals("configuration node created with type=DHV, tunnel-bandwidth=servInstance-9972-3-BandwidthMigration", true, 
244                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
245                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
246                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-3-BandwidthMigration")
247                                 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration")
248                                 .has("configuration-type", "DHV").has("tunnel-bandwidth", "bandwidth-total-3-BandwidthMigration")
249                                 .hasNext());
250                 
251                 assertEquals("configuration node created with type=DHV, tunnel-bandwidth=servInstance-9972-2-BandwidthMigration", true, 
252                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
253                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
254                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-2-BandwidthMigration")
255                                 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration")
256                                 .has("configuration-type", "DHV").has("tunnel-bandwidth", "bandwidth-total-2-BandwidthMigration")
257                                 .hasNext());
258                 
259                 // configuration modified for ServInstance-9972-1-BandwidthMigration
260                 assertEquals("configuration node modified for ServInstance-9972-1-BandwidthMigration, tunnel-bandwidth=2380", true, 
261                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
262                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
263                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "ServInstance-9972-1-BandwidthMigration")
264                                 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration")
265                                 .has("configuration-type", "DHV").has("tunnel-bandwidth", "2380")
266                                 .hasNext());
267         }
268         
269         @Test
270         public void testGetAffectedNodeTypes() {
271                 Optional<String[]> types = migration.getAffectedNodeTypes();
272                 Optional<String[]> expected = Optional.of(new String[]{"service-instance"});
273                 
274                 assertNotNull(types);
275                 assertArrayEquals(expected.get(), types.get());
276         }
277
278         @Test
279         public void testGetMigrationName() {
280                 String migrationName = migration.getMigrationName();
281
282                 assertNotNull(migrationName);
283                 assertEquals("service-instance-to-configuration", migrationName);
284         }
285 }