8d5539cf8aef3de447f6214774e21e5817fab4e6
[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.stubFor;
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.junit.Assert.assertTrue;
33
34 import java.io.IOException;
35 import java.nio.file.Files;
36 import java.nio.file.Paths;
37
38 import javax.ws.rs.core.MediaType;
39 import javax.ws.rs.core.Response;
40
41 import org.apache.http.HttpStatus;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.onap.so.db.catalog.beans.Service;
45 import org.onap.so.db.catalog.beans.ServiceRecipe;
46 import org.onap.so.db.request.beans.OperationStatus;
47 import org.onap.so.serviceinstancebeans.RequestError;
48 import org.onap.so.serviceinstancebeans.ServiceException;
49 import org.springframework.http.HttpEntity;
50 import org.springframework.http.HttpHeaders;
51 import org.springframework.http.HttpMethod;
52 import org.springframework.http.ResponseEntity;
53 import org.springframework.web.util.UriComponentsBuilder;
54
55 import com.fasterxml.jackson.core.JsonProcessingException;
56 import com.fasterxml.jackson.databind.ObjectMapper;
57 import com.github.tomakehurst.wiremock.http.Fault;
58
59 public class E2EServiceInstancesTest extends BaseTest {
60 private final ObjectMapper mapper = new ObjectMapper(); 
61         
62         private final String e2eServInstancesUri = "/onap/so/infra/e2eServiceInstances/";
63         
64         
65         @Before
66         public void init() throws JsonProcessingException {
67                 stubFor(post(urlPathEqualTo("/testOrchestrationUri"))
68                                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
69                                 .withStatus(HttpStatus.SC_OK)));
70                 stubFor(post(urlPathEqualTo("/infraActiveRequests/")).withRequestBody(equalToJson("{\"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\"}")).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
71                                 .withStatus(HttpStatus.SC_OK)));
72                 Service defaultService = new Service();
73                 defaultService.setModelUUID("d88da85c-d9e8-4f73-b837-3a72a431622a");
74                 ServiceRecipe serviceRecipe = new ServiceRecipe();
75                 serviceRecipe.setServiceModelUUID(defaultService.getModelUUID());
76                 serviceRecipe.setAction(Action.scaleInstance.name());
77                 serviceRecipe.setRecipeTimeout(180);
78                 serviceRecipe.setOrchestrationUri("/testOrchestrationUri");
79
80                 stubFor(get(urlPathEqualTo("/service/search/findFirstByModelNameOrderByModelVersionDesc"))
81                                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
82                                                 .withBody(mapper.writeValueAsString(defaultService))
83                                 .withStatus(HttpStatus.SC_OK)));
84
85                 stubFor(get(urlPathEqualTo("/serviceRecipe/search/findFirstByServiceModelUUIDAndAction"))
86                                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
87                                                 .withBody(mapper.writeValueAsString(serviceRecipe))
88                                                 .withStatus(HttpStatus.SC_OK)));
89
90         }
91         public String inputStream(String JsonInput)throws IOException{
92                 JsonInput = "src/test/resources/E2EServiceInstancesTest" + JsonInput;
93                 return new String(Files.readAllBytes(Paths.get(JsonInput)));
94         }
95         public ResponseEntity<String> sendRequest(String requestJson, String uriPath, HttpMethod reqMethod){
96                 HttpHeaders headers = new HttpHeaders();
97                 headers.set("Accept", MediaType.APPLICATION_JSON);
98                 headers.set("Content-Type",MediaType.APPLICATION_JSON);
99                 
100                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(uriPath));
101                 HttpEntity<String> request = new HttpEntity<>(requestJson, headers);
102
103                 return restTemplate.exchange(builder.toUriString(),
104                                 reqMethod, request, String.class);
105         }
106         
107         @Test
108         public void createE2EServiceInstanceNoRequestInfo() throws IOException{
109                 String uri = e2eServInstancesUri + "v3";
110                 ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.POST);
111                 
112                 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
113         }
114         @Test
115         public void updateE2EServiceInstanceJSONMappingError() throws IOException{
116                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
117                 ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.PUT);
118                 
119                 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
120                 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
121                 assertTrue(realResponse.getServiceException().getText().contains("Mapping of request to JSON object failed"));
122         }
123         @Test
124         public void updateE2EServiceInstanceNoRequestorId() throws IOException{
125                 RequestError expectedResponse = new RequestError();
126                 ServiceException exception = new ServiceException();
127                 exception.setMessageId("SVC0002");
128                 exception.setText("Error parsing request.  Error parsing request: No valid requestorId is specified");
129                 expectedResponse.setServiceException(exception);
130                 
131                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
132                 ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.PUT);
133                 
134                 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
135                 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
136                 assertThat(realResponse, sameBeanAs(expectedResponse));
137         }
138         @Test
139         public void deleteE2EServiceInstance() throws IOException{
140                 RequestError expectedResponse = new RequestError();
141                 ServiceException exception = new ServiceException();
142                 exception.setMessageId("SVC1000");
143                 exception.setText("No communication to catalog DB null");
144                 expectedResponse.setServiceException(exception);
145                 
146                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
147                 ResponseEntity<String> response = sendRequest(inputStream("/DeleteRequest.json"), uri, HttpMethod.DELETE);
148                 
149                 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
150         }
151         @Test
152         public void deleteE2EServiceInstanceNotValid() throws IOException{
153                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
154                 ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.DELETE);
155                 
156                 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
157                 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
158                 assertTrue(realResponse.getServiceException().getText().contains("Mapping of request to JSON object failed"));
159         }
160         @Test
161         public void getE2EServiceInstanceNullOperationalStatus() throws IOException{
162                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/operations/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
163                 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         @Test
171         public void scaleE2EServiceInstanceMappingError() throws IOException{
172                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/scale";
173                 ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.POST);
174                 
175                 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
176                 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
177                 assertTrue(realResponse.getServiceException().getText().contains("Mapping of request to JSON object failed"));
178         }
179         @Test
180         public void scaleE2EServiceInstance() throws IOException{
181                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/scale";
182                 ResponseEntity<String> response = sendRequest(inputStream("/ScaleRequest.json"), uri, HttpMethod.POST);
183                 
184                 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
185         }
186
187         @Test
188         public void updateE2EServiceInstance() throws IOException{
189                 String uri = e2eServInstancesUri + "v3/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
190                 ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.PUT);
191                 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
192         }
193
194         @Test
195         public void getE2EServiceInstance() throws IOException{
196                 OperationStatus status = new OperationStatus();
197                 status.setOperationId("operationId");
198                 status.setServiceId("9b9f02c0-298b-458a-bc9c-be3692e4f35e");
199                 stubFor(get(urlPathEqualTo("/operationStatus/search/findOneByServiceIdAndOperationId"))
200                                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
201                                                 .withBody(mapper.writeValueAsString(status))
202                                                 .withStatus(HttpStatus.SC_OK)));
203                 String uri = e2eServInstancesUri + "v3/9b9f02c0-298b-458a-bc9c-be3692e4f35e/operations/operationId";
204                 ResponseEntity<String> response = sendRequest("", uri, HttpMethod.GET);
205                 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
206         }
207         @Test
208         public void compareModelWithTargetVersionBadRequest() throws IOException{
209                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
210                 ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.POST);
211                 
212                 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
213                 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
214                 assertTrue(realResponse.getServiceException().getText().contains("Mapping of request to JSON object failed"));
215         }
216         @Test
217         public void compareModelWithTargetVersion() throws IOException{
218                 stubFor(post(urlPathEqualTo("/mso/async/services/CompareModelofE2EServiceInstance"))
219                                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
220                                                 .withBodyFile("Camunda/SuccessfulResponse.json").withStatus(org.apache.http.HttpStatus.SC_ACCEPTED)));
221                 
222                 String expectedResponse = "success";
223                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
224                 ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.POST);
225                 
226                 assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatusCode().value());
227                 String actualResponse = response.getBody();
228                 assertEquals(expectedResponse, actualResponse);
229         }
230         @Test
231         public void compareModelWithTargetVersionEmptyResponse() throws IOException{
232                 stubFor(post(urlPathEqualTo("/mso/async/services/CompareModelofE2EServiceInstance"))
233                                 .willReturn(aResponse().withFault(Fault.EMPTY_RESPONSE)));
234                 
235                 RequestError expectedResponse = new RequestError();
236                 ServiceException exception = new ServiceException();
237                 exception.setMessageId("SVC1000");
238                 exception.setText("Failed calling bpmn localhost:" + env.getProperty("wiremock.server.port") + " failed to respond");
239                 expectedResponse.setServiceException(exception);
240                 
241                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
242                 ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.POST);
243                 
244                 assertEquals(Response.Status.BAD_GATEWAY.getStatusCode(), response.getStatusCode().value());
245                 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
246                 assertThat(realResponse, sameBeanAs(expectedResponse));
247         }
248         @Test
249         public void compareModelWithTargetVersionBadBpelResponse() throws IOException{
250                 stubFor(post(urlPathEqualTo("/mso/async/services/CompareModelofE2EServiceInstance"))
251                                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
252                                                 .withBodyFile("Camunda/TestResponse.json").withStatus(org.apache.http.HttpStatus.SC_BAD_GATEWAY)));
253                 
254                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
255                 ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.POST);
256                 
257                 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatusCode().value());
258                 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
259                 assertTrue(realResponse.getServiceException().getText().contains("Request Failed due to BPEL error with HTTP Status"));
260         }
261         @Test
262         public void compareModelWithTargetVersionNoBPELResponse() throws IOException{
263                 stubFor(post(urlPathEqualTo("/mso/async/services/CompareModelofE2EServiceInstance"))
264                                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
265                                                 .withBody("{}").withStatus(org.apache.http.HttpStatus.SC_BAD_GATEWAY)));
266
267                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
268                 ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.POST);
269                 
270                 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatusCode().value());
271                 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
272                 assertTrue(realResponse.getServiceException().getText().contains("Request Failed due to BPEL error with HTTP Status"));
273         }
274 }