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