delete redundant test dependency
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / flowspecific / tasks / GenericVnfHealthCheckTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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 package org.onap.so.bpmn.infrastructure.flowspecific.tasks;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.eq;
25 import static org.mockito.Mockito.doNothing;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.doThrow;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31 import java.util.HashMap;
32 import java.util.Optional;
33 import java.util.UUID;
34 import org.camunda.bpm.engine.delegate.BpmnError;
35 import org.junit.Before;
36 import org.junit.Ignore;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.ArgumentMatchers;
40 import org.mockito.InjectMocks;
41 import org.mockito.Mock;
42 import org.mockito.junit.MockitoJUnitRunner;
43 import org.onap.appc.client.lcm.model.Action;
44 import org.onap.so.bpmn.common.BuildingBlockExecution;
45 import org.onap.so.bpmn.common.data.TestDataSetup;
46 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
47 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
48 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
49 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
50 import org.onap.so.client.appc.ApplicationControllerAction;
51 import org.onap.so.client.exception.BBObjectNotFoundException;
52 import org.onap.so.client.exception.ExceptionBuilder;
53 import org.onap.so.db.catalog.beans.ControllerSelectionReference;
54 import org.onap.so.db.catalog.client.CatalogDbClient;
55
56 @RunWith(MockitoJUnitRunner.Silent.class)
57 public class GenericVnfHealthCheckTest extends TestDataSetup {
58
59     @Mock
60     protected ExceptionBuilder exceptionUtil;
61     @Mock
62     protected CatalogDbClient catalogDbClient;
63     @Mock
64     protected ApplicationControllerAction appCClient;
65     @Mock
66     protected ExtractPojosForBB extractPojosForBBMock;
67     @InjectMocks
68     private GenericVnfHealthCheck genericVnfHealthCheck;
69
70     private GenericVnf genericVnf;
71     private RequestContext requestContext;
72     private String msoRequestId;
73
74     @Before
75     public void before() throws BBObjectNotFoundException {
76         genericVnf = setGenericVnf();
77         msoRequestId = UUID.randomUUID().toString();
78         requestContext = setRequestContext();
79         requestContext.setMsoRequestId(msoRequestId);
80         gBBInput.setRequestContext(requestContext);
81
82         doThrow(new BpmnError("BPMN Error")).when(exceptionUtil)
83                 .buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(Exception.class));
84         when(extractPojosForBBMock.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.GENERIC_VNF_ID)))
85                 .thenReturn(genericVnf);
86     }
87
88     @Test
89     public void setParamsForGenericVnfHealthCheckTest() {
90         ControllerSelectionReference controllerSelectionReference = new ControllerSelectionReference();
91         controllerSelectionReference.setControllerName("testName");
92         controllerSelectionReference.setActionCategory("testAction");
93         controllerSelectionReference.setVnfType("testVnfType");
94
95         doReturn(controllerSelectionReference).when(catalogDbClient)
96                 .getControllerSelectionReferenceByVnfTypeAndActionCategory(genericVnf.getVnfType(),
97                         Action.HealthCheck.toString());
98
99         genericVnfHealthCheck.setParamsForGenericVnfHealthCheck(execution);
100
101         assertEquals(genericVnf.getVnfId(), execution.getVariable("vnfId"));
102         assertEquals(genericVnf.getVnfName(), execution.getVariable("vnfName"));
103         assertEquals(genericVnf.getIpv4OamAddress(), execution.getVariable("oamIpAddress"));
104         assertEquals("HealthCheck", execution.getVariable("action"));
105         assertEquals(requestContext.getMsoRequestId(), execution.getVariable("msoRequestId"));
106         assertEquals(controllerSelectionReference.getControllerName(), execution.getVariable("controllerType"));
107     }
108
109     @Test
110     public void callAppcClientTest() {
111         Action action = Action.HealthCheck;
112         String vnfId = genericVnf.getVnfId();
113         String payload = "{\"testName\":\"testValue\",}";
114         String controllerType = "testType";
115         HashMap<String, String> payloadInfo = new HashMap<>();
116         payloadInfo.put("vnfName", "testVnfName");
117         payloadInfo.put("vfModuleId", "testVfModuleId");
118         payloadInfo.put("oamIpAddress", "testOamIpAddress");
119         payloadInfo.put("vnfHostIpAddress", "testOamIpAddress");
120         execution.setVariable("action", Action.HealthCheck.toString());
121         execution.setVariable("msoRequestId", msoRequestId);
122         execution.setVariable("controllerType", controllerType);
123         execution.setVariable("vnfId", "testVnfId1");
124         execution.setVariable("vnfName", "testVnfName");
125         execution.setVariable("vfModuleId", "testVfModuleId");
126         execution.setVariable("oamIpAddress", "testOamIpAddress");
127         execution.setVariable("vnfHostIpAddress", "testOamIpAddress");
128         execution.setVariable("payload", payload);
129
130         doNothing().when(appCClient).runAppCCommand(action, msoRequestId, vnfId, Optional.of(payload), payloadInfo,
131                 controllerType);
132
133         genericVnfHealthCheck.callAppcClient(execution);
134         verify(appCClient, times(1)).runAppCCommand(action, msoRequestId, vnfId, Optional.of(payload), payloadInfo,
135                 controllerType);
136     }
137
138     @Test
139     public void callAppcClientExceptionTest() {
140         expectedException.expect(BpmnError.class);
141         Action action = Action.HealthCheck;
142         String vnfId = genericVnf.getVnfId();
143         String payload = "{\"testName\":\"testValue\",}";
144         String controllerType = "testType";
145         HashMap<String, String> payloadInfo = new HashMap<>();
146         payloadInfo.put("vnfName", "testVnfName");
147         payloadInfo.put("vfModuleId", "testVfModuleId");
148         payloadInfo.put("oamIpAddress", "testOamIpAddress");
149         payloadInfo.put("vnfHostIpAddress", "testOamIpAddress");
150         execution.setVariable("action", Action.HealthCheck.toString());
151         execution.setVariable("msoRequestId", msoRequestId);
152         execution.setVariable("controllerType", controllerType);
153         execution.setVariable("vnfId", "testVnfId1");
154         execution.setVariable("vnfName", "testVnfName");
155         execution.setVariable("vfModuleId", "testVfModuleId");
156         execution.setVariable("oamIpAddress", "testOamIpAddress");
157         execution.setVariable("vnfHostIpAddress", "testOamIpAddress");
158         execution.setVariable("payload", payload);
159
160         doThrow(new BpmnError("BPMN Error")).when(exceptionUtil)
161                 .buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1002), eq("APPC Client Failed"));
162         doThrow(new RuntimeException("APPC Client Failed")).when(appCClient).runAppCCommand(action, msoRequestId, vnfId,
163                 Optional.of(payload), payloadInfo, controllerType);
164
165
166         genericVnfHealthCheck.callAppcClient(execution);
167         verify(appCClient, times(1)).runAppCCommand(action, msoRequestId, vnfId, Optional.of(payload), payloadInfo,
168                 controllerType);
169     }
170
171     @Test
172     @Ignore // The runAppCCommand method in not capable of throwing this exception
173     public void callAppcClientTimeOutExceptionTest() {
174         expectedException.expect(java.util.concurrent.TimeoutException.class);
175         Action action = Action.HealthCheck;
176         String vnfId = genericVnf.getVnfId();
177         String payload = "{\"testName\":\"testValue\",}";
178         String controllerType = "testType";
179         HashMap<String, String> payloadInfo = new HashMap<>();
180         payloadInfo.put("vnfName", "testVnfName");
181         payloadInfo.put("vfModuleId", "testVfModuleId");
182         payloadInfo.put("oamIpAddress", "testOamIpAddress");
183         payloadInfo.put("vnfHostIpAddress", "testOamIpAddress");
184         execution.setVariable("action", Action.HealthCheck.toString());
185         execution.setVariable("msoRequestId", msoRequestId);
186         execution.setVariable("controllerType", controllerType);
187         execution.setVariable("vnfId", "testVnfId1");
188         execution.setVariable("vnfName", "testVnfName");
189         execution.setVariable("vfModuleId", "testVfModuleId");
190         execution.setVariable("oamIpAddress", "testOamIpAddress");
191         execution.setVariable("vnfHostIpAddress", "testOamIpAddress");
192         execution.setVariable("payload", payload);
193
194         doThrow(java.util.concurrent.TimeoutException.class).when(appCClient).runAppCCommand(action, msoRequestId,
195                 vnfId, Optional.of(payload), payloadInfo, controllerType);
196
197
198         genericVnfHealthCheck.callAppcClient(execution);
199         verify(appCClient, times(1)).runAppCCommand(action, msoRequestId, vnfId, Optional.of(payload), payloadInfo,
200                 controllerType);
201     }
202 }