2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.so.apihandlerinfra;
22 import static com.shazam.shazamcrest.MatcherAssert.assertThat;
23 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.anyBoolean;
26 import static org.mockito.ArgumentMatchers.anyString;
27 import static org.mockito.ArgumentMatchers.nullable;
28 import static org.mockito.Mockito.doNothing;
29 import static org.mockito.Mockito.doReturn;
30 import static org.mockito.Mockito.doThrow;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33 import java.io.IOException;
34 import java.nio.file.Files;
35 import java.nio.file.Paths;
36 import java.sql.Timestamp;
37 import java.util.HashMap;
38 import javax.ws.rs.container.ContainerRequestContext;
39 import javax.ws.rs.core.Response;
40 import org.junit.Before;
41 import org.junit.Rule;
42 import org.junit.Test;
43 import org.junit.rules.ExpectedException;
44 import org.junit.runner.RunWith;
45 import org.mockito.InjectMocks;
46 import org.mockito.Mock;
47 import org.mockito.Spy;
48 import org.mockito.junit.MockitoJUnitRunner;
49 import org.onap.so.apihandler.common.RequestClientParameter;
50 import org.onap.so.apihandlerinfra.exceptions.ApiException;
51 import org.onap.so.apihandlerinfra.exceptions.DuplicateRequestException;
52 import org.onap.so.apihandlerinfra.exceptions.RequestDbFailureException;
53 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
54 import org.onap.so.db.request.beans.InfraActiveRequests;
55 import org.onap.so.db.request.client.RequestsDbClient;
56 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
57 import org.springframework.web.client.HttpClientErrorException;
58 import com.fasterxml.jackson.databind.ObjectMapper;
60 @RunWith(MockitoJUnitRunner.class)
61 public class ResumeOrchestrationRequestTest {
63 private RequestHandlerUtils requestHandler;
66 private MsoRequest msoRequest;
69 private ServiceInstances serviceInstances;
72 private RequestsDbClient requestDbClient;
76 private ResumeOrchestrationRequest resumeReq;
79 public ExpectedException thrown = ExpectedException.none();
81 private static final String CURRENT_REQUEST_ID = "eca3a1b1-43ab-457e-ab1c-367263d148b4";
82 private static final String REQUEST_ID = "00032ab7-na18-42e5-965d-8ea592502019";
83 private static final String SERVICE_INSTANCE_ID = "00032ab7-na18-42e5-965d-8ea592502018";
84 private static final String SERVICE_INSTANCE_NAME = "serviceInstanceName";
85 private static final String REQUEST_SCOPE = "service";
86 private final Timestamp startTimeStamp = new Timestamp(System.currentTimeMillis());
87 private final Action action = Action.createInstance;
88 private final Boolean aLaCarte = false;
89 private final String version = "7";
90 private final String requestUri =
91 "http:localhost:6746/onap/so/infra/orchestrationRequests/v7/00032ab7-na18-42e5-965d-8ea592502019/resume";
92 private final RecipeLookupResult lookupResult = new RecipeLookupResult("/mso/async/services/WorkflowActionBB", 80);
93 private RequestClientParameter requestClientParameter = null;
94 private ObjectMapper mapper = new ObjectMapper();
95 private InfraActiveRequests infraActiveRequest = new InfraActiveRequests();
96 private InfraActiveRequests currentActiveRequest = new InfraActiveRequests();
97 private ServiceInstancesRequest sir = new ServiceInstancesRequest();
98 private ServiceInstancesRequest sirNullALaCarte = new ServiceInstancesRequest();
99 private String requestBody = null;
100 private String requestBodyNullALaCarte = null;
101 private ContainerRequestContext requestContext = null;
102 private HashMap<String, String> instanceIdMap = new HashMap<>();
106 public void setup() throws ValidateException, IOException {
107 // Setup general requestHandler mocks
108 when(requestHandler.getRequestUri(any(), anyString())).thenReturn(requestUri);
110 // Setup InfraActiveRequests
111 setInfraActiveRequest();
112 setCurrentActiveRequest();
114 requestBody = infraActiveRequest.getRequestBody();
115 sir = mapper.readValue(requestBody, ServiceInstancesRequest.class);
116 requestBodyNullALaCarte = getRequestBody("/ALaCarteNull.json");
117 sirNullALaCarte = sir = mapper.readValue(requestBodyNullALaCarte, ServiceInstancesRequest.class);
118 setRequestClientParameter();
119 instanceIdMap.put("serviceInstanceId", SERVICE_INSTANCE_ID);
122 public String getRequestBody(String request) throws IOException {
123 request = "src/test/resources/ResumeOrchestrationRequest" + request;
124 return new String(Files.readAllBytes(Paths.get(request)));
127 private void setInfraActiveRequest() throws IOException {
128 infraActiveRequest.setTenantId("tenant-id");
129 infraActiveRequest.setRequestBody(getRequestBody("/RequestBody.json"));
130 infraActiveRequest.setAicCloudRegion("cloudRegion");
131 infraActiveRequest.setRequestScope(REQUEST_SCOPE);
132 infraActiveRequest.setServiceInstanceId(SERVICE_INSTANCE_ID);
133 infraActiveRequest.setServiceInstanceName(SERVICE_INSTANCE_NAME);
134 infraActiveRequest.setRequestStatus(Status.IN_PROGRESS.toString());
135 infraActiveRequest.setRequestAction(Action.createInstance.toString());
136 infraActiveRequest.setServiceType("serviceType");
139 private void setCurrentActiveRequest() throws IOException {
140 currentActiveRequest.setRequestId(CURRENT_REQUEST_ID);
141 currentActiveRequest.setSource("VID");
142 currentActiveRequest.setStartTime(startTimeStamp);
143 currentActiveRequest.setTenantId("tenant-id");
144 currentActiveRequest.setRequestBody(getRequestBody("/RequestBody.json"));
145 currentActiveRequest.setAicCloudRegion("cloudRegion");
146 currentActiveRequest.setRequestScope(REQUEST_SCOPE);
147 currentActiveRequest.setServiceInstanceId(SERVICE_INSTANCE_ID);
148 currentActiveRequest.setServiceInstanceName(SERVICE_INSTANCE_NAME);
149 currentActiveRequest.setRequestStatus(Status.IN_PROGRESS.toString());
150 currentActiveRequest.setLastModifiedBy(Constants.MODIFIED_BY_APIHANDLER);
151 currentActiveRequest.setRequestAction(Action.createInstance.toString());
152 currentActiveRequest.setRequestUrl(requestUri);
153 currentActiveRequest.setRequestorId("xxxxxx");
154 currentActiveRequest.setProgress(new Long(5));
157 private void setRequestClientParameter() {
158 requestClientParameter = new RequestClientParameter.Builder().setRequestId(CURRENT_REQUEST_ID)
159 .setRecipeTimeout(80).setRequestAction(Action.createInstance.toString())
160 .setServiceInstanceId(SERVICE_INSTANCE_ID).setPnfCorrelationId("pnfCorrelationId").setVnfId(null)
161 .setVfModuleId(null).setVolumeGroupId(null).setNetworkId(null).setServiceType("serviceType")
162 .setVnfType(null).setNetworkType(null).setRequestDetails(requestBody).setApiVersion(version)
163 .setALaCarte(aLaCarte).setRequestUri(requestUri).setInstanceGroupId(null).build();
167 public void resumeOrchestationRequestTest() throws Exception {
168 Response response = null;
169 when(requestDbClient.getInfraActiveRequestbyRequestId(REQUEST_ID)).thenReturn(infraActiveRequest);
170 doReturn(currentActiveRequest).when(requestHandler).createNewRecordCopyFromInfraActiveRequest(
171 any(InfraActiveRequests.class), nullable(String.class), any(Timestamp.class), nullable(String.class),
172 nullable(String.class), nullable(String.class));
173 doReturn(response).when(resumeReq).resumeRequest(any(InfraActiveRequests.class), any(InfraActiveRequests.class),
174 anyString(), nullable(String.class));
176 resumeReq.resumeOrchestrationRequest(REQUEST_ID, "v7", requestContext);
178 verify(resumeReq).resumeRequest(infraActiveRequest, currentActiveRequest, version, null);
182 public void resumeOrchestationRequestDbNullResultTest() throws Exception {
183 when(requestDbClient.getInfraActiveRequestbyRequestId("00032ab7-na18-42e5-965d-8ea592502018")).thenReturn(null);
185 thrown.expect(ValidateException.class);
186 thrown.expectMessage(
187 "Null response from requestDB when searching by requestId: 00032ab7-na18-42e5-965d-8ea592502018");
188 resumeReq.resumeOrchestrationRequest("00032ab7-na18-42e5-965d-8ea592502018", "v7", requestContext);
192 public void resumeOrchestationRequestDbErrorTest() throws Exception {
193 when(requestDbClient.getInfraActiveRequestbyRequestId("00032ab7-na18-42e5-965d-8ea592502018"))
194 .thenThrow(HttpClientErrorException.class);
196 thrown.expect(ValidateException.class);
197 thrown.expectMessage("Exception while performing requestDb lookup by requestId");
198 resumeReq.resumeOrchestrationRequest("00032ab7-na18-42e5-965d-8ea592502018", "v7", requestContext);
202 public void resumeRequestTest() throws ApiException, IOException {
203 Response response = null;
204 when(requestHandler.convertJsonToServiceInstanceRequest(anyString(), any(Actions.class), anyString(),
205 anyString())).thenReturn(sir);
206 when(serviceInstances.getPnfCorrelationId(any(ServiceInstancesRequest.class))).thenReturn("pnfCorrelationId");
207 doReturn(lookupResult).when(resumeReq).serviceRecipeLookup(currentActiveRequest, sir, action, aLaCarte);
208 doReturn(requestClientParameter).when(resumeReq).setRequestClientParameter(lookupResult, version,
209 infraActiveRequest, currentActiveRequest, "pnfCorrelationId", aLaCarte, sir);
210 doNothing().when(resumeReq).requestDbSave(currentActiveRequest);
211 when(requestHandler.postBPELRequest(any(InfraActiveRequests.class), any(RequestClientParameter.class),
212 anyString(), anyString())).thenReturn(response);
213 doNothing().when(resumeReq).checkForInProgressRequest(currentActiveRequest, SERVICE_INSTANCE_ID, REQUEST_SCOPE,
214 SERVICE_INSTANCE_NAME, action);
216 resumeReq.resumeRequest(infraActiveRequest, currentActiveRequest, version,
217 "/onap/so/infra/orchestrationRequests/v7/requests/00032ab7-na18-42e5-965d-8ea592502018/resume");
218 verify(requestHandler).postBPELRequest(currentActiveRequest, requestClientParameter,
219 lookupResult.getOrchestrationURI(), infraActiveRequest.getRequestScope());
223 public void serviceRecipeLookupTest() throws ApiException, IOException {
224 when(serviceInstances.getServiceURI(any(ServiceInstancesRequest.class), any(Action.class), anyBoolean()))
225 .thenReturn(lookupResult);
226 RecipeLookupResult result = resumeReq.serviceRecipeLookup(currentActiveRequest, sir, action, aLaCarte);
227 assertThat(result, sameBeanAs(lookupResult));
231 public void setRequestClientParameterTest() throws ApiException, IOException {
232 when(requestHandler.mapJSONtoMSOStyle(anyString(), any(ServiceInstancesRequest.class), anyBoolean(),
233 any(Action.class))).thenReturn(requestBody);
234 RequestClientParameter result = resumeReq.setRequestClientParameter(lookupResult, version, infraActiveRequest,
235 currentActiveRequest, "pnfCorrelationId", aLaCarte, sir);
236 assertThat(requestClientParameter, sameBeanAs(result));
240 public void requestDbSaveTest() throws RequestDbFailureException {
241 doNothing().when(requestDbClient).save(currentActiveRequest);
242 resumeReq.requestDbSave(currentActiveRequest);
243 verify(requestDbClient).save(currentActiveRequest);
247 public void resumeRequestTestALaCarteNull() throws ApiException, IOException {
248 Response response = null;
250 when(requestHandler.convertJsonToServiceInstanceRequest(anyString(), any(Actions.class), anyString(),
251 anyString())).thenReturn(sirNullALaCarte);
252 when(serviceInstances.getPnfCorrelationId(any(ServiceInstancesRequest.class))).thenReturn("pnfCorrelationId");
253 doReturn(lookupResult).when(resumeReq).serviceRecipeLookup(currentActiveRequest, sir, action, aLaCarte);
254 doReturn(requestClientParameter).when(resumeReq).setRequestClientParameter(lookupResult, version,
255 infraActiveRequest, currentActiveRequest, "pnfCorrelationId", aLaCarte, sir);
256 doNothing().when(resumeReq).requestDbSave(currentActiveRequest);
257 when(requestHandler.postBPELRequest(any(InfraActiveRequests.class), any(RequestClientParameter.class),
258 anyString(), anyString())).thenReturn(response);
259 doNothing().when(resumeReq).checkForInProgressRequest(currentActiveRequest, SERVICE_INSTANCE_ID, REQUEST_SCOPE,
260 SERVICE_INSTANCE_NAME, action);
262 resumeReq.resumeRequest(infraActiveRequest, currentActiveRequest, version,
263 "/onap/so/infra/orchestrationRequests/v7/requests/00032ab7-na18-42e5-965d-8ea592502018/resume");
264 verify(requestHandler).postBPELRequest(currentActiveRequest, requestClientParameter,
265 lookupResult.getOrchestrationURI(), infraActiveRequest.getRequestScope());
269 public void serviceRecipeLookupErrorTest() throws IOException, ApiException {
270 when(serviceInstances.getServiceURI(sir, action, aLaCarte))
271 .thenThrow(new IOException("Error occurred on recipe lookup"));
272 doNothing().when(requestHandler).updateStatus(any(InfraActiveRequests.class), any(Status.class), anyString());
274 thrown.expect(ValidateException.class);
275 thrown.expectMessage("Error occurred on recipe lookup");
276 resumeReq.serviceRecipeLookup(currentActiveRequest, sir, action, aLaCarte);
280 public void setRequestClientParameterErrorTest() throws ApiException, IOException {
281 when(requestHandler.mapJSONtoMSOStyle(anyString(), any(ServiceInstancesRequest.class), anyBoolean(),
282 any(Action.class))).thenThrow(new IOException("IOException"));
284 thrown.expect(ValidateException.class);
285 thrown.expectMessage("IOException while generating requestClientParameter to send to BPMN: IOException");
286 resumeReq.setRequestClientParameter(lookupResult, version, infraActiveRequest, currentActiveRequest,
287 "pnfCorrelationId", aLaCarte, sir);
291 public void checkForInProgressRequest() throws ApiException {
292 doReturn(infraActiveRequest).when(requestHandler).duplicateCheck(action, instanceIdMap, SERVICE_INSTANCE_NAME,
293 REQUEST_SCOPE, currentActiveRequest);
294 doReturn(true).when(requestHandler).camundaHistoryCheck(infraActiveRequest, currentActiveRequest);
295 doThrow(DuplicateRequestException.class).when(requestHandler).buildErrorOnDuplicateRecord(currentActiveRequest,
296 action, instanceIdMap, SERVICE_INSTANCE_NAME, REQUEST_SCOPE, infraActiveRequest);
298 thrown.expect(DuplicateRequestException.class);
299 resumeReq.checkForInProgressRequest(currentActiveRequest, SERVICE_INSTANCE_ID, REQUEST_SCOPE,
300 SERVICE_INSTANCE_NAME, action);
304 public void checkForInProgressRequestNoInProgressRequests() throws ApiException {
305 doReturn(null).when(requestHandler).duplicateCheck(action, instanceIdMap, SERVICE_INSTANCE_NAME, REQUEST_SCOPE,
306 currentActiveRequest);
308 resumeReq.checkForInProgressRequest(currentActiveRequest, SERVICE_INSTANCE_ID, REQUEST_SCOPE,
309 SERVICE_INSTANCE_NAME, action);
310 verify(requestHandler).duplicateCheck(action, instanceIdMap, SERVICE_INSTANCE_NAME, REQUEST_SCOPE,
311 currentActiveRequest);
315 public void checkForInProgressRequestCamundaHistoryCheckReturnsNoInProgress() throws ApiException {
316 doReturn(infraActiveRequest).when(requestHandler).duplicateCheck(action, instanceIdMap, SERVICE_INSTANCE_NAME,
317 REQUEST_SCOPE, currentActiveRequest);
318 doReturn(false).when(requestHandler).camundaHistoryCheck(infraActiveRequest, currentActiveRequest);
320 resumeReq.checkForInProgressRequest(currentActiveRequest, SERVICE_INSTANCE_ID, REQUEST_SCOPE,
321 SERVICE_INSTANCE_NAME, action);
322 verify(requestHandler).duplicateCheck(action, instanceIdMap, SERVICE_INSTANCE_NAME, REQUEST_SCOPE,
323 currentActiveRequest);
324 verify(requestHandler).camundaHistoryCheck(infraActiveRequest, currentActiveRequest);