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 com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
29 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
30 import static org.junit.Assert.assertEquals;
31 import static org.junit.Assert.assertThat;
32 import static org.onap.so.logger.HttpHeadersConstants.ONAP_REQUEST_ID;
33 import static org.onap.so.logger.HttpHeadersConstants.REQUESTOR_ID;
34 import static org.onap.so.logger.MdcConstants.CLIENT_ID;
37 import java.io.IOException;
38 import java.net.MalformedURLException;
40 import java.nio.file.Files;
41 import java.nio.file.Paths;
43 import javax.ws.rs.core.MediaType;
44 import javax.ws.rs.core.Response;
46 import org.apache.http.HttpStatus;
47 import org.junit.Before;
48 import org.junit.Test;
49 import org.onap.logging.ref.slf4j.ONAPLogConstants;
50 import org.onap.so.logger.HttpHeadersConstants;
51 import org.onap.so.serviceinstancebeans.RequestReferences;
52 import org.onap.so.serviceinstancebeans.ServiceInstancesResponse;
53 import org.springframework.beans.factory.annotation.Value;
54 import org.springframework.http.HttpEntity;
55 import org.springframework.http.HttpHeaders;
56 import org.springframework.http.HttpMethod;
57 import org.springframework.http.ResponseEntity;
58 import org.springframework.util.ResourceUtils;
59 import org.springframework.web.util.UriComponentsBuilder;
61 import com.fasterxml.jackson.databind.DeserializationFeature;
62 import com.fasterxml.jackson.databind.ObjectMapper;
64 public class InstanceManagementTest extends BaseTest{
66 private final ObjectMapper mapper = new ObjectMapper();
67 private ObjectMapper errorMapper = new ObjectMapper();
69 @Value("${wiremock.server.port}")
70 private String wiremockPort;
72 private final String instanceManagementUri = "/onap/so/infra/instanceManagement/";
76 private URL initialUrl;
77 private int initialPort;
78 private HttpHeaders headers;
81 public void beforeClass() {
82 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
83 errorMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
84 errorMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
86 headers = new HttpHeaders();
87 headers.set(ONAPLogConstants.Headers.PARTNER_NAME, "test_name");
88 headers.set(HttpHeadersConstants.TRANSACTION_ID, "32807a28-1a14-4b88-b7b3-2950918aa76d");
89 headers.set(ONAP_REQUEST_ID, "32807a28-1a14-4b88-b7b3-2950918aa76d");
90 headers.set(ONAPLogConstants.MDCs.REQUEST_ID, "32807a28-1a14-4b88-b7b3-2950918aa76d");
91 headers.set(CLIENT_ID, "VID");
92 headers.set(REQUESTOR_ID, "xxxxxx");
93 try { // generate one-time port number to avoid RANDOM port number later.
94 initialUrl = new URL(createURLWithPort(Constants.ORCHESTRATION_REQUESTS_PATH));
95 initialPort = initialUrl.getPort();
96 } catch (MalformedURLException e) {
99 wireMockServer.stubFor(post(urlMatching(".*/infraActiveRequests.*"))
100 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
101 .withStatus(HttpStatus.SC_OK)));
104 public String inputStream(String JsonInput)throws IOException{
105 JsonInput = "src/test/resources/ServiceInstanceTest" + JsonInput;
106 return new String(Files.readAllBytes(Paths.get(JsonInput)));
109 private URL createExpectedSelfLink(String version, String requestId) {
110 System.out.println("createdUrl: " + initialUrl.toString());
112 selfLink = new URL(initialUrl.toString().concat("/").concat(version).concat("/").concat(requestId));
113 } catch (MalformedURLException e) {
119 private String getWiremockResponseForCatalogdb(String file) {
121 File resource= ResourceUtils.getFile("classpath:__files/catalogdb/"+file);
122 return new String(Files.readAllBytes(resource.toPath())).replaceAll("localhost:8090","localhost:"+wiremockPort);
123 } catch (IOException e) {
130 public ResponseEntity<String> sendRequest(String requestJson, String uriPath, HttpMethod reqMethod, HttpHeaders headers){
132 if (!headers.containsKey(HttpHeaders.ACCEPT)) {
133 headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
135 if (!headers.containsKey(HttpHeaders.CONTENT_TYPE)) {
136 headers.set(HttpHeaders.CONTENT_TYPE,MediaType.APPLICATION_JSON);
139 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(uriPath, initialPort));
141 HttpEntity<String> request = new HttpEntity<>(requestJson, headers);
143 return restTemplate.exchange(builder.toUriString(),
144 reqMethod, request, String.class);
147 public ResponseEntity<String> sendRequest(String requestJson, String uriPath, HttpMethod reqMethod){
148 return sendRequest(requestJson, uriPath, reqMethod, new HttpHeaders());
152 public void executeCustomWorkflow() throws IOException {
153 wireMockServer.stubFor(post(urlPathEqualTo("/mso/async/services/testingWorkflow"))
154 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
155 .withBodyFile("Camunda/TestResponse.json").withStatus(org.apache.http.HttpStatus.SC_OK)));
157 wireMockServer.stubFor(get(urlMatching(".*/workflow/search/findByArtifactUUID[?]artifactUUID=71526781-e55c-4cb7-adb3-97e09d9c76be"))
158 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
159 .withBody(getWiremockResponseForCatalogdb("workflow_Response.json"))
160 .withStatus(org.apache.http.HttpStatus.SC_OK)));
163 ServiceInstancesResponse expectedResponse = new ServiceInstancesResponse();
164 RequestReferences requestReferences = new RequestReferences();
165 requestReferences.setInstanceId("1882939");
166 requestReferences.setRequestSelfLink(createExpectedSelfLink("v1","32807a28-1a14-4b88-b7b3-2950918aa76d"));
167 expectedResponse.setRequestReferences(requestReferences);
168 uri = instanceManagementUri + "v1" + "/serviceInstances/f7ce78bb-423b-11e7-93f8-0050569a7968/vnfs/ff305d54-75b4-431b-adb2-eb6b9e5ff000/workflows/71526781-e55c-4cb7-adb3-97e09d9c76be";
169 ResponseEntity<String> response = sendRequest(inputStream("/ExecuteCustomWorkflow.json"), uri, HttpMethod.POST, headers);
171 assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatusCode().value());
172 ServiceInstancesResponse realResponse = mapper.readValue(response.getBody(), ServiceInstancesResponse.class);
173 assertThat(realResponse, sameBeanAs(expectedResponse).ignoring("requestReferences.requestId"));