2 * ============LICENSE_START=======================================================
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
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.any;
25 import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
26 import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson;
27 import static com.github.tomakehurst.wiremock.client.WireMock.get;
28 import static com.github.tomakehurst.wiremock.client.WireMock.post;
29 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
30 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
31 import static com.shazam.shazamcrest.MatcherAssert.assertThat;
32 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
33 import static org.junit.Assert.assertEquals;
36 import java.io.IOException;
37 import java.nio.file.Files;
38 import java.nio.file.Paths;
39 import java.util.ArrayList;
40 import java.util.HashMap;
41 import java.util.List;
44 import javax.ws.rs.core.HttpHeaders;
45 import javax.ws.rs.core.MediaType;
46 import javax.ws.rs.core.Response;
48 import org.apache.http.HttpStatus;
49 import org.junit.Ignore;
50 import org.junit.Test;
51 import org.onap.so.apihandler.common.ErrorNumbers;
52 import org.onap.so.db.request.beans.InfraActiveRequests;
53 import org.onap.so.db.request.client.RequestsDbClient;
54 import org.onap.so.db.request.data.repository.InfraActiveRequestsRepository;
55 import org.onap.so.exceptions.ValidationException;
56 import org.onap.so.serviceinstancebeans.GetOrchestrationListResponse;
57 import org.onap.so.serviceinstancebeans.GetOrchestrationResponse;
58 import org.onap.so.serviceinstancebeans.Request;
59 import org.onap.so.serviceinstancebeans.RequestError;
60 import org.onap.so.serviceinstancebeans.RequestProcessingData;
61 import org.onap.so.serviceinstancebeans.ServiceException;
62 import org.springframework.beans.factory.annotation.Autowired;
63 import org.springframework.http.HttpEntity;
64 import org.springframework.http.HttpMethod;
65 import org.springframework.http.ResponseEntity;
66 import org.springframework.web.util.UriComponentsBuilder;
68 import com.fasterxml.jackson.core.JsonParseException;
69 import com.fasterxml.jackson.core.type.TypeReference;
70 import com.fasterxml.jackson.databind.DeserializationFeature;
71 import com.fasterxml.jackson.databind.JsonMappingException;
72 import com.fasterxml.jackson.databind.ObjectMapper;
74 public class OrchestrationRequestsTest extends BaseTest {
76 private InfraActiveRequestsRepository iar;
79 private RequestsDbClient requestsDbClient;
82 private OrchestrationRequests orchReq;
84 private static final String CHECK_HTML = "<!DOCTYPE html><html><head><meta charset=\"ISO-8859-1\"><title></title></head><body></body></html>";
85 private static final GetOrchestrationListResponse ORCHESTRATION_LIST = generateOrchestrationList();
86 private static final String INVALID_REQUEST_ID = "invalid-request-id";
88 private static GetOrchestrationListResponse generateOrchestrationList() {
89 GetOrchestrationListResponse list = null;
91 ObjectMapper mapper = new ObjectMapper();
92 list = mapper.readValue(new File("src/test/resources/OrchestrationRequest/OrchestrationList.json"),
93 GetOrchestrationListResponse.class);
94 } catch (JsonParseException jpe) {
95 jpe.printStackTrace();
96 } catch (JsonMappingException jme) {
97 jme.printStackTrace();
98 } catch (IOException ioe) {
99 ioe.printStackTrace();
105 public void testGetOrchestrationRequest() throws Exception {
106 setupTestGetOrchestrationRequest();
107 // TEST VALID REQUEST
108 GetOrchestrationResponse testResponse = new GetOrchestrationResponse();
110 Request request = ORCHESTRATION_LIST.getRequestList().get(1).getRequest();
111 testResponse.setRequest(request);
112 String testRequestId = request.getRequestId();
114 headers.set("Accept", MediaType.APPLICATION_JSON);
115 headers.set("Content-Type", MediaType.APPLICATION_JSON);
116 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
118 UriComponentsBuilder builder = UriComponentsBuilder
119 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/" + testRequestId));
121 ResponseEntity<GetOrchestrationResponse> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET,
122 entity, GetOrchestrationResponse.class);
124 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
125 assertThat(response.getBody(),
126 sameBeanAs(testResponse).ignoring("request.startTime").ignoring("request.requestStatus.finishTime"));
127 assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
128 assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
129 assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
130 assertEquals("7.0.0", response.getHeaders().get("X-LatestVersion").get(0));
131 assertEquals("00032ab7-na18-42e5-965d-8ea592502018", response.getHeaders().get("X-TransactionID").get(0));
135 public void testGetOrchestrationRequestRequestDetails() throws Exception {
136 setupTestGetOrchestrationRequestRequestDetails("00032ab7-3fb3-42e5-965d-8ea592502017", "COMPLETED");
137 //Test request with modelInfo request body
138 GetOrchestrationResponse testResponse = new GetOrchestrationResponse();
140 Request request = ORCHESTRATION_LIST.getRequestList().get(0).getRequest();
141 testResponse.setRequest(request);
142 String testRequestId = request.getRequestId();
144 headers.set("Accept", MediaType.APPLICATION_JSON);
145 headers.set("Content-Type", MediaType.APPLICATION_JSON);
146 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
148 UriComponentsBuilder builder = UriComponentsBuilder
149 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/" + testRequestId));
151 ResponseEntity<GetOrchestrationResponse> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET,
152 entity, GetOrchestrationResponse.class);
154 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
155 assertThat(response.getBody(),
156 sameBeanAs(testResponse).ignoring("request.startTime").ignoring("request.requestStatus.finishTime"));
157 assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
158 assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
159 assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
160 assertEquals("7.0.0", response.getHeaders().get("X-LatestVersion").get(0));
161 assertEquals("00032ab7-3fb3-42e5-965d-8ea592502017", response.getHeaders().get("X-TransactionID").get(0));
165 public void testGetOrchestrationRequestNoRequestID() {
166 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
167 headers.set("Accept", MediaType.APPLICATION_JSON);
169 UriComponentsBuilder builder = UriComponentsBuilder
170 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v6/"));
172 ResponseEntity<GetOrchestrationListResponse> response = restTemplate.exchange(builder.toUriString(),
173 HttpMethod.GET, entity, GetOrchestrationListResponse.class);
174 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
178 public void testGetOrchestrationRequestFilter() throws Exception {
179 setupTestGetOrchestrationRequestFilter();
180 List<String> values = new ArrayList<>();
181 values.add("EQUALS");
182 values.add("vfModule");
184 ObjectMapper mapper = new ObjectMapper();
185 GetOrchestrationListResponse testResponse = mapper.readValue(new File("src/test/resources/OrchestrationRequest/OrchestrationFilterResponse.json"),
186 GetOrchestrationListResponse.class);
188 Map<String, List<String>> orchestrationMap = new HashMap<>();
189 orchestrationMap.put("modelType", values);
190 List<GetOrchestrationResponse> testResponses = new ArrayList<>();
192 List<InfraActiveRequests> requests = requestsDbClient.getOrchestrationFiltersFromInfraActive(orchestrationMap);
193 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
194 headers.set("Accept", MediaType.APPLICATION_JSON);
196 UriComponentsBuilder builder = UriComponentsBuilder
197 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v6?filter=modelType:EQUALS:vfModule"));
199 ResponseEntity<GetOrchestrationListResponse> response = restTemplate.exchange(builder.toUriString(),
200 HttpMethod.GET, entity, GetOrchestrationListResponse.class);
201 assertThat(response.getBody(),
202 sameBeanAs(testResponse).ignoring("requestList.request.startTime").ignoring("requestList.request.requestStatus.finishTime"));
203 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
204 assertEquals(requests.size(), response.getBody().getRequestList().size());
209 public void testUnlockOrchestrationRequest() throws Exception {
210 setupTestUnlockOrchestrationRequest("0017f68c-eb2d-45bb-b7c7-ec31b37dc349", "UNLOCKED");
211 ObjectMapper mapper = new ObjectMapper();
212 mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
213 String requestJSON = new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/Request.json")));
215 headers.set("Accept", MediaType.APPLICATION_JSON);
216 headers.set("Content-Type", MediaType.APPLICATION_JSON);
217 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
219 UriComponentsBuilder builder;
220 ResponseEntity<String> response;
221 RequestError expectedRequestError;
222 RequestError actualRequestError;
226 expectedRequestError = new RequestError();
227 se = new ServiceException();
228 se.setMessageId(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR);
229 se.setText("Orchestration RequestId 0017f68c-eb2d-45bb-b7c7-ec31b37dc349 has a status of UNLOCKED and can not be unlocked");
230 expectedRequestError.setServiceException(se);
232 builder = UriComponentsBuilder.fromHttpUrl(
233 createURLWithPort("/onap/so/infra/orchestrationRequests/v6/0017f68c-eb2d-45bb-b7c7-ec31b37dc349/unlock"));
235 response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
236 actualRequestError = mapper.readValue(response.getBody(), RequestError.class);
238 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
239 assertThat(actualRequestError, sameBeanAs(expectedRequestError));
243 public void testUnlockOrchestrationRequest_invalid_Json() throws Exception {
244 setupTestUnlockOrchestrationRequest_invalid_Json();
245 ObjectMapper mapper = new ObjectMapper();
246 mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
247 String requestJSON = new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/Request.json")));
249 headers.set("Accept", MediaType.APPLICATION_JSON);
250 headers.set("Content-Type", MediaType.APPLICATION_JSON);
251 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
253 UriComponentsBuilder builder;
254 ResponseEntity<String> response;
255 RequestError expectedRequestError;
256 RequestError actualRequestError;
259 // Test invalid requestId
260 expectedRequestError = new RequestError();
261 se = new ServiceException();
262 se.setMessageId(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR);
263 se.setText("Null response from RequestDB when searching by RequestId");
264 expectedRequestError.setServiceException(se);
266 builder = UriComponentsBuilder.fromHttpUrl(
267 createURLWithPort("/onap/so/infra/orchestrationRequests/v6/" + INVALID_REQUEST_ID + "/unlock"));
269 response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
270 actualRequestError = mapper.readValue(response.getBody(), RequestError.class);
272 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatusCode().value());
273 assertThat(actualRequestError, sameBeanAs(expectedRequestError));
277 public void testUnlockOrchestrationRequest_Valid_Status()
278 throws JsonParseException, JsonMappingException, IOException, ValidationException {
279 setupTestUnlockOrchestrationRequest_Valid_Status("5ffbabd6-b793-4377-a1ab-082670fbc7ac", "PENDING");
280 ObjectMapper mapper = new ObjectMapper();
281 String requestJSON = new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/Request.json")));
283 headers.set("Accept", MediaType.APPLICATION_JSON);
284 headers.set("Content-Type", MediaType.APPLICATION_JSON);
285 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
287 UriComponentsBuilder builder;
288 ResponseEntity<String> response;
292 request = ORCHESTRATION_LIST.getRequestList().get(1).getRequest();
293 builder = UriComponentsBuilder.fromHttpUrl(
294 createURLWithPort("/onap/so/infra/orchestrationRequests/v7/" + "5ffbabd6-b793-4377-a1ab-082670fbc7ac" + "/unlock"));
296 response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
297 //Cannot assert anything further here, already have a wiremock in place which ensures that the post was properly called to update.
300 @Ignore //What is this testing?
302 public void testGetOrchestrationRequestRequestDetailsWhiteSpace() throws Exception {
303 InfraActiveRequests requests = new InfraActiveRequests();
304 requests.setAction("create");
305 requests.setRequestBody(" ");
306 requests.setRequestId("requestId");
307 requests.setRequestScope("service");
308 requests.setRequestType("createInstance");
309 ObjectMapper mapper = new ObjectMapper();
310 String json = mapper.writeValueAsString(requests);
312 requestsDbClient.save(requests);
314 headers.set("Accept", MediaType.APPLICATION_JSON);
315 headers.set("Content-Type", MediaType.APPLICATION_JSON);
316 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
318 UriComponentsBuilder builder = UriComponentsBuilder
319 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/requestId"));
321 ResponseEntity<GetOrchestrationResponse> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET,
322 entity, GetOrchestrationResponse.class);
324 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
325 assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
326 assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
327 assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
328 assertEquals("7.0.0", response.getHeaders().get("X-LatestVersion").get(0));
329 assertEquals("requestId", response.getHeaders().get("X-TransactionID").get(0));
332 @Ignore //What is this testing?
334 public void testGetOrchestrationRequestRequestDetailsAlaCarte() throws IOException {
335 InfraActiveRequests requests = new InfraActiveRequests();
337 String requestJSON = new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/AlaCarteRequest.json")));
339 requests.setAction("create");
340 requests.setRequestBody(requestJSON);
341 requests.setRequestId("requestId");
342 requests.setRequestScope("service");
343 requests.setRequestType("createInstance");
346 headers.set("Accept", MediaType.APPLICATION_JSON);
347 headers.set("Content-Type", MediaType.APPLICATION_JSON);
348 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
350 UriComponentsBuilder builder = UriComponentsBuilder
351 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/requestId"));
353 ResponseEntity<GetOrchestrationResponse> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET,
354 entity, GetOrchestrationResponse.class);
356 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
357 assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
358 assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
359 assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
360 assertEquals("7.0.0", response.getHeaders().get("X-LatestVersion").get(0));
361 assertEquals("requestId", response.getHeaders().get("X-TransactionID").get(0));
364 public void mapRequestProcessingDataTest() throws JsonParseException, JsonMappingException, IOException{
365 RequestProcessingData entry = new RequestProcessingData();
366 RequestProcessingData secondEntry = new RequestProcessingData();
367 List<HashMap<String, String>> expectedList = new ArrayList<>();
368 HashMap<String, String> expectedMap = new HashMap<>();
369 List<HashMap<String, String>> secondExpectedList = new ArrayList<>();
370 HashMap<String, String> secondExpectedMap = new HashMap<>();
371 List<RequestProcessingData> expectedDataList = new ArrayList<>();
372 entry.setGroupingId("7d2e8c07-4d10-456d-bddc-37abf38ca714");
373 entry.setTag("pincFabricConfigRequest");
374 expectedMap.put("requestAction", "assign");
375 expectedMap.put("pincFabricId", "testId");
376 expectedList.add(expectedMap);
377 entry.setDataPairs(expectedList);
378 secondEntry.setGroupingId("7d2e8c07-4d10-456d-bddc-37abf38ca715");
379 secondEntry.setTag("pincFabricConfig");
380 secondExpectedMap.put("requestAction", "unassign");
381 secondExpectedList.add(secondExpectedMap);
382 secondEntry.setDataPairs(secondExpectedList);
383 expectedDataList.add(entry);
384 expectedDataList.add(secondEntry);
386 List<org.onap.so.db.request.beans.RequestProcessingData> processingData = new ArrayList<>();
387 List<RequestProcessingData> actualProcessingData = new ArrayList<>();
388 ObjectMapper mapper = new ObjectMapper();
389 processingData = mapper.readValue(new File("src/test/resources/OrchestrationRequest/RequestProcessingData.json"),
390 new TypeReference<List<org.onap.so.db.request.beans.RequestProcessingData>>(){});
391 actualProcessingData = orchReq.mapRequestProcessingData(processingData);
392 assertThat(actualProcessingData,sameBeanAs(expectedDataList));
395 public void setupTestGetOrchestrationRequest() throws Exception{
396 //For testGetOrchestrationRequest
397 stubFor(any(urlPathEqualTo("/infraActiveRequests/00032ab7-na18-42e5-965d-8ea592502018")).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
398 .withBody(new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/getOrchestrationRequest.json"))))
399 .withStatus(HttpStatus.SC_OK)));
400 stubFor(get(urlPathEqualTo("/requestProcessingData/search/findBySoRequestIdOrderByGroupingIdDesc/"))
401 .withQueryParam("SO_REQUEST_ID", equalTo("00032ab7-na18-42e5-965d-8ea592502018"))
402 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
403 .withBody(new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/getRequestProcessingData.json"))))
404 .withStatus(HttpStatus.SC_OK)));
407 private void setupTestGetOrchestrationRequestRequestDetails(String requestId, String status) throws Exception{
408 stubFor(get(urlPathEqualTo(getTestUrl(requestId))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
409 .withBody(new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/getOrchestrationRequestDetails.json"))))
410 .withStatus(HttpStatus.SC_OK)));
413 private void setupTestUnlockOrchestrationRequest(String requestId, String status) {
414 stubFor(get(urlPathEqualTo(getTestUrl(requestId))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
415 .withBody(String.format(getResponseTemplate, requestId, status))
416 .withStatus(HttpStatus.SC_OK)));
422 private void setupTestUnlockOrchestrationRequest_invalid_Json() {
423 stubFor(get(urlPathEqualTo(getTestUrl(INVALID_REQUEST_ID))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
424 .withStatus(HttpStatus.SC_NOT_FOUND)));
428 private void setupTestUnlockOrchestrationRequest_Valid_Status(String requestID, String status) {
429 stubFor(get(urlPathEqualTo(getTestUrl(requestID))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
430 .withBody(String.format(getResponseTemplate, requestID, status))
431 .withStatus(HttpStatus.SC_OK)));
433 stubFor(post(urlPathEqualTo(getTestUrl(""))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
434 .withBody(String.format(infraActivePost, requestID))
435 .withStatus(HttpStatus.SC_OK)));
438 private void setupTestGetOrchestrationRequestFilter() throws Exception{
439 //for testGetOrchestrationRequestFilter();
440 stubFor(any(urlPathEqualTo("/infraActiveRequests/getOrchestrationFiltersFromInfraActive/")).withRequestBody(equalToJson("{\"modelType\":[\"EQUALS\",\"vfModule\"]}")).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
441 .withBody(new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/getRequestDetailsFilter.json"))))
442 .withStatus(HttpStatus.SC_OK)));