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