Replaced all tabs with spaces in java and pom.xml
[so.git] / mso-api-handlers / mso-api-handler-infra / src / test / java / org / onap / so / apihandlerinfra / E2EServiceInstancesTest.java
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                 "{\"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");
75
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)));
79
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)));
83
84     }
85
86     public String inputStream(String JsonInput) throws IOException {
87         JsonInput = "src/test/resources/E2EServiceInstancesTest" + JsonInput;
88         return new String(Files.readAllBytes(Paths.get(JsonInput)));
89     }
90
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);
95
96         UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(uriPath));
97         HttpEntity<String> request = new HttpEntity<>(requestJson, headers);
98
99         return restTemplate.exchange(builder.toUriString(), reqMethod, request, String.class);
100     }
101
102     @Test
103     public void createE2EServiceInstanceNoRequestInfo() throws IOException {
104         String uri = e2eServInstancesUri + "v3";
105         ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.POST);
106
107         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
108     }
109
110     @Test
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);
114
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"));
118     }
119
120     @Test
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);
127
128         String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
129         ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.PUT);
130
131         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
132         RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
133         assertThat(realResponse, sameBeanAs(expectedResponse));
134     }
135
136     @Test
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);
143
144         String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
145         ResponseEntity<String> response = sendRequest(inputStream("/DeleteRequest.json"), uri, HttpMethod.DELETE);
146
147         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
148     }
149
150     @Test
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);
154
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"));
158     }
159
160     @Test
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);
168
169         assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatusCode().value());
170     }
171
172     @Test
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);
176
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"));
180     }
181
182     @Test
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);
186
187         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
188     }
189
190     @Test
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());
195     }
196
197     @Test
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());
208     }
209
210     @Test
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);
214
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"));
218     }
219
220     @Test
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)));
226
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);
230
231         assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatusCode().value());
232         String actualResponse = response.getBody();
233         assertEquals(expectedResponse, actualResponse);
234     }
235
236     @Test
237     public void compareModelWithTargetVersionEmptyResponse() throws IOException {
238         wireMockServer.stubFor(post(urlPathEqualTo("/mso/async/services/CompareModelofE2EServiceInstance"))
239                 .willReturn(aResponse().withFault(Fault.EMPTY_RESPONSE)));
240
241         RequestError expectedResponse = new RequestError();
242         ServiceException exception = new ServiceException();
243         exception.setMessageId("SVC1000");
244         exception.setText(
245                 "Failed calling bpmn localhost:" + env.getProperty("wiremock.server.port") + " failed to respond");
246         expectedResponse.setServiceException(exception);
247
248         String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
249         ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.POST);
250
251         assertEquals(Response.Status.BAD_GATEWAY.getStatusCode(), response.getStatusCode().value());
252         RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
253         assertThat(realResponse, sameBeanAs(expectedResponse));
254     }
255
256     @Test
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)));
261
262         String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
263         ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.POST);
264
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"));
269     }
270
271     @Test
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)));
276
277         String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
278         ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.POST);
279
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"));
284     }
285 }