f23bbd8c91012356a27927e7fe2e9368323749f8
[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.mockito.ArgumentMatchers;
39 import org.mockito.InjectMocks;
40 import org.onap.appc.client.lcm.model.Action;
41 import org.onap.so.bpmn.BaseTaskTest;
42 import org.onap.so.bpmn.common.BuildingBlockExecution;
43 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
44 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
45 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
46 import org.onap.so.client.exception.BBObjectNotFoundException;
47 import org.onap.so.db.catalog.beans.ControllerSelectionReference;
48
49 public class GenericVnfHealthCheckTest extends BaseTaskTest {
50
51     @InjectMocks
52     private GenericVnfHealthCheck genericVnfHealthCheck = new GenericVnfHealthCheck();
53
54     private GenericVnf genericVnf;
55     private RequestContext requestContext;
56     private String msoRequestId;
57
58     @Before
59     public void before() throws BBObjectNotFoundException {
60         genericVnf = setGenericVnf();
61         msoRequestId = UUID.randomUUID().toString();
62         requestContext = setRequestContext();
63         requestContext.setMsoRequestId(msoRequestId);
64         gBBInput.setRequestContext(requestContext);
65
66         doThrow(new BpmnError("BPMN Error")).when(exceptionUtil)
67                 .buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(Exception.class));
68         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.GENERIC_VNF_ID)))
69                 .thenReturn(genericVnf);
70     }
71
72     @Test
73     public void setParamsForGenericVnfHealthCheckTest() throws Exception {
74         ControllerSelectionReference controllerSelectionReference = new ControllerSelectionReference();
75         controllerSelectionReference.setControllerName("testName");
76         controllerSelectionReference.setActionCategory("testAction");
77         controllerSelectionReference.setVnfType("testVnfType");
78
79         doReturn(controllerSelectionReference).when(catalogDbClient)
80                 .getControllerSelectionReferenceByVnfTypeAndActionCategory(genericVnf.getVnfType(),
81                         Action.HealthCheck.toString());
82
83         genericVnfHealthCheck.setParamsForGenericVnfHealthCheck(execution);
84
85         assertEquals(genericVnf.getVnfId(), execution.getVariable("vnfId"));
86         assertEquals(genericVnf.getVnfName(), execution.getVariable("vnfName"));
87         assertEquals(genericVnf.getIpv4OamAddress(), execution.getVariable("oamIpAddress"));
88         assertEquals("HealthCheck", execution.getVariable("action"));
89         assertEquals(requestContext.getMsoRequestId(), execution.getVariable("msoRequestId"));
90         assertEquals(controllerSelectionReference.getControllerName(), execution.getVariable("controllerType"));
91     }
92
93     @Test
94     public void callAppcClientTest() throws Exception {
95         Action action = Action.HealthCheck;
96         String vnfId = genericVnf.getVnfId();
97         String payload = "{\"testName\":\"testValue\",}";
98         String controllerType = "testType";
99         HashMap<String, String> payloadInfo = new HashMap<String, String>();
100         payloadInfo.put("vnfName", "testVnfName");
101         payloadInfo.put("vfModuleId", "testVfModuleId");
102         payloadInfo.put("oamIpAddress", "testOamIpAddress");
103         payloadInfo.put("vnfHostIpAddress", "testOamIpAddress");
104         execution.setVariable("action", Action.HealthCheck.toString());
105         execution.setVariable("msoRequestId", msoRequestId);
106         execution.setVariable("controllerType", controllerType);
107         execution.setVariable("vnfId", "testVnfId1");
108         execution.setVariable("vnfName", "testVnfName");
109         execution.setVariable("vfModuleId", "testVfModuleId");
110         execution.setVariable("oamIpAddress", "testOamIpAddress");
111         execution.setVariable("vnfHostIpAddress", "testOamIpAddress");
112         execution.setVariable("payload", payload);
113
114         doNothing().when(appCClient).runAppCCommand(action, msoRequestId, vnfId, Optional.of(payload), payloadInfo,
115                 controllerType);
116
117         genericVnfHealthCheck.callAppcClient(execution);
118         verify(appCClient, times(1)).runAppCCommand(action, msoRequestId, vnfId, Optional.of(payload), payloadInfo,
119                 controllerType);
120     }
121
122     @Test
123     public void callAppcClientExceptionTest() throws Exception {
124         expectedException.expect(BpmnError.class);
125         Action action = Action.HealthCheck;
126         String vnfId = genericVnf.getVnfId();
127         String payload = "{\"testName\":\"testValue\",}";
128         String controllerType = "testType";
129         HashMap<String, String> payloadInfo = new HashMap<String, String>();
130         payloadInfo.put("vnfName", "testVnfName");
131         payloadInfo.put("vfModuleId", "testVfModuleId");
132         payloadInfo.put("oamIpAddress", "testOamIpAddress");
133         payloadInfo.put("vnfHostIpAddress", "testOamIpAddress");
134         execution.setVariable("action", Action.HealthCheck.toString());
135         execution.setVariable("msoRequestId", msoRequestId);
136         execution.setVariable("controllerType", controllerType);
137         execution.setVariable("vnfId", "testVnfId1");
138         execution.setVariable("vnfName", "testVnfName");
139         execution.setVariable("vfModuleId", "testVfModuleId");
140         execution.setVariable("oamIpAddress", "testOamIpAddress");
141         execution.setVariable("vnfHostIpAddress", "testOamIpAddress");
142         execution.setVariable("payload", payload);
143
144         doThrow(new BpmnError("BPMN Error")).when(exceptionUtil)
145                 .buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1002), eq("APPC Client Failed"));
146         doThrow(new RuntimeException("APPC Client Failed")).when(appCClient).runAppCCommand(action, msoRequestId, vnfId,
147                 Optional.of(payload), payloadInfo, controllerType);
148
149
150         genericVnfHealthCheck.callAppcClient(execution);
151         verify(appCClient, times(1)).runAppCCommand(action, msoRequestId, vnfId, Optional.of(payload), payloadInfo,
152                 controllerType);
153     }
154
155     @Test
156     @Ignore // The runAppCCommand method in not capable of throwing this exception
157     public void callAppcClientTimeOutExceptionTest() {
158         expectedException.expect(java.util.concurrent.TimeoutException.class);
159         Action action = Action.HealthCheck;
160         String vnfId = genericVnf.getVnfId();
161         String payload = "{\"testName\":\"testValue\",}";
162         String controllerType = "testType";
163         HashMap<String, String> payloadInfo = new HashMap<String, String>();
164         payloadInfo.put("vnfName", "testVnfName");
165         payloadInfo.put("vfModuleId", "testVfModuleId");
166         payloadInfo.put("oamIpAddress", "testOamIpAddress");
167         payloadInfo.put("vnfHostIpAddress", "testOamIpAddress");
168         execution.setVariable("action", Action.HealthCheck.toString());
169         execution.setVariable("msoRequestId", msoRequestId);
170         execution.setVariable("controllerType", controllerType);
171         execution.setVariable("vnfId", "testVnfId1");
172         execution.setVariable("vnfName", "testVnfName");
173         execution.setVariable("vfModuleId", "testVfModuleId");
174         execution.setVariable("oamIpAddress", "testOamIpAddress");
175         execution.setVariable("vnfHostIpAddress", "testOamIpAddress");
176         execution.setVariable("payload", payload);
177
178         doThrow(java.util.concurrent.TimeoutException.class).when(appCClient).runAppCCommand(action, msoRequestId,
179                 vnfId, Optional.of(payload), payloadInfo, controllerType);
180
181
182         genericVnfHealthCheck.callAppcClient(execution);
183         verify(appCClient, times(1)).runAppCCommand(action, msoRequestId, vnfId, Optional.of(payload), payloadInfo,
184                 controllerType);
185     }
186 }