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_PARTNER_NAME;
33 import static org.onap.logging.filter.base.Constants.HttpHeaders.ONAP_REQUEST_ID;
34 import static org.onap.logging.filter.base.Constants.HttpHeaders.TRANSACTION_ID;
35 import static org.onap.so.logger.HttpHeadersConstants.REQUESTOR_ID;
36 import com.fasterxml.jackson.databind.DeserializationFeature;
37 import com.fasterxml.jackson.databind.ObjectMapper;
39 import java.io.IOException;
40 import java.net.MalformedURLException;
42 import java.nio.file.Files;
43 import java.nio.file.Paths;
44 import javax.ws.rs.core.MediaType;
45 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.db.request.beans.InfraActiveRequests;
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 public class InstanceManagementTest extends BaseTest {
64 private final ObjectMapper mapper = new ObjectMapper();
65 private ObjectMapper errorMapper = new ObjectMapper();
68 InstanceManagement instanceManagement;
70 @Value("${wiremock.server.port}")
71 private String wiremockPort;
73 private final String instanceManagementUri = "/onap/so/infra/instanceManagement/";
77 private URL initialUrl;
78 private int initialPort;
79 private HttpHeaders headers;
82 public void beforeClass() {
83 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
84 errorMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
85 errorMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
87 headers = new HttpHeaders();
88 headers.set(ONAPLogConstants.Headers.PARTNER_NAME, "test_name");
89 headers.set(TRANSACTION_ID, "32807a28-1a14-4b88-b7b3-2950918aa76d");
90 headers.set(ONAP_REQUEST_ID, "32807a28-1a14-4b88-b7b3-2950918aa76d");
91 headers.set(ONAPLogConstants.MDCs.REQUEST_ID, "32807a28-1a14-4b88-b7b3-2950918aa76d");
92 headers.set(ONAP_PARTNER_NAME, "VID");
93 headers.set(REQUESTOR_ID, "xxxxxx");
94 try { // generate one-time port number to avoid RANDOM port number later.
95 initialUrl = new URL(createURLWithPort(Constants.ORCHESTRATION_REQUESTS_PATH));
96 initialPort = initialUrl.getPort();
97 } catch (MalformedURLException e) {
100 wireMockServer.stubFor(post(urlMatching(".*/infraActiveRequests.*")).willReturn(aResponse()
101 .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).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",
123 "localhost:" + wiremockPort);
124 } catch (IOException e) {
131 public ResponseEntity<String> sendRequest(String requestJson, String uriPath, HttpMethod reqMethod,
132 HttpHeaders headers) {
134 if (!headers.containsKey(HttpHeaders.ACCEPT)) {
135 headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
137 if (!headers.containsKey(HttpHeaders.CONTENT_TYPE)) {
138 headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
141 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(uriPath, initialPort));
143 HttpEntity<String> request = new HttpEntity<>(requestJson, headers);
145 return restTemplate.exchange(builder.toUriString(), reqMethod, request, String.class);
148 public ResponseEntity<String> sendRequest(String requestJson, String uriPath, HttpMethod reqMethod) {
149 return sendRequest(requestJson, uriPath, reqMethod, new HttpHeaders());
153 public void executeCustomWorkflow() throws IOException {
154 wireMockServer.stubFor(post(urlPathEqualTo("/mso/async/services/testingWorkflow"))
155 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
156 .withBodyFile("Camunda/TestResponse.json").withStatus(org.apache.http.HttpStatus.SC_OK)));
158 wireMockServer.stubFor(get(urlMatching(
159 ".*/workflow/search/findByArtifactUUID[?]artifactUUID=71526781-e55c-4cb7-adb3-97e09d9c76be"))
160 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
161 .withBody(getWiremockResponseForCatalogdb("workflow_Response.json"))
162 .withStatus(org.apache.http.HttpStatus.SC_OK)));
165 ServiceInstancesResponse expectedResponse = new ServiceInstancesResponse();
166 RequestReferences requestReferences = new RequestReferences();
167 requestReferences.setInstanceId("1882939");
168 requestReferences.setRequestSelfLink(createExpectedSelfLink("v1", "32807a28-1a14-4b88-b7b3-2950918aa76d"));
169 expectedResponse.setRequestReferences(requestReferences);
170 uri = instanceManagementUri + "v1"
171 + "/serviceInstances/f7ce78bb-423b-11e7-93f8-0050569a7968/vnfs/ff305d54-75b4-431b-adb2-eb6b9e5ff000/workflows/71526781-e55c-4cb7-adb3-97e09d9c76be";
172 ResponseEntity<String> response =
173 sendRequest(inputStream("/ExecuteCustomWorkflow.json"), uri, HttpMethod.POST, headers);
175 assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatusCode().value());
176 ServiceInstancesResponse realResponse = mapper.readValue(response.getBody(), ServiceInstancesResponse.class);
177 assertThat(realResponse, sameBeanAs(expectedResponse).ignoring("requestReferences.requestId"));
181 public void executePNFCustomWorkflow() throws IOException {
182 wireMockServer.stubFor(post(urlPathEqualTo("/mso/async/services/testingPNFWorkflow"))
183 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
184 .withBodyFile("Camunda/TestResponse.json").withStatus(org.apache.http.HttpStatus.SC_OK)));
186 wireMockServer.stubFor(get(urlMatching(
187 ".*/workflow/search/findByArtifactUUID[?]artifactUUID=81526781-e55c-4cb7-adb3-97e09d9c76bf"))
188 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
189 .withBody(getWiremockResponseForCatalogdb("workflow_pnf_Response.json"))
190 .withStatus(org.apache.http.HttpStatus.SC_OK)));
193 ServiceInstancesResponse expectedResponse = new ServiceInstancesResponse();
194 RequestReferences requestReferences = new RequestReferences();
195 requestReferences.setInstanceId("1882939");
196 requestReferences.setRequestSelfLink(createExpectedSelfLink("v1", "32807a28-1a14-4b88-b7b3-2950918aa76d"));
197 expectedResponse.setRequestReferences(requestReferences);
198 uri = instanceManagementUri + "v1"
199 + "/serviceInstances/5df8b6de-2083-11e7-93ae-92361f002676/pnfs/testPnfName/workflows/81526781-e55c-4cb7-adb3-97e09d9c76bf";
200 ResponseEntity<String> response =
201 sendRequest(inputStream("/ExecutePNFCustomWorkflow.json"), uri, HttpMethod.POST, headers);
203 assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatusCode().value());
205 ServiceInstancesResponse realResponse = mapper.readValue(response.getBody(), ServiceInstancesResponse.class);
206 assertThat(realResponse, sameBeanAs(expectedResponse).ignoring("requestReferences.requestId"));
210 public void executeServiceLevelCustomWorkflow() throws IOException {
211 wireMockServer.stubFor(post(urlPathEqualTo("/mso/async/services/testingServiceLevelWorkflow"))
212 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
213 .withBodyFile("Camunda/TestResponse.json").withStatus(org.apache.http.HttpStatus.SC_OK)));
215 wireMockServer.stubFor(get(urlMatching(
216 ".*/workflow/search/findByArtifactUUID[?]artifactUUID=81526781-e55c-4cb7-adb3-97e09d9c76bf"))
217 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
218 .withBody(getWiremockResponseForCatalogdb("workflow_ServiceLevel_Response.json"))
219 .withStatus(org.apache.http.HttpStatus.SC_OK)));
222 ServiceInstancesResponse expectedResponse = new ServiceInstancesResponse();
223 RequestReferences requestReferences = new RequestReferences();
224 requestReferences.setInstanceId("1882939");
225 requestReferences.setRequestSelfLink(createExpectedSelfLink("v1", "32807a28-1a14-4b88-b7b3-2950918aa76d"));
226 expectedResponse.setRequestReferences(requestReferences);
227 uri = instanceManagementUri + "v1"
228 + "/serviceInstances/5df8b6de-2083-11e7-93ae-92361f002676/workflows/81526781-e55c-4cb7-adb3-97e09d9c76bf";
229 ResponseEntity<String> response =
230 sendRequest(inputStream("/ExecuteServiceLevelCustomWorkflow.json"), uri, HttpMethod.POST, headers);
232 assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatusCode().value());
234 ServiceInstancesResponse realResponse = mapper.readValue(response.getBody(), ServiceInstancesResponse.class);
235 assertThat(realResponse, sameBeanAs(expectedResponse).ignoring("requestReferences.requestId"));
239 public void workflowAndOperationNameTest() {
240 wireMockServer.stubFor(get(urlMatching(
241 ".*/workflow/search/findByArtifactUUID[?]artifactUUID=71526781-e55c-4cb7-adb3-97e09d9c76be"))
242 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
243 .withBody(getWiremockResponseForCatalogdb("workflow_Response.json"))
244 .withStatus(org.apache.http.HttpStatus.SC_OK)));
245 InfraActiveRequests activeReq = new InfraActiveRequests();
247 instanceManagement.setWorkflowNameAndOperationName(activeReq, "71526781-e55c-4cb7-adb3-97e09d9c76be");
248 assertEquals(activeReq.getWorkflowName(), "testingWorkflow");
249 assertEquals(activeReq.getOperationName(), "testOperation");