Use resource list for SkipCDSBuildingBlockListener
[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 java.util.UUID;
28 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.InjectMocks;
33 import org.mockito.Mock;
34 import org.mockito.junit.MockitoJUnitRunner;
35 import org.onap.so.bpmn.common.BBConstants;
36 import org.onap.so.bpmn.common.BuildingBlockExecution;
37 import org.onap.so.bpmn.common.DelegateExecutionImpl;
38 import org.onap.so.bpmn.infrastructure.workflow.tasks.Resource;
39 import org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowType;
40 import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
41 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
42 import org.onap.so.db.catalog.beans.PnfResourceCustomization;
43 import org.onap.so.db.catalog.beans.Service;
44 import org.onap.so.db.catalog.beans.VfModuleCustomization;
45 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
46 import org.onap.so.db.catalog.client.CatalogDbClient;
47 import org.onap.so.serviceinstancebeans.RequestDetails;
48
49 @RunWith(MockitoJUnitRunner.Silent.class)
50 public class SkipCDSBuildingBlockListenerTest {
51
52     private static final String SERVICE_SCOPE = "service";
53     private static final String VNF_SCOPE = "VNF";
54     private static final String VF_SCOPE = "VFModule";
55     private static final String PNF_SCOPE = "pnf";
56     private static final String VNF_TEST_ACTION = "VnfConfigAssign";
57     private static final String VFModule_TEST_ACTION = "VfModuleConfigAssign";
58     private static final String PNFModule_TEST_ACTION = "config-assign";
59     private static final String SERVICE_MODEL_VERSION_ID = UUID.randomUUID().toString();
60     private static final String VNF_MODEL_CUSTOMIZATION_ID = UUID.randomUUID().toString();
61     private static final String VF_MODULE_CUSTOMIZATION_ID = UUID.randomUUID().toString();
62     private static final String PNF_CUSTOMIZATION_ID = UUID.randomUUID().toString();
63     private static final String BBNAME = "ControllerExecutionBB";
64     private static final boolean ISFIRST = true;
65
66     private List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
67     private ExecuteBuildingBlock executeBuildingBlock;
68     private RequestDetails reqDetail = new RequestDetails();
69     private BuildingBlockExecution buildingBlockExecution = new DelegateExecutionImpl(new DelegateExecutionFake());
70     private VnfResourceCustomization vnfCust;
71     private VfModuleCustomization vfCust;
72     private PnfResourceCustomization pnfResourceCustomization;
73     private BuildingBlock buildingBlock;
74
75     @InjectMocks
76     private SkipCDSBuildingBlockListener skipCDSBuildingBlockListener;
77     @Mock
78     private CatalogDbClient catalogDbClient;
79
80     @Before
81     public void before() {
82         executeBuildingBlock = new ExecuteBuildingBlock();
83         buildingBlock = new BuildingBlock();
84
85         ArrayList<Resource> resources = new ArrayList<>();
86         Resource service = new Resource(WorkflowType.SERVICE, SERVICE_MODEL_VERSION_ID, false, null);
87         service.setModelVersionId(SERVICE_MODEL_VERSION_ID);
88         resources.add(service);
89         Resource vnf = new Resource(WorkflowType.VNF, VNF_MODEL_CUSTOMIZATION_ID, false, null);
90         vnf.setModelCustomizationId(VNF_MODEL_CUSTOMIZATION_ID);
91         resources.add(vnf);
92         Resource vfModule = new Resource(WorkflowType.VFMODULE, VF_MODULE_CUSTOMIZATION_ID, false, null);
93         vfModule.setModelCustomizationId(VF_MODULE_CUSTOMIZATION_ID);
94         resources.add(vfModule);
95         Resource pnf = new Resource(WorkflowType.PNF, PNF_CUSTOMIZATION_ID, false, null);
96         pnf.setModelCustomizationId(PNF_CUSTOMIZATION_ID);
97         resources.add(pnf);
98         buildingBlockExecution.setVariable("resources", resources);
99
100         vnfCust = new VnfResourceCustomization();
101         vfCust = new VfModuleCustomization();
102         pnfResourceCustomization = new PnfResourceCustomization();
103     }
104
105     @Test
106     public void testTrigger() {
107         BuildingBlockExecution execution = new DelegateExecutionImpl(new DelegateExecutionFake());
108         skipCDSBuildingBlockListener.shouldRunFor(BBNAME, ISFIRST, execution);
109         assertEquals("ControllerExecutionBB", BBNAME);
110     }
111
112     @Test
113     public void testSkipCDSforService() {
114         setBuildingBlockAndCurrentSequence(SERVICE_SCOPE, "service-config-assign", SERVICE_MODEL_VERSION_ID, 0);
115         when(catalogDbClient.getServiceByID(SERVICE_MODEL_VERSION_ID)).thenReturn(new Service());
116
117         skipCDSBuildingBlockListener.run(flowsToExecute, executeBuildingBlock, buildingBlockExecution);
118
119         assertEquals(1, (int) buildingBlockExecution.getVariable(BBConstants.G_CURRENT_SEQUENCE));
120     }
121
122     @Test
123     public void testProcessForVNFToSkipCDSBB() {
124         setBuildingBlockAndCurrentSequence(VNF_SCOPE, VNF_TEST_ACTION, VNF_MODEL_CUSTOMIZATION_ID, 0);
125         when(catalogDbClient.getVnfResourceCustomizationByModelCustomizationUUID(VNF_MODEL_CUSTOMIZATION_ID))
126                 .thenReturn(vnfCust);
127
128         skipCDSBuildingBlockListener.run(flowsToExecute, executeBuildingBlock, buildingBlockExecution);
129
130         assertEquals(1, (int) buildingBlockExecution.getVariable(BBConstants.G_CURRENT_SEQUENCE));
131     }
132
133     @Test
134     public void testProcessForVNFNotToSkipCDSBB() {
135         setBuildingBlockAndCurrentSequence(VNF_SCOPE, VNF_TEST_ACTION, VNF_MODEL_CUSTOMIZATION_ID, 0);
136         vnfCust.setSkipPostInstConf(false);
137         when(catalogDbClient.getVnfResourceCustomizationByModelCustomizationUUID(VNF_MODEL_CUSTOMIZATION_ID))
138                 .thenReturn(vnfCust);
139
140         skipCDSBuildingBlockListener.run(flowsToExecute, executeBuildingBlock, buildingBlockExecution);
141
142         assertEquals(0, (int) buildingBlockExecution.getVariable(BBConstants.G_CURRENT_SEQUENCE));
143     }
144
145
146     @Test
147     public void testProcessForVFToSkipCDSBB() {
148         setBuildingBlockAndCurrentSequence(VF_SCOPE, VFModule_TEST_ACTION, VF_MODULE_CUSTOMIZATION_ID, 0);
149         when(catalogDbClient.getVfModuleCustomizationByModelCuztomizationUUID(VF_MODULE_CUSTOMIZATION_ID))
150                 .thenReturn(vfCust);
151
152         skipCDSBuildingBlockListener.run(flowsToExecute, executeBuildingBlock, buildingBlockExecution);
153
154         assertEquals(1, (int) buildingBlockExecution.getVariable(BBConstants.G_CURRENT_SEQUENCE));
155     }
156
157     @Test
158     public void testProcessForVFNotToSkipCDSBB() {
159         setBuildingBlockAndCurrentSequence(VF_SCOPE, VFModule_TEST_ACTION, VF_MODULE_CUSTOMIZATION_ID, 0);
160         vfCust.setSkipPostInstConf(false);
161         when(catalogDbClient
162                 .getVfModuleCustomizationByModelCuztomizationUUID(executeBuildingBlock.getBuildingBlock().getKey()))
163                         .thenReturn(vfCust);
164
165         skipCDSBuildingBlockListener.run(flowsToExecute, executeBuildingBlock, buildingBlockExecution);
166
167         assertEquals(0, (int) buildingBlockExecution.getVariable(BBConstants.G_CURRENT_SEQUENCE));
168     }
169
170     @Test
171     public void testProcessForPNFToSkipCDSBB() {
172         setBuildingBlockAndCurrentSequence(PNF_SCOPE, PNFModule_TEST_ACTION, PNF_CUSTOMIZATION_ID, 0);
173         when(catalogDbClient.getPnfResourceCustomizationByModelCustomizationUUID(PNF_CUSTOMIZATION_ID))
174                 .thenReturn(pnfResourceCustomization);
175
176         skipCDSBuildingBlockListener.run(flowsToExecute, executeBuildingBlock, buildingBlockExecution);
177
178         assertEquals(1, (int) buildingBlockExecution.getVariable(BBConstants.G_CURRENT_SEQUENCE));
179     }
180
181     @Test
182     public void testProcessForPNFNotToSkipCDSBB() {
183         setBuildingBlockAndCurrentSequence(PNF_SCOPE, PNFModule_TEST_ACTION, PNF_CUSTOMIZATION_ID, 0);
184         pnfResourceCustomization.setSkipPostInstConf(false);
185         when(catalogDbClient.getPnfResourceCustomizationByModelCustomizationUUID(PNF_CUSTOMIZATION_ID))
186                 .thenReturn(pnfResourceCustomization);
187
188         skipCDSBuildingBlockListener.run(flowsToExecute, executeBuildingBlock, buildingBlockExecution);
189
190         assertEquals(0, (int) buildingBlockExecution.getVariable(BBConstants.G_CURRENT_SEQUENCE));
191     }
192
193     /**
194      * setting scope action in buildingBlock and BB current sequence in BuildingBlockExecution
195      *
196      * @param scope
197      * @param action
198      * @param squence
199      */
200     private void setBuildingBlockAndCurrentSequence(String scope, String action, String key, int sequence) {
201         buildingBlock.setBpmnScope(scope);
202         buildingBlock.setBpmnAction(action);
203         buildingBlock.setBpmnFlowName("ControllerExecutionBB");
204         buildingBlock.setKey(key);
205         executeBuildingBlock.setBuildingBlock(buildingBlock);
206         buildingBlockExecution.setVariable(BBConstants.G_CURRENT_SEQUENCE, sequence);
207     }
208
209 }