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.Autowired;
54 import org.springframework.beans.factory.annotation.Value;
55 import org.springframework.http.HttpEntity;
56 import org.springframework.http.HttpHeaders;
57 import org.springframework.http.HttpMethod;
58 import org.springframework.http.ResponseEntity;
59 import org.springframework.util.ResourceUtils;
60 import org.springframework.web.util.UriComponentsBuilder;
62 import com.fasterxml.jackson.databind.DeserializationFeature;
63 import com.fasterxml.jackson.databind.ObjectMapper;
65 public class InstanceManagementTest extends BaseTest{
67 private final ObjectMapper mapper = new ObjectMapper();
68 private ObjectMapper errorMapper = new ObjectMapper();
71 private InstanceManagement instanceManagement;
73 @Value("${wiremock.server.port}")
74 private String wiremockPort;
76 private final String instanceManagementUri = "/onap/so/infra/instanceManagement/";
80 private URL initialUrl;
81 private int initialPort;
82 private HttpHeaders headers;
85 public void beforeClass() {
86 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
87 errorMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
88 errorMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
90 headers = new HttpHeaders();
91 headers.set(ONAPLogConstants.Headers.PARTNER_NAME, "test_name");
92 headers.set(HttpHeadersConstants.TRANSACTION_ID, "32807a28-1a14-4b88-b7b3-2950918aa76d");
93 headers.set(ONAP_REQUEST_ID, "32807a28-1a14-4b88-b7b3-2950918aa76d");
94 headers.set(ONAPLogConstants.MDCs.REQUEST_ID, "32807a28-1a14-4b88-b7b3-2950918aa76d");
95 headers.set(CLIENT_ID, "VID");
96 headers.set(REQUESTOR_ID, "xxxxxx");
97 try { // generate one-time port number to avoid RANDOM port number later.
98 initialUrl = new URL(createURLWithPort(Constants.ORCHESTRATION_REQUESTS_PATH));
99 initialPort = initialUrl.getPort();
100 } catch (MalformedURLException e) {
103 wireMockServer.stubFor(post(urlMatching(".*/infraActiveRequests.*"))
104 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
105 .withStatus(HttpStatus.SC_OK)));
108 public String inputStream(String JsonInput)throws IOException{
109 JsonInput = "src/test/resources/ServiceInstanceTest" + JsonInput;
110 return new String(Files.readAllBytes(Paths.get(JsonInput)));
113 private URL createExpectedSelfLink(String version, String requestId) {
114 System.out.println("createdUrl: " + initialUrl.toString());
116 selfLink = new URL(initialUrl.toString().concat("/").concat(version).concat("/").concat(requestId));
117 } catch (MalformedURLException e) {
123 private String getWiremockResponseForCatalogdb(String file) {
125 File resource= ResourceUtils.getFile("classpath:__files/catalogdb/"+file);
126 return new String(Files.readAllBytes(resource.toPath())).replaceAll("localhost:8090","localhost:"+wiremockPort);
127 } catch (IOException e) {
134 public ResponseEntity<String> sendRequest(String requestJson, String uriPath, HttpMethod reqMethod, HttpHeaders headers){
136 if (!headers.containsKey(HttpHeaders.ACCEPT)) {
137 headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
139 if (!headers.containsKey(HttpHeaders.CONTENT_TYPE)) {
140 headers.set(HttpHeaders.CONTENT_TYPE,MediaType.APPLICATION_JSON);
143 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(uriPath, initialPort));
145 HttpEntity<String> request = new HttpEntity<>(requestJson, headers);
147 return restTemplate.exchange(builder.toUriString(),
148 reqMethod, request, String.class);
151 public ResponseEntity<String> sendRequest(String requestJson, String uriPath, HttpMethod reqMethod){
152 return sendRequest(requestJson, uriPath, reqMethod, new HttpHeaders());
156 public void executeCustomWorkflow() throws IOException {
157 wireMockServer.stubFor(post(urlPathEqualTo("/mso/async/services/VnfInPlaceUpdate"))
158 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
159 .withBodyFile("Camunda/TestResponse.json").withStatus(org.apache.http.HttpStatus.SC_OK)));
161 wireMockServer.stubFor(get(urlMatching(".*/vnfRecipe/search/findFirstVnfRecipeByNfRoleAndAction[?]" +
162 "nfRole=GR-API-DEFAULT&action=inPlaceSoftwareUpdate"))
163 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
164 .withBody(getWiremockResponseForCatalogdb("vnfRecipeInPlaceUpdate_Response.json"))
165 .withStatus(org.apache.http.HttpStatus.SC_OK)));
168 ServiceInstancesResponse expectedResponse = new ServiceInstancesResponse();
169 RequestReferences requestReferences = new RequestReferences();
170 requestReferences.setInstanceId("1882939");
171 requestReferences.setRequestSelfLink(createExpectedSelfLink("v1","32807a28-1a14-4b88-b7b3-2950918aa76d"));
172 expectedResponse.setRequestReferences(requestReferences);
173 uri = instanceManagementUri + "v1" + "/serviceInstances/f7ce78bb-423b-11e7-93f8-0050569a7968/vnfs/ff305d54-75b4-431b-adb2-eb6b9e5ff000/workflows/71526781-e55c-4cb7-adb3-97e09d9c76be";
174 ResponseEntity<String> response = sendRequest(inputStream("/ExecuteCustomWorkflow.json"), uri, HttpMethod.POST, headers);
176 assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatusCode().value());
177 ServiceInstancesResponse realResponse = mapper.readValue(response.getBody(), ServiceInstancesResponse.class);
178 assertThat(realResponse, sameBeanAs(expectedResponse).ignoring("requestReferences.requestId"));