2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.so.bpmn.infrastructure.decisionpoint.impl.buildingblock;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.anyInt;
26 import static org.mockito.Mockito.times;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29 import static org.onap.so.bpmn.infrastructure.decisionpoint.impl.buildingblock.MockControllerBB.TEST_ACTION;
30 import static org.onap.so.bpmn.infrastructure.decisionpoint.impl.buildingblock.MockControllerBB.TEST_ACTOR;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.ArgumentCaptor;
35 import org.onap.logging.filter.base.ONAPComponents;
36 import org.onap.so.bpmn.common.BuildingBlockExecution;
37 import org.onap.so.client.cds.PayloadConstants;
38 import org.onap.so.client.exception.ExceptionBuilder;
39 import org.onap.so.db.catalog.beans.ControllerSelectionReference;
40 import org.onap.so.db.catalog.beans.PnfResourceCustomization;
41 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
42 import org.onap.so.db.catalog.client.CatalogDbClient;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.boot.test.mock.mockito.MockBean;
45 import org.springframework.test.context.ContextConfiguration;
46 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
48 @RunWith(SpringJUnit4ClassRunner.class)
49 @ContextConfiguration(classes = {ControllerExecutionBB.class, MockControllerBB.class, ExceptionBuilder.class})
50 public class ControllerExecutionBBTest {
52 private static final String ACTOR_PARAM = "actor";
53 private static final String ACTION_PARAM = "action";
54 private static final String RESOURCE_TYPE_PARAM = "resource_type";
56 private static final String TEST_RESOURCE_CUSTOMIZATION_UUID = "68dc9a92-214c-11e7-93ae-92361f002680";
57 private static final String TEST_CATALOGDB_CONTROLLER_ACTOR = "cds";
58 private static final String TEST_RESOURCE_TYPE = "Firewall";
59 private static final String TEST_CONTROLLER_REFERENCE_ACTOR = "sdnc";
60 private static final String TEST_SCOPE = "pnf";
63 private ControllerExecutionBB controllerExecutionBB;
66 private BuildingBlockExecution execution;
69 private CatalogDbClient client;
72 private PnfResourceCustomization pnfResourceCustomization;
75 private VnfResourceCustomization vnfResourceCustomization;
78 private ControllerSelectionReference controllerSelectionReference;
81 ExceptionBuilder exceptionBuilder;
85 when(execution.getVariable(ACTOR_PARAM)).thenReturn(TEST_ACTOR);
86 when(execution.getVariable(ACTION_PARAM)).thenReturn(MockControllerBB.TEST_ACTION);
88 when(pnfResourceCustomization.getControllerActor()).thenReturn(TEST_CATALOGDB_CONTROLLER_ACTOR);
89 when(client.getPnfResourceCustomizationByModelCustomizationUUID(TEST_RESOURCE_CUSTOMIZATION_UUID))
90 .thenReturn(pnfResourceCustomization);
92 when(execution.getVariable(RESOURCE_TYPE_PARAM)).thenReturn(TEST_RESOURCE_TYPE);
93 when(controllerSelectionReference.getControllerName()).thenReturn(TEST_CONTROLLER_REFERENCE_ACTOR);
94 when(client.getControllerSelectionReferenceByVnfTypeAndActionCategory(TEST_RESOURCE_TYPE, TEST_ACTION))
95 .thenReturn(controllerSelectionReference);
99 public void testExecution_validInput_expectedOutput() {
100 controllerExecutionBB.execute(execution);
101 verify(execution).setVariable("stage", "ready");
102 verify(execution).setVariable("stage", "prepare");
103 verify(execution).setVariable("stage", "run");
107 public void testGetController_availableInExecutionContext_returnFromExecutionContext() {
108 String controllerActor = controllerExecutionBB.getControllerActor(execution, TEST_SCOPE, "", TEST_ACTION);
109 assertEquals(TEST_ACTOR, controllerActor);
113 public void testGetController_soRefDataInExecutionContext_returnFromControllerReference() {
114 when(execution.getVariable(ACTOR_PARAM)).thenReturn("so-ref-data");
115 String controllerActor = controllerExecutionBB.getControllerActor(execution, TEST_SCOPE, "", TEST_ACTION);
116 assertEquals(TEST_CONTROLLER_REFERENCE_ACTOR, controllerActor);
120 public void testGetController_notAvailableInExecutionContext_returnFromCatalogdb() {
121 when(execution.getVariable(ACTOR_PARAM)).thenReturn("");
122 String controllerActor = controllerExecutionBB.getControllerActor(execution, TEST_SCOPE,
123 TEST_RESOURCE_CUSTOMIZATION_UUID, TEST_ACTION);
124 assertEquals("The controller actor should be the same as in the catalogdb", TEST_CATALOGDB_CONTROLLER_ACTOR,
129 public void testGetController_notAvailableInExecutionContextAndSoRefDataInCatalogdb_returnFromControllerReference() {
130 when(execution.getVariable(ACTOR_PARAM)).thenReturn("");
131 when(pnfResourceCustomization.getControllerActor()).thenReturn("SO-REF-DATA");
132 String controllerActor = controllerExecutionBB.getControllerActor(execution, TEST_SCOPE,
133 TEST_RESOURCE_CUSTOMIZATION_UUID, TEST_ACTION);
134 assertEquals("The controller actor should be the same as in the catalogdb", TEST_CONTROLLER_REFERENCE_ACTOR,
139 public void testGetController_notAvailableInExecutionContextAndCatalogdb_returnFromControllerReference() {
140 when(execution.getVariable(ACTOR_PARAM)).thenReturn("");
141 when(pnfResourceCustomization.getControllerActor()).thenReturn("");
143 String controllerActor = controllerExecutionBB.getControllerActor(execution, TEST_SCOPE,
144 TEST_RESOURCE_CUSTOMIZATION_UUID, TEST_ACTION);
145 assertEquals("The controller actor should be the same as in the controller reference table",
146 TEST_CONTROLLER_REFERENCE_ACTOR, controllerActor);
150 public void testGetController_notAvailableInExecutionContextVnfType_returnFromCatalogdb() {
151 when(execution.getVariable(ACTOR_PARAM)).thenReturn("");
153 String expectedVnfControllerActor = "appc";
155 String[] controllerScopeArray = new String[] {"vnf", "vfModule"};
157 for (String controllerScope : controllerScopeArray) {
159 when(vnfResourceCustomization.getControllerActor()).thenReturn(expectedVnfControllerActor);
160 when(client.getVnfResourceCustomizationByModelCustomizationUUID(TEST_RESOURCE_CUSTOMIZATION_UUID))
161 .thenReturn(vnfResourceCustomization);
163 String controllerActor = controllerExecutionBB.getControllerActor(execution, controllerScope,
164 TEST_RESOURCE_CUSTOMIZATION_UUID, TEST_ACTION);
166 assertEquals("The controller actor should be the same as in the catalogdb for scope: " + controllerScope,
167 expectedVnfControllerActor, controllerActor);
172 public void testHandleFailure() {
173 when(execution.getVariable(PayloadConstants.CONTROLLER_ERROR_MESSAGE)).thenReturn("ERROR MESSAGE");
175 controllerExecutionBB.handleFailure(execution);
177 verify(exceptionBuilder).buildAndThrowWorkflowException(execution, 9003, "ERROR MESSAGE", ONAPComponents.SO);
181 public void testHandleTimeoutFailure() {
182 when(execution.getVariable(PayloadConstants.CONTROLLER_MSG_TIMEOUT_REACHED)).thenReturn(true);
184 controllerExecutionBB.handleFailure(execution);
186 ArgumentCaptor<String> errMsgCaptor = ArgumentCaptor.forClass(String.class);
187 verify(exceptionBuilder, times(1)).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), anyInt(),
188 errMsgCaptor.capture(), any());
190 assertTrue(errMsgCaptor.getValue().contains("timeout"));