Containerization feature of SO
[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.post;
25 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
26 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
27 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertThat;
30 import static org.junit.Assert.assertTrue;
31
32 import java.io.IOException;
33 import java.nio.file.Files;
34 import java.nio.file.Paths;
35
36 import javax.ws.rs.core.MediaType;
37 import javax.ws.rs.core.Response;
38
39 import org.junit.Ignore;
40 import org.junit.Test;
41 import org.onap.so.serviceinstancebeans.RequestError;
42 import org.onap.so.serviceinstancebeans.ServiceException;
43 import org.springframework.http.HttpEntity;
44 import org.springframework.http.HttpMethod;
45 import org.springframework.http.ResponseEntity;
46 import org.springframework.web.util.UriComponentsBuilder;
47
48 import com.fasterxml.jackson.core.JsonParseException;
49 import com.fasterxml.jackson.databind.JsonMappingException;
50 import com.fasterxml.jackson.databind.ObjectMapper;
51 import com.github.tomakehurst.wiremock.http.Fault;
52
53 public class E2EServiceInstancesTest extends BaseTest {
54 private ObjectMapper mapper = new ObjectMapper();       
55         
56         private final String e2eServInstancesUri = "/e2eServiceInstances/";
57         
58         public String inputStream(String JsonInput)throws IOException{
59                 JsonInput = "src/test/resources/E2EServiceInstancesTest" + JsonInput;
60                 String input = new String(Files.readAllBytes(Paths.get(JsonInput)));
61                 return input;
62         }
63         public ResponseEntity<String> sendRequest(String requestJson, String uriPath, HttpMethod reqMethod){             
64                 headers.set("Accept", MediaType.APPLICATION_JSON);
65                 headers.set("Content-Type",MediaType.APPLICATION_JSON);
66                 
67                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(uriPath));
68                 HttpEntity<String> request = new HttpEntity<String>(requestJson, headers);  
69                 ResponseEntity<String> response = restTemplate.exchange(builder.toUriString(),
70                                 reqMethod, request, String.class);
71                 
72                 return response;
73         }
74         //Currently returning a 500 response
75         @Ignore
76         @Test
77         public void createE2EServiceInstanceNoRequestInfo() throws JsonParseException, JsonMappingException, IOException{
78                 String uri = e2eServInstancesUri + "v5";
79                 ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.POST);
80                 
81                 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
82         }
83         @Test
84         public void updateE2EServiceInstanceJSONMappingError() throws JsonParseException, JsonMappingException, IOException{
85                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
86                 ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.PUT);
87                 
88                 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
89                 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
90                 assertTrue(realResponse.getServiceException().getText().contains("Mapping of request to JSON object failed"));
91         }
92         @Test
93         public void updateE2EServiceInstanceNoRequestorId() throws JsonParseException, JsonMappingException, IOException{
94                 RequestError expectedResponse = new RequestError();
95                 ServiceException exception = new ServiceException();
96                 exception.setMessageId("SVC0002");
97                 exception.setText("Error parsing request.  Error parsing request: No valid requestorId is specified");
98                 expectedResponse.setServiceException(exception);
99                 
100                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
101                 ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.PUT);
102                 
103                 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
104                 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
105                 assertThat(realResponse, sameBeanAs(expectedResponse));
106         }
107         @Test
108         public void deleteE2EServiceInstanceNoRecipe() throws JsonParseException, JsonMappingException, IOException{
109                 RequestError expectedResponse = new RequestError();
110                 ServiceException exception = new ServiceException();
111                 exception.setMessageId("SVC1000");
112                 exception.setText("No communication to catalog DB null");
113                 expectedResponse.setServiceException(exception);
114                 
115                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
116                 ResponseEntity<String> response = sendRequest(inputStream("/DeleteRequest.json"), uri, HttpMethod.DELETE);
117                 
118                 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatusCode().value());
119                 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
120                 assertThat(realResponse, sameBeanAs(expectedResponse));
121         }
122         @Test
123         public void deleteE2EServiceInstanceNotValid() throws JsonParseException, JsonMappingException, IOException{
124                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
125                 ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.DELETE);
126                 
127                 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
128                 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
129                 assertTrue(realResponse.getServiceException().getText().contains("Mapping of request to JSON object failed"));
130         }
131         @Test
132         public void getE2EServiceInstanceNullOperationalStatus() throws JsonParseException, JsonMappingException, IOException{
133                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/operations/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
134                 ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.GET);
135                 
136                 assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatusCode().value());
137         }
138         @Test
139         public void scaleE2EServiceInstanceMappingError() throws JsonParseException, JsonMappingException, IOException{
140                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/scale";
141                 ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.POST);
142                 
143                 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
144                 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
145                 assertTrue(realResponse.getServiceException().getText().contains("Mapping of request to JSON object failed"));
146         }
147         @Test
148         public void scaleE2EServiceInstance() throws JsonParseException, JsonMappingException, IOException{
149                 RequestError expectedResponse = new RequestError();
150                 ServiceException exception = new ServiceException();
151                 exception.setMessageId("SVC1000");
152                 exception.setText("No communication to catalog DB null");
153                 expectedResponse.setServiceException(exception);
154                 
155                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/scale";
156                 ResponseEntity<String> response = sendRequest(inputStream("/ScaleRequest.json"), uri, HttpMethod.POST);
157                 
158                 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatusCode().value());
159                 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
160                 assertThat(realResponse, sameBeanAs(expectedResponse));
161         }
162         @Test
163         public void compareModelWithTargetVersionBadRequest() throws JsonParseException, JsonMappingException, IOException{
164                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
165                 ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.POST);
166                 
167                 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
168                 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
169                 assertTrue(realResponse.getServiceException().getText().contains("Mapping of request to JSON object failed"));
170         }
171         @Test
172         public void compareModelWithTargetVersion() throws JsonParseException, JsonMappingException, IOException{
173                 stubFor(post(urlPathEqualTo("/mso/async/services/CompareModelofE2EServiceInstance"))
174                                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
175                                                 .withBodyFile("Camunda/SuccessfulResponse.json").withStatus(org.apache.http.HttpStatus.SC_ACCEPTED)));
176                 
177                 String expectedResponse = "success";
178                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
179                 ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.POST);
180                 
181                 assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatusCode().value());
182                 String actualResponse = response.getBody();
183                 assertEquals(expectedResponse, actualResponse);
184         }
185         @Test
186         public void compareModelWithTargetVersionEmptyResponse() throws JsonParseException, JsonMappingException, IOException{
187                 stubFor(post(urlPathEqualTo("/mso/async/services/CompareModelofE2EServiceInstance"))
188                                 .willReturn(aResponse().withFault(Fault.EMPTY_RESPONSE)));
189                 
190                 RequestError expectedResponse = new RequestError();
191                 ServiceException exception = new ServiceException();
192                 exception.setMessageId("SVC1000");
193                 exception.setText("Failed calling bpmn localhost:" + env.getProperty("wiremock.server.port") + " failed to respond");
194                 expectedResponse.setServiceException(exception);
195                 
196                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
197                 ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.POST);
198                 
199                 assertEquals(Response.Status.BAD_GATEWAY.getStatusCode(), response.getStatusCode().value());
200                 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
201                 assertThat(realResponse, sameBeanAs(expectedResponse));
202         }
203         @Test
204         public void compareModelWithTargetVersionBadBpelResponse() throws JsonParseException, JsonMappingException, IOException{
205                 stubFor(post(urlPathEqualTo("/mso/async/services/CompareModelofE2EServiceInstance"))
206                                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
207                                                 .withBodyFile("Camunda/TestResponse.json").withStatus(org.apache.http.HttpStatus.SC_BAD_GATEWAY)));
208                 
209                 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
210                 ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.POST);
211                 
212                 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatusCode().value());
213                 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
214                 assertTrue(realResponse.getServiceException().getText().contains("Request Failed due to BPEL error with HTTP Status"));
215         }
216         @Test
217         public void compareModelWithTargetVersionNoBPELResponse() throws JsonParseException, JsonMappingException, IOException{
218                 stubFor(post(urlPathEqualTo("/mso/async/services/CompareModelofE2EServiceInstance"))
219                                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
220                                                 .withBody("{}").withStatus(org.apache.http.HttpStatus.SC_BAD_GATEWAY)));
221
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.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatusCode().value());
226                 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
227                 assertTrue(realResponse.getServiceException().getText().contains("Request Failed due to BPEL error with HTTP Status"));
228         }
229 }