5932f6d206649cffabd79c1b784153b6699608af
[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                 ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.GET);
158                 
159                 assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatusCode().value());
160         }
161         @Test
162         public void scaleE2EServiceInstanceMappingError() throws IOException{
163                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/scale";
164                 ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.POST);
165                 
166                 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
167                 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
168                 assertTrue(realResponse.getServiceException().getText().contains("Mapping of request to JSON object failed"));
169         }
170         @Test
171         public void scaleE2EServiceInstance() throws IOException{
172                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/scale";
173                 ResponseEntity<String> response = sendRequest(inputStream("/ScaleRequest.json"), uri, HttpMethod.POST);
174                 
175                 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
176         }
177
178         @Test
179         public void updateE2EServiceInstance() throws IOException{
180                 String uri = e2eServInstancesUri + "v3/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
181                 ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.PUT);
182                 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
183         }
184
185         @Test
186         public void getE2EServiceInstance() throws IOException{
187                 OperationStatus status = new OperationStatus();
188                 status.setOperationId("operationId");
189                 status.setServiceId("9b9f02c0-298b-458a-bc9c-be3692e4f35e");
190                 stubFor(get(urlPathEqualTo("/operationStatus/search/findOneByServiceIdAndOperationId"))
191                                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
192                                                 .withBody(mapper.writeValueAsString(status))
193                                                 .withStatus(HttpStatus.SC_OK)));
194                 String uri = e2eServInstancesUri + "v3/9b9f02c0-298b-458a-bc9c-be3692e4f35e/operations/operationId";
195                 ResponseEntity<String> response = sendRequest("", uri, HttpMethod.GET);
196                 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
197         }
198         @Test
199         public void compareModelWithTargetVersionBadRequest() throws IOException{
200                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
201                 ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.POST);
202                 
203                 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
204                 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
205                 assertTrue(realResponse.getServiceException().getText().contains("Mapping of request to JSON object failed"));
206         }
207         @Test
208         public void compareModelWithTargetVersion() throws IOException{
209                 stubFor(post(urlPathEqualTo("/mso/async/services/CompareModelofE2EServiceInstance"))
210                                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
211                                                 .withBodyFile("Camunda/SuccessfulResponse.json").withStatus(org.apache.http.HttpStatus.SC_ACCEPTED)));
212                 
213                 String expectedResponse = "success";
214                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
215                 ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.POST);
216                 
217                 assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatusCode().value());
218                 String actualResponse = response.getBody();
219                 assertEquals(expectedResponse, actualResponse);
220         }
221         @Test
222         public void compareModelWithTargetVersionEmptyResponse() throws IOException{
223                 stubFor(post(urlPathEqualTo("/mso/async/services/CompareModelofE2EServiceInstance"))
224                                 .willReturn(aResponse().withFault(Fault.EMPTY_RESPONSE)));
225                 
226                 RequestError expectedResponse = new RequestError();
227                 ServiceException exception = new ServiceException();
228                 exception.setMessageId("SVC1000");
229                 exception.setText("Failed calling bpmn localhost:" + env.getProperty("wiremock.server.port") + " failed to respond");
230                 expectedResponse.setServiceException(exception);
231                 
232                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
233                 ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.POST);
234                 
235                 assertEquals(Response.Status.BAD_GATEWAY.getStatusCode(), response.getStatusCode().value());
236                 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
237                 assertThat(realResponse, sameBeanAs(expectedResponse));
238         }
239         @Test
240         public void compareModelWithTargetVersionBadBpelResponse() throws IOException{
241                 stubFor(post(urlPathEqualTo("/mso/async/services/CompareModelofE2EServiceInstance"))
242                                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
243                                                 .withBodyFile("Camunda/TestResponse.json").withStatus(org.apache.http.HttpStatus.SC_BAD_GATEWAY)));
244                 
245                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
246                 ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.POST);
247                 
248                 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatusCode().value());
249                 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
250                 assertTrue(realResponse.getServiceException().getText().contains("Request Failed due to BPEL error with HTTP Status"));
251         }
252         @Test
253         public void compareModelWithTargetVersionNoBPELResponse() throws IOException{
254                 stubFor(post(urlPathEqualTo("/mso/async/services/CompareModelofE2EServiceInstance"))
255                                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
256                                                 .withBody("{}").withStatus(org.apache.http.HttpStatus.SC_BAD_GATEWAY)));
257
258                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
259                 ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.POST);
260                 
261                 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatusCode().value());
262                 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
263                 assertTrue(realResponse.getServiceException().getText().contains("Request Failed due to BPEL error with HTTP Status"));
264         }
265 }