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 static org.junit.Assert.assertEquals;
 
  23 import static org.junit.Assert.assertNotNull;
 
  24 import static org.junit.Assert.assertNull;
 
  25 import static org.junit.Assert.assertTrue;
 
  26 import static org.onap.so.adapters.etsisol003adapter.lcm.v1.model.OperationStateEnum.COMPLETED;
 
  27 import static org.onap.so.adapters.etsisol003adapter.lcm.v1.model.OperationStatusRetrievalStatusEnum.STATUS_FOUND;
 
  28 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.extclients.vnfm.Sol003AdapterConfiguration.SOL003_ADAPTER_REST_TEMPLATE_BEAN;
 
  29 import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
 
  30 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
 
  31 import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
 
  32 import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
 
  33 import java.util.HashMap;
 
  35 import org.camunda.bpm.engine.history.HistoricProcessInstance;
 
  36 import org.camunda.bpm.engine.history.HistoricVariableInstance;
 
  37 import org.camunda.bpm.engine.runtime.ProcessInstance;
 
  38 import org.junit.Before;
 
  39 import org.junit.Test;
 
  40 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.CreateVnfResponse;
 
  41 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.QueryJobResponse;
 
  42 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.BaseTest;
 
  43 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants;
 
  44 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.GsonProvider;
 
  45 import org.onap.so.etsi.nfvo.ns.lcm.model.InlineResponse400;
 
  46 import org.springframework.beans.factory.annotation.Autowired;
 
  47 import org.springframework.beans.factory.annotation.Qualifier;
 
  48 import org.springframework.http.HttpMethod;
 
  49 import org.springframework.http.HttpStatus;
 
  50 import org.springframework.http.MediaType;
 
  51 import org.springframework.http.converter.json.GsonHttpMessageConverter;
 
  52 import org.springframework.test.web.client.MockRestServiceServer;
 
  53 import org.springframework.web.client.RestTemplate;
 
  54 import com.google.gson.Gson;
 
  57  * @author Waqas Ikram (waqas.ikram@est.tech)
 
  60 public class MonitorSol003AdapterCreateJobTaskTest extends BaseTest {
 
  62     private static final String MONITOR_SOL003_ADAPTER_CREATE_JOB_WORKFLOW = "MonitorSol003AdapterCreateJob";
 
  65     @Qualifier(SOL003_ADAPTER_REST_TEMPLATE_BEAN)
 
  66     private RestTemplate restTemplate;
 
  69     private GsonProvider gsonProvider;
 
  71     private MockRestServiceServer mockRestServiceServer;
 
  75     public void before() {
 
  76         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 testMonitorSol003AdapterCreateJobTaskWorkflow_SuccessfullCase() throws InterruptedException {
 
  89         mockRestServiceServer.expect(requestTo(SOL003_ADAPTER_ENDPOINT_URL + "/jobs/" + RANDOM_JOB_ID))
 
  90                 .andExpect(method(HttpMethod.GET))
 
  91                 .andRespond(withSuccess(gson.toJson(getQueryJobResponse()), MediaType.APPLICATION_JSON));
 
  93         final ProcessInstance processInstance = executeWorkflow(MONITOR_SOL003_ADAPTER_CREATE_JOB_WORKFLOW,
 
  94                 RANDOM_JOB_ID, getVariables(RANDOM_JOB_ID, new CreateVnfResponse().jobId(RANDOM_JOB_ID)));
 
  96         assertTrue(waitForProcessInstanceToFinish(processInstance.getProcessInstanceId()));
 
  98         final HistoricProcessInstance historicProcessInstance =
 
  99                 getHistoricProcessInstance(processInstance.getProcessInstanceId());
 
 100         assertNotNull(historicProcessInstance);
 
 102         assertEquals(HistoricProcessInstance.STATE_COMPLETED, historicProcessInstance.getState());
 
 104         final HistoricVariableInstance nsResponseVariable = getVariable(processInstance.getProcessInstanceId(),
 
 105                 CamundaVariableNameConstants.OPERATION_STATUS_PARAM_NAME);
 
 107         assertNotNull(nsResponseVariable);
 
 108         assertEquals(COMPLETED, nsResponseVariable.getValue());
 
 113     public void testMonitorSol003AdapterCreateJobTaskWorkflow_FailurelCase() throws InterruptedException {
 
 115         mockRestServiceServer.expect(requestTo(SOL003_ADAPTER_ENDPOINT_URL + "/jobs/" + RANDOM_JOB_ID))
 
 116                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.NOT_FOUND));
 
 118         final ProcessInstance processInstance = executeWorkflow(MONITOR_SOL003_ADAPTER_CREATE_JOB_WORKFLOW,
 
 119                 RANDOM_JOB_ID, getVariables(RANDOM_JOB_ID, new CreateVnfResponse().jobId(RANDOM_JOB_ID)));
 
 121         assertTrue(waitForProcessInstanceToFinish(processInstance.getProcessInstanceId()));
 
 123         final HistoricProcessInstance historicProcessInstance =
 
 124                 getHistoricProcessInstance(processInstance.getProcessInstanceId());
 
 125         assertNotNull(historicProcessInstance);
 
 127         assertEquals(HistoricProcessInstance.STATE_COMPLETED, historicProcessInstance.getState());
 
 129         final HistoricVariableInstance nsResponseVariable = getVariable(processInstance.getProcessInstanceId(),
 
 130                 CamundaVariableNameConstants.OPERATION_STATUS_PARAM_NAME);
 
 132         assertNull(nsResponseVariable);
 
 134         final HistoricVariableInstance workflowExceptionVariable = getVariable(processInstance.getProcessInstanceId(),
 
 135                 CamundaVariableNameConstants.CREATE_NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME);
 
 137         final InlineResponse400 problemDetails = (InlineResponse400) workflowExceptionVariable.getValue();
 
 138         assertNotNull(problemDetails);
 
 139         assertNotNull(problemDetails.getDetail());
 
 143     private QueryJobResponse getQueryJobResponse() {
 
 144         return new QueryJobResponse().id(RANDOM_JOB_ID).operationState(COMPLETED)
 
 145                 .operationStatusRetrievalStatus(STATUS_FOUND);
 
 148     private Map<String, Object> getVariables(final String jobId, final CreateVnfResponse createVnfResponse) {
 
 149         final Map<String, Object> variables = new HashMap<>();
 
 150         variables.put(CamundaVariableNameConstants.JOB_ID_PARAM_NAME, jobId);
 
 151         variables.put(CamundaVariableNameConstants.CREATE_VNF_RESPONSE_PARAM_NAME, createVnfResponse);