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.MediaType;
45 import javax.ws.rs.core.Response;
47 import org.apache.http.HttpStatus;
48 import org.junit.Ignore;
49 import org.junit.Test;
50 import org.onap.so.apihandler.common.ErrorNumbers;
51 import org.onap.so.db.request.beans.InfraActiveRequests;
52 import org.onap.so.db.request.client.RequestsDbClient;
53 import org.onap.so.exceptions.ValidationException;
54 import org.onap.so.serviceinstancebeans.GetOrchestrationListResponse;
55 import org.onap.so.serviceinstancebeans.GetOrchestrationResponse;
56 import org.onap.so.serviceinstancebeans.Request;
57 import org.onap.so.serviceinstancebeans.RequestError;
58 import org.onap.so.serviceinstancebeans.RequestProcessingData;
59 import org.onap.so.serviceinstancebeans.ServiceException;
60 import org.springframework.beans.factory.annotation.Autowired;
61 import org.springframework.http.HttpEntity;
62 import org.springframework.http.HttpHeaders;
63 import org.springframework.http.HttpMethod;
64 import org.springframework.http.ResponseEntity;
65 import org.springframework.web.util.UriComponentsBuilder;
67 import com.fasterxml.jackson.core.JsonParseException;
68 import com.fasterxml.jackson.core.type.TypeReference;
69 import com.fasterxml.jackson.databind.DeserializationFeature;
70 import com.fasterxml.jackson.databind.JsonMappingException;
71 import com.fasterxml.jackson.databind.ObjectMapper;
73 public class OrchestrationRequestsTest extends BaseTest {
75 private RequestsDbClient requestsDbClient;
78 private OrchestrationRequests orchReq;
80 private static final GetOrchestrationListResponse ORCHESTRATION_LIST = generateOrchestrationList();
81 private static final String INVALID_REQUEST_ID = "invalid-request-id";
83 private static GetOrchestrationListResponse generateOrchestrationList() {
84 GetOrchestrationListResponse list = null;
86 ObjectMapper mapper = new ObjectMapper();
87 list = mapper.readValue(new File("src/test/resources/OrchestrationRequest/OrchestrationList.json"),
88 GetOrchestrationListResponse.class);
89 } catch (JsonParseException jpe) {
90 jpe.printStackTrace();
91 } catch (JsonMappingException jme) {
92 jme.printStackTrace();
93 } catch (IOException ioe) {
94 ioe.printStackTrace();
100 public void testGetOrchestrationRequest() throws Exception {
101 setupTestGetOrchestrationRequest();
102 // TEST VALID REQUEST
103 GetOrchestrationResponse testResponse = new GetOrchestrationResponse();
105 Request request = ORCHESTRATION_LIST.getRequestList().get(1).getRequest();
106 testResponse.setRequest(request);
107 String testRequestId = request.getRequestId();
108 HttpHeaders headers = new HttpHeaders();
109 headers.set("Accept", MediaType.APPLICATION_JSON);
110 headers.set("Content-Type", MediaType.APPLICATION_JSON);
111 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
113 UriComponentsBuilder builder = UriComponentsBuilder
114 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/" + testRequestId));
116 ResponseEntity<GetOrchestrationResponse> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET,
117 entity, GetOrchestrationResponse.class);
119 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
120 assertThat(response.getBody(),
121 sameBeanAs(testResponse).ignoring("request.startTime").ignoring("request.requestStatus.finishTime"));
122 assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
123 assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
124 assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
125 assertEquals("7.0.0", response.getHeaders().get("X-LatestVersion").get(0));
126 assertEquals("00032ab7-na18-42e5-965d-8ea592502018", response.getHeaders().get("X-TransactionID").get(0));
130 public void testGetOrchestrationRequestRequestDetails() throws Exception {
131 setupTestGetOrchestrationRequestRequestDetails("00032ab7-3fb3-42e5-965d-8ea592502017", "COMPLETED");
132 //Test request with modelInfo request body
133 GetOrchestrationResponse testResponse = new GetOrchestrationResponse();
135 Request request = ORCHESTRATION_LIST.getRequestList().get(0).getRequest();
136 testResponse.setRequest(request);
137 String testRequestId = request.getRequestId();
139 HttpHeaders headers = new HttpHeaders();
140 headers.set("Accept", MediaType.APPLICATION_JSON);
141 headers.set("Content-Type", MediaType.APPLICATION_JSON);
142 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
144 UriComponentsBuilder builder = UriComponentsBuilder
145 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/" + testRequestId));
147 ResponseEntity<GetOrchestrationResponse> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET,
148 entity, GetOrchestrationResponse.class);
150 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
151 assertThat(response.getBody(),
152 sameBeanAs(testResponse).ignoring("request.startTime").ignoring("request.requestStatus.finishTime"));
153 assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
154 assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
155 assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
156 assertEquals("7.0.0", response.getHeaders().get("X-LatestVersion").get(0));
157 assertEquals("00032ab7-3fb3-42e5-965d-8ea592502017", response.getHeaders().get("X-TransactionID").get(0));
161 public void testGetOrchestrationRequestNoRequestID() {
162 HttpHeaders headers = new HttpHeaders();
163 headers.set("Accept", "application/json; charset=UTF-8");
164 headers.set("Content-Type", "application/json; charset=UTF-8");
165 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
166 UriComponentsBuilder builder = UriComponentsBuilder
167 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v6/"));
169 ResponseEntity<GetOrchestrationListResponse> response = restTemplate.exchange(builder.toUriString(),
170 HttpMethod.GET, entity, GetOrchestrationListResponse.class);
171 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
175 public void testGetOrchestrationRequestFilter() throws Exception {
176 setupTestGetOrchestrationRequestFilter();
177 List<String> values = new ArrayList<>();
178 values.add("EQUALS");
179 values.add("vfModule");
181 ObjectMapper mapper = new ObjectMapper();
182 GetOrchestrationListResponse testResponse = mapper.readValue(new File("src/test/resources/OrchestrationRequest/OrchestrationFilterResponse.json"),
183 GetOrchestrationListResponse.class);
185 Map<String, List<String>> orchestrationMap = new HashMap<>();
186 orchestrationMap.put("modelType", values);
187 List<GetOrchestrationResponse> testResponses = new ArrayList<>();
189 List<InfraActiveRequests> requests = requestsDbClient.getOrchestrationFiltersFromInfraActive(orchestrationMap);
190 HttpHeaders headers = new HttpHeaders();
191 headers.set("Accept", MediaType.APPLICATION_JSON);
192 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
194 UriComponentsBuilder builder = UriComponentsBuilder
195 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v6?filter=modelType:EQUALS:vfModule"));
197 ResponseEntity<GetOrchestrationListResponse> response = restTemplate.exchange(builder.toUriString(),
198 HttpMethod.GET, entity, GetOrchestrationListResponse.class);
199 assertThat(response.getBody(),
200 sameBeanAs(testResponse).ignoring("requestList.request.startTime").ignoring("requestList.request.requestStatus.finishTime"));
201 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
202 assertEquals(requests.size(), response.getBody().getRequestList().size());
207 public void testUnlockOrchestrationRequest() throws Exception {
208 setupTestUnlockOrchestrationRequest("0017f68c-eb2d-45bb-b7c7-ec31b37dc349", "UNLOCKED");
209 ObjectMapper mapper = new ObjectMapper();
210 mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
211 String requestJSON = new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/Request.json")));
212 HttpHeaders headers = new HttpHeaders();
213 headers.set("Accept", MediaType.APPLICATION_JSON);
214 headers.set("Content-Type", MediaType.APPLICATION_JSON);
215 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
217 UriComponentsBuilder builder;
218 ResponseEntity<String> response;
219 RequestError expectedRequestError;
220 RequestError actualRequestError;
224 expectedRequestError = new RequestError();
225 se = new ServiceException();
226 se.setMessageId(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR);
227 se.setText("Orchestration RequestId 0017f68c-eb2d-45bb-b7c7-ec31b37dc349 has a status of UNLOCKED and can not be unlocked");
228 expectedRequestError.setServiceException(se);
230 builder = UriComponentsBuilder.fromHttpUrl(
231 createURLWithPort("/onap/so/infra/orchestrationRequests/v6/0017f68c-eb2d-45bb-b7c7-ec31b37dc349/unlock"));
233 response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
234 actualRequestError = mapper.readValue(response.getBody(), RequestError.class);
236 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
237 assertThat(actualRequestError, sameBeanAs(expectedRequestError));
241 public void testUnlockOrchestrationRequest_invalid_Json() throws Exception {
242 setupTestUnlockOrchestrationRequest_invalid_Json();
243 ObjectMapper mapper = new ObjectMapper();
244 mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
245 String requestJSON = new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/Request.json")));
246 HttpHeaders headers = new HttpHeaders();
247 headers.set("Accept", MediaType.APPLICATION_JSON);
248 headers.set("Content-Type", MediaType.APPLICATION_JSON);
249 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
251 UriComponentsBuilder builder;
252 ResponseEntity<String> response;
253 RequestError expectedRequestError;
254 RequestError actualRequestError;
257 // Test invalid requestId
258 expectedRequestError = new RequestError();
259 se = new ServiceException();
260 se.setMessageId(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR);
261 se.setText("Null response from RequestDB when searching by RequestId");
262 expectedRequestError.setServiceException(se);
264 builder = UriComponentsBuilder.fromHttpUrl(
265 createURLWithPort("/onap/so/infra/orchestrationRequests/v6/" + INVALID_REQUEST_ID + "/unlock"));
267 response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
268 actualRequestError = mapper.readValue(response.getBody(), RequestError.class);
270 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatusCode().value());
271 assertThat(actualRequestError, sameBeanAs(expectedRequestError));
275 public void testUnlockOrchestrationRequest_Valid_Status()
276 throws JsonParseException, JsonMappingException, IOException, ValidationException {
277 setupTestUnlockOrchestrationRequest_Valid_Status("5ffbabd6-b793-4377-a1ab-082670fbc7ac", "PENDING");
278 ObjectMapper mapper = new ObjectMapper();
279 String requestJSON = new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/Request.json")));
280 HttpHeaders headers = new HttpHeaders();
281 headers.set("Accept", MediaType.APPLICATION_JSON);
282 headers.set("Content-Type", MediaType.APPLICATION_JSON);
283 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
285 UriComponentsBuilder builder;
286 ResponseEntity<String> response;
290 request = ORCHESTRATION_LIST.getRequestList().get(1).getRequest();
291 builder = UriComponentsBuilder.fromHttpUrl(
292 createURLWithPort("/onap/so/infra/orchestrationRequests/v7/" + "5ffbabd6-b793-4377-a1ab-082670fbc7ac" + "/unlock"));
294 response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
295 //Cannot assert anything further here, already have a wiremock in place which ensures that the post was properly called to update.
298 @Ignore //What is this testing?
300 public void testGetOrchestrationRequestRequestDetailsWhiteSpace() throws Exception {
301 InfraActiveRequests requests = new InfraActiveRequests();
302 requests.setAction("create");
303 requests.setRequestBody(" ");
304 requests.setRequestId("requestId");
305 requests.setRequestScope("service");
306 requests.setRequestType("createInstance");
307 ObjectMapper mapper = new ObjectMapper();
308 String json = mapper.writeValueAsString(requests);
310 requestsDbClient.save(requests);
311 HttpHeaders headers = new HttpHeaders();
312 headers.set("Accept", MediaType.APPLICATION_JSON);
313 headers.set("Content-Type", MediaType.APPLICATION_JSON);
314 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
316 UriComponentsBuilder builder = UriComponentsBuilder
317 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/requestId"));
319 ResponseEntity<GetOrchestrationResponse> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET,
320 entity, GetOrchestrationResponse.class);
322 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
323 assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
324 assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
325 assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
326 assertEquals("7.0.0", response.getHeaders().get("X-LatestVersion").get(0));
327 assertEquals("requestId", response.getHeaders().get("X-TransactionID").get(0));
330 @Ignore //What is this testing?
332 public void testGetOrchestrationRequestRequestDetailsAlaCarte() throws IOException {
333 InfraActiveRequests requests = new InfraActiveRequests();
335 String requestJSON = new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/AlaCarteRequest.json")));
337 requests.setAction("create");
338 requests.setRequestBody(requestJSON);
339 requests.setRequestId("requestId");
340 requests.setRequestScope("service");
341 requests.setRequestType("createInstance");
342 // iar.save(requests);
343 HttpHeaders headers = new HttpHeaders();
344 headers.set("Accept", MediaType.APPLICATION_JSON);
345 headers.set("Content-Type", MediaType.APPLICATION_JSON);
346 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
348 UriComponentsBuilder builder = UriComponentsBuilder
349 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/requestId"));
351 ResponseEntity<GetOrchestrationResponse> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET,
352 entity, GetOrchestrationResponse.class);
354 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
355 assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
356 assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
357 assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
358 assertEquals("7.0.0", response.getHeaders().get("X-LatestVersion").get(0));
359 assertEquals("requestId", response.getHeaders().get("X-TransactionID").get(0));
362 public void mapRequestProcessingDataTest() throws JsonParseException, JsonMappingException, IOException{
363 RequestProcessingData entry = new RequestProcessingData();
364 RequestProcessingData secondEntry = new RequestProcessingData();
365 List<HashMap<String, String>> expectedList = new ArrayList<>();
366 HashMap<String, String> expectedMap = new HashMap<>();
367 List<HashMap<String, String>> secondExpectedList = new ArrayList<>();
368 HashMap<String, String> secondExpectedMap = new HashMap<>();
369 List<RequestProcessingData> expectedDataList = new ArrayList<>();
370 entry.setGroupingId("7d2e8c07-4d10-456d-bddc-37abf38ca714");
371 entry.setTag("pincFabricConfigRequest");
372 expectedMap.put("requestAction", "assign");
373 expectedMap.put("pincFabricId", "testId");
374 expectedList.add(expectedMap);
375 entry.setDataPairs(expectedList);
376 secondEntry.setGroupingId("7d2e8c07-4d10-456d-bddc-37abf38ca715");
377 secondEntry.setTag("pincFabricConfig");
378 secondExpectedMap.put("requestAction", "unassign");
379 secondExpectedList.add(secondExpectedMap);
380 secondEntry.setDataPairs(secondExpectedList);
381 expectedDataList.add(entry);
382 expectedDataList.add(secondEntry);
384 List<org.onap.so.db.request.beans.RequestProcessingData> processingData = new ArrayList<>();
385 List<RequestProcessingData> actualProcessingData = new ArrayList<>();
386 ObjectMapper mapper = new ObjectMapper();
387 processingData = mapper.readValue(new File("src/test/resources/OrchestrationRequest/RequestProcessingData.json"),
388 new TypeReference<List<org.onap.so.db.request.beans.RequestProcessingData>>(){});
389 actualProcessingData = orchReq.mapRequestProcessingData(processingData);
390 assertThat(actualProcessingData,sameBeanAs(expectedDataList));
393 public void setupTestGetOrchestrationRequest() throws Exception{
394 //For testGetOrchestrationRequest
395 stubFor(any(urlPathEqualTo("/infraActiveRequests/00032ab7-na18-42e5-965d-8ea592502018")).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
396 .withBody(new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/getOrchestrationRequest.json"))))
397 .withStatus(HttpStatus.SC_OK)));
398 stubFor(get(urlPathEqualTo("/requestProcessingData/search/findBySoRequestIdOrderByGroupingIdDesc/"))
399 .withQueryParam("SO_REQUEST_ID", equalTo("00032ab7-na18-42e5-965d-8ea592502018"))
400 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
401 .withBody(new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/getRequestProcessingData.json"))))
402 .withStatus(HttpStatus.SC_OK)));
405 private void setupTestGetOrchestrationRequestRequestDetails(String requestId, String status) throws Exception{
406 stubFor(get(urlPathEqualTo(getTestUrl(requestId))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
407 .withBody(new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/getOrchestrationRequestDetails.json"))))
408 .withStatus(HttpStatus.SC_OK)));
411 private void setupTestUnlockOrchestrationRequest(String requestId, String status) {
412 stubFor(get(urlPathEqualTo(getTestUrl(requestId))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
413 .withBody(String.format(getResponseTemplate, requestId, status))
414 .withStatus(HttpStatus.SC_OK)));
420 private void setupTestUnlockOrchestrationRequest_invalid_Json() {
421 stubFor(get(urlPathEqualTo(getTestUrl(INVALID_REQUEST_ID))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
422 .withStatus(HttpStatus.SC_NOT_FOUND)));
426 private void setupTestUnlockOrchestrationRequest_Valid_Status(String requestID, String status) {
427 stubFor(get(urlPathEqualTo(getTestUrl(requestID))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
428 .withBody(String.format(getResponseTemplate, requestID, status))
429 .withStatus(HttpStatus.SC_OK)));
431 stubFor(post(urlPathEqualTo(getTestUrl(""))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
432 .withBody(String.format(infraActivePost, requestID))
433 .withStatus(HttpStatus.SC_OK)));
436 private void setupTestGetOrchestrationRequestFilter() throws Exception{
437 //for testGetOrchestrationRequestFilter();
438 stubFor(any(urlPathEqualTo("/infraActiveRequests/getOrchestrationFiltersFromInfraActive/")).withRequestBody(equalToJson("{\"modelType\":[\"EQUALS\",\"vfModule\"]}")).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
439 .withBody(new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/getRequestDetailsFilter.json"))))
440 .withStatus(HttpStatus.SC_OK)));