Merge "Removing so-monitoring module"
[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 / MonitorSol003AdapterCreateJobTaskTest.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 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;
34 import java.util.Map;
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;
55
56 /**
57  * @author Waqas Ikram (waqas.ikram@est.tech)
58  *
59  */
60 public class MonitorSol003AdapterCreateJobTaskTest extends BaseTest {
61
62     private static final String MONITOR_SOL003_ADAPTER_CREATE_JOB_WORKFLOW = "MonitorSol003AdapterCreateJob";
63
64     @Autowired
65     @Qualifier(SOL003_ADAPTER_REST_TEMPLATE_BEAN)
66     private RestTemplate restTemplate;
67
68     @Autowired
69     private GsonProvider gsonProvider;
70
71     private MockRestServiceServer mockRestServiceServer;
72     private Gson gson;
73
74     @Before
75     public void before() {
76         wireMockServer.resetAll();
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 testMonitorSol003AdapterCreateJobTaskWorkflow_SuccessfullCase() throws InterruptedException {
88
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));
92
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)));
95
96         assertTrue(waitForProcessInstanceToFinish(processInstance.getProcessInstanceId()));
97
98         final HistoricProcessInstance historicProcessInstance =
99                 getHistoricProcessInstance(processInstance.getProcessInstanceId());
100         assertNotNull(historicProcessInstance);
101
102         assertEquals(HistoricProcessInstance.STATE_COMPLETED, historicProcessInstance.getState());
103
104         final HistoricVariableInstance nsResponseVariable = getVariable(processInstance.getProcessInstanceId(),
105                 CamundaVariableNameConstants.OPERATION_STATUS_PARAM_NAME);
106
107         assertNotNull(nsResponseVariable);
108         assertEquals(COMPLETED, nsResponseVariable.getValue());
109
110     }
111
112     @Test
113     public void testMonitorSol003AdapterCreateJobTaskWorkflow_FailurelCase() throws InterruptedException {
114
115         mockRestServiceServer.expect(requestTo(SOL003_ADAPTER_ENDPOINT_URL + "/jobs/" + RANDOM_JOB_ID))
116                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.NOT_FOUND));
117
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)));
120
121         assertTrue(waitForProcessInstanceToFinish(processInstance.getProcessInstanceId()));
122
123         final HistoricProcessInstance historicProcessInstance =
124                 getHistoricProcessInstance(processInstance.getProcessInstanceId());
125         assertNotNull(historicProcessInstance);
126
127         assertEquals(HistoricProcessInstance.STATE_COMPLETED, historicProcessInstance.getState());
128
129         final HistoricVariableInstance nsResponseVariable = getVariable(processInstance.getProcessInstanceId(),
130                 CamundaVariableNameConstants.OPERATION_STATUS_PARAM_NAME);
131
132         assertNull(nsResponseVariable);
133
134         final HistoricVariableInstance workflowExceptionVariable = getVariable(processInstance.getProcessInstanceId(),
135                 CamundaVariableNameConstants.NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME);
136
137         final InlineResponse400 problemDetails = (InlineResponse400) workflowExceptionVariable.getValue();
138         assertNotNull(problemDetails);
139         assertNotNull(problemDetails.getDetail());
140     }
141
142
143     private QueryJobResponse getQueryJobResponse() {
144         return new QueryJobResponse().id(RANDOM_JOB_ID).operationState(COMPLETED)
145                 .operationStatusRetrievalStatus(STATUS_FOUND);
146     }
147
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);
152
153         return variables;
154     }
155
156 }