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.logging.filter.base.Constants.HttpHeaders.ONAP_REQUEST_ID;
33 import static org.onap.so.logger.HttpHeadersConstants.REQUESTOR_ID;
34 import static org.onap.logging.filter.base.Constants.HttpHeaders.CLIENT_ID;
35 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 javax.ws.rs.core.MediaType;
43 import javax.ws.rs.core.Response;
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.logger.HttpHeadersConstants;
49 import org.onap.so.serviceinstancebeans.RequestReferences;
50 import org.onap.so.serviceinstancebeans.ServiceInstancesResponse;
51 import org.springframework.beans.factory.annotation.Value;
52 import org.springframework.http.HttpEntity;
53 import org.springframework.http.HttpHeaders;
54 import org.springframework.http.HttpMethod;
55 import org.springframework.http.ResponseEntity;
56 import org.springframework.util.ResourceUtils;
57 import org.springframework.web.util.UriComponentsBuilder;
58 import com.fasterxml.jackson.databind.DeserializationFeature;
59 import com.fasterxml.jackson.databind.ObjectMapper;
61 public class InstanceManagementTest extends BaseTest {
63 private final ObjectMapper mapper = new ObjectMapper();
64 private ObjectMapper errorMapper = new ObjectMapper();
66 @Value("${wiremock.server.port}")
67 private String wiremockPort;
69 private final String instanceManagementUri = "/onap/so/infra/instanceManagement/";
73 private URL initialUrl;
74 private int initialPort;
75 private HttpHeaders headers;
78 public void beforeClass() {
79 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
80 errorMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
81 errorMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
83 headers = new HttpHeaders();
84 headers.set(ONAPLogConstants.Headers.PARTNER_NAME, "test_name");
85 headers.set(TRANSACTION_ID, "32807a28-1a14-4b88-b7b3-2950918aa76d");
86 headers.set(ONAP_REQUEST_ID, "32807a28-1a14-4b88-b7b3-2950918aa76d");
87 headers.set(ONAPLogConstants.MDCs.REQUEST_ID, "32807a28-1a14-4b88-b7b3-2950918aa76d");
88 headers.set(CLIENT_ID, "VID");
89 headers.set(REQUESTOR_ID, "xxxxxx");
90 try { // generate one-time port number to avoid RANDOM port number later.
91 initialUrl = new URL(createURLWithPort(Constants.ORCHESTRATION_REQUESTS_PATH));
92 initialPort = initialUrl.getPort();
93 } catch (MalformedURLException e) {
96 wireMockServer.stubFor(post(urlMatching(".*/infraActiveRequests.*")).willReturn(aResponse()
97 .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).withStatus(HttpStatus.SC_OK)));
100 public String inputStream(String JsonInput) throws IOException {
101 JsonInput = "src/test/resources/ServiceInstanceTest" + JsonInput;
102 return new String(Files.readAllBytes(Paths.get(JsonInput)));
105 private URL createExpectedSelfLink(String version, String requestId) {
106 System.out.println("createdUrl: " + initialUrl.toString());
108 selfLink = new URL(initialUrl.toString().concat("/").concat(version).concat("/").concat(requestId));
109 } catch (MalformedURLException e) {
115 private String getWiremockResponseForCatalogdb(String file) {
117 File resource = ResourceUtils.getFile("classpath:__files/catalogdb/" + file);
118 return new String(Files.readAllBytes(resource.toPath())).replaceAll("localhost:8090",
119 "localhost:" + wiremockPort);
120 } catch (IOException e) {
127 public ResponseEntity<String> sendRequest(String requestJson, String uriPath, HttpMethod reqMethod,
128 HttpHeaders headers) {
130 if (!headers.containsKey(HttpHeaders.ACCEPT)) {
131 headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
133 if (!headers.containsKey(HttpHeaders.CONTENT_TYPE)) {
134 headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
137 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(uriPath, initialPort));
139 HttpEntity<String> request = new HttpEntity<>(requestJson, headers);
141 return restTemplate.exchange(builder.toUriString(), reqMethod, request, String.class);
144 public ResponseEntity<String> sendRequest(String requestJson, String uriPath, HttpMethod reqMethod) {
145 return sendRequest(requestJson, uriPath, reqMethod, new HttpHeaders());
149 public void executeCustomWorkflow() throws IOException {
150 wireMockServer.stubFor(post(urlPathEqualTo("/mso/async/services/testingWorkflow"))
151 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
152 .withBodyFile("Camunda/TestResponse.json").withStatus(org.apache.http.HttpStatus.SC_OK)));
154 wireMockServer.stubFor(get(urlMatching(
155 ".*/workflow/search/findByArtifactUUID[?]artifactUUID=71526781-e55c-4cb7-adb3-97e09d9c76be"))
156 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
157 .withBody(getWiremockResponseForCatalogdb("workflow_Response.json"))
158 .withStatus(org.apache.http.HttpStatus.SC_OK)));
161 ServiceInstancesResponse expectedResponse = new ServiceInstancesResponse();
162 RequestReferences requestReferences = new RequestReferences();
163 requestReferences.setInstanceId("1882939");
164 requestReferences.setRequestSelfLink(createExpectedSelfLink("v1", "32807a28-1a14-4b88-b7b3-2950918aa76d"));
165 expectedResponse.setRequestReferences(requestReferences);
166 uri = instanceManagementUri + "v1"
167 + "/serviceInstances/f7ce78bb-423b-11e7-93f8-0050569a7968/vnfs/ff305d54-75b4-431b-adb2-eb6b9e5ff000/workflows/71526781-e55c-4cb7-adb3-97e09d9c76be";
168 ResponseEntity<String> response =
169 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"));