2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.apihandlerinfra;
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;
32 import java.io.IOException;
33 import java.nio.file.Files;
34 import java.nio.file.Paths;
36 import javax.ws.rs.core.MediaType;
37 import javax.ws.rs.core.Response;
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;
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;
53 public class E2EServiceInstancesTest extends BaseTest {
54 private ObjectMapper mapper = new ObjectMapper();
56 private final String e2eServInstancesUri = "/e2eServiceInstances/";
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)));
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);
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);
74 //Currently returning a 500 response
77 public void createE2EServiceInstanceNoRequestInfo() throws JsonParseException, JsonMappingException, IOException{
78 String uri = e2eServInstancesUri + "v5";
79 ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.POST);
81 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
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);
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"));
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);
100 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
101 ResponseEntity<String> response = sendRequest(inputStream("/Request.json"), uri, HttpMethod.PUT);
103 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
104 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
105 assertThat(realResponse, sameBeanAs(expectedResponse));
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);
115 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e";
116 ResponseEntity<String> response = sendRequest(inputStream("/DeleteRequest.json"), uri, HttpMethod.DELETE);
118 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatusCode().value());
119 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
120 assertThat(realResponse, sameBeanAs(expectedResponse));
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);
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"));
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);
136 assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatusCode().value());
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);
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"));
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);
155 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/scale";
156 ResponseEntity<String> response = sendRequest(inputStream("/ScaleRequest.json"), uri, HttpMethod.POST);
158 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatusCode().value());
159 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
160 assertThat(realResponse, sameBeanAs(expectedResponse));
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);
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"));
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)));
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);
181 assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatusCode().value());
182 String actualResponse = response.getBody();
183 assertEquals(expectedResponse, actualResponse);
186 public void compareModelWithTargetVersionEmptyResponse() throws JsonParseException, JsonMappingException, IOException{
187 stubFor(post(urlPathEqualTo("/mso/async/services/CompareModelofE2EServiceInstance"))
188 .willReturn(aResponse().withFault(Fault.EMPTY_RESPONSE)));
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);
196 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
197 ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.POST);
199 assertEquals(Response.Status.BAD_GATEWAY.getStatusCode(), response.getStatusCode().value());
200 RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
201 assertThat(realResponse, sameBeanAs(expectedResponse));
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)));
209 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
210 ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.POST);
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"));
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)));
222 String uri = e2eServInstancesUri + "v5/9b9f02c0-298b-458a-bc9c-be3692e4f35e/modeldifferences";
223 ResponseEntity<String> response = sendRequest(inputStream("/CompareModelRequest.json"), uri, HttpMethod.POST);
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"));