fdf4d36c893ff22c62e1d2b8764929cf79c4d2b4
[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.VfModuleCustomization;
41 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
42 import org.onap.so.db.catalog.client.CatalogDbClient;
43 import org.onap.so.serviceinstancebeans.ModelInfo;
44 import org.onap.so.serviceinstancebeans.RequestDetails;
45
46 @RunWith(MockitoJUnitRunner.Silent.class)
47 public class SkipCDSBuildingBlockListenerTest {
48
49     private static final String VNF_SCOPE = "VNF";
50     private static final String VF_SCOPE = "VFModule";
51     private static final String PNF_SCOPE = "pnf";
52     private static final String TEST_MODELUUID = "123456789";
53     private static final String VNF_TEST_ACTION = "VnfConfigAssign";
54     private static final String VFModule_TEST_ACTION = "VfModuleConfigAssign";
55     private static final String PNFModule_TEST_ACTION = "config-assign";
56     private static final String MODELCUSTOMIZATIONUUID = "123456789";
57     private static final String BBNAME = "ControllerExecutionBB";
58     private static final boolean ISFIRST = true;
59
60     private int actual;
61     private List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
62     private List<VnfResourceCustomization> vnfResourceCustomization;
63     private List<VfModuleCustomization> vfModuleCustomization;
64     private ExecuteBuildingBlock executeBuildingBlock = new ExecuteBuildingBlock();
65     private RequestDetails reqDetail = new RequestDetails();
66     private BuildingBlockExecution buildingBlockExecution = new DelegateExecutionImpl(new DelegateExecutionFake());
67     private VnfResourceCustomization vnfCust = new VnfResourceCustomization();
68     private VfModuleCustomization vfCust = new VfModuleCustomization();
69     private PnfResourceCustomization pnfResourceCustomization = new PnfResourceCustomization();
70     private BuildingBlock buildingBlock = new BuildingBlock();
71
72     @InjectMocks
73     private SkipCDSBuildingBlockListener skipCDSBuildingBlockListener;
74     @Mock
75     private CatalogDbClient catalogDbClient;
76
77     @Before
78     public void before() {
79         ModelInfo model = new ModelInfo();
80         model.setModelUuid(TEST_MODELUUID);
81         reqDetail.setModelInfo(model);
82         executeBuildingBlock.setRequestDetails(reqDetail);
83     }
84
85     @Test
86     public void testTrigger() {
87         BuildingBlockExecution execution = new DelegateExecutionImpl(new DelegateExecutionFake());
88         skipCDSBuildingBlockListener.shouldRunFor(BBNAME, ISFIRST, execution);
89         assertEquals("ControllerExecutionBB", BBNAME);
90     }
91
92     @Test
93     public void testProcessForVNFToSkipCDSBB() {
94         // given
95         setBuildingBlockAndCurrentSequence(VNF_SCOPE, VNF_TEST_ACTION, 0);
96         vnfResourceCustomization = getVnfResourceCustomizationList(true);
97
98         when(catalogDbClient.getVnfResourceCustomizationByModelUuid(
99                 executeBuildingBlock.getRequestDetails().getModelInfo().getModelUuid()))
100                         .thenReturn(vnfResourceCustomization);
101         when(catalogDbClient.findVnfResourceCustomizationInList(executeBuildingBlock.getBuildingBlock().getKey(),
102                 vnfResourceCustomization)).thenReturn(vnfCust);
103
104         // when
105         skipCDSBuildingBlockListener.run(flowsToExecute, executeBuildingBlock, buildingBlockExecution);
106
107         // then
108         actual = buildingBlockExecution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
109         assertEquals(1, actual);
110
111     }
112
113     @Test
114     public void testProcessForVNFNotToSkipCDSBB() {
115         // given
116         setBuildingBlockAndCurrentSequence(VNF_SCOPE, VNF_TEST_ACTION, 0);
117         vnfResourceCustomization = getVnfResourceCustomizationList(false);
118
119         when(catalogDbClient.getVnfResourceCustomizationByModelUuid(
120                 executeBuildingBlock.getRequestDetails().getModelInfo().getModelUuid()))
121                         .thenReturn(vnfResourceCustomization);
122         when(catalogDbClient.findVnfResourceCustomizationInList(executeBuildingBlock.getBuildingBlock().getKey(),
123                 vnfResourceCustomization)).thenReturn(vnfCust);
124
125         // when
126         skipCDSBuildingBlockListener.run(flowsToExecute, executeBuildingBlock, buildingBlockExecution);
127
128         // then
129         actual = buildingBlockExecution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
130         assertEquals(0, actual);
131
132     }
133
134
135     @Test
136     public void testProcessForVFToSkipCDSBB() {
137         // given
138         setBuildingBlockAndCurrentSequence(VF_SCOPE, VFModule_TEST_ACTION, 0);
139         vfModuleCustomization = getVfModuleCustomizationList(true);
140
141         when(catalogDbClient
142                 .getVfModuleCustomizationByModelCuztomizationUUID(executeBuildingBlock.getBuildingBlock().getKey()))
143                         .thenReturn(vfCust);
144
145         // when
146         skipCDSBuildingBlockListener.run(flowsToExecute, executeBuildingBlock, buildingBlockExecution);
147
148         // then
149         actual = buildingBlockExecution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
150         assertEquals(1, actual);
151
152     }
153
154     @Test
155     public void testProcessForVFNotToSkipCDSBB() {
156         // given
157         setBuildingBlockAndCurrentSequence(VF_SCOPE, VFModule_TEST_ACTION, 0);
158         vfModuleCustomization = getVfModuleCustomizationList(false);
159
160         when(catalogDbClient
161                 .getVfModuleCustomizationByModelCuztomizationUUID(executeBuildingBlock.getBuildingBlock().getKey()))
162                         .thenReturn(vfCust);
163
164         // when
165         skipCDSBuildingBlockListener.run(flowsToExecute, executeBuildingBlock, buildingBlockExecution);
166
167         // then
168         actual = buildingBlockExecution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
169         assertEquals(0, actual);
170
171     }
172
173     @Test
174     public void testProcessForPNFToSkipCDSBB() {
175         // given
176         setBuildingBlockAndCurrentSequence(PNF_SCOPE, PNFModule_TEST_ACTION, 0);
177         pnfResourceCustomization = getPnfResourceCustomization(true);
178
179         when(catalogDbClient
180                 .getPnfResourceCustomizationByModelCustomizationUUID(executeBuildingBlock.getBuildingBlock().getKey()))
181                         .thenReturn(pnfResourceCustomization);
182
183         // when
184         skipCDSBuildingBlockListener.run(flowsToExecute, executeBuildingBlock, buildingBlockExecution);
185
186         // then
187         actual = buildingBlockExecution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
188         assertEquals(1, actual);
189
190     }
191
192     @Test
193     public void testProcessForPNFNotToSkipCDSBB() {
194         // given
195         setBuildingBlockAndCurrentSequence(PNF_SCOPE, PNFModule_TEST_ACTION, 0);
196         pnfResourceCustomization = getPnfResourceCustomization(false);
197
198         when(catalogDbClient
199                 .getPnfResourceCustomizationByModelCustomizationUUID(executeBuildingBlock.getBuildingBlock().getKey()))
200                         .thenReturn(pnfResourceCustomization);
201
202         // when
203         skipCDSBuildingBlockListener.run(flowsToExecute, executeBuildingBlock, buildingBlockExecution);
204
205         // then
206         actual = buildingBlockExecution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
207         assertEquals(0, actual);
208
209     }
210
211     /**
212      * setting scope action in buildingBlock and BB current sequence in BuildingBlockExecution
213      *
214      * @param scope
215      * @param action
216      * @param squence
217      */
218     private void setBuildingBlockAndCurrentSequence(String scope, String action, int sequence) {
219         buildingBlock.setBpmnScope(scope);
220         buildingBlock.setBpmnAction(action);
221         buildingBlock.setBpmnFlowName("ControllerExecutionBB");
222         buildingBlock.setKey(MODELCUSTOMIZATIONUUID);
223         executeBuildingBlock.setBuildingBlock(buildingBlock);
224         buildingBlockExecution.setVariable(BBConstants.G_CURRENT_SEQUENCE, sequence);
225
226     }
227
228     private List<VnfResourceCustomization> getVnfResourceCustomizationList(boolean setSkippost) {
229         List<VnfResourceCustomization> vnfResourceCustomizations = new ArrayList<>();
230         vnfCust.setModelCustomizationUUID(MODELCUSTOMIZATIONUUID);
231         vnfCust.setSkipPostInstConf(setSkippost);
232         vnfResourceCustomizations.add(vnfCust);
233         return vnfResourceCustomizations;
234     }
235
236     private List<VfModuleCustomization> getVfModuleCustomizationList(boolean setSkippost) {
237         List<VfModuleCustomization> vfModuleCustomizations = new ArrayList<>();
238         vfCust.setModelCustomizationUUID(MODELCUSTOMIZATIONUUID);
239         vfCust.setSkipPostInstConf(setSkippost);
240         vfModuleCustomizations.add(vfCust);
241         return vfModuleCustomizations;
242     }
243
244     private PnfResourceCustomization getPnfResourceCustomization(boolean setSkippost) {
245         PnfResourceCustomization pnfResourceCustomization = new PnfResourceCustomization();
246         pnfResourceCustomization.setModelCustomizationUUID(MODELCUSTOMIZATIONUUID);
247         pnfResourceCustomization.setSkipPostInstConf(setSkippost);
248         return pnfResourceCustomization;
249     }
250
251 }