Catchup commits for Dublin
[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
32 import java.util.HashMap;
33 import java.util.Optional;
34 import java.util.UUID;
35
36 import org.camunda.bpm.engine.delegate.BpmnError;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.ArgumentMatchers;
40 import org.mockito.InjectMocks;
41 import org.onap.appc.client.lcm.model.Action;
42 import org.onap.so.bpmn.BaseTaskTest;
43 import org.onap.so.bpmn.common.BuildingBlockExecution;
44 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
45 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
46 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
47 import org.onap.so.client.exception.BBObjectNotFoundException;
48 import org.onap.so.db.catalog.beans.ControllerSelectionReference;
49
50 public class GenericVnfHealthCheckTest extends BaseTaskTest {
51         
52         @InjectMocks
53         private GenericVnfHealthCheck genericVnfHealthCheck = new GenericVnfHealthCheck();
54         
55         private GenericVnf genericVnf;
56         private RequestContext requestContext;
57         private String msoRequestId;
58
59         @Before
60         public void before() throws BBObjectNotFoundException {
61                 genericVnf = setGenericVnf();
62                 msoRequestId = UUID.randomUUID().toString();
63                 requestContext = setRequestContext();
64                 requestContext.setMsoRequestId(msoRequestId);
65                 gBBInput.setRequestContext(requestContext);
66                 
67                 doThrow(new BpmnError("BPMN Error")).when(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(Exception.class));     
68                 when(extractPojosForBB.extractByKey(any(),ArgumentMatchers.eq(ResourceKey.GENERIC_VNF_ID), any())).thenReturn(genericVnf);
69         }
70         
71         @Test
72         public void setParamsForGenericVnfHealthCheckTest() throws Exception {
73                 ControllerSelectionReference controllerSelectionReference = new ControllerSelectionReference();
74                 controllerSelectionReference.setControllerName("testName");
75                 controllerSelectionReference.setActionCategory("testAction");
76                 controllerSelectionReference.setVnfType("testVnfType");
77                 
78                 doReturn(controllerSelectionReference).when(catalogDbClient).getControllerSelectionReferenceByVnfTypeAndActionCategory(genericVnf.getVnfType(), Action.HealthCheck.toString());
79                 
80                 genericVnfHealthCheck.setParamsForGenericVnfHealthCheck(execution);
81                 
82                 assertEquals(genericVnf.getVnfId(), execution.getVariable("vnfId"));
83                 assertEquals(genericVnf.getVnfName(), execution.getVariable("vnfName"));
84                 assertEquals(genericVnf.getIpv4OamAddress(), execution.getVariable("oamIpAddress"));
85                 assertEquals("HealthCheck", execution.getVariable("action"));
86                 assertEquals(requestContext.getMsoRequestId(), execution.getVariable("msoRequestId"));
87                 assertEquals(controllerSelectionReference.getControllerName(), execution.getVariable("controllerType"));
88         }
89         @Test
90         public void callAppcClientTest() throws Exception {
91                 Action action = Action.HealthCheck;
92                 String vnfId = genericVnf.getVnfId();
93                 String payload = "{\"testName\":\"testValue\",}";
94                 String controllerType = "testType";
95                 HashMap<String, String> payloadInfo = new HashMap<String, String>();
96                 payloadInfo.put("vnfName", "testVnfName");
97                 payloadInfo.put("vfModuleId", "testVfModuleId");
98                 payloadInfo.put("oamIpAddress", "testOamIpAddress");
99                 payloadInfo.put("vnfHostIpAddress", "testOamIpAddress");
100                 execution.setVariable("action", Action.HealthCheck.toString());
101                 execution.setVariable("msoRequestId", msoRequestId);
102                 execution.setVariable("controllerType", controllerType);
103                 execution.setVariable("vnfId", "testVnfId1");
104                 execution.setVariable("vnfName", "testVnfName");
105                 execution.setVariable("vfModuleId", "testVfModuleId");
106                 execution.setVariable("oamIpAddress", "testOamIpAddress");
107                 execution.setVariable("vnfHostIpAddress", "testOamIpAddress");
108                 execution.setVariable("payload", payload);
109                 
110                 doNothing().when(appCClient).runAppCCommand(action, msoRequestId, vnfId, Optional.of(payload), payloadInfo, controllerType);
111                 
112                 genericVnfHealthCheck.callAppcClient(execution);
113                 verify(appCClient, times(1)).runAppCCommand(action, msoRequestId, vnfId, Optional.of(payload), payloadInfo, controllerType);
114         }
115 }