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.exceptions.ValidationException;
55 import org.onap.so.serviceinstancebeans.GetOrchestrationListResponse;
56 import org.onap.so.serviceinstancebeans.GetOrchestrationResponse;
57 import org.onap.so.serviceinstancebeans.Request;
58 import org.onap.so.serviceinstancebeans.RequestError;
59 import org.onap.so.serviceinstancebeans.RequestProcessingData;
60 import org.onap.so.serviceinstancebeans.ServiceException;
61 import org.springframework.beans.factory.annotation.Autowired;
62 import org.springframework.http.HttpEntity;
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();
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 headers.set("Accept", MediaType.APPLICATION_JSON);
140 headers.set("Content-Type", MediaType.APPLICATION_JSON);
141 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
143 UriComponentsBuilder builder = UriComponentsBuilder
144 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/" + testRequestId));
146 ResponseEntity<GetOrchestrationResponse> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET,
147 entity, GetOrchestrationResponse.class);
149 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
150 assertThat(response.getBody(),
151 sameBeanAs(testResponse).ignoring("request.startTime").ignoring("request.requestStatus.finishTime"));
152 assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
153 assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
154 assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
155 assertEquals("7.0.0", response.getHeaders().get("X-LatestVersion").get(0));
156 assertEquals("00032ab7-3fb3-42e5-965d-8ea592502017", response.getHeaders().get("X-TransactionID").get(0));
160 public void testGetOrchestrationRequestNoRequestID() {
161 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
162 headers.set("Accept", MediaType.APPLICATION_JSON);
164 UriComponentsBuilder builder = UriComponentsBuilder
165 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v6/"));
167 ResponseEntity<GetOrchestrationListResponse> response = restTemplate.exchange(builder.toUriString(),
168 HttpMethod.GET, entity, GetOrchestrationListResponse.class);
169 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
173 public void testGetOrchestrationRequestFilter() throws Exception {
174 setupTestGetOrchestrationRequestFilter();
175 List<String> values = new ArrayList<>();
176 values.add("EQUALS");
177 values.add("vfModule");
179 ObjectMapper mapper = new ObjectMapper();
180 GetOrchestrationListResponse testResponse = mapper.readValue(new File("src/test/resources/OrchestrationRequest/OrchestrationFilterResponse.json"),
181 GetOrchestrationListResponse.class);
183 Map<String, List<String>> orchestrationMap = new HashMap<>();
184 orchestrationMap.put("modelType", values);
185 List<GetOrchestrationResponse> testResponses = new ArrayList<>();
187 List<InfraActiveRequests> requests = requestsDbClient.getOrchestrationFiltersFromInfraActive(orchestrationMap);
188 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
189 headers.set("Accept", MediaType.APPLICATION_JSON);
191 UriComponentsBuilder builder = UriComponentsBuilder
192 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v6?filter=modelType:EQUALS:vfModule"));
194 ResponseEntity<GetOrchestrationListResponse> response = restTemplate.exchange(builder.toUriString(),
195 HttpMethod.GET, entity, GetOrchestrationListResponse.class);
196 assertThat(response.getBody(),
197 sameBeanAs(testResponse).ignoring("requestList.request.startTime").ignoring("requestList.request.requestStatus.finishTime"));
198 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
199 assertEquals(requests.size(), response.getBody().getRequestList().size());
204 public void testUnlockOrchestrationRequest() throws Exception {
205 setupTestUnlockOrchestrationRequest("0017f68c-eb2d-45bb-b7c7-ec31b37dc349", "UNLOCKED");
206 ObjectMapper mapper = new ObjectMapper();
207 mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
208 String requestJSON = new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/Request.json")));
210 headers.set("Accept", MediaType.APPLICATION_JSON);
211 headers.set("Content-Type", MediaType.APPLICATION_JSON);
212 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
214 UriComponentsBuilder builder;
215 ResponseEntity<String> response;
216 RequestError expectedRequestError;
217 RequestError actualRequestError;
221 expectedRequestError = new RequestError();
222 se = new ServiceException();
223 se.setMessageId(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR);
224 se.setText("Orchestration RequestId 0017f68c-eb2d-45bb-b7c7-ec31b37dc349 has a status of UNLOCKED and can not be unlocked");
225 expectedRequestError.setServiceException(se);
227 builder = UriComponentsBuilder.fromHttpUrl(
228 createURLWithPort("/onap/so/infra/orchestrationRequests/v6/0017f68c-eb2d-45bb-b7c7-ec31b37dc349/unlock"));
230 response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
231 actualRequestError = mapper.readValue(response.getBody(), RequestError.class);
233 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
234 assertThat(actualRequestError, sameBeanAs(expectedRequestError));
238 public void testUnlockOrchestrationRequest_invalid_Json() throws Exception {
239 setupTestUnlockOrchestrationRequest_invalid_Json();
240 ObjectMapper mapper = new ObjectMapper();
241 mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
242 String requestJSON = new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/Request.json")));
244 headers.set("Accept", MediaType.APPLICATION_JSON);
245 headers.set("Content-Type", MediaType.APPLICATION_JSON);
246 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
248 UriComponentsBuilder builder;
249 ResponseEntity<String> response;
250 RequestError expectedRequestError;
251 RequestError actualRequestError;
254 // Test invalid requestId
255 expectedRequestError = new RequestError();
256 se = new ServiceException();
257 se.setMessageId(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR);
258 se.setText("Null response from RequestDB when searching by RequestId");
259 expectedRequestError.setServiceException(se);
261 builder = UriComponentsBuilder.fromHttpUrl(
262 createURLWithPort("/onap/so/infra/orchestrationRequests/v6/" + INVALID_REQUEST_ID + "/unlock"));
264 response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
265 actualRequestError = mapper.readValue(response.getBody(), RequestError.class);
267 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatusCode().value());
268 assertThat(actualRequestError, sameBeanAs(expectedRequestError));
272 public void testUnlockOrchestrationRequest_Valid_Status()
273 throws JsonParseException, JsonMappingException, IOException, ValidationException {
274 setupTestUnlockOrchestrationRequest_Valid_Status("5ffbabd6-b793-4377-a1ab-082670fbc7ac", "PENDING");
275 ObjectMapper mapper = new ObjectMapper();
276 String requestJSON = new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/Request.json")));
278 headers.set("Accept", MediaType.APPLICATION_JSON);
279 headers.set("Content-Type", MediaType.APPLICATION_JSON);
280 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
282 UriComponentsBuilder builder;
283 ResponseEntity<String> response;
287 request = ORCHESTRATION_LIST.getRequestList().get(1).getRequest();
288 builder = UriComponentsBuilder.fromHttpUrl(
289 createURLWithPort("/onap/so/infra/orchestrationRequests/v7/" + "5ffbabd6-b793-4377-a1ab-082670fbc7ac" + "/unlock"));
291 response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
292 //Cannot assert anything further here, already have a wiremock in place which ensures that the post was properly called to update.
295 @Ignore //What is this testing?
297 public void testGetOrchestrationRequestRequestDetailsWhiteSpace() throws Exception {
298 InfraActiveRequests requests = new InfraActiveRequests();
299 requests.setAction("create");
300 requests.setRequestBody(" ");
301 requests.setRequestId("requestId");
302 requests.setRequestScope("service");
303 requests.setRequestType("createInstance");
304 ObjectMapper mapper = new ObjectMapper();
305 String json = mapper.writeValueAsString(requests);
307 requestsDbClient.save(requests);
309 headers.set("Accept", MediaType.APPLICATION_JSON);
310 headers.set("Content-Type", MediaType.APPLICATION_JSON);
311 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
313 UriComponentsBuilder builder = UriComponentsBuilder
314 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/requestId"));
316 ResponseEntity<GetOrchestrationResponse> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET,
317 entity, GetOrchestrationResponse.class);
319 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
320 assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
321 assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
322 assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
323 assertEquals("7.0.0", response.getHeaders().get("X-LatestVersion").get(0));
324 assertEquals("requestId", response.getHeaders().get("X-TransactionID").get(0));
327 @Ignore //What is this testing?
329 public void testGetOrchestrationRequestRequestDetailsAlaCarte() throws IOException {
330 InfraActiveRequests requests = new InfraActiveRequests();
332 String requestJSON = new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/AlaCarteRequest.json")));
334 requests.setAction("create");
335 requests.setRequestBody(requestJSON);
336 requests.setRequestId("requestId");
337 requests.setRequestScope("service");
338 requests.setRequestType("createInstance");
339 // iar.save(requests);
341 headers.set("Accept", MediaType.APPLICATION_JSON);
342 headers.set("Content-Type", MediaType.APPLICATION_JSON);
343 HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
345 UriComponentsBuilder builder = UriComponentsBuilder
346 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/requestId"));
348 ResponseEntity<GetOrchestrationResponse> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET,
349 entity, GetOrchestrationResponse.class);
351 assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
352 assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
353 assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
354 assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
355 assertEquals("7.0.0", response.getHeaders().get("X-LatestVersion").get(0));
356 assertEquals("requestId", response.getHeaders().get("X-TransactionID").get(0));
359 public void mapRequestProcessingDataTest() throws JsonParseException, JsonMappingException, IOException{
360 RequestProcessingData entry = new RequestProcessingData();
361 RequestProcessingData secondEntry = new RequestProcessingData();
362 List<HashMap<String, String>> expectedList = new ArrayList<>();
363 HashMap<String, String> expectedMap = new HashMap<>();
364 List<HashMap<String, String>> secondExpectedList = new ArrayList<>();
365 HashMap<String, String> secondExpectedMap = new HashMap<>();
366 List<RequestProcessingData> expectedDataList = new ArrayList<>();
367 entry.setGroupingId("7d2e8c07-4d10-456d-bddc-37abf38ca714");
368 entry.setTag("pincFabricConfigRequest");
369 expectedMap.put("requestAction", "assign");
370 expectedMap.put("pincFabricId", "testId");
371 expectedList.add(expectedMap);
372 entry.setDataPairs(expectedList);
373 secondEntry.setGroupingId("7d2e8c07-4d10-456d-bddc-37abf38ca715");
374 secondEntry.setTag("pincFabricConfig");
375 secondExpectedMap.put("requestAction", "unassign");
376 secondExpectedList.add(secondExpectedMap);
377 secondEntry.setDataPairs(secondExpectedList);
378 expectedDataList.add(entry);
379 expectedDataList.add(secondEntry);
381 List<org.onap.so.db.request.beans.RequestProcessingData> processingData = new ArrayList<>();
382 List<RequestProcessingData> actualProcessingData = new ArrayList<>();
383 ObjectMapper mapper = new ObjectMapper();
384 processingData = mapper.readValue(new File("src/test/resources/OrchestrationRequest/RequestProcessingData.json"),
385 new TypeReference<List<org.onap.so.db.request.beans.RequestProcessingData>>(){});
386 actualProcessingData = orchReq.mapRequestProcessingData(processingData);
387 assertThat(actualProcessingData,sameBeanAs(expectedDataList));
390 public void setupTestGetOrchestrationRequest() throws Exception{
391 //For testGetOrchestrationRequest
392 stubFor(any(urlPathEqualTo("/infraActiveRequests/00032ab7-na18-42e5-965d-8ea592502018")).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
393 .withBody(new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/getOrchestrationRequest.json"))))
394 .withStatus(HttpStatus.SC_OK)));
395 stubFor(get(urlPathEqualTo("/requestProcessingData/search/findBySoRequestIdOrderByGroupingIdDesc/"))
396 .withQueryParam("SO_REQUEST_ID", equalTo("00032ab7-na18-42e5-965d-8ea592502018"))
397 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
398 .withBody(new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/getRequestProcessingData.json"))))
399 .withStatus(HttpStatus.SC_OK)));
402 private void setupTestGetOrchestrationRequestRequestDetails(String requestId, String status) throws Exception{
403 stubFor(get(urlPathEqualTo(getTestUrl(requestId))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
404 .withBody(new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/getOrchestrationRequestDetails.json"))))
405 .withStatus(HttpStatus.SC_OK)));
408 private void setupTestUnlockOrchestrationRequest(String requestId, String status) {
409 stubFor(get(urlPathEqualTo(getTestUrl(requestId))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
410 .withBody(String.format(getResponseTemplate, requestId, status))
411 .withStatus(HttpStatus.SC_OK)));
417 private void setupTestUnlockOrchestrationRequest_invalid_Json() {
418 stubFor(get(urlPathEqualTo(getTestUrl(INVALID_REQUEST_ID))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
419 .withStatus(HttpStatus.SC_NOT_FOUND)));
423 private void setupTestUnlockOrchestrationRequest_Valid_Status(String requestID, String status) {
424 stubFor(get(urlPathEqualTo(getTestUrl(requestID))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
425 .withBody(String.format(getResponseTemplate, requestID, status))
426 .withStatus(HttpStatus.SC_OK)));
428 stubFor(post(urlPathEqualTo(getTestUrl(""))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
429 .withBody(String.format(infraActivePost, requestID))
430 .withStatus(HttpStatus.SC_OK)));
433 private void setupTestGetOrchestrationRequestFilter() throws Exception{
434 //for testGetOrchestrationRequestFilter();
435 stubFor(any(urlPathEqualTo("/infraActiveRequests/getOrchestrationFiltersFromInfraActive/")).withRequestBody(equalToJson("{\"modelType\":[\"EQUALS\",\"vfModule\"]}")).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
436 .withBody(new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/getRequestDetailsFilter.json"))))
437 .withStatus(HttpStatus.SC_OK)));