1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
/*-
* ============LICENSE_START=======================================================
* ONAP - SO
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.onap.so.apihandlerinfra;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static com.shazam.shazamcrest.MatcherAssert.assertThat;
import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
import static org.junit.Assert.assertEquals;
import static org.onap.logging.filter.base.Constants.HttpHeaders.ECOMP_REQUEST_ID;
import static org.onap.logging.filter.base.Constants.HttpHeaders.ONAP_PARTNER_NAME;
import java.io.IOException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.http.HttpStatus;
import org.junit.Test;
import org.onap.so.apihandlerinfra.tasksbeans.RequestDetails;
import org.onap.so.apihandlerinfra.tasksbeans.RequestInfo;
import org.onap.so.apihandlerinfra.tasksbeans.TaskRequestReference;
import org.onap.so.apihandlerinfra.tasksbeans.TasksRequest;
import org.onap.so.apihandlerinfra.tasksbeans.ValidResponses;
import org.onap.so.serviceinstancebeans.RequestError;
import org.onap.so.serviceinstancebeans.ServiceException;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.util.UriComponentsBuilder;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.tomakehurst.wiremock.http.Fault;
public class ManualTasksTest extends BaseTest {
private final String basePath = "/onap/so/infra/tasks/v1/";
@Test
public void testCreateOpEnvObjectMapperError() throws IOException {
TestAppender.events.clear();
wireMockServer.stubFor(post(urlPathEqualTo("/sobpmnengine/task/55/complete"))
.willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_OK)));
String taskId = "55";
TasksRequest taskReq = new TasksRequest();
RequestDetails reqDetail = new RequestDetails();
RequestInfo reqInfo = new RequestInfo();
reqInfo.setRequestorId("testId");
reqInfo.setSource("testSource");
reqInfo.setResponseValue(ValidResponses.skip);
reqDetail.setRequestInfo(reqInfo);
taskReq.setRequestDetails(reqDetail);
// expected response
TaskRequestReference expectedResponse = new TaskRequestReference();
expectedResponse.setTaskId(taskId);
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON);
headers.set("Content-Type", MediaType.APPLICATION_JSON);
headers.set(ECOMP_REQUEST_ID, "987654321");
headers.set(ONAP_PARTNER_NAME, "VID");
HttpEntity<TasksRequest> entity = new HttpEntity<TasksRequest>(taskReq, headers);
UriComponentsBuilder builder =
UriComponentsBuilder.fromHttpUrl(createURLWithPort(basePath) + taskId + "/complete");
ResponseEntity<String> response =
restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
TaskRequestReference realResponse = mapper.readValue(response.getBody(), TaskRequestReference.class);
// then
assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatusCode().value());
assertThat(realResponse, sameBeanAs(expectedResponse));
}
@Test
public void completeTaskMappingError() throws IOException {
String invalidRequest = "test";
RequestError expectedResponse = new RequestError();
ServiceException se = new ServiceException();
se.setMessageId("SVC0002");
se.setText("Mapping of request to JSON object failed: Unrecognized token \'test\': "
+ "was expecting \'null\', \'true\', \'false\' or NaN\n at [Source: (String)\"test\"; line: 1, column: 9]");
expectedResponse.setServiceException(se);
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON);
headers.set("Content-Type", MediaType.APPLICATION_JSON);
headers.set(ECOMP_REQUEST_ID, "987654321");
headers.set(ONAP_PARTNER_NAME, "VID");
HttpEntity<String> entity = new HttpEntity<String>(invalidRequest, headers);
UriComponentsBuilder builder =
UriComponentsBuilder.fromHttpUrl(createURLWithPort(basePath) + "55" + "/complete");
ResponseEntity<String> response =
restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
assertThat(realResponse, sameBeanAs(expectedResponse));
}
@Test
public void completeTaskValidationError() throws IOException {
String taskId = "55";
TasksRequest taskReq = new TasksRequest();
RequestDetails reqDetail = new RequestDetails();
RequestInfo reqInfo = new RequestInfo();
reqInfo.setSource("testSource");
reqInfo.setResponseValue(ValidResponses.skip);
reqDetail.setRequestInfo(reqInfo);
taskReq.setRequestDetails(reqDetail);
RequestError expectedResponse = new RequestError();
ServiceException se = new ServiceException();
se.setMessageId("SVC0002");
se.setText("Mapping of request to JSON Object failed. No valid requestorId is specified");
expectedResponse.setServiceException(se);
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON);
headers.set("Content-Type", MediaType.APPLICATION_JSON);
headers.set(ECOMP_REQUEST_ID, "987654321");
headers.set(ONAP_PARTNER_NAME, "VID");
HttpEntity<TasksRequest> entity = new HttpEntity<TasksRequest>(taskReq, headers);
UriComponentsBuilder builder =
UriComponentsBuilder.fromHttpUrl(createURLWithPort(basePath) + taskId + "/complete");
ResponseEntity<String> response =
restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
assertThat(realResponse, sameBeanAs(expectedResponse));
}
@Test
public void completeTaskBpelResponseError() throws IOException {
wireMockServer.stubFor(post(urlPathEqualTo("/sobpmnengine/task/55/complete")).willReturn(
aResponse().withHeader("Content-Type", "application/json").withFault(Fault.EMPTY_RESPONSE)));
String taskId = "55";
TasksRequest taskReq = new TasksRequest();
RequestDetails reqDetail = new RequestDetails();
RequestInfo reqInfo = new RequestInfo();
reqInfo.setRequestorId("testId");
reqInfo.setSource("testSource");
reqInfo.setResponseValue(ValidResponses.skip);
reqDetail.setRequestInfo(reqInfo);
taskReq.setRequestDetails(reqDetail);
RequestError expectedResponse = new RequestError();
ServiceException se = new ServiceException();
se.setMessageId("SVC1000");
se.setText("Client from http://localhost:" + env.getProperty("wiremock.server.port")
+ "/sobpmnengine/task/55/complete failed to connect or respond");
expectedResponse.setServiceException(se);
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON);
headers.set("Content-Type", MediaType.APPLICATION_JSON);
headers.set(ECOMP_REQUEST_ID, "987654321");
headers.set(ONAP_PARTNER_NAME, "VID");
HttpEntity<TasksRequest> entity = new HttpEntity<TasksRequest>(taskReq, headers);
UriComponentsBuilder builder =
UriComponentsBuilder.fromHttpUrl(createURLWithPort(basePath) + taskId + "/complete");
ResponseEntity<String> response =
restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
assertEquals(Response.Status.BAD_GATEWAY.getStatusCode(), response.getStatusCode().value());
assertThat(realResponse, sameBeanAs(expectedResponse));
}
}
|