fixed JUnits in 3 files
[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-2019 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  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.dg.common.impl;
26
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.Map;
30 import java.util.Set;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Matchers;
35 import org.onap.appc.dg.common.VnfExecutionFlow;
36 import org.onap.appc.dg.dependencymanager.DependencyManager;
37 import org.onap.appc.dg.dependencymanager.exception.DependencyModelNotFound;
38 import org.onap.appc.dg.dependencymanager.impl.DependencyModelFactory;
39 import org.onap.appc.dg.flowbuilder.exception.InvalidDependencyModelException;
40 import org.onap.appc.dg.objects.Node;
41 import org.onap.appc.dg.objects.VnfcDependencyModel;
42 import org.onap.appc.domainmodel.Vnfc;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
44 import org.osgi.framework.FrameworkUtil;
45 import org.powermock.api.mockito.PowerMockito;
46 import org.powermock.core.classloader.annotations.PrepareForTest;
47 import org.powermock.modules.junit4.PowerMockRunner;
48 import org.powermock.reflect.Whitebox;
49 import com.att.eelf.configuration.EELFLogger;
50 import com.att.eelf.configuration.EELFManager;
51 import static org.junit.Assert.assertNotNull;
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         assertNotNull(params);
82     }
83
84     @Test
85     public void testComplexFlow() throws DependencyModelNotFound, InvalidDependencyModelException {
86         Map<String, String> params = prepareParams();
87         SvcLogicContext context = prepareContextForComplexDependency();
88         VnfcDependencyModel dependencyModel = readComplexDependencyModel();
89
90         PowerMockito.mockStatic(DependencyModelFactory.class);
91         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
92
93         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
94         PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
95                 .thenReturn(dependencyModel);
96
97         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
98         Whitebox.setInternalState(vnfExecutionFlow, "logger", logger);
99         vnfExecutionFlow.getVnfExecutionFlowData(params, context);
100         assertNotNull(params);
101     }
102
103     @Test(expected = RuntimeException.class)
104     public void testCycleFlow() throws DependencyModelNotFound, InvalidDependencyModelException {
105         Map<String, String> params = prepareParams();
106         SvcLogicContext context = prepareContextForComplexDependency();
107         VnfcDependencyModel dependencyModel = readCyclicDependencyModel();
108         PowerMockito.mockStatic(DependencyModelFactory.class);
109         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
110
111         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
112         PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
113                 .thenReturn(dependencyModel);
114
115         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
116         Whitebox.setInternalState(vnfExecutionFlow, "logger", logger);
117         vnfExecutionFlow.getVnfExecutionFlowData(params, context);
118     }
119
120     private VnfcDependencyModel readCyclicDependencyModel() {
121
122         Vnfc a = createVnfc("A", "Active-Passive", null, false);
123         Vnfc b = createVnfc("B", "Active-Active", null, false);
124         Vnfc c = createVnfc("C", "Active-Active", null, false);
125         Vnfc d = createVnfc("D", "Active-Active", null, false);
126         Vnfc e = createVnfc("E", "Active-Active", null, false);
127         Vnfc f = createVnfc("F", "Active-Active", null, false);
128         Vnfc g = createVnfc("G", "Active-Active", null, false);
129
130         Node aNode = new Node(a);
131         Node bNode = new Node(b);
132         Node cNode = new Node(c);
133         Node dNode = new Node(d);
134         Node eNode = new Node(e);
135         Node fNode = new Node(f);
136         Node gNode = new Node(g);
137
138         bNode.addParent(a);
139         cNode.addParent(a);
140         cNode.addParent(b);
141
142         bNode.addParent(d);
143         dNode.addParent(c);
144
145         Set<Node<Vnfc>> dependencies = new HashSet<>();
146         dependencies.add(aNode);
147         dependencies.add(bNode);
148         dependencies.add(cNode);
149         dependencies.add(dNode);
150         dependencies.add(eNode);
151         dependencies.add(fNode);
152         dependencies.add(gNode);
153
154         return new VnfcDependencyModel(dependencies);
155
156     }
157
158     private Vnfc createVnfc(String vnfcType, String resilienceType, String vnfcName, boolean mandatory) {
159         Vnfc vnfc = new Vnfc();
160         vnfc.setVnfcType(vnfcType);
161         vnfc.setResilienceType(resilienceType);
162         vnfc.setVnfcName(vnfcName);
163         vnfc.setMandatory(mandatory);
164         return vnfc;
165     }
166
167     private SvcLogicContext prepareContextForComplexDependency() {
168         SvcLogicContext context = new SvcLogicContext();
169         context.setAttribute("input.action-identifiers.vnf-id", "1");
170         context.setAttribute("vnf.type", "vSCP");
171         context.setAttribute("vnf.vnfcCount", "7");
172
173         context.setAttribute("vnf.vnfc[0].name", "A");
174         context.setAttribute("vnf.vnfc[0].type", "A");
175         context.setAttribute("vnf.vnfc[0].vm_count", "2");
176         context.setAttribute("vnf.vnfc[0].vm[0].url", "A1");
177         context.setAttribute("vnf.vnfc[0].vm[1].url", "A2");
178
179         context.setAttribute("vnf.vnfc[1].name", "B");
180         context.setAttribute("vnf.vnfc[1].type", "B");
181         context.setAttribute("vnf.vnfc[1].vm_count", "5");
182         context.setAttribute("vnf.vnfc[1].vm[0].url", "B1");
183         context.setAttribute("vnf.vnfc[1].vm[1].url", "B2");
184         context.setAttribute("vnf.vnfc[1].vm[2].url", "B3");
185         context.setAttribute("vnf.vnfc[1].vm[3].url", "B4");
186         context.setAttribute("vnf.vnfc[1].vm[4].url", "B5");
187
188         context.setAttribute("vnf.vnfc[2].name", "C");
189         context.setAttribute("vnf.vnfc[2].type", "C");
190         context.setAttribute("vnf.vnfc[2].vm_count", "4");
191         context.setAttribute("vnf.vnfc[2].vm[0].url", "C1");
192         context.setAttribute("vnf.vnfc[2].vm[1].url", "C2");
193         context.setAttribute("vnf.vnfc[2].vm[2].url", "C3");
194         context.setAttribute("vnf.vnfc[2].vm[3].url", "C4");
195
196         context.setAttribute("vnf.vnfc[3].name", "D");
197         context.setAttribute("vnf.vnfc[3].type", "D");
198         context.setAttribute("vnf.vnfc[3].vm_count", "3");
199         context.setAttribute("vnf.vnfc[3].vm[0].url", "D1");
200         context.setAttribute("vnf.vnfc[3].vm[1].url", "D2");
201         context.setAttribute("vnf.vnfc[3].vm[2].url", "D3");
202
203         context.setAttribute("vnf.vnfc[4].name", "E");
204         context.setAttribute("vnf.vnfc[4].type", "E");
205         context.setAttribute("vnf.vnfc[4].vm_count", "2");
206         context.setAttribute("vnf.vnfc[4].vm[0].url", "E1");
207         context.setAttribute("vnf.vnfc[4].vm[1].url", "E2");
208
209         context.setAttribute("vnf.vnfc[5].name", "F");
210         context.setAttribute("vnf.vnfc[5].type", "F");
211         context.setAttribute("vnf.vnfc[5].vm_count", "1");
212         context.setAttribute("vnf.vnfc[5].vm[0].url", "F1");
213
214         context.setAttribute("vnf.vnfc[6].name", "G");
215         context.setAttribute("vnf.vnfc[6].type", "G");
216         context.setAttribute("vnf.vnfc[6].vm_count", "1");
217         context.setAttribute("vnf.vnfc[6].vm[0].url", "G1");
218
219         return context;
220     }
221
222     private VnfcDependencyModel readComplexDependencyModel() {
223         Vnfc a = createVnfc("A", "Active-Passive", null, false);
224         Vnfc b = createVnfc("B", "Active-Active", null, false);
225         Vnfc c = createVnfc("C", "Active-Active", null, false);
226         Vnfc d = createVnfc("D", "Active-Active", null, false);
227         Vnfc e = createVnfc("E", "Active-Active", null, false);
228         Vnfc f = createVnfc("F", "Active-Active", null, false);
229         Vnfc g = createVnfc("G", "Active-Active", null, false);
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         Node smpNode = new Node(smp);
269         Node beNode = new Node(be);
270         Node feNode = new Node(fe);
271
272         beNode.addParent(smp);
273         feNode.addParent(be);
274 //        smpNode.addParent(fe);
275
276         Set<Node<Vnfc>> dependencies = new HashSet<>();
277         dependencies.add(smpNode);
278         dependencies.add(feNode);
279         dependencies.add(beNode);
280
281         return new VnfcDependencyModel(dependencies);
282     }
283
284     private Map<String, String> prepareParams() {
285         Map<String, String> params = new HashMap<>();
286         params.put(Constants.DEPENDENCY_TYPE, "RESOURCE");
287         params.put(Constants.FLOW_STRATEGY, "FORWARD");
288
289         params.put(Constants.VNF_TYPE, "vSCP");
290         params.put(Constants.VNF_VERION, "1.00");
291         return params;
292     }
293
294     private SvcLogicContext prepareContext() {
295         SvcLogicContext context = new SvcLogicContext();
296         context.setAttribute("input.action-identifiers.vnf-id", "1");
297         context.setAttribute("vnf.type", "vSCP");
298         context.setAttribute("vnf.vnfcCount", "3");
299
300         context.setAttribute("vnf.vnfc[0].name", "SMPname");
301         context.setAttribute("vnf.vnfc[0].type", "SMP");
302         context.setAttribute("vnf.vnfc[0].vm_count", "2");
303         context.setAttribute("vnf.vnfc[0].vm[0].url", "SMP_URL1");
304         context.setAttribute("vnf.vnfc[0].vm[1].url", "SMP_URL2");
305
306         context.setAttribute("vnf.vnfc[1].name", "BEname");
307         context.setAttribute("vnf.vnfc[1].type", "BE");
308         context.setAttribute("vnf.vnfc[1].vm_count", "5");
309         context.setAttribute("vnf.vnfc[1].vm[0].url", "BE_URL1");
310         context.setAttribute("vnf.vnfc[1].vm[1].url", "BE_URL2");
311         context.setAttribute("vnf.vnfc[1].vm[2].url", "BE_URL3");
312         context.setAttribute("vnf.vnfc[1].vm[3].url", "BE_URL4");
313         context.setAttribute("vnf.vnfc[1].vm[4].url", "BE_URL5");
314
315         context.setAttribute("vnf.vnfc[2].name", "FEname");
316         context.setAttribute("vnf.vnfc[2].type", "FE");
317         context.setAttribute("vnf.vnfc[2].vm_count", "2");
318         context.setAttribute("vnf.vnfc[2].vm[0].url", "FE_URL1");
319         context.setAttribute("vnf.vnfc[2].vm[1].url", "FE_URL2");
320
321         return context;
322     }
323
324     @Test(expected = RuntimeException.class)
325     public void testMissingVnfcTypeInDependencyModel() throws DependencyModelNotFound, InvalidDependencyModelException {
326         Map<String, String> params = prepareParams();
327         SvcLogicContext context = prepareContext();
328         context.setAttribute("vnf.vnfc[3].name", "XEname");
329         context.setAttribute("vnf.vnfc[3].type", "XE");
330         context.setAttribute("vnf.vnfc[3].vm_count", "2");
331         context.setAttribute("vnf.vnfc[3].vm[0].url", "XE_URL1");
332         context.setAttribute("vnf.vnfc[3].vm[1].url", "XE_URL2");
333         context.setAttribute("vnf.vnfcCount", "4");
334
335         VnfcDependencyModel dependencyModel = readDependencyModel();
336
337         PowerMockito.mockStatic(DependencyModelFactory.class);
338         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
339
340         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
341         PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
342                 .thenReturn(dependencyModel);
343
344         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
345         vnfExecutionFlow.getVnfExecutionFlowData(params, context);
346     }
347
348     @Test(expected = RuntimeException.class)
349     public void testMissingMandatoryVnfcTypeInInventoryModel()
350             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()
372             throws DependencyModelNotFound, InvalidDependencyModelException {
373         Map<String, String> params = prepareParams();
374         SvcLogicContext context = prepareContext();
375         VnfcDependencyModel dependencyModel = readDependencyModel();
376
377         Vnfc xe = createVnfc("XE", "Active-Active", null, false);
378         Node xeNode = new Node(xe);
379         dependencyModel.getDependencies().add(xeNode);
380
381         PowerMockito.mockStatic(DependencyModelFactory.class);
382         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
383
384         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
385         PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
386                 .thenReturn(dependencyModel);
387
388         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
389         vnfExecutionFlow.getVnfExecutionFlowData(params, context);
390         assertNotNull(params);
391     }
392
393     @Test
394     public void testMissingOptionalVnfcTypeInInventoryModelWithDependentChild()
395             throws DependencyModelNotFound, InvalidDependencyModelException {
396         Map<String, String> params = prepareParams();
397         SvcLogicContext context = prepareContext();
398         context.setAttribute("vnf.vnfc[3].name", "YEname");
399         context.setAttribute("vnf.vnfc[3].type", "YE");
400         context.setAttribute("vnf.vnfc[3].vm_count", "2");
401         context.setAttribute("vnf.vnfc[3].vm[0].url", "YE_URL1");
402         context.setAttribute("vnf.vnfc[3].vm[1].url", "YE_URL2");
403         context.setAttribute("vnf.vnfcCount", "4");
404
405         VnfcDependencyModel dependencyModel = readDependencyModel();
406
407         Vnfc xe = createVnfc("XE", "Active-Active", null, false);
408         Vnfc ye = createVnfc("YE", "Active-Active", null, true);
409         Node xeNode = new Node(xe);
410         Node yeNode = new Node(ye);
411         yeNode.addParent(xe);
412
413         dependencyModel.getDependencies().add(yeNode);
414         dependencyModel.getDependencies().add(xeNode);
415
416         PowerMockito.mockStatic(DependencyModelFactory.class);
417         DependencyManager dependencyManager = PowerMockito.mock(DependencyManager.class);
418
419         PowerMockito.when(DependencyModelFactory.createDependencyManager()).thenReturn(dependencyManager);
420         PowerMockito.when(dependencyManager.getVnfcDependencyModel(Matchers.any(), Matchers.any()))
421                 .thenReturn(dependencyModel);
422
423         VnfExecutionFlow vnfExecutionFlow = new VnfExecutionFlowImpl();
424         vnfExecutionFlow.getVnfExecutionFlowData(params, context);
425         assertNotNull(params);
426     }
427 }