aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/workflow/tasks/listeners/SkipCDSBuildingBlockListenerTest.java
blob: b23147e3510470d7be90475856bbcc32e0d0ddd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2020  Tech Mahindra
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.so.bpmn.infrastructure.workflow.tasks.listeners;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.List;
import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.onap.so.bpmn.common.BBConstants;
import org.onap.so.bpmn.common.BuildingBlockExecution;
import org.onap.so.bpmn.common.DelegateExecutionImpl;
import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
import org.onap.so.db.catalog.beans.PnfResourceCustomization;
import org.onap.so.db.catalog.beans.Service;
import org.onap.so.db.catalog.beans.VfModuleCustomization;
import org.onap.so.db.catalog.beans.VnfResourceCustomization;
import org.onap.so.db.catalog.client.CatalogDbClient;
import org.onap.so.serviceinstancebeans.ModelInfo;
import org.onap.so.serviceinstancebeans.RequestDetails;

@RunWith(MockitoJUnitRunner.Silent.class)
public class SkipCDSBuildingBlockListenerTest {

    private static final String SERVICE_SCOPE = "service";
    private static final String VNF_SCOPE = "VNF";
    private static final String VF_SCOPE = "VFModule";
    private static final String PNF_SCOPE = "pnf";
    private static final String TEST_MODELUUID = "123456789";
    private static final String VNF_TEST_ACTION = "VnfConfigAssign";
    private static final String VFModule_TEST_ACTION = "VfModuleConfigAssign";
    private static final String PNFModule_TEST_ACTION = "config-assign";
    private static final String MODELCUSTOMIZATIONUUID = "123456789";
    private static final String BBNAME = "ControllerExecutionBB";
    private static final boolean ISFIRST = true;

    private int actual;
    private List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
    private List<VnfResourceCustomization> vnfResourceCustomization;
    private List<VfModuleCustomization> vfModuleCustomization;
    private ExecuteBuildingBlock executeBuildingBlock = new ExecuteBuildingBlock();
    private RequestDetails reqDetail = new RequestDetails();
    private BuildingBlockExecution buildingBlockExecution = new DelegateExecutionImpl(new DelegateExecutionFake());
    private VnfResourceCustomization vnfCust = new VnfResourceCustomization();
    private VfModuleCustomization vfCust = new VfModuleCustomization();
    private PnfResourceCustomization pnfResourceCustomization = new PnfResourceCustomization();
    private BuildingBlock buildingBlock = new BuildingBlock();

    @InjectMocks
    private SkipCDSBuildingBlockListener skipCDSBuildingBlockListener;
    @Mock
    private CatalogDbClient catalogDbClient;

    @Before
    public void before() {
        ModelInfo model = new ModelInfo();
        model.setModelUuid(TEST_MODELUUID);
        reqDetail.setModelInfo(model);
        executeBuildingBlock.setRequestDetails(reqDetail);
    }

    @Test
    public void testTrigger() {
        BuildingBlockExecution execution = new DelegateExecutionImpl(new DelegateExecutionFake());
        skipCDSBuildingBlockListener.shouldRunFor(BBNAME, ISFIRST, execution);
        assertEquals("ControllerExecutionBB", BBNAME);
    }

    @Test
    public void testSkipCDSforService() {
        setBuildingBlockAndCurrentSequence(SERVICE_SCOPE, "service-config-assign", 0);
        Service service = new Service();
        when(catalogDbClient.getServiceByID(TEST_MODELUUID)).thenReturn(service);

        skipCDSBuildingBlockListener.run(flowsToExecute, executeBuildingBlock, buildingBlockExecution);

        actual = buildingBlockExecution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
        assertEquals(1, actual);
    }

    @Test
    public void testProcessForVNFToSkipCDSBB() {
        // given
        setBuildingBlockAndCurrentSequence(VNF_SCOPE, VNF_TEST_ACTION, 0);
        vnfResourceCustomization = getVnfResourceCustomizationList(true);

        when(catalogDbClient.getVnfResourceCustomizationByModelUuid(
                executeBuildingBlock.getRequestDetails().getModelInfo().getModelUuid()))
                        .thenReturn(vnfResourceCustomization);
        when(catalogDbClient.findVnfResourceCustomizationInList(executeBuildingBlock.getBuildingBlock().getKey(),
                vnfResourceCustomization)).thenReturn(vnfCust);

        // when
        skipCDSBuildingBlockListener.run(flowsToExecute, executeBuildingBlock, buildingBlockExecution);

        // then
        actual = buildingBlockExecution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
        assertEquals(1, actual);

    }

    @Test
    public void testProcessForVNFNotToSkipCDSBB() {
        // given
        setBuildingBlockAndCurrentSequence(VNF_SCOPE, VNF_TEST_ACTION, 0);
        vnfResourceCustomization = getVnfResourceCustomizationList(false);

        when(catalogDbClient.getVnfResourceCustomizationByModelUuid(
                executeBuildingBlock.getRequestDetails().getModelInfo().getModelUuid()))
                        .thenReturn(vnfResourceCustomization);
        when(catalogDbClient.findVnfResourceCustomizationInList(executeBuildingBlock.getBuildingBlock().getKey(),
                vnfResourceCustomization)).thenReturn(vnfCust);

        // when
        skipCDSBuildingBlockListener.run(flowsToExecute, executeBuildingBlock, buildingBlockExecution);

        // then
        actual = buildingBlockExecution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
        assertEquals(0, actual);

    }


