13fff6d32b1b0c9a1cf6efdcc34a26a569d99d22
[so.git] / so-etsi-nfvo / so-etsi-nfvo-ns-lcm / so-etsi-nfvo-ns-lcm-bpmn-flows / src / test / java / org / onap / so / etsi / nfvo / ns / workflow / engine / tasks / MonitorSol003AdapterTerminateJobTaskTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation.
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  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.etsi.nfvo.ns.workflow.engine.tasks;
21
22 import com.google.gson.Gson;
23 import org.camunda.bpm.engine.history.HistoricProcessInstance;
24 import org.camunda.bpm.engine.history.HistoricVariableInstance;
25 import org.camunda.bpm.engine.runtime.ProcessInstance;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.DeleteVnfResponse;
29 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.OperationStateEnum;
30 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.QueryJobResponse;
31 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.BaseTest;
32 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants;
33 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.GsonProvider;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.beans.factory.annotation.Qualifier;
36 import org.springframework.http.HttpMethod;
37 import org.springframework.http.MediaType;
38 import org.springframework.http.converter.json.GsonHttpMessageConverter;
39 import org.springframework.test.web.client.MockRestServiceServer;
40 import org.springframework.web.client.RestTemplate;
41 import java.util.HashMap;
42 import java.util.Map;
43 import static org.junit.Assert.assertEquals;
44 import static org.junit.Assert.assertNotNull;
45 import static org.junit.Assert.assertTrue;
46 import static org.onap.so.adapters.etsisol003adapter.lcm.v1.model.OperationStateEnum.COMPLETED;
47 import static org.onap.so.adapters.etsisol003adapter.lcm.v1.model.OperationStateEnum.PROCESSING;
48 import static org.onap.so.adapters.etsisol003adapter.lcm.v1.model.OperationStatusRetrievalStatusEnum.STATUS_FOUND;
49 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.extclients.vnfm.Sol003AdapterConfiguration.SOL003_ADAPTER_REST_TEMPLATE_BEAN;
50 import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
51 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
52 import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
53
54 /**
55  * @author Waqas Ikram (waqas.ikram@est.tech)
56  * @author Andrew Lamb (andrew.a.lamb@est.tech)
57  *
58  */
59 public class MonitorSol003AdapterTerminateJobTaskTest extends BaseTest {
60
61     private static final String MONITOR_SOL003_ADAPTER_TERMINATE_JOB_WORKFLOW = "MonitorSol003AdapterTerminateJob";
62
63     @Autowired
64     @Qualifier(SOL003_ADAPTER_REST_TEMPLATE_BEAN)
65     private RestTemplate restTemplate;
66
67     @Autowired
68     private GsonProvider gsonProvider;
69
70     private MockRestServiceServer mockRestServiceServer;
71     private Gson gson;
72
73     @Before
74     public void before() {
75         wireMockServer.resetAll();
76
77         final MockRestServiceServer.MockRestServiceServerBuilder builder = MockRestServiceServer.bindTo(restTemplate);
78         builder.ignoreExpectOrder(true);
79         mockRestServiceServer = builder.build();
80
81         gson = gsonProvider.getGson();
82         restTemplate.getMessageConverters().add(new GsonHttpMessageConverter(gson));
83     }
84
85
86     @Test
87     public void testMonitorSol003AdapterTerminateJobTaskWorkflow_SuccessfulCase() throws InterruptedException {
88         mockRestServiceServer.expect(requestTo(SOL003_ADAPTER_ENDPOINT_URL + "/jobs/" + RANDOM_JOB_ID))
89                 .andExpect(method(HttpMethod.GET))
90                 .andRespond(withSuccess(gson.toJson(getQueryJobResponse(COMPLETED)), MediaType.APPLICATION_JSON));
91
92         final ProcessInstance processInstance = executeWorkflow(MONITOR_SOL003_ADAPTER_TERMINATE_JOB_WORKFLOW,
93                 RANDOM_JOB_ID, getVariables(RANDOM_JOB_ID, new DeleteVnfResponse().jobId(RANDOM_JOB_ID)));
94         assertTrue(waitForProcessInstanceToFinish(processInstance.getProcessInstanceId()));
95
96         final HistoricProcessInstance historicProcessInstance =
97                 getHistoricProcessInstance(processInstance.getProcessInstanceId());
98         assertNotNull(historicProcessInstance);
99         assertEquals(HistoricProcessInstance.STATE_COMPLETED, historicProcessInstance.getState());
100
101         final HistoricVariableInstance nsResponseVariable = getVariable(processInstance.getProcessInstanceId(),
102                 CamundaVariableNameConstants.OPERATION_STATUS_PARAM_NAME);
103         assertNotNull(nsResponseVariable);
104         assertEquals(COMPLETED, nsResponseVariable.getValue());
105     }
106
107     @Test
108     public void testMonitorSol003AdapterTerminateJobTaskWorkflow_SuccessfulCaseFollowingProcessingDelay()
109             throws InterruptedException {
110         mockRestServiceServer.expect(requestTo(SOL003_ADAPTER_ENDPOINT_URL + "/jobs/" + RANDOM_JOB_ID))
111                 .andExpect(method(HttpMethod.GET))
112                 .andRespond(withSuccess(gson.toJson(getQueryJobResponse(PROCESSING)), MediaType.APPLICATION_JSON));
113         mockRestServiceServer.expect(requestTo(SOL003_ADAPTER_ENDPOINT_URL + "/jobs/" + RANDOM_JOB_ID))
114                 .andExpect(method(HttpMethod.GET))
115                 .andRespond(withSuccess(gson.toJson(getQueryJobResponse(PROCESSING)), MediaType.APPLICATION_JSON));
116         mockRestServiceServer.expect(requestTo(SOL003_ADAPTER_ENDPOINT_URL + "/jobs/" + RANDOM_JOB_ID))
117                 .andExpect(method(HttpMethod.GET))
118                 .andRespond(withSuccess(gson.toJson(getQueryJobResponse(COMPLETED)), MediaType.APPLICATION_JSON));
119
120         final ProcessInstance processInstance = executeWorkflow(MONITOR_SOL003_ADAPTER_TERMINATE_JOB_WORKFLOW,
121                 RANDOM_JOB_ID, getVariables(RANDOM_JOB_ID, new DeleteVnfResponse().jobId(RANDOM_JOB_ID)));
122         assertTrue(waitForProcessInstanceToFinish(processInstance.getProcessInstanceId()));
123
124         final HistoricProcessInstance historicProcessInstance =
125                 getHistoricProcessInstance(processInstance.getProcessInstanceId());
126         assertNotNull(historicProcessInstance);
127         assertEquals(HistoricProcessInstance.STATE_COMPLETED, historicProcessInstance.getState());
128
129         final HistoricVariableInstance nsResponseVariable = getVariable(processInstance.getProcessInstanceId(),
130                 CamundaVariableNameConstants.OPERATION_STATUS_PARAM_NAME);
131         assertNotNull(nsResponseVariable);
132         assertEquals(COMPLETED, nsResponseVariable.getValue());
133     }
134
135     private QueryJobResponse getQueryJobResponse(final OperationStateEnum operationState) {
136         return new QueryJobResponse().id(RANDOM_JOB_ID).operationState(operationState)
137                 .operationStatusRetrievalStatus(STATUS_FOUND);
138     }
139
140     private Map<String, Object> getVariables(final String jobId, final DeleteVnfResponse deleteVnfResponse) {
141         final Map<String, Object> variables = new HashMap<>();
142         variables.put(CamundaVariableNameConstants.JOB_ID_PARAM_NAME, jobId);
143         variables.put(CamundaVariableNameConstants.DELETE_VNF_RESPONSE_PARAM_NAME, deleteVnfResponse);
144
145         return variables;
146     }
147
148 }