Merge "bug fixing with reading dmaap message" into casablanca
[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.Mockito.doNothing;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.doThrow;
26 import static org.mockito.Mockito.times;
27 import static org.mockito.Mockito.verify;
28
29 import java.util.HashMap;
30 import java.util.Optional;
31 import java.util.UUID;
32
33 import org.camunda.bpm.engine.delegate.BpmnError;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.appc.client.lcm.model.Action;
37 import org.onap.so.bpmn.BaseTaskTest;
38 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
39 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
40 import org.onap.so.db.catalog.beans.ControllerSelectionReference;
41 import org.springframework.beans.factory.annotation.Autowired;
42
43 public class GenericVnfHealthCheckTest extends BaseTaskTest {
44         
45         @Autowired
46         private GenericVnfHealthCheck genericVnfHealthCheck;
47         
48         private GenericVnf genericVnf;
49         private RequestContext requestContext;
50         private String msoRequestId;
51
52         @Before
53         public void before() {
54                 genericVnf = setGenericVnf();
55                 msoRequestId = UUID.randomUUID().toString();
56                 requestContext = setRequestContext();
57                 requestContext.setMsoRequestId(msoRequestId);
58                 gBBInput.setRequestContext(requestContext);
59         }
60         
61         @Test
62         public void setParamsForGenericVnfHealthCheckTest() throws Exception {
63                 ControllerSelectionReference controllerSelectionReference = new ControllerSelectionReference();
64                 controllerSelectionReference.setControllerName("testName");
65                 controllerSelectionReference.setActionCategory("testAction");
66                 controllerSelectionReference.setVnfType("testVnfType");
67                 
68                 doReturn(controllerSelectionReference).when(catalogDbClient).getControllerSelectionReferenceByVnfTypeAndActionCategory(genericVnf.getVnfType(), Action.HealthCheck.toString());
69                 
70                 genericVnfHealthCheck.setParamsForGenericVnfHealthCheck(execution);
71                 
72                 assertEquals(genericVnf.getVnfId(), execution.getVariable("vnfId"));
73                 assertEquals(genericVnf.getVnfName(), execution.getVariable("vnfName"));
74                 assertEquals(genericVnf.getIpv4OamAddress(), execution.getVariable("oamIpAddress"));
75                 assertEquals("HealthCheck", execution.getVariable("action"));
76                 assertEquals(requestContext.getMsoRequestId(), execution.getVariable("msoRequestId"));
77                 assertEquals(controllerSelectionReference.getControllerName(), execution.getVariable("controllerType"));
78         }
79         @Test
80         public void callAppcClientTest() throws Exception {
81                 Action action = Action.HealthCheck;
82                 String vnfId = genericVnf.getVnfId();
83                 String payload = "{\"testName\":\"testValue\",}";
84                 String controllerType = "testType";
85                 HashMap<String, String> payloadInfo = new HashMap<String, String>();
86                 payloadInfo.put("vnfName", "testVnfName");
87                 payloadInfo.put("vfModuleId", "testVfModuleId");
88                 payloadInfo.put("oamIpAddress", "testOamIpAddress");
89                 payloadInfo.put("vnfHostIpAddress", "testOamIpAddress");
90                 execution.setVariable("action", Action.HealthCheck.toString());
91                 execution.setVariable("msoRequestId", msoRequestId);
92                 execution.setVariable("controllerType", controllerType);
93                 execution.setVariable("vnfId", "testVnfId1");
94                 execution.setVariable("vnfName", "testVnfName");
95                 execution.setVariable("vfModuleId", "testVfModuleId");
96                 execution.setVariable("oamIpAddress", "testOamIpAddress");
97                 execution.setVariable("vnfHostIpAddress", "testOamIpAddress");
98                 execution.setVariable("payload", payload);
99                 
100                 doNothing().when(appCClient).runAppCCommand(action, msoRequestId, vnfId, Optional.of(payload), payloadInfo, controllerType);
101                 
102                 genericVnfHealthCheck.callAppcClient(execution);
103                 verify(appCClient, times(1)).runAppCCommand(action, msoRequestId, vnfId, Optional.of(payload), payloadInfo, controllerType);
104         }
105         
106         @Test
107         public void callAppcClientExceptionTest() throws Exception {
108                 expectedException.expect(BpmnError.class);
109                 Action action = Action.HealthCheck;
110                 String vnfId = genericVnf.getVnfId();
111                 String payload = "{\"testName\":\"testValue\",}";
112                 String controllerType = "testType";
113                 HashMap<String, String> payloadInfo = new HashMap<String, String>();
114                 payloadInfo.put("vnfName", "testVnfName");
115                 payloadInfo.put("vfModuleId", "testVfModuleId");
116                 payloadInfo.put("oamIpAddress", "testOamIpAddress");
117                 payloadInfo.put("vnfHostIpAddress", "testOamIpAddress");
118                 execution.setVariable("action", Action.HealthCheck.toString());
119                 execution.setVariable("msoRequestId", msoRequestId);
120                 execution.setVariable("controllerType", controllerType);
121                 execution.setVariable("vnfId", "testVnfId1");
122                 execution.setVariable("vnfName", "testVnfName");
123                 execution.setVariable("vfModuleId", "testVfModuleId");
124                 execution.setVariable("oamIpAddress", "testOamIpAddress");
125                 execution.setVariable("vnfHostIpAddress", "testOamIpAddress");
126                 execution.setVariable("payload", payload);
127                 
128                 doThrow(Exception.class).when(appCClient).runAppCCommand(action, msoRequestId, vnfId, Optional.of(payload), payloadInfo, controllerType);
129                 
130                 
131                 genericVnfHealthCheck.callAppcClient(execution);
132                 verify(appCClient, times(1)).runAppCCommand(action, msoRequestId, vnfId, Optional.of(payload), payloadInfo, controllerType);
133         }
134 }