    @Test
    public void testProcessForVFToSkipCDSBB() {
        // given
        setBuildingBlockAndCurrentSequence(VF_SCOPE, VFModule_TEST_ACTION, 0);
        vfModuleCustomization = getVfModuleCustomizationList(true);

        when(catalogDbClient
                .getVfModuleCustomizationByModelCuztomizationUUID(executeBuildingBlock.getBuildingBlock().getKey()))
                        .thenReturn(vfCust);

        // when
        skipCDSBuildingBlockListener.run(flowsToExecute, executeBuildingBlock, buildingBlockExecution);

        // then
        actual = buildingBlockExecution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
        assertEquals(1, actual);

    }

    @Test
    public void testProcessForVFNotToSkipCDSBB() {
        // given
        setBuildingBlockAndCurrentSequence(VF_SCOPE, VFModule_TEST_ACTION, 0);
        vfModuleCustomization = getVfModuleCustomizationList(false);

        when(catalogDbClient
                .getVfModuleCustomizationByModelCuztomizationUUID(executeBuildingBlock.getBuildingBlock().getKey()))
                        .thenReturn(vfCust);

        // when
        skipCDSBuildingBlockListener.run(flowsToExecute, executeBuildingBlock, buildingBlockExecution);

        // then
        actual = buildingBlockExecution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
        assertEquals(0, actual);

    }

    @Test
    public void testProcessForPNFToSkipCDSBB() {
        // given
        setBuildingBlockAndCurrentSequence(PNF_SCOPE, PNFModule_TEST_ACTION, 0);
        pnfResourceCustomization = getPnfResourceCustomization(true);

        when(catalogDbClient
                .getPnfResourceCustomizationByModelCustomizationUUID(executeBuildingBlock.getBuildingBlock().getKey()))
                        .thenReturn(pnfResourceCustomization);

        // when
        skipCDSBuildingBlockListener.run(flowsToExecute, executeBuildingBlock, buildingBlockExecution);

        // then
        actual = buildingBlockExecution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
        assertEquals(1, actual);

    }

    @Test
    public void testProcessForPNFNotToSkipCDSBB() {
        // given
        setBuildingBlockAndCurrentSequence(PNF_SCOPE, PNFModule_TEST_ACTION, 0);
        pnfResourceCustomization = getPnfResourceCustomization(false);

        when(catalogDbClient
                .getPnfResourceCustomizationByModelCustomizationUUID(executeBuildingBlock.getBuildingBlock().getKey()))
                        .thenReturn(pnfResourceCustomization);

        // when
        skipCDSBuildingBlockListener.run(flowsToExecute, executeBuildingBlock, buildingBlockExecution);

        // then
        actual = buildingBlockExecution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
        assertEquals(0, actual);

    }

    /**
     * setting scope action in buildingBlock and BB current sequence in BuildingBlockExecution
     *
     * @param scope
     * @param action
     * @param squence
     */
    private void setBuildingBlockAndCurrentSequence(String scope, String action, int sequence) {
        buildingBlock.setBpmnScope(scope);
        buildingBlock.setBpmnAction(action);
        buildingBlock.setBpmnFlowName("ControllerExecutionBB");
        buildingBlock.setKey(MODELCUSTOMIZATIONUUID);
        executeBuildingBlock.setBuildingBlock(buildingBlock);
        buildingBlockExecution.setVariable(BBConstants.G_CURRENT_SEQUENCE, sequence);

    }

    private List<VnfResourceCustomization> getVnfResourceCustomizationList(boolean setSkippost) {
        List<VnfResourceCustomization> vnfResourceCustomizations = new ArrayList<>();
        vnfCust.setModelCustomizationUUID(MODELCUSTOMIZATIONUUID);
        vnfCust.setSkipPostInstConf(setSkippost);
        vnfResourceCustomizations.add(vnfCust);
        return vnfResourceCustomizations;
    }

    private List<VfModuleCustomization> getVfModuleCustomizationList(boolean setSkippost) {
        List<VfModuleCustomization> vfModuleCustomizations = new ArrayList<>();
        vfCust.setModelCustomizationUUID(MODELCUSTOMIZATIONUUID);
        vfCust.setSkipPostInstConf(setSkippost);
        vfModuleCustomizations.add(vfCust);
        return vfModuleCustomizations;
    }

    private PnfResourceCustomization getPnfResourceCustomization(boolean setSkippost) {
        PnfResourceCustomization pnfResourceCustomization = new PnfResourceCustomization();
        pnfResourceCustomization.setModelCustomizationUUID(MODELCUSTOMIZATIONUUID);
        pnfResourceCustomization.setSkipPostInstConf(setSkippost);
        return pnfResourceCustomization;
    }

}