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