99542f02ffea601e6f56190efa146cc7e33382fe
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. 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.urlPathEqualTo;
26 import static com.shazam.shazamcrest.MatcherAssert.assertThat;
27 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
28 import static org.junit.Assert.assertEquals;
29 import static org.onap.logging.filter.base.Constants.HttpHeaders.CLIENT_ID;
30 import static org.onap.logging.filter.base.Constants.HttpHeaders.ECOMP_REQUEST_ID;
31 import java.io.IOException;
32 import javax.ws.rs.core.MediaType;
33 import javax.ws.rs.core.Response;
34 import org.apache.http.HttpStatus;
35 import org.junit.Test;
36 import org.onap.so.apihandlerinfra.tasksbeans.RequestDetails;
37 import org.onap.so.apihandlerinfra.tasksbeans.RequestInfo;
38 import org.onap.so.apihandlerinfra.tasksbeans.TaskRequestReference;
39 import org.onap.so.apihandlerinfra.tasksbeans.TasksRequest;
40 import org.onap.so.apihandlerinfra.tasksbeans.ValidResponses;
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.HttpHeaders;
45 import org.springframework.http.HttpMethod;
46 import org.springframework.http.ResponseEntity;
47 import org.springframework.web.util.UriComponentsBuilder;
48 import com.fasterxml.jackson.databind.DeserializationFeature;
49 import com.fasterxml.jackson.databind.ObjectMapper;
50 import com.github.tomakehurst.wiremock.http.Fault;
51
52
53 public class ManualTasksTest extends BaseTest {
54
55     private final String basePath = "/tasks/v1/";
56
57     @Test
58     public void testCreateOpEnvObjectMapperError() throws IOException {
59         TestAppender.events.clear();
60         wireMockServer.stubFor(post(urlPathEqualTo("/sobpmnengine/task/55/complete"))
61                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_OK)));
62
63         String taskId = "55";
64         TasksRequest taskReq = new TasksRequest();
65         RequestDetails reqDetail = new RequestDetails();
66         RequestInfo reqInfo = new RequestInfo();
67         reqInfo.setRequestorId("testId");
68         reqInfo.setSource("testSource");
69         reqInfo.setResponseValue(ValidResponses.skip);
70         reqDetail.setRequestInfo(reqInfo);
71         taskReq.setRequestDetails(reqDetail);
72
73         // expected response
74         TaskRequestReference expectedResponse = new TaskRequestReference();
75         expectedResponse.setTaskId(taskId);
76         HttpHeaders headers = new HttpHeaders();
77         headers.set("Accept", MediaType.APPLICATION_JSON);
78         headers.set("Content-Type", MediaType.APPLICATION_JSON);
79         headers.set(ECOMP_REQUEST_ID, "987654321");
80         headers.set(CLIENT_ID, "VID");
81         HttpEntity<TasksRequest> entity = new HttpEntity<TasksRequest>(taskReq, headers);
82
83         UriComponentsBuilder builder =
84                 UriComponentsBuilder.fromHttpUrl(createURLWithPort(basePath) + taskId + "/complete");
85         ResponseEntity<String> response =
86                 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
87
88
89         ObjectMapper mapper = new ObjectMapper();
90         mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
91         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
92
93         TaskRequestReference realResponse = mapper.readValue(response.getBody(), TaskRequestReference.class);
94
95
96         // then
97         assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatusCode().value());
98         assertThat(realResponse, sameBeanAs(expectedResponse));
99     }
100
101     @Test
102     public void completeTaskMappingError() throws IOException {
103         String invalidRequest = "test";
104         RequestError expectedResponse = new RequestError();
105         ServiceException se = new ServiceException();
106         se.setMessageId("SVC0002");
107         se.setText("Mapping of request to JSON object failed: Unrecognized token \'test\': "
108                 + "was expecting \'null\', \'true\', \'false\' or NaN\n at [Source: (String)\"test\"; line: 1, column: 9]");
109         expectedResponse.setServiceException(se);
110
111         HttpHeaders headers = new HttpHeaders();
112         headers.set("Accept", MediaType.APPLICATION_JSON);
113         headers.set("Content-Type", MediaType.APPLICATION_JSON);
114         headers.set(ECOMP_REQUEST_ID, "987654321");
115         headers.set(CLIENT_ID, "VID");
116         HttpEntity<String> entity = new HttpEntity<String>(invalidRequest, headers);
117
118         UriComponentsBuilder builder =
119                 UriComponentsBuilder.fromHttpUrl(createURLWithPort(basePath) + "55" + "/complete");
120         ResponseEntity<String> response =
121                 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
122
123         ObjectMapper mapper = new ObjectMapper();
124         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
125
126         RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
127         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
128         assertThat(realResponse, sameBeanAs(expectedResponse));
129     }
130
131     @Test
132     public void completeTaskValidationError() throws IOException {
133         String taskId = "55";
134         TasksRequest taskReq = new TasksRequest();
135         RequestDetails reqDetail = new RequestDetails();
136         RequestInfo reqInfo = new RequestInfo();
137         reqInfo.setSource("testSource");
138         reqInfo.setResponseValue(ValidResponses.skip);
139         reqDetail.setRequestInfo(reqInfo);
140         taskReq.setRequestDetails(reqDetail);
141
142         RequestError expectedResponse = new RequestError();
143         ServiceException se = new ServiceException();
144         se.setMessageId("SVC0002");
145         se.setText("Mapping of request to JSON Object failed. No valid requestorId is specified");
146         expectedResponse.setServiceException(se);
147         HttpHeaders headers = new HttpHeaders();
148         headers.set("Accept", MediaType.APPLICATION_JSON);
149         headers.set("Content-Type", MediaType.APPLICATION_JSON);
150         headers.set(ECOMP_REQUEST_ID, "987654321");
151         headers.set(CLIENT_ID, "VID");
152         HttpEntity<TasksRequest> entity = new HttpEntity<TasksRequest>(taskReq, headers);
153
154         UriComponentsBuilder builder =
155                 UriComponentsBuilder.fromHttpUrl(createURLWithPort(basePath) + taskId + "/complete");
156         ResponseEntity<String> response =
157                 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
158
159         ObjectMapper mapper = new ObjectMapper();
160         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
161
162         RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
163         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
164         assertThat(realResponse, sameBeanAs(expectedResponse));
165     }
166
167     @Test
168     public void completeTaskBpelResponseError() throws IOException {
169         wireMockServer.stubFor(post(urlPathEqualTo("/sobpmnengine/task/55/complete")).willReturn(
170                 aResponse().withHeader("Content-Type", "application/json").withFault(Fault.EMPTY_RESPONSE)));
171
172         String taskId = "55";
173         TasksRequest taskReq = new TasksRequest();
174         RequestDetails reqDetail = new RequestDetails();
175         RequestInfo reqInfo = new RequestInfo();
176         reqInfo.setRequestorId("testId");
177         reqInfo.setSource("testSource");
178         reqInfo.setResponseValue(ValidResponses.skip);
179         reqDetail.setRequestInfo(reqInfo);
180         taskReq.setRequestDetails(reqDetail);
181
182         RequestError expectedResponse = new RequestError();
183         ServiceException se = new ServiceException();
184         se.setMessageId("SVC1000");
185         se.setText("Request Failed due to BPEL error with HTTP Status = 502");
186         expectedResponse.setServiceException(se);
187         HttpHeaders headers = new HttpHeaders();
188         headers.set("Accept", MediaType.APPLICATION_JSON);
189         headers.set("Content-Type", MediaType.APPLICATION_JSON);
190         headers.set(ECOMP_REQUEST_ID, "987654321");
191         headers.set(CLIENT_ID, "VID");
192         HttpEntity<TasksRequest> entity = new HttpEntity<TasksRequest>(taskReq, headers);
193
194         UriComponentsBuilder builder =
195                 UriComponentsBuilder.fromHttpUrl(createURLWithPort(basePath) + taskId + "/complete");
196         ResponseEntity<String> response =
197                 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
198
199
200         ObjectMapper mapper = new ObjectMapper();
201         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
202
203         RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
204         assertEquals(Response.Status.BAD_GATEWAY.getStatusCode(), response.getStatusCode().value());
205         assertThat(realResponse, sameBeanAs(expectedResponse));
206     }
207 }