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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.etsi.nfvo.ns.workflow.engine.tasks;
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;
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;
55 * @author Waqas Ikram (waqas.ikram@est.tech)
56 * @author Andrew Lamb (andrew.a.lamb@est.tech)
59 public class MonitorSol003AdapterTerminateJobTaskTest extends BaseTest {
61 private static final String MONITOR_SOL003_ADAPTER_TERMINATE_JOB_WORKFLOW = "MonitorSol003AdapterTerminateJob";
64 @Qualifier(SOL003_ADAPTER_REST_TEMPLATE_BEAN)
65 private RestTemplate restTemplate;
68 private GsonProvider gsonProvider;
70 private MockRestServiceServer mockRestServiceServer;
74 public void before() {
75 wireMockServer.resetAll();
77 final MockRestServiceServer.MockRestServiceServerBuilder builder = MockRestServiceServer.bindTo(restTemplate);
78 builder.ignoreExpectOrder(true);
79 mockRestServiceServer = builder.build();
81 gson = gsonProvider.getGson();
82 restTemplate.getMessageConverters().add(new GsonHttpMessageConverter(gson));
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));
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()));
96 final HistoricProcessInstance historicProcessInstance =
97 getHistoricProcessInstance(processInstance.getProcessInstanceId());
98 assertNotNull(historicProcessInstance);
99 assertEquals(HistoricProcessInstance.STATE_COMPLETED, historicProcessInstance.getState());
101 final HistoricVariableInstance nsResponseVariable = getVariable(processInstance.getProcessInstanceId(),
102 CamundaVariableNameConstants.OPERATION_STATUS_PARAM_NAME);
103 assertNotNull(nsResponseVariable);
104 assertEquals(COMPLETED, nsResponseVariable.getValue());
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));
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()));
124 final HistoricProcessInstance historicProcessInstance =
125 getHistoricProcessInstance(processInstance.getProcessInstanceId());
126 assertNotNull(historicProcessInstance);
127 assertEquals(HistoricProcessInstance.STATE_COMPLETED, historicProcessInstance.getState());
129 final HistoricVariableInstance nsResponseVariable = getVariable(processInstance.getProcessInstanceId(),
130 CamundaVariableNameConstants.OPERATION_STATUS_PARAM_NAME);
131 assertNotNull(nsResponseVariable);
132 assertEquals(COMPLETED, nsResponseVariable.getValue());
135 private QueryJobResponse getQueryJobResponse(final OperationStateEnum operationState) {
136 return new QueryJobResponse().id(RANDOM_JOB_ID).operationState(operationState)
137 .operationStatusRetrievalStatus(STATUS_FOUND);
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);