0f9b4d9012c96f5aefad9080fd6101cf5bb12a14
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / decisionpoint / impl / buildingblock / ControllerExecutionBBTest.java
1 /*-
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
8  *
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.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.so.bpmn.infrastructure.decisionpoint.impl.buildingblock;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25 import static org.onap.so.bpmn.infrastructure.decisionpoint.impl.buildingblock.MockControllerBB.TEST_ACTION;
26 import static org.onap.so.bpmn.infrastructure.decisionpoint.impl.buildingblock.MockControllerBB.TEST_ACTOR;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.onap.so.bpmn.common.BuildingBlockExecution;
31 import org.onap.so.client.exception.ExceptionBuilder;
32 import org.onap.so.db.catalog.beans.ControllerSelectionReference;
33 import org.onap.so.db.catalog.beans.PnfResourceCustomization;
34 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
35 import org.onap.so.db.catalog.client.CatalogDbClient;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.boot.test.mock.mockito.MockBean;
38 import org.springframework.test.context.ContextConfiguration;
39 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
40
41 @RunWith(SpringJUnit4ClassRunner.class)
42 @ContextConfiguration(classes = {ControllerExecutionBB.class, MockControllerBB.class, ExceptionBuilder.class})
43 public class ControllerExecutionBBTest {
44
45     private static final String ACTOR_PARAM = "actor";
46     private static final String ACTION_PARAM = "action";
47     private static final String RESOURCE_TYPE_PARAM = "resource_type";
48
49     private static final String TEST_RESOURCE_CUSTOMIZATION_UUID = "68dc9a92-214c-11e7-93ae-92361f002680";
50     private static final String TEST_CATALOGDB_CONTROLLER_ACTOR = "cds";
51     private static final String TEST_RESOURCE_TYPE = "Firewall";
52     private static final String TEST_CONTROLLER_REFERENCE_ACTOR = "sdnc";
53     private static final String TEST_SCOPE = "pnf";
54
55     @Autowired
56     private ControllerExecutionBB controllerExecutionBB;
57
58     @MockBean
59     private BuildingBlockExecution execution;
60
61     @MockBean
62     private CatalogDbClient client;
63
64     @MockBean
65     private PnfResourceCustomization pnfResourceCustomization;
66
67     @MockBean
68     private VnfResourceCustomization vnfResourceCustomization;
69
70     @MockBean
71     private ControllerSelectionReference controllerSelectionReference;
72
73     @Before
74     public void setUp() {
75         when(execution.getVariable(ACTOR_PARAM)).thenReturn(TEST_ACTOR);
76         when(execution.getVariable(ACTION_PARAM)).thenReturn(MockControllerBB.TEST_ACTION);
77
78         when(pnfResourceCustomization.getControllerActor()).thenReturn(TEST_CATALOGDB_CONTROLLER_ACTOR);
79         when(client.getPnfResourceCustomizationByModelCustomizationUUID(TEST_RESOURCE_CUSTOMIZATION_UUID))
80                 .thenReturn(pnfResourceCustomization);
81
82         when(execution.getVariable(RESOURCE_TYPE_PARAM)).thenReturn(TEST_RESOURCE_TYPE);
83         when(controllerSelectionReference.getControllerName()).thenReturn(TEST_CONTROLLER_REFERENCE_ACTOR);
84         when(client.getControllerSelectionReferenceByVnfTypeAndActionCategory(TEST_RESOURCE_TYPE, TEST_ACTION))
85                 .thenReturn(controllerSelectionReference);
86     }
87
88     @Test
89     public void testExecution_validInput_expectedOutput() {
90         controllerExecutionBB.execute(execution);
91         verify(execution).setVariable("stage", "ready");
92         verify(execution).setVariable("stage", "prepare");
93         verify(execution).setVariable("stage", "run");
94     }
95
96     @Test
97     public void testGetController_availableInExecutionContext_returnFromExecutionContext() {
98         String controllerActor = controllerExecutionBB.getControllerActor(execution, TEST_SCOPE, "", TEST_ACTION);
99         assertEquals(TEST_ACTOR, controllerActor);
100     }
101
102     @Test
103     public void testGetController_soRefDataInExecutionContext_returnFromControllerReference() {
104         when(execution.getVariable(ACTOR_PARAM)).thenReturn("so-ref-data");
105         String controllerActor = controllerExecutionBB.getControllerActor(execution, TEST_SCOPE, "", TEST_ACTION);
106         assertEquals(TEST_CONTROLLER_REFERENCE_ACTOR, controllerActor);
107     }
108
109     @Test
110     public void testGetController_notAvailableInExecutionContext_returnFromCatalogdb() {
111         when(execution.getVariable(ACTOR_PARAM)).thenReturn("");
112         String controllerActor = controllerExecutionBB.getControllerActor(execution, TEST_SCOPE,
113                 TEST_RESOURCE_CUSTOMIZATION_UUID, TEST_ACTION);
114         assertEquals("The controller actor should be the same as in the catalogdb", TEST_CATALOGDB_CONTROLLER_ACTOR,
115                 controllerActor);
116     }
117
118     @Test
119     public void testGetController_notAvailableInExecutionContextAndSoRefDataInCatalogdb_returnFromControllerReference() {
120         when(execution.getVariable(ACTOR_PARAM)).thenReturn("");
121         when(pnfResourceCustomization.getControllerActor()).thenReturn("SO-REF-DATA");
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_CONTROLLER_REFERENCE_ACTOR,
125                 controllerActor);
126     }
127
128     @Test
129     public void testGetController_notAvailableInExecutionContextAndCatalogdb_returnFromControllerReference() {
130         when(execution.getVariable(ACTOR_PARAM)).thenReturn("");
131         when(pnfResourceCustomization.getControllerActor()).thenReturn("");
132
133         String controllerActor = controllerExecutionBB.getControllerActor(execution, TEST_SCOPE,
134                 TEST_RESOURCE_CUSTOMIZATION_UUID, TEST_ACTION);
135         assertEquals("The controller actor should be the same as in the controller reference table",
136                 TEST_CONTROLLER_REFERENCE_ACTOR, controllerActor);
137     }
138
139     @Test
140     public void testGetController_notAvailableInExecutionContextVnfType_returnFromCatalogdb() {
141         when(execution.getVariable(ACTOR_PARAM)).thenReturn("");
142
143         String expectedVnfControllerActor = "appc";
144
145         String[] controllerScopeArray = new String[] {"vnf", "vfModule"};
146
147         for (String controllerScope : controllerScopeArray) {
148
149             when(vnfResourceCustomization.getControllerActor()).thenReturn(expectedVnfControllerActor);
150             when(client.getVnfResourceCustomizationByModelCustomizationUUID(TEST_RESOURCE_CUSTOMIZATION_UUID))
151                     .thenReturn(vnfResourceCustomization);
152
153             String controllerActor = controllerExecutionBB.getControllerActor(execution, controllerScope,
154                     TEST_RESOURCE_CUSTOMIZATION_UUID, TEST_ACTION);
155
156             assertEquals("The controller actor should be the same as in the catalogdb for scope: " + controllerScope,
157                     expectedVnfControllerActor, controllerActor);
158         }
159     }
160 }