2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 Huawei Technologies Co., Ltd. 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;
23 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
24 import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson;
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.urlPathEqualTo;
28 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertThat;
31 import static org.junit.Assert.assertTrue;
32 import java.io.IOException;
33 import java.nio.file.Files;
34 import java.nio.file.Paths;
35 import javax.ws.rs.core.MediaType;
36 import javax.ws.rs.core.Response;
37 import org.apache.http.HttpStatus;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.so.db.catalog.beans.Service;
41 import org.onap.so.db.catalog.beans.ServiceRecipe;
42 import org.onap.so.db.request.beans.OperationStatus;
43 import org.onap.so.serviceinstancebeans.RequestError;
44 import org.onap.so.serviceinstancebeans.ServiceException;
45 import org.springframework.http.HttpEntity;
46 import org.springframework.http.HttpHeaders;
47 import org.springframework.http.HttpMethod;
48 import org.springframework.http.ResponseEntity;
49 import org.springframework.web.util.UriComponentsBuilder;
50 import com.fasterxml.jackson.core.JsonProcessingException;
51 import com.fasterxml.jackson.databind.ObjectMapper;
52 import com.github.tomakehurst.wiremock.http.Fault;
54 public class E2EServiceInstancesTest extends BaseTest {
55 private final ObjectMapper mapper = new ObjectMapper();
57 private final String e2eServInstancesUri = "/onap/so/infra/e2eServiceInstances/";
61 public void init() throws JsonProcessingException {
62 wireMockServer.stubFor(post(urlPathEqualTo("/testOrchestrationUri")).willReturn(aResponse()
63 .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).withStatus(HttpStatus.SC_OK)));
64 wireMockServer.stubFor(post(urlPathEqualTo("/infraActiveRequests/")).withRequestBody(equalToJson(
65 "{\"clientRequestId\":null,\"action\":null,\"requestStatus\":\"FAILED\",\"statusMessage\":\"Error parsing request: No valid requestorId is specified\",\"progress\":100,\"startTime\":1533541051247,\"endTime\":1533541051247,\"source\":null,\"vnfId\":null,\"vnfName\":null,\"vnfType\":null,\"serviceType\":null,\"aicNodeClli\":null,\"tenantId\":null,\"provStatus\":null,\"vnfParams\":null,\"vnfOutputs\":null,\"requestBody\":\"{\\r\\n \\\"service\\\":{\\r\\n \\\"name\\\":\\\"so_test4\\\",\\r\\n \\\"description\\\":\\\"so_test2\\\",\\r\\n \\\"serviceInvariantUuid\\\":\\\"60c3e96e-0970-4871-b6e0-3b6de7561519\\\",\\r\\n \\\"serviceUuid\\\":\\\"592f9437-a9c0-4303-b9f6-c445bb7e9814\\\",\\r\\n \\\"globalSubscriberId\\\":\\\"123457\\\",\\r\\n \\\"serviceType\\\":\\\"voLTE\\\",\\r\\n \\\"parameters\\\":{\\r\\n \\\"resources\\\":[\\r\\n {\\r\\n \\\"resourceName\\\":\\\"vIMS\\\",\\r\\n \\\"resourceInvariantUuid\\\":\\\"60c3e96e-0970-4871-b6e0-3b6de7561516\\\",\\r\\n \\\"resourceUuid\\\":\\\"60c3e96e-0970-4871-b6e0-3b6de7561512\\\",\\r\\n \\\"parameters\\\":{\\r\\n \\\"locationConstraints\\\":[\\r\\n {\\r\\n \\\"vnfProfileId\\\":\\\"zte-vBAS-1.0\\\",\\r\\n \\\"locationConstraints\\\":{\\r\\n \\\"vimId\\\":\\\"4050083f-465f-4838-af1e-47a545222ad0\\\"\\r\\n }\\r\\n },\\r\\n {\\r\\n \\\"vnfProfileId\\\":\\\"zte-vMME-1.0\\\",\\r\\n \\\"locationConstraints\\\":{\\r\\n \\\"vimId\\\":\\\"4050083f-465f-4838-af1e-47a545222ad0\\\"\\r\\n }\\r\\n }\\r\\n ]\\r\\n }\\r\\n },\\r\\n {\\r\\n \\\"resourceName\\\":\\\"vEPC\\\",\\r\\n \\\"resourceInvariantUuid\\\":\\\"61c3e96e-0970-4871-b6e0-3b6de7561516\\\",\\r\\n \\\"resourceUuid\\\":\\\"62c3e96e-0970-4871-b6e0-3b6de7561512\\\",\\r\\n \\\"parameters\\\":{\\r\\n \\\"locationConstraints\\\":[\\r\\n {\\r\\n \\\"vnfProfileId\\\":\\\"zte-CSCF-1.0\\\",\\r\\n \\\"locationConstraints\\\":{\\r\\n \\\"vimId\\\":\\\"4050083f-465f-4838-af1e-47a545222ad1\\\"\\r\\n }\\r\\n }\\r\\n ]\\r\\n }\\r\\n },\\r\\n {\\r\\n \\\"resourceName\\\":\\\"underlayvpn\\\",\\r\\n \\\"resourceInvariantUuid\\\":\\\"60c3e96e-0970-4871-b6e0-3b6de7561513\\\",\\r\\n \\\"resourceUuid\\\":\\\"60c3e96e-0970-4871-b6e0-3b6de7561514\\\",\\r\\n \\\"parameters\\\":{\\r\\n \\\"locationConstraints\\\":[\\r\\n\\r\\n ]\\r\\n }\\r\\n },\\r\\n {\\r\\n \\\"resourceName\\\":\\\"overlayvpn\\\",\\r\\n \\\"resourceInvariantUuid\\\":\\\"60c3e96e-0970-4871-b6e0-3b6de7561517\\\",\\r\\n \\\"resourceUuid\\\":\\\"60c3e96e-0970-4871-b6e0-3b6de7561518\\\",\\r\\n \\\"parameters\\\":{\\r\\n \\\"locationConstraints\\\":[\\r\\n\\r\\n ]\\r\\n }\\r\\n }\\r\\n ],\\r\\n \\\"requestInputs\\\":{\\r\\n \\\"externalDataNetworkName\\\":\\\"Flow_out_net\\\",\\r\\n \\\"m6000_mng_ip\\\":\\\"181.18.20.2\\\",\\r\\n \\\"externalCompanyFtpDataNetworkName\\\":\\\"Flow_out_net\\\",\\r\\n \\\"externalPluginManageNetworkName\\\":\\\"plugin_net_2014\\\",\\r\\n \\\"externalManageNetworkName\\\":\\\"mng_net_2017\\\",\\r\\n \\\"sfc_data_network\\\":\\\"sfc_data_net_2016\\\",\\r\\n \\\"NatIpRange\\\":\\\"210.1.1.10-210.1.1.20\\\",\\r\\n \\\"location\\\":\\\"4050083f-465f-4838-af1e-47a545222ad0\\\",\\r\\n \\\"sdncontroller\\\":\\\"9b9f02c0-298b-458a-bc9c-be3692e4f35e\\\"\\r\\n }\\r\\n }\\r\\n\\r\\n }\\r\\n\\r\\n}\",\"responseBody\":null,\"lastModifiedBy\":\"APIH\",\"modifyTime\":null,\"requestType\":null,\"volumeGroupId\":null,\"volumeGroupName\":null,\"vfModuleId\":null,\"vfModuleName\":null,\"vfModuleModelName\":null,\"aaiServiceId\":null,\"aicCloudRegion\":null,\"callBackUrl\":null,\"correlator\":null,\"serviceInstanceId\":null,\"serviceInstanceName\":null,\"requestScope\":\"service\",\"requestAction\":\"createInstance\",\"networkId\":null,\"networkName\":null,\"networkType\":null,\"requestorId\":null,\"configurationId\":null,\"configurationName\":null,\"operationalEnvId\":null,\"operationalEnvName\":null,\"requestURI\":\"d167c9d0-1785-4e93-b319-996ebbcc3272\"}"))
66 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
67 .withStatus(HttpStatus.SC_OK)));
68 Service defaultService = new Service();
69 defaultService.setModelUUID("d88da85c-d9e8-4f73-b837-3a72a431622a");
70 ServiceRecipe serviceRecipe = new ServiceRecipe();
71 serviceRecipe.setServiceModelUUID(defaultService.getModelUUID());
72 serviceRecipe.setAction(Action.scaleInstance.name());
73 serviceRecipe.setRecipeTimeout(180);
74 serviceRecipe.setOrchestrationUri("/testOrchestrationUri");
76 wireMockServer.stubFor(get(urlPathEqualTo("/service/search/findFirstByModelNameOrderByModelVersionDesc"))
77 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
78 .withBody(mapper.writeValueAsString(defaultService)).withStatus(HttpStatus.SC_OK)));
80 wireMockServer.stubFor(get(urlPathEqualTo("/serviceRecipe/search/findFirstByServiceModelUUIDAndAction"))
81 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
82 .withBody(mapper.writeValueAsString(serviceRecipe)).withStatus(HttpStatus.SC_OK)));
86 public String inputStream(String JsonInput) throws IOException {
87 JsonInput = "src/test/resources/E2EServiceInstancesTest" + JsonInput;
88 return new String(Files.readAllBytes(Paths.get(JsonInput)));
91 public ResponseEntity<String> sendRequest(String requestJson, String uriPath, HttpMethod reqMethod) {
92 HttpHeaders headers = new HttpHeaders();
93 headers.set("Accept", MediaType.APPLICATION_JSON);
94 headers.set("Content-Type", MediaType.APPLICATION_JSON);
96 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(uriPath));
97 HttpEntity<String> request = new HttpEntity<>(requestJson, headers);
99 return restTemplate.exchange(builder.toUriString(), reqMethod, request, String.class);
103 public void createE2EServiceInstanceNoRequestInfo() throws IOException {
104 String uri = e2eServInstancesUri + "v3";
105 ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.POST);
107 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
111 public void updateE2EServiceInstanceJSONMappingError() throws IOException {
112 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
113 ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.PUT);
115 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
116 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
117 assertTrue(realResponse.getServiceException().getText().contains("Mapping of request to JSON object failed"));
121 public void updateE2EServiceInstanceNoRequestorId() throws IOException {
122 RequestError expectedResponse = new RequestError();
123 ServiceException exception = new ServiceException();
124 exception.setMessageId("SVC0002");
125 exception.setText("Error parsing request. Error parsing request: No valid requestorId is specified");
126 expectedResponse.setServiceException(exception);
128 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
129 ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.PUT);
131 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
132 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
133 assertThat(realResponse, sameBeanAs(expectedResponse));
137 public void deleteE2EServiceInstance() throws IOException {
138 RequestError expectedResponse = new RequestError();
139 ServiceException exception = new ServiceException();
140 exception.setMessageId("SVC1000");
141 exception.setText("No communication to catalog DB null");
142 expectedResponse.setServiceException(exception);
144 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
145 ResponseEntity<String> response = sendRequest(inputStream("/DeleteRequest.json"), uri, HttpMethod.DELETE);
147 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
151 public void deleteE2EServiceInstanceNotValid() throws IOException {
152 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
153 ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.DELETE);
155 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
156 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
157 assertTrue(realResponse.getServiceException().getText().contains("Mapping of request to JSON object failed"));
161 public void getE2EServiceInstanceNullOperationalStatus() throws IOException {
162 String uri = e2eServInstancesUri
163 + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/operations/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
164 wireMockServer.stubFor(get(urlPathEqualTo("/operationStatus/search/findOneByServiceIdAndOperationId"))
165 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
166 .withStatus(HttpStatus.SC_OK)));
167 ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.GET);
169 assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatusCode().value());
173 public void scaleE2EServiceInstanceMappingError() throws IOException {
174 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/scale";
175 ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.POST);
177 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
178 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
179 assertTrue(realResponse.getServiceException().getText().contains("Mapping of request to JSON object failed"));
183 public void scaleE2EServiceInstance() throws IOException {
184 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/scale";
185 ResponseEntity<String> response = sendRequest(inputStream("/ScaleRequest.json"), uri, HttpMethod.POST);
187 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
191 public void updateE2EServiceInstance() throws IOException {
192 String uri = e2eServInstancesUri + "v3/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
193 ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.PUT);
194 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
198 public void getE2EServiceInstance() throws IOException {
199 OperationStatus status = new OperationStatus();
200 status.setOperationId("operationId");
201 status.setServiceId("9b9f02c0-298b-458a-bc9c-be3692e4f35e");
202 wireMockServer.stubFor(get(urlPathEqualTo("/operationStatus/search/findOneByServiceIdAndOperationId"))
203 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
204 .withBody(mapper.writeValueAsString(status)).withStatus(HttpStatus.SC_OK)));
205 String uri = e2eServInstancesUri + "v3/9b9f02c0-298b-458a-bc9c-be3692e4f35e/operations/operationId";
206 ResponseEntity<String> response = sendRequest("", uri, HttpMethod.GET);
207 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
211 public void compareModelWithTargetVersionBadRequest() throws IOException {
212 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
213 ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.POST);
215 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
216 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
217 assertTrue(realResponse.getServiceException().getText().contains("Mapping of request to JSON object failed"));
221 public void compareModelWithTargetVersion() throws IOException {
222 wireMockServer.stubFor(
223 post(urlPathEqualTo("/mso/async/services/CompareModelofE2EServiceInstance")).willReturn(aResponse()
224 .withHeader("Content-Type", "application/json").withBodyFile("Camunda/SuccessfulResponse.json")
225 .withStatus(org.apache.http.HttpStatus.SC_ACCEPTED)));
227 String expectedResponse = "success";
228 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
229 ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.POST);
231 assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatusCode().value());
232 String actualResponse = response.getBody();
233 assertEquals(expectedResponse, actualResponse);
237 public void compareModelWithTargetVersionEmptyResponse() throws IOException {
238 wireMockServer.stubFor(post(urlPathEqualTo("/mso/async/services/CompareModelofE2EServiceInstance"))
239 .willReturn(aResponse().withFault(Fault.EMPTY_RESPONSE)));
241 RequestError expectedResponse = new RequestError();
242 ServiceException exception = new ServiceException();
243 exception.setMessageId("SVC1000");
245 "Failed calling bpmn localhost:" + env.getProperty("wiremock.server.port") + " failed to respond");
246 expectedResponse.setServiceException(exception);
248 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
249 ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.POST);
251 assertEquals(Response.Status.BAD_GATEWAY.getStatusCode(), response.getStatusCode().value());
252 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
253 assertThat(realResponse, sameBeanAs(expectedResponse));
257 public void compareModelWithTargetVersionBadBpelResponse() throws IOException {
258 wireMockServer.stubFor(post(urlPathEqualTo("/mso/async/services/CompareModelofE2EServiceInstance")).willReturn(
259 aResponse().withHeader("Content-Type", "application/json").withBodyFile("Camunda/TestResponse.json")
260 .withStatus(org.apache.http.HttpStatus.SC_BAD_GATEWAY)));
262 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
263 ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.POST);
265 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatusCode().value());
266 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
267 assertTrue(realResponse.getServiceException().getText()
268 .contains("Request Failed due to BPEL error with HTTP Status"));
272 public void compareModelWithTargetVersionNoBPELResponse() throws IOException {
273 wireMockServer.stubFor(post(urlPathEqualTo("/mso/async/services/CompareModelofE2EServiceInstance"))
274 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody("{}")
275 .withStatus(org.apache.http.HttpStatus.SC_BAD_GATEWAY)));
277 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
278 ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.POST);
280 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatusCode().value());
281 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
282 assertTrue(realResponse.getServiceException().getText()
283 .contains("Request Failed due to BPEL error with HTTP Status"));