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