2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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=========================================================
21 package org.onap.so.apihandlerinfra;
24 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
25 import static com.github.tomakehurst.wiremock.client.WireMock.get;
26 import static com.github.tomakehurst.wiremock.client.WireMock.post;
27 import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertFalse;
30 import static org.junit.Assert.assertNotEquals;
31 import static org.junit.Assert.assertNull;
32 import static org.junit.Assert.assertTrue;
33 import static org.onap.logging.filter.base.Constants.HttpHeaders.ONAP_REQUEST_ID;
34 import static org.onap.so.logger.HttpHeadersConstants.REQUESTOR_ID;
35 import static org.onap.logging.filter.base.Constants.HttpHeaders.ONAP_PARTNER_NAME;
36 import static org.onap.logging.filter.base.Constants.HttpHeaders.TRANSACTION_ID;
37 import java.io.IOException;
38 import java.net.MalformedURLException;
40 import java.nio.file.Files;
41 import java.nio.file.Paths;
42 import java.util.List;
43 import javax.ws.rs.core.MediaType;
44 import org.apache.http.HttpStatus;
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.onap.logging.ref.slf4j.ONAPLogConstants;
48 import org.onap.so.apihandlerinfra.exceptions.ContactCamundaException;
49 import org.onap.so.apihandlerinfra.exceptions.RequestDbFailureException;
50 import org.onap.so.db.catalog.beans.Service;
51 import org.onap.so.db.request.beans.InfraActiveRequests;
52 import org.onap.so.logger.HttpHeadersConstants;
53 import org.onap.so.serviceinstancebeans.ModelInfo;
54 import org.onap.so.serviceinstancebeans.ModelType;
55 import org.onap.so.serviceinstancebeans.RequestDetails;
56 import org.onap.so.serviceinstancebeans.RequestInfo;
57 import org.onap.so.serviceinstancebeans.RequestParameters;
58 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
59 import org.springframework.beans.factory.annotation.Autowired;
60 import org.springframework.beans.factory.annotation.Value;
61 import org.springframework.http.HttpEntity;
62 import org.springframework.http.HttpHeaders;
63 import org.springframework.http.HttpMethod;
64 import org.springframework.http.ResponseEntity;
65 import org.springframework.web.util.UriComponentsBuilder;
66 import com.fasterxml.jackson.annotation.JsonInclude.Include;
67 import com.fasterxml.jackson.core.JsonParseException;
68 import com.fasterxml.jackson.core.JsonProcessingException;
69 import com.fasterxml.jackson.databind.DeserializationFeature;
70 import com.fasterxml.jackson.databind.JsonMappingException;
71 import com.fasterxml.jackson.databind.ObjectMapper;
73 public class RequestHandlerUtilsTest extends BaseTest {
75 private final ObjectMapper mapper = new ObjectMapper();
76 private ObjectMapper errorMapper = new ObjectMapper();
79 private RequestHandlerUtils requestHandlerUtils;
82 private CamundaRequestHandler camundaRequestHandler;
84 @Value("${wiremock.server.port}")
85 private String wiremockPort;
87 private URL initialUrl;
88 private int initialPort;
89 private HttpHeaders headers;
92 public void beforeClass() {
93 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
94 errorMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
95 errorMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
97 headers = new HttpHeaders();
98 headers.set(ONAPLogConstants.Headers.PARTNER_NAME, "test_name");
99 headers.set(TRANSACTION_ID, "32807a28-1a14-4b88-b7b3-2950918aa76d");
100 headers.set(ONAP_REQUEST_ID, "32807a28-1a14-4b88-b7b3-2950918aa76d");
101 headers.set(ONAPLogConstants.MDCs.REQUEST_ID, "32807a28-1a14-4b88-b7b3-2950918aa76d");
102 headers.set(ONAP_PARTNER_NAME, "VID");
103 headers.set(REQUESTOR_ID, "xxxxxx");
104 try { // generate one-time port number to avoid RANDOM port number later.
105 initialUrl = new URL(createURLWithPort(Constants.ORCHESTRATION_REQUESTS_PATH));
106 initialPort = initialUrl.getPort();
107 } catch (MalformedURLException e) {
110 wireMockServer.stubFor(post(urlMatching(".*/infraActiveRequests.*")).willReturn(aResponse()
111 .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).withStatus(HttpStatus.SC_OK)));
114 public String inputStream(String JsonInput) throws IOException {
115 JsonInput = "src/test/resources/ServiceInstanceTest" + JsonInput;
116 return new String(Files.readAllBytes(Paths.get(JsonInput)));
119 public ResponseEntity<String> sendRequest(String requestJson, String uriPath, HttpMethod reqMethod,
120 HttpHeaders headers) {
122 if (!headers.containsKey(HttpHeaders.ACCEPT)) {
123 headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
125 if (!headers.containsKey(HttpHeaders.CONTENT_TYPE)) {
126 headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
129 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(uriPath, initialPort));
131 HttpEntity<String> request = new HttpEntity<>(requestJson, headers);
133 return restTemplate.exchange(builder.toUriString(), reqMethod, request, String.class);
136 public ResponseEntity<String> sendRequest(String requestJson, String uriPath, HttpMethod reqMethod) {
137 return sendRequest(requestJson, uriPath, reqMethod, new HttpHeaders());
141 public void test_mapJSONtoMSOStyle() throws IOException {
142 ObjectMapper mapper = new ObjectMapper();
143 mapper.setSerializationInclusion(Include.NON_NULL);
144 String testRequest = inputStream("/ServiceInstanceDefault.json");
145 String resultString = requestHandlerUtils.mapJSONtoMSOStyle(testRequest, null, false, null);
146 ServiceInstancesRequest sir = mapper.readValue(resultString, ServiceInstancesRequest.class);
147 ModelInfo modelInfo = sir.getRequestDetails().getModelInfo();
148 assertEquals("f7ce78bb-423b-11e7-93f8-0050569a796", modelInfo.getModelCustomizationUuid());
149 assertEquals("modelInstanceName", modelInfo.getModelInstanceName());
150 assertEquals("f7ce78bb-423b-11e7-93f8-0050569a7965", modelInfo.getModelInvariantUuid());
151 assertEquals("10", modelInfo.getModelUuid());
156 public void test_mapJSONtoMSOStyleCustomWorkflowRequest() throws IOException {
157 ObjectMapper mapper = new ObjectMapper();
158 mapper.setSerializationInclusion(Include.NON_NULL);
159 String testRequest = inputStream("/CustomWorkflowRequest.json");
160 String resultString =
161 requestHandlerUtils.mapJSONtoMSOStyle(testRequest, null, true, Action.inPlaceSoftwareUpdate);
162 ServiceInstancesRequest sir = mapper.readValue(resultString, ServiceInstancesRequest.class);
163 assertEquals(sir.getRequestDetails().getCloudConfiguration().getTenantId(), "88a6ca3ee0394ade9403f075db23167e");
164 assertNotEquals(sir.getRequestDetails().getRequestParameters().getUserParams().size(), 0);
169 public void test_mapJSONtoMSOStyleUsePreload() throws IOException {
170 ObjectMapper mapper = new ObjectMapper();
171 mapper.setSerializationInclusion(Include.NON_NULL);
172 String testRequest = inputStream("/ServiceInstanceDefault.json");
173 ServiceInstancesRequest sir = new ServiceInstancesRequest();
174 RequestDetails rd = new RequestDetails();
175 RequestParameters rp = new RequestParameters();
176 rp.setUsePreload(true);
177 rd.setRequestParameters(rp);
178 sir.setRequestDetails(rd);
179 String resultString = requestHandlerUtils.mapJSONtoMSOStyle(testRequest, sir, false, null);
180 ServiceInstancesRequest sir1 = mapper.readValue(resultString, ServiceInstancesRequest.class);
181 assertTrue(sir1.getRequestDetails().getRequestParameters().getUsePreload());
185 public void setServiceTypeTestALaCarte() throws JsonProcessingException {
186 String requestScope = ModelType.service.toString();
187 Boolean aLaCarteFlag = true;
188 ServiceInstancesRequest sir = new ServiceInstancesRequest();
189 RequestDetails requestDetails = new RequestDetails();
190 RequestInfo requestInfo = new RequestInfo();
191 requestInfo.setSource("VID");
192 requestDetails.setRequestInfo(requestInfo);
193 sir.setRequestDetails(requestDetails);
194 Service defaultService = new Service();
195 defaultService.setServiceType("testServiceTypeALaCarte");
197 wireMockServer.stubFor(get(urlMatching(".*/service/search/.*"))
198 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
199 .withBody(mapper.writeValueAsString(defaultService)).withStatus(HttpStatus.SC_OK)));
201 String serviceType = requestHandlerUtils.getServiceType(requestScope, sir, aLaCarteFlag);
202 assertEquals(serviceType, "testServiceTypeALaCarte");
206 public void setServiceTypeTest() throws JsonProcessingException {
207 String requestScope = ModelType.service.toString();
208 Boolean aLaCarteFlag = false;
209 ServiceInstancesRequest sir = new ServiceInstancesRequest();
210 RequestDetails requestDetails = new RequestDetails();
211 RequestInfo requestInfo = new RequestInfo();
212 ModelInfo modelInfo = new ModelInfo();
213 modelInfo.setModelVersionId("0dd91181-49da-446b-b839-cd959a96f04a");
214 requestInfo.setSource("VID");
215 requestDetails.setModelInfo(modelInfo);
216 requestDetails.setRequestInfo(requestInfo);
217 sir.setRequestDetails(requestDetails);
218 Service defaultService = new Service();
219 defaultService.setServiceType("testServiceType");
221 wireMockServer.stubFor(get(urlMatching(".*/service/0dd91181-49da-446b-b839-cd959a96f04a"))
222 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
223 .withBody(mapper.writeValueAsString(defaultService)).withStatus(HttpStatus.SC_OK)));
225 String serviceType = requestHandlerUtils.getServiceType(requestScope, sir, aLaCarteFlag);
226 assertEquals(serviceType, "testServiceType");
230 public void setServiceTypeTestDefault() throws JsonProcessingException {
231 String requestScope = ModelType.service.toString();
232 Boolean aLaCarteFlag = false;
233 ServiceInstancesRequest sir = new ServiceInstancesRequest();
234 RequestDetails requestDetails = new RequestDetails();
235 RequestInfo requestInfo = new RequestInfo();
236 ModelInfo modelInfo = new ModelInfo();
237 modelInfo.setModelVersionId("0dd91181-49da-446b-b839-cd959a96f04a");
238 requestInfo.setSource("VID");
239 requestDetails.setModelInfo(modelInfo);
240 requestDetails.setRequestInfo(requestInfo);
241 sir.setRequestDetails(requestDetails);
242 Service defaultService = new Service();
243 defaultService.setServiceType("testServiceType");
245 wireMockServer.stubFor(get(urlMatching(".*/service/0dd91181-49da-446b-b839-cd959a96f04a"))
246 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
247 .withStatus(HttpStatus.SC_NOT_FOUND)));
248 wireMockServer.stubFor(get(urlMatching(".*/service/search/.*"))
249 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
250 .withBody(mapper.writeValueAsString(defaultService)).withStatus(HttpStatus.SC_OK)));
252 String serviceType = requestHandlerUtils.getServiceType(requestScope, sir, aLaCarteFlag);
253 assertEquals(serviceType, "testServiceType");
257 public void setServiceTypeTestNetwork() throws JsonProcessingException {
258 String requestScope = ModelType.network.toString();
259 Boolean aLaCarteFlag = null;
260 ServiceInstancesRequest sir = new ServiceInstancesRequest();
261 RequestDetails requestDetails = new RequestDetails();
262 RequestInfo requestInfo = new RequestInfo();
263 ModelInfo modelInfo = new ModelInfo();
264 modelInfo.setModelName("networkModelName");
265 requestInfo.setSource("VID");
266 requestDetails.setModelInfo(modelInfo);
267 requestDetails.setRequestInfo(requestInfo);
268 sir.setRequestDetails(requestDetails);
270 String serviceType = requestHandlerUtils.getServiceType(requestScope, sir, aLaCarteFlag);
271 assertEquals(serviceType, "networkModelName");
275 public void setServiceInstanceIdInstanceGroupTest() throws JsonParseException, JsonMappingException, IOException {
276 String requestScope = "instanceGroup";
277 ServiceInstancesRequest sir =
278 mapper.readValue(inputStream("/CreateInstanceGroup.json"), ServiceInstancesRequest.class);
279 assertEquals("ddcbbf3d-f2c1-4ca0-8852-76a807285efc",
280 requestHandlerUtils.setServiceInstanceId(requestScope, sir));
284 public void setServiceInstanceIdTest() {
285 String requestScope = "vnf";
286 ServiceInstancesRequest sir = new ServiceInstancesRequest();
287 sir.setServiceInstanceId("f0a35706-efc4-4e27-80ea-a995d7a2a40f");
288 assertEquals("f0a35706-efc4-4e27-80ea-a995d7a2a40f",
289 requestHandlerUtils.setServiceInstanceId(requestScope, sir));
293 public void setServiceInstanceIdReturnNullTest() {
294 String requestScope = "vnf";
295 ServiceInstancesRequest sir = new ServiceInstancesRequest();
296 assertNull(requestHandlerUtils.setServiceInstanceId(requestScope, sir));
300 public void camundaHistoryCheckTest() throws ContactCamundaException, RequestDbFailureException {
301 wireMockServer.stubFor(get(
302 ("/sobpmnengine/history/process-instance?variables=mso-request-id_eq_f0a35706-efc4-4e27-80ea-a995d7a2a40f"))
303 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
304 .withBodyFile("Camunda/HistoryCheckResponse.json")
305 .withStatus(org.apache.http.HttpStatus.SC_OK)));
307 InfraActiveRequests duplicateRecord = new InfraActiveRequests();
308 duplicateRecord.setRequestId("f0a35706-efc4-4e27-80ea-a995d7a2a40f");
309 boolean inProgress = false;
310 inProgress = requestHandlerUtils.camundaHistoryCheck(duplicateRecord, null);
311 assertTrue(inProgress);
315 public void camundaHistoryCheckNoneFoundTest() throws ContactCamundaException, RequestDbFailureException {
316 wireMockServer.stubFor(get(
317 ("/sobpmnengine/history/process-instance?variables=mso-request-id_eq_f0a35706-efc4-4e27-80ea-a995d7a2a40f"))
318 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
319 .withBody("[]").withStatus(org.apache.http.HttpStatus.SC_OK)));
321 InfraActiveRequests duplicateRecord = new InfraActiveRequests();
322 duplicateRecord.setRequestId("f0a35706-efc4-4e27-80ea-a995d7a2a40f");
323 boolean inProgress = false;
324 inProgress = requestHandlerUtils.camundaHistoryCheck(duplicateRecord, null);
325 assertFalse(inProgress);
329 public void camundaHistoryCheckNotInProgressTest() throws ContactCamundaException, RequestDbFailureException {
330 wireMockServer.stubFor(get(
331 ("/sobpmnengine/history/process-instance?variables=mso-request-id_eq_f0a35706-efc4-4e27-80ea-a995d7a2a40f"))
332 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
333 .withBodyFile("Camunda/HistoryCheckResponseCompleted.json")
334 .withStatus(org.apache.http.HttpStatus.SC_OK)));
336 InfraActiveRequests duplicateRecord = new InfraActiveRequests();
337 duplicateRecord.setRequestId("f0a35706-efc4-4e27-80ea-a995d7a2a40f");
338 boolean inProgress = false;
339 inProgress = requestHandlerUtils.camundaHistoryCheck(duplicateRecord, null);
340 assertFalse(inProgress);
344 public void setCamundaHeadersTest() throws ContactCamundaException, RequestDbFailureException {
345 String encryptedAuth = "015E7ACF706C6BBF85F2079378BDD2896E226E09D13DC2784BA309E27D59AB9FAD3A5E039DF0BB8408"; // user:password
346 String key = "07a7159d3bf51a0e53be7a8f89699be7";
347 HttpHeaders headers = camundaRequestHandler.setCamundaHeaders(encryptedAuth, key);
348 List<org.springframework.http.MediaType> acceptedType = headers.getAccept();
349 String expectedAcceptedType = "application/json";
350 assertEquals(expectedAcceptedType, acceptedType.get(0).toString());
351 String basicAuth = headers.getFirst(HttpHeaders.AUTHORIZATION);
352 String expectedBasicAuth = "Basic dXNlcjpwYXNzd29yZA==";
353 assertEquals(expectedBasicAuth, basicAuth);