Merge of new rebased code
[appc.git] / appc-dg / appc-dg-shared / appc-dg-common / src / test / java / org / openecomp / appc / dg / common / impl / TestVnfExecutionFlowImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.appc.dg.common.impl;
23
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import org.openecomp.sdnc.sli.SvcLogicContext;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Matchers;
31 import org.openecomp.appc.dg.common.VnfExecutionFlow;
32 import org.openecomp.appc.dg.common.impl.Constants;
33 import org.openecomp.appc.dg.common.impl.VnfExecutionFlowImpl;
34 import org.openecomp.appc.dg.dependencymanager.DependencyManager;
35 import org.openecomp.appc.dg.dependencymanager.exception.DependencyModelNotFound;
36 import org.openecomp.appc.dg.dependencymanager.impl.DependencyModelFactory;
37 import org.openecomp.appc.dg.flowbuilder.exception.InvalidDependencyModel;
38 import org.openecomp.appc.dg.objects.DependencyTypes;
39 import org.openecomp.appc.dg.objects.Node;
40 import org.openecomp.appc.dg.objects.VnfcDependencyModel;
41 import org.openecomp.appc.domainmodel.Vnfc;
42 import org.openecomp.appc.metadata.objects.DependencyModelIdentifier;
43 import org.osgi.framework.FrameworkUtil;
44 import org.powermock.api.mockito.PowerMockito;
45 import org.powermock.core.classloader.annotations.PrepareForTest;
46 import org.powermock.modules.junit4.PowerMockRunner;
47
48 import java.util.HashMap;
49 import java.util.HashSet;
50 import java.util.Map;
51 import java.util.Set;
52
53 @RunWith(PowerMockRunner.class)
54 @PrepareForTest({TestVnfExecutionFlowImpl.class, FrameworkUtil.class,DependencyManager.class,DependencyModelFactory.class})
55 public class TestVnfExecutionFlowImpl {
56
57     private static final EELFLogger logger = EELFManager.getInstance().getLogger(TestVnfExecutionFlowImpl.class);
58
59     @Before
60     public void setUp() {
61         logger.setLevel(EELFLogger.Level.DEBUG);
62     }
63
64     @Test
65     public void testPositiveFlow() throws DependencyModelNotFound {
66         Map<String, String> params = prepareParams();
67         SvcLogicContext context = prepareContext();
68         VnfcDependencyModel dependencyModel = readDependencyModel();
69
70         PowerMockito.mockStatic(DependencyModelFactory.class);
71         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
72
73         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
74         PowerMockito.when(dependencyManager.getVnfcDependencyModel((
75                 DependencyModelIdentifier) Matchers.any(),(DependencyTypes) Matchers.any()))
76                 .thenReturn(dependencyModel);
77
78         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
79         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
80     }
81
82     @Test
83     public void testComplexFlow() throws DependencyModelNotFound {
84         Map<String, String> params = prepareParams();
85         SvcLogicContext context = prepareContextForComplexDependency();
86         VnfcDependencyModel dependencyModel = readComplexDependencyModel();
87
88         PowerMockito.mockStatic(DependencyModelFactory.class);
89         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
90
91         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
92         PowerMockito.when(dependencyManager.getVnfcDependencyModel((
93                 DependencyModelIdentifier) Matchers.any(),(DependencyTypes) Matchers.any()))
94                 .thenReturn(dependencyModel);
95
96         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
97         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
98     }
99
100     @Test(expected = InvalidDependencyModel.class)
101     public void testCycleFlow() throws DependencyModelNotFound {
102         Map<String, String> params = prepareParams();
103         SvcLogicContext context = prepareContextForComplexDependency();
104         VnfcDependencyModel dependencyModel = readCyclicDependencyModel();
105         PowerMockito.mockStatic(DependencyModelFactory.class);
106         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
107
108         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
109         PowerMockito.when(dependencyManager.getVnfcDependencyModel((
110                 DependencyModelIdentifier) Matchers.any(),(DependencyTypes) Matchers.any()))
111                 .thenReturn(dependencyModel);
112
113         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
114         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
115     }
116
117     private VnfcDependencyModel readCyclicDependencyModel() {
118
119         Vnfc a = new Vnfc("A","Active-Passive",null);
120         Vnfc b = new Vnfc("B","Active-Active",null);
121         Vnfc c = new Vnfc("C","Active-Active",null);
122         Vnfc d = new Vnfc("D","Active-Active",null);
123         Vnfc e = new Vnfc("E","Active-Active",null);
124         Vnfc f = new Vnfc("F","Active-Active",null);
125         Vnfc g = new Vnfc("G","Active-Active",null);
126
127         Node aNode = new Node(a);
128         Node bNode = new Node(b);
129         Node cNode = new Node(c);
130         Node dNode = new Node(d);
131         Node eNode = new Node(e);
132         Node fNode = new Node(f);
133         Node gNode = new Node(g);
134
135         bNode.addParent(a);
136         cNode.addParent(a);
137         cNode.addParent(b);
138
139         bNode.addParent(d);
140         dNode.addParent(c);
141
142         Set<Node<Vnfc>> dependencies = new HashSet<>();
143         dependencies.add(aNode);
144         dependencies.add(bNode);
145         dependencies.add(cNode);
146         dependencies.add(dNode);
147         dependencies.add(eNode);
148         dependencies.add(fNode);
149         dependencies.add(gNode);
150
151         return new VnfcDependencyModel(dependencies);
152
153     }
154
155     private SvcLogicContext prepareContextForComplexDependency() {
156         SvcLogicContext context = new SvcLogicContext();
157         context.setAttribute("input.action-identifiers.vnf-id","1");
158         context.setAttribute("vnf.type","vSCP");
159         context.setAttribute("vnf.vnfcCount","7");
160
161         context.setAttribute("vnf.vnfc[0].name","A");
162         context.setAttribute("vnf.vnfc[0].type","A");
163         context.setAttribute("vnf.vnfc[0].vm_count","2");
164         context.setAttribute("vnf.vnfc[0].vm[0].url","A1");
165         context.setAttribute("vnf.vnfc[0].vm[1].url","A2");
166
167         context.setAttribute("vnf.vnfc[1].name","B");
168         context.setAttribute("vnf.vnfc[1].type","B");
169         context.setAttribute("vnf.vnfc[1].vm_count","5");
170         context.setAttribute("vnf.vnfc[1].vm[0].url","B1");
171         context.setAttribute("vnf.vnfc[1].vm[1].url","B2");
172         context.setAttribute("vnf.vnfc[1].vm[2].url","B3");
173         context.setAttribute("vnf.vnfc[1].vm[3].url","B4");
174         context.setAttribute("vnf.vnfc[1].vm[4].url","B5");
175
176         context.setAttribute("vnf.vnfc[2].name","C");
177         context.setAttribute("vnf.vnfc[2].type","C");
178         context.setAttribute("vnf.vnfc[2].vm_count","4");
179         context.setAttribute("vnf.vnfc[2].vm[0].url","C1");
180         context.setAttribute("vnf.vnfc[2].vm[1].url","C2");
181         context.setAttribute("vnf.vnfc[2].vm[2].url","C3");
182         context.setAttribute("vnf.vnfc[2].vm[3].url","C4");
183
184         context.setAttribute("vnf.vnfc[3].name","D");
185         context.setAttribute("vnf.vnfc[3].type","D");
186         context.setAttribute("vnf.vnfc[3].vm_count","3");
187         context.setAttribute("vnf.vnfc[3].vm[0].url","D1");
188         context.setAttribute("vnf.vnfc[3].vm[1].url","D2");
189         context.setAttribute("vnf.vnfc[3].vm[2].url","D3");
190
191         context.setAttribute("vnf.vnfc[4].name","E");
192         context.setAttribute("vnf.vnfc[4].type","E");
193         context.setAttribute("vnf.vnfc[4].vm_count","2");
194         context.setAttribute("vnf.vnfc[4].vm[0].url","E1");
195         context.setAttribute("vnf.vnfc[4].vm[1].url","E2");
196
197         context.setAttribute("vnf.vnfc[5].name","F");
198         context.setAttribute("vnf.vnfc[5].type","F");
199         context.setAttribute("vnf.vnfc[5].vm_count","1");
200         context.setAttribute("vnf.vnfc[5].vm[0].url","F1");
201
202         context.setAttribute("vnf.vnfc[6].name","G");
203         context.setAttribute("vnf.vnfc[6].type","G");
204         context.setAttribute("vnf.vnfc[6].vm_count","1");
205         context.setAttribute("vnf.vnfc[6].vm[0].url","G1");
206
207
208         return context;
209     }
210
211     private VnfcDependencyModel readComplexDependencyModel() {
212         Vnfc a = new Vnfc("A","Active-Passive",null);
213         Vnfc b = new Vnfc("B","Active-Active",null);
214         Vnfc c = new Vnfc("C","Active-Active",null);
215         Vnfc d = new Vnfc("D","Active-Active",null);
216         Vnfc e = new Vnfc("E","Active-Active",null);
217         Vnfc f = new Vnfc("F","Active-Active",null);
218         Vnfc g = new Vnfc("G","Active-Active",null);
219
220
221         Node aNode = new Node(a);
222         Node bNode = new Node(b);
223         Node cNode = new Node(c);
224         Node dNode = new Node(d);
225         Node eNode = new Node(e);
226         Node fNode = new Node(f);
227         Node gNode = new Node(g);
228
229         bNode.addParent(a);
230         cNode.addParent(a);
231
232         dNode.addParent(b);
233         eNode.addParent(b);
234         gNode.addParent(b);
235
236         fNode.addParent(c);
237
238         gNode.addParent(f);
239
240         Set<Node<Vnfc>> dependencies = new HashSet<>();
241         dependencies.add(aNode);
242         dependencies.add(bNode);
243         dependencies.add(cNode);
244         dependencies.add(dNode);
245         dependencies.add(eNode);
246         dependencies.add(fNode);
247         dependencies.add(gNode);
248
249         return new VnfcDependencyModel(dependencies);
250     }
251
252     private VnfcDependencyModel readDependencyModel() {
253
254         Vnfc smp = new Vnfc("SMP","Active-Passive",null);
255         Vnfc be = new Vnfc("BE","Active-Active",null);
256         Vnfc fe = new Vnfc("FE","Active-Active",null);
257
258
259         Node smpNode = new Node(smp);
260         Node beNode = new Node(be);
261         Node feNode = new Node(fe);
262
263         beNode.addParent(smp);
264         feNode.addParent(be);
265 //        smpNode.addParent(fe);
266
267         Set<Node<Vnfc>> dependencies = new HashSet<>();
268         dependencies.add(smpNode);
269         dependencies.add(feNode);
270         dependencies.add(beNode);
271
272         return new VnfcDependencyModel(dependencies);
273     }
274
275     private Map<String, String> prepareParams() {
276         Map<String,String> params = new HashMap<>();
277         params.put(Constants.DEPENDENCY_TYPE,"RESOURCE");
278         params.put(Constants.FLOW_STRATEGY,"FORWARD");
279
280         params.put(Constants.VNF_TYPE,"vSCP");
281         params.put(Constants.VNF_VERION,"1.00");
282         return params;
283     }
284
285     private SvcLogicContext prepareContext() {
286         SvcLogicContext context = new SvcLogicContext();
287         context.setAttribute("input.action-identifiers.vnf-id","1");
288         context.setAttribute("vnf.type","vSCP");
289         context.setAttribute("vnf.vnfcCount","3");
290
291         context.setAttribute("vnf.vnfc[0].name","SMPname");
292         context.setAttribute("vnf.vnfc[0].type","SMP");
293         context.setAttribute("vnf.vnfc[0].vm_count","2");
294         context.setAttribute("vnf.vnfc[0].vm[0].url","SMP_URL1");
295         context.setAttribute("vnf.vnfc[0].vm[1].url","SMP_URL2");
296
297         context.setAttribute("vnf.vnfc[1].name","BEname");
298         context.setAttribute("vnf.vnfc[1].type","BE");
299         context.setAttribute("vnf.vnfc[1].vm_count","5");
300         context.setAttribute("vnf.vnfc[1].vm[0].url","BE_URL1");
301         context.setAttribute("vnf.vnfc[1].vm[1].url","BE_URL2");
302         context.setAttribute("vnf.vnfc[1].vm[2].url","BE_URL3");
303         context.setAttribute("vnf.vnfc[1].vm[3].url","BE_URL4");
304         context.setAttribute("vnf.vnfc[1].vm[4].url","BE_URL5");
305
306         context.setAttribute("vnf.vnfc[2].name","FEname");
307         context.setAttribute("vnf.vnfc[2].type","FE");
308         context.setAttribute("vnf.vnfc[2].vm_count","2");
309         context.setAttribute("vnf.vnfc[2].vm[0].url","FE_URL1");
310         context.setAttribute("vnf.vnfc[2].vm[1].url","FE_URL2");
311
312         return context;
313     }
314
315     @Test(expected = RuntimeException.class)
316     public void testMissingVnfcTypeInDependencyModel() throws DependencyModelNotFound {
317         Map<String, String> params = prepareParams();
318         SvcLogicContext context = prepareContext();
319         context.setAttribute("vnf.vnfc[3].name","XEname");
320         context.setAttribute("vnf.vnfc[3].type","XE");
321         context.setAttribute("vnf.vnfc[3].vm_count","2");
322         context.setAttribute("vnf.vnfc[3].vm[0].url","XE_URL1");
323         context.setAttribute("vnf.vnfc[3].vm[1].url","XE_URL2");
324         context.setAttribute("vnf.vnfcCount","4");
325
326         VnfcDependencyModel dependencyModel = readDependencyModel();
327
328         PowerMockito.mockStatic(DependencyModelFactory.class);
329         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
330
331         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
332         PowerMockito.when(dependencyManager.getVnfcDependencyModel((
333                 DependencyModelIdentifier) Matchers.any(),(DependencyTypes) Matchers.any()))
334                 .thenReturn(dependencyModel);
335
336         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
337         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
338     }
339
340     @Test(expected = RuntimeException.class)
341     public void testMissingMandatoryVnfcTypeInInventoryModel() throws DependencyModelNotFound {
342         Map<String, String> params = prepareParams();
343         SvcLogicContext context = prepareContext();
344         VnfcDependencyModel dependencyModel = readDependencyModel();
345
346         Vnfc xe = new Vnfc("XE","Active-Active",null, true);
347         Node xeNode = new Node(xe);
348         dependencyModel.getDependencies().add(xeNode);
349
350         PowerMockito.mockStatic(DependencyModelFactory.class);
351         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
352
353         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
354         PowerMockito.when(dependencyManager.getVnfcDependencyModel((
355                 DependencyModelIdentifier) Matchers.any(),(DependencyTypes) Matchers.any()))
356                 .thenReturn(dependencyModel);
357
358         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
359         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
360     }
361
362     @Test
363     public void testMissingOptionalVnfcTypeInInventoryModel() throws DependencyModelNotFound {
364         Map<String, String> params = prepareParams();
365         SvcLogicContext context = prepareContext();
366         VnfcDependencyModel dependencyModel = readDependencyModel();
367
368         Vnfc xe = new Vnfc("XE","Active-Active",null, false);
369         Node xeNode = new Node(xe);
370         dependencyModel.getDependencies().add(xeNode);
371
372         PowerMockito.mockStatic(DependencyModelFactory.class);
373         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
374
375         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
376         PowerMockito.when(dependencyManager.getVnfcDependencyModel((
377                 DependencyModelIdentifier) Matchers.any(),(DependencyTypes) Matchers.any()))
378                 .thenReturn(dependencyModel);
379
380         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
381         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
382     }
383
384     @Test
385     public void testMissingOptionalVnfcTypeInInventoryModelWithDependentChild() throws DependencyModelNotFound {
386         Map<String, String> params = prepareParams();
387         SvcLogicContext context = prepareContext();
388         context.setAttribute("vnf.vnfc[3].name","YEname");
389         context.setAttribute("vnf.vnfc[3].type","YE");
390         context.setAttribute("vnf.vnfc[3].vm_count","2");
391         context.setAttribute("vnf.vnfc[3].vm[0].url","YE_URL1");
392         context.setAttribute("vnf.vnfc[3].vm[1].url","YE_URL2");
393         context.setAttribute("vnf.vnfcCount","4");
394
395         VnfcDependencyModel dependencyModel = readDependencyModel();
396
397         Vnfc xe = new Vnfc("XE","Active-Active",null, false);
398         Vnfc ye = new Vnfc("YE","Active-Active",null, true);
399         Node xeNode = new Node(xe);
400         Node yeNode = new Node(ye);
401         yeNode.addParent(xe);
402
403         dependencyModel.getDependencies().add(yeNode);
404         dependencyModel.getDependencies().add(xeNode);
405
406         PowerMockito.mockStatic(DependencyModelFactory.class);
407         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
408
409         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
410         PowerMockito.when(dependencyManager.getVnfcDependencyModel((
411                 DependencyModelIdentifier) Matchers.any(),(DependencyTypes) Matchers.any()))
412                 .thenReturn(dependencyModel);
413
414         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
415         vnfExecutionFlow.getVnfExecutionFlowData(params,context);
416     }
417 }