Merge "Enable DeleteChildService functionality" into recursive-orch
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / workflow / tasks / listeners / SkipCDSBuildingBlockListenerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2020  Tech Mahindra
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.workflow.tasks.listeners;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.Mockito.when;
25 import java.util.ArrayList;
26 import java.util.List;
27 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.InjectMocks;
32 import org.mockito.Mock;
33 import org.mockito.junit.MockitoJUnitRunner;
34 import org.onap.so.bpmn.common.BBConstants;
35 import org.onap.so.bpmn.common.BuildingBlockExecution;
36 import org.onap.so.bpmn.common.DelegateExecutionImpl;
37 import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
38 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
39 import org.onap.so.db.catalog.beans.PnfResourceCustomization;
40 import org.onap.so.db.catalog.beans.Service;
41 import org.onap.so.db.catalog.beans.VfModuleCustomization;
42 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
43 import org.onap.so.db.catalog.client.CatalogDbClient;
44 import org.onap.so.serviceinstancebeans.ModelInfo;
45 import org.onap.so.serviceinstancebeans.RequestDetails;
46
47 @RunWith(MockitoJUnitRunner.Silent.class)
48 public class SkipCDSBuildingBlockListenerTest {
49
50     private static final String SERVICE_SCOPE = "service";
51     private static final String VNF_SCOPE = "VNF";
52     private static final String VF_SCOPE = "VFModule";
53     private static final String PNF_SCOPE = "pnf";
54     private static final String TEST_MODELUUID = "123456789";
55     private static final String VNF_TEST_ACTION = "VnfConfigAssign";
56     private static final String VFModule_TEST_ACTION = "VfModuleConfigAssign";
57     private static final String PNFModule_TEST_ACTION = "config-assign";
58     private static final String MODELCUSTOMIZATIONUUID = "123456789";
59     private static final String BBNAME = "ControllerExecutionBB";
60     private static final boolean ISFIRST = true;
61
62     private int actual;
63     private List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
64     private List<VnfResourceCustomization> vnfResourceCustomization;
65     private List<VfModuleCustomization> vfModuleCustomization;
66     private ExecuteBuildingBlock executeBuildingBlock = new ExecuteBuildingBlock();
67     private RequestDetails reqDetail = new RequestDetails();
68     private BuildingBlockExecution buildingBlockExecution = new DelegateExecutionImpl(new DelegateExecutionFake());
69     private VnfResourceCustomization vnfCust = new VnfResourceCustomization();
70     private VfModuleCustomization vfCust = new VfModuleCustomization();
71     private PnfResourceCustomization pnfResourceCustomization = new PnfResourceCustomization();
72     private BuildingBlock buildingBlock = new BuildingBlock();
73
74     @InjectMocks
75     private SkipCDSBuildingBlockListener skipCDSBuildingBlockListener;
76     @Mock
77     private CatalogDbClient catalogDbClient;
78
79     @Before
80     public void before() {
81         ModelInfo model = new ModelInfo();
82         model.setModelUuid(TEST_MODELUUID);
83         reqDetail.setModelInfo(model);
84         executeBuildingBlock.setRequestDetails(reqDetail);
85     }
86
87     @Test
88     public void testTrigger() {
89         BuildingBlockExecution execution = new DelegateExecutionImpl(new DelegateExecutionFake());
90         skipCDSBuildingBlockListener.shouldRunFor(BBNAME, ISFIRST, execution);
91         assertEquals("ControllerExecutionBB", BBNAME);
92     }
93
94     @Test
95     public void testSkipCDSforService() {
96         setBuildingBlockAndCurrentSequence(SERVICE_SCOPE, "service-config-assign", 0);
97         Service service = new Service();
98         when(catalogDbClient.getServiceByID(TEST_MODELUUID)).thenReturn(service);
99
100         skipCDSBuildingBlockListener.run(flowsToExecute, executeBuildingBlock, buildingBlockExecution);
101
102         actual = buildingBlockExecution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
103         assertEquals(1, actual);
104     }
105
106     @Test
107     public void testProcessForVNFToSkipCDSBB() {
108         // given
109         setBuildingBlockAndCurrentSequence(VNF_SCOPE, VNF_TEST_ACTION, 0);
110         vnfResourceCustomization = getVnfResourceCustomizationList(true);
111
112         when(catalogDbClient.getVnfResourceCustomizationByModelUuid(
113                 executeBuildingBlock.getRequestDetails().getModelInfo().getModelUuid()))
114                         .thenReturn(vnfResourceCustomization);
115         when(catalogDbClient.findVnfResourceCustomizationInList(executeBuildingBlock.getBuildingBlock().getKey(),
116                 vnfResourceCustomization)).thenReturn(vnfCust);
117
118         // when
119         skipCDSBuildingBlockListener.run(flowsToExecute, executeBuildingBlock, buildingBlockExecution);
120
121         // then
122         actual = buildingBlockExecution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
123         assertEquals(1, actual);
124
125     }
126
127     @Test
128     public void testProcessForVNFNotToSkipCDSBB() {
129         // given
130         setBuildingBlockAndCurrentSequence(VNF_SCOPE, VNF_TEST_ACTION, 0);
131         vnfResourceCustomization = getVnfResourceCustomizationList(false);
132
133         when(catalogDbClient.getVnfResourceCustomizationByModelUuid(
134                 executeBuildingBlock.getRequestDetails().getModelInfo().getModelUuid()))
135                         .thenReturn(vnfResourceCustomization);
136         when(catalogDbClient.findVnfResourceCustomizationInList(executeBuildingBlock.getBuildingBlock().getKey(),
137                 vnfResourceCustomization)).thenReturn(vnfCust);
138
139         // when
140         skipCDSBuildingBlockListener.run(flowsToExecute, executeBuildingBlock, buildingBlockExecution);
141
142         // then
143         actual = buildingBlockExecution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
144         assertEquals(0, actual);
145
146     }
147
148
149     @Test
150     public void testProcessForVFToSkipCDSBB() {
151         // given
152         setBuildingBlockAndCurrentSequence(VF_SCOPE, VFModule_TEST_ACTION, 0);
153         vfModuleCustomization = getVfModuleCustomizationList(true);
154
155         when(catalogDbClient
156                 .getVfModuleCustomizationByModelCuztomizationUUID(executeBuildingBlock.getBuildingBlock().getKey()))
157                         .thenReturn(vfCust);
158
159         // when
160         skipCDSBuildingBlockListener.run(flowsToExecute, executeBuildingBlock, buildingBlockExecution);
161
162         // then
163         actual = buildingBlockExecution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
164         assertEquals(1, actual);
165
166     }
167
168     @Test
169     public void testProcessForVFNotToSkipCDSBB() {
170         // given
171         setBuildingBlockAndCurrentSequence(VF_SCOPE, VFModule_TEST_ACTION, 0);
172         vfModuleCustomization = getVfModuleCustomizationList(false);
173
174         when(catalogDbClient
175                 .getVfModuleCustomizationByModelCuztomizationUUID(executeBuildingBlock.getBuildingBlock().getKey()))
176                         .thenReturn(vfCust);
177
178         // when
179         skipCDSBuildingBlockListener.run(flowsToExecute, executeBuildingBlock, buildingBlockExecution);
180
181         // then
182         actual = buildingBlockExecution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
183         assertEquals(0, actual);
184
185     }
186
187     @Test
188     public void testProcessForPNFToSkipCDSBB() {
189         // given
190         setBuildingBlockAndCurrentSequence(PNF_SCOPE, PNFModule_TEST_ACTION, 0);
191         pnfResourceCustomization = getPnfResourceCustomization(true);
192
193         when(catalogDbClient
194                 .getPnfResourceCustomizationByModelCustomizationUUID(executeBuildingBlock.getBuildingBlock().getKey()))
195                         .thenReturn(pnfResourceCustomization);
196
197         // when
198         skipCDSBuildingBlockListener.run(flowsToExecute, executeBuildingBlock, buildingBlockExecution);
199
200         // then
201         actual = buildingBlockExecution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
202         assertEquals(1, actual);
203
204     }
205
206     @Test
207     public void testProcessForPNFNotToSkipCDSBB() {
208         // given
209         setBuildingBlockAndCurrentSequence(PNF_SCOPE, PNFModule_TEST_ACTION, 0);
210         pnfResourceCustomization = getPnfResourceCustomization(false);
211
212         when(catalogDbClient
213                 .getPnfResourceCustomizationByModelCustomizationUUID(executeBuildingBlock.getBuildingBlock().getKey()))
214                         .thenReturn(pnfResourceCustomization);
215
216         // when
217         skipCDSBuildingBlockListener.run(flowsToExecute, executeBuildingBlock, buildingBlockExecution);
218
219         // then
220         actual = buildingBlockExecution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
221         assertEquals(0, actual);
222
223     }
224
225     /**
226      * setting scope action in buildingBlock and BB current sequence in BuildingBlockExecution
227      *
228      * @param scope
229      * @param action
230      * @param squence
231      */
232     private void setBuildingBlockAndCurrentSequence(String scope, String action, int sequence) {
233         buildingBlock.setBpmnScope(scope);
234         buildingBlock.setBpmnAction(action);
235         buildingBlock.setBpmnFlowName("ControllerExecutionBB");
236         buildingBlock.setKey(MODELCUSTOMIZATIONUUID);
237         executeBuildingBlock.setBuildingBlock(buildingBlock);
238         buildingBlockExecution.setVariable(BBConstants.G_CURRENT_SEQUENCE, sequence);
239
240     }
241
242     private List<VnfResourceCustomization> getVnfResourceCustomizationList(boolean setSkippost) {
243         List<VnfResourceCustomization> vnfResourceCustomizations = new ArrayList<>();
244         vnfCust.setModelCustomizationUUID(MODELCUSTOMIZATIONUUID);
245         vnfCust.setSkipPostInstConf(setSkippost);
246         vnfResourceCustomizations.add(vnfCust);
247         return vnfResourceCustomizations;
248     }
249
250     private List<VfModuleCustomization> getVfModuleCustomizationList(boolean setSkippost) {
251         List<VfModuleCustomization> vfModuleCustomizations = new ArrayList<>();
252         vfCust.setModelCustomizationUUID(MODELCUSTOMIZATIONUUID);
253         vfCust.setSkipPostInstConf(setSkippost);
254         vfModuleCustomizations.add(vfCust);
255         return vfModuleCustomizations;
256     }
257
258     private PnfResourceCustomization getPnfResourceCustomization(boolean setSkippost) {
259         PnfResourceCustomization pnfResourceCustomization = new PnfResourceCustomization();
260         pnfResourceCustomization.setModelCustomizationUUID(MODELCUSTOMIZATIONUUID);
261         pnfResourceCustomization.setSkipPostInstConf(setSkippost);
262         return pnfResourceCustomization;
263     }
264
265 }