9fc9e4a51b1d4070ad2e2be73458ff57142e0e82
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.apihandlerinfra;
22
23
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;
35 import java.io.File;
36 import java.io.IOException;
37 import java.net.MalformedURLException;
38 import java.net.URL;
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;
59
60 public class InstanceManagementTest extends BaseTest {
61
62     private final ObjectMapper mapper = new ObjectMapper();
63     private ObjectMapper errorMapper = new ObjectMapper();
64
65     @Value("${wiremock.server.port}")
66     private String wiremockPort;
67
68     private final String instanceManagementUri = "/onap/so/infra/instanceManagement/";
69
70     private String uri;
71     private URL selfLink;
72     private URL initialUrl;
73     private int initialPort;
74     private HttpHeaders headers;
75
76     @Before
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);
81         // set headers
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) {
93             e.printStackTrace();
94         }
95         wireMockServer.stubFor(post(urlMatching(".*/infraActiveRequests.*")).willReturn(aResponse()
96                 .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).withStatus(HttpStatus.SC_OK)));
97     }
98
99     public String inputStream(String JsonInput) throws IOException {
100         JsonInput = "src/test/resources/ServiceInstanceTest" + JsonInput;
101         return new String(Files.readAllBytes(Paths.get(JsonInput)));
102     }
103
104     private URL createExpectedSelfLink(String version, String requestId) {
105         System.out.println("createdUrl: " + initialUrl.toString());
106         try {
107             selfLink = new URL(initialUrl.toString().concat("/").concat(version).concat("/").concat(requestId));
108         } catch (MalformedURLException e) {
109             e.printStackTrace();
110         }
111         return selfLink;
112     }
113
114     private String getWiremockResponseForCatalogdb(String file) {
115         try {
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) {
120             e.printStackTrace();
121             return null;
122         }
123
124     }
125
126     public ResponseEntity<String> sendRequest(String requestJson, String uriPath, HttpMethod reqMethod,
127             HttpHeaders headers) {
128
129         if (!headers.containsKey(HttpHeaders.ACCEPT)) {
130             headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
131         }
132         if (!headers.containsKey(HttpHeaders.CONTENT_TYPE)) {
133             headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
134         }
135
136         UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(uriPath, initialPort));
137
138         HttpEntity<String> request = new HttpEntity<>(requestJson, headers);
139
140         return restTemplate.exchange(builder.toUriString(), reqMethod, request, String.class);
141     }
142
143     public ResponseEntity<String> sendRequest(String requestJson, String uriPath, HttpMethod reqMethod) {
144         return sendRequest(requestJson, uriPath, reqMethod, new HttpHeaders());
145     }
146
147     @Test
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)));
152
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)));
158
159         // expected response
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);
169
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"));
173     }
174 }