0ce8113dcb150dd99353303d00a17e6ece802894
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
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 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;
53
54 public class E2EServiceInstancesTest extends BaseTest {
55     private final ObjectMapper mapper = new ObjectMapper();
56
57     private final String e2eServInstancesUri = "/onap/so/infra/e2eServiceInstances/";
58
59
60     @Before
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                 "{\"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,\"tenantId\":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,\"volumeGroupId\":null,\"volumeGroupName\":null,\"vfModuleId\":null,\"vfModuleName\":null,\"vfModuleModelName\":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.setRecipeTimeout(180);
73         serviceRecipe.setOrchestrationUri("/testOrchestrationUri");
74
75         wireMockServer.stubFor(get(urlPathEqualTo("/service/search/findFirstByModelNameOrderByModelVersionDesc"))
76                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
77                         .withBody(mapper.writeValueAsString(defaultService)).withStatus(HttpStatus.SC_OK)));
78
79         wireMockServer.stubFor(get(urlPathEqualTo("/serviceRecipe/search/findFirstByServiceModelUUIDAndAction"))
80                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
81                         .withBody(mapper.writeValueAsString(serviceRecipe)).withStatus(HttpStatus.SC_OK)));
82
83     }
84
85     public String inputStream(String JsonInput) throws IOException {
86         JsonInput = "src/test/resources/E2EServiceInstancesTest" + JsonInput;
87         return new String(Files.readAllBytes(Paths.get(JsonInput)));
88     }
89
90     public ResponseEntity<String> sendRequest(String requestJson, String uriPath, HttpMethod reqMethod) {
91         HttpHeaders headers = new HttpHeaders();
92         headers.set("Accept", MediaType.APPLICATION_JSON);
93         headers.set("Content-Type", MediaType.APPLICATION_JSON);
94
95         UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(uriPath));
96         HttpEntity<String> request = new HttpEntity<>(requestJson, headers);
97
98         return restTemplate.exchange(builder.toUriString(), reqMethod, request, String.class);
99     }
100
101     @Test
102     public void createE2EServiceInstanceNoRequestInfo() throws IOException {
103         String uri = e2eServInstancesUri + "v3";
104         ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.POST);
105
106         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
107     }
108
109     @Test
110     public void updateE2EServiceInstanceJSONMappingError() throws IOException {
111         String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
112         ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.PUT);
113
114         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
115         RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
116         assertTrue(realResponse.getServiceException().getText().contains("Mapping of request to JSON object failed"));
117     }
118
119     @Test
120     public void updateE2EServiceInstanceNoRequestorId() throws IOException {
121         RequestError expectedResponse = new RequestError();
122         ServiceException exception = new ServiceException();
123         exception.setMessageId("SVC0002");
124         exception.setText("Error parsing request.  Error parsing request: No valid requestorId is specified");
125         expectedResponse.setServiceException(exception);
126
127         String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
128         ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.PUT);
129
130         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
131         RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
132         assertThat(realResponse, sameBeanAs(expectedResponse));
133     }
134
135     @Test
136     public void deleteE2EServiceInstance() throws IOException {
137         RequestError expectedResponse = new RequestError();
138         ServiceException exception = new ServiceException();
139         exception.setMessageId("SVC1000");
140         exception.setText("No communication to catalog DB null");
141         expectedResponse.setServiceException(exception);
142
143         String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
144         ResponseEntity<String> response = sendRequest(inputStream("/DeleteRequest.json"), uri, HttpMethod.DELETE);
145
146         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
147     }
148
149     @Test
150     public void deleteE2EServiceInstanceNotValid() throws IOException {
151         String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
152         ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.DELETE);
153
154         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
155         RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
156         assertTrue(realResponse.getServiceException().getText().contains("Mapping of request to JSON object failed"));
157     }
158
159     @Test
160     public void getE2EServiceInstanceNullOperationalStatus() throws IOException {
161         String uri = e2eServInstancesUri
162                 + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/operations/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
163         wireMockServer.stubFor(get(urlPathEqualTo("/operationStatus/search/findOneByServiceIdAndOperationId"))
164                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
165                         .withStatus(HttpStatus.SC_OK)));
166         ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.GET);
167
168         assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatusCode().value());
169     }
170
171     @Test
172     public void scaleE2EServiceInstanceMappingError() throws IOException {
173         String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/scale";
174         ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.POST);
175
176         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
177         RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
178         assertTrue(realResponse.getServiceException().getText().contains("Mapping of request to JSON object failed"));
179     }
180
181     @Test
182     public void scaleE2EServiceInstance() throws IOException {
183         String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/scale";
184         ResponseEntity<String> response = sendRequest(inputStream("/ScaleRequest.json"), uri, HttpMethod.POST);
185
186         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
187     }
188
189     @Test
190     public void updateE2EServiceInstance() throws IOException {
191         String uri = e2eServInstancesUri + "v3/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
192         ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.PUT);
193         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
194     }
195
196     @Test
197     public void getE2EServiceInstance() throws IOException {
198         OperationStatus status = new OperationStatus();
199         status.setOperationId("operationId");
200         status.setServiceId("9b9f02c0-298b-458a-bc9c-be3692e4f35e");
201         wireMockServer.stubFor(get(urlPathEqualTo("/operationStatus/search/findOneByServiceIdAndOperationId"))
202                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
203                         .withBody(mapper.writeValueAsString(status)).withStatus(HttpStatus.SC_OK)));
204         String uri = e2eServInstancesUri + "v3/9b9f02c0-298b-458a-bc9c-be3692e4f35e/operations/operationId";
205         ResponseEntity<String> response = sendRequest("", uri, HttpMethod.GET);
206         assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
207     }
208
209     @Test
210     public void compareModelWithTargetVersionBadRequest() throws IOException {
211         String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
212         ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.POST);
213
214         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
215         RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
216         assertTrue(realResponse.getServiceException().getText().contains("Mapping of request to JSON object failed"));
217     }
218
219     @Test
220     public void compareModelWithTargetVersion() throws IOException {
221         wireMockServer.stubFor(
222                 post(urlPathEqualTo("/mso/async/services/CompareModelofE2EServiceInstance")).willReturn(aResponse()
223                         .withHeader("Content-Type", "application/json").withBodyFile("Camunda/SuccessfulResponse.json")
224                         .withStatus(org.apache.http.HttpStatus.SC_ACCEPTED)));
225
226         String expectedResponse = "success";
227         String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
228         ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.POST);
229
230         assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatusCode().value());
231         String actualResponse = response.getBody();
232         assertEquals(expectedResponse, actualResponse);
233     }
234
235     @Test
236     public void compareModelWithTargetVersionEmptyResponse() throws IOException {
237         wireMockServer.stubFor(post(urlPathEqualTo("/mso/async/services/CompareModelofE2EServiceInstance"))
238                 .willReturn(aResponse().withFault(Fault.EMPTY_RESPONSE)));
239
240         RequestError expectedResponse = new RequestError();
241         ServiceException exception = new ServiceException();
242         exception.setMessageId("SVC1000");
243         exception.setText(
244                 "Failed calling bpmn localhost:" + env.getProperty("wiremock.server.port") + " failed to respond");
245         expectedResponse.setServiceException(exception);
246
247         String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
248         ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.POST);
249
250         assertEquals(Response.Status.BAD_GATEWAY.getStatusCode(), response.getStatusCode().value());
251         RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
252         assertThat(realResponse, sameBeanAs(expectedResponse));
253     }
254
255     @Test
256     public void compareModelWithTargetVersionBadBpelResponse() throws IOException {
257         wireMockServer.stubFor(post(urlPathEqualTo("/mso/async/services/CompareModelofE2EServiceInstance")).willReturn(
258                 aResponse().withHeader("Content-Type", "application/json").withBodyFile("Camunda/TestResponse.json")
259                         .withStatus(org.apache.http.HttpStatus.SC_BAD_GATEWAY)));
260
261         String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
262         ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.POST);
263
264         assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatusCode().value());
265         RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
266         assertTrue(realResponse.getServiceException().getText()
267                 .contains("Request Failed due to BPEL error with HTTP Status"));
268     }
269
270     @Test
271     public void compareModelWithTargetVersionNoBPELResponse() throws IOException {
272         wireMockServer.stubFor(post(urlPathEqualTo("/mso/async/services/CompareModelofE2EServiceInstance"))
273                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody("{}")
274                         .withStatus(org.apache.http.HttpStatus.SC_BAD_GATEWAY)));
275
276         String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
277         ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.POST);
278
279         assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatusCode().value());
280         RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
281         assertTrue(realResponse.getServiceException().getText()
282                 .contains("Request Failed due to BPEL error with HTTP Status"));
283     }
284 }