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