23c2892c78a0f67b04970d0ec44a0b0a9e64c338
[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.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.urlMatching;
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;
34 import static org.junit.Assert.assertNotNull;
35 import java.io.File;
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;
42 import java.util.Map;
43 import javax.ws.rs.core.MediaType;
44 import javax.ws.rs.core.Response;
45 import org.apache.http.HttpStatus;
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.onap.so.apihandler.common.ErrorNumbers;
49 import org.onap.so.db.request.beans.InfraActiveRequests;
50 import org.onap.so.db.request.client.RequestsDbClient;
51 import org.onap.so.exceptions.ValidationException;
52 import org.onap.so.serviceinstancebeans.CloudRequestData;
53 import org.onap.so.serviceinstancebeans.GetOrchestrationListResponse;
54 import org.onap.so.serviceinstancebeans.GetOrchestrationResponse;
55 import org.onap.so.serviceinstancebeans.Request;
56 import org.onap.so.serviceinstancebeans.RequestError;
57 import org.onap.so.serviceinstancebeans.RequestProcessingData;
58 import org.onap.so.serviceinstancebeans.ServiceException;
59 import org.springframework.beans.factory.annotation.Autowired;
60 import org.springframework.http.HttpEntity;
61 import org.springframework.http.HttpHeaders;
62 import org.springframework.http.HttpMethod;
63 import org.springframework.http.ResponseEntity;
64 import org.springframework.web.util.UriComponentsBuilder;
65 import com.fasterxml.jackson.core.JsonParseException;
66 import com.fasterxml.jackson.core.type.TypeReference;
67 import com.fasterxml.jackson.databind.JsonMappingException;
68 import com.fasterxml.jackson.databind.ObjectMapper;
69
70 public class OrchestrationRequestsTest extends BaseTest {
71     @Autowired
72     private RequestsDbClient requestsDbClient;
73
74     @Autowired
75     private OrchestrationRequests orchReq;
76
77     private static final GetOrchestrationListResponse ORCHESTRATION_LIST = generateOrchestrationList();
78     private static final String INVALID_REQUEST_ID = "invalid-request-id";
79
80     private static GetOrchestrationListResponse generateOrchestrationList() {
81         GetOrchestrationListResponse list = null;
82         try {
83             ObjectMapper mapper = new ObjectMapper();
84             list = mapper.readValue(new File("src/test/resources/OrchestrationRequest/OrchestrationList.json"),
85                     GetOrchestrationListResponse.class);
86         } catch (JsonParseException jpe) {
87             jpe.printStackTrace();
88         } catch (JsonMappingException jme) {
89             jme.printStackTrace();
90         } catch (IOException ioe) {
91             ioe.printStackTrace();
92         }
93         return list;
94     }
95
96     @Before
97     public void setup() throws IOException {
98         wireMockServer.stubFor(get(urlMatching("/sobpmnengine/history/process-instance.*")).willReturn(aResponse()
99                 .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
100                 .withBody(new String(Files.readAllBytes(
101                         Paths.get("src/test/resources/OrchestrationRequest/ProcessInstanceHistoryResponse.json"))))
102                 .withStatus(org.apache.http.HttpStatus.SC_OK)));
103         wireMockServer.stubFor(
104                 get(("/sobpmnengine/history/activity-instance?processInstanceId=c4c6b647-a26e-11e9-b144-0242ac14000b"))
105                         .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
106                                 .withBody(new String(Files.readAllBytes(Paths.get(
107                                         "src/test/resources/OrchestrationRequest/ActivityInstanceHistoryResponse.json"))))
108                                 .withStatus(HttpStatus.SC_OK)));
109         wireMockServer.stubFor(get(
110                 ("/requestProcessingData/search/findBySoRequestIdAndIsDataInternalOrderByGroupingIdDesc?SO_REQUEST_ID=00032ab7-1a18-42e5-965d-8ea592502018&IS_INTERNAL_DATA=false"))
111                         .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
112                                 .withBody(new String(Files.readAllBytes(Paths.get(
113                                         "src/test/resources/OrchestrationRequest/getRequestProcessingDataArray.json"))))
114                                 .withStatus(HttpStatus.SC_OK)));
115         wireMockServer.stubFor(get(
116                 ("/requestProcessingData/search/findBySoRequestIdAndIsDataInternalOrderByGroupingIdDesc?SO_REQUEST_ID=00032ab7-3fb3-42e5-965d-8ea592502017&IS_INTERNAL_DATA=false"))
117                         .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
118                                 .withBody(new String(Files.readAllBytes(Paths.get(
119                                         "src/test/resources/OrchestrationRequest/getRequestProcessingDataArray.json"))))
120                                 .withStatus(HttpStatus.SC_OK)));
121     }
122
123     @Test
124     public void testGetOrchestrationRequest() throws Exception {
125         setupTestGetOrchestrationRequest();
126         // TEST VALID REQUEST
127         GetOrchestrationResponse testResponse = new GetOrchestrationResponse();
128
129         Request request = ORCHESTRATION_LIST.getRequestList().get(1).getRequest();
130         testResponse.setRequest(request);
131         testResponse.getRequest().setRequestProcessingData(new ArrayList<RequestProcessingData>());
132         RequestProcessingData e = new RequestProcessingData();
133         e.setGroupingId("7d2e8c07-4d10-456d-bddc-37abf38ca714");
134         e.setTag("pincFabricConfigRequest");
135         List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
136         HashMap<String, String> data1 = new HashMap<String, String>();
137         data1.put("requestAction", "assign");
138         data.add(data1);
139         e.setDataPairs(data);
140         testResponse.getRequest().getRequestProcessingData().add(e);
141         String testRequestId = request.getRequestId();
142         HttpHeaders headers = new HttpHeaders();
143         headers.set("Accept", MediaType.APPLICATION_JSON);
144         headers.set("Content-Type", MediaType.APPLICATION_JSON);
145         HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
146
147         UriComponentsBuilder builder = UriComponentsBuilder
148                 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/" + testRequestId));
149
150         ResponseEntity<GetOrchestrationResponse> response =
151                 restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, GetOrchestrationResponse.class);
152
153         assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
154         assertThat(response.getBody(), sameBeanAs(testResponse).ignoring("request.startTime")
155                 .ignoring("request.finishTime").ignoring("request.requestStatus.timeStamp"));
156         assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
157         assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
158         assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
159         assertEquals("7.0.0", response.getHeaders().get("X-LatestVersion").get(0));
160         assertEquals("00032ab7-1a18-42e5-965d-8ea592502018", response.getHeaders().get("X-TransactionID").get(0));
161         assertNotNull(response.getBody().getRequest().getFinishTime());
162     }
163
164     @Test
165     public void getOrchestrationRequestSimpleTest() throws Exception {
166         setupTestGetOrchestrationRequest();
167         // TEST VALID REQUEST
168         GetOrchestrationResponse testResponse = new GetOrchestrationResponse();
169
170         Request request = ORCHESTRATION_LIST.getRequestList().get(1).getRequest();
171         request.setRequestProcessingData(null);
172         testResponse.setRequest(request);
173
174         String testRequestId = request.getRequestId();
175         HttpHeaders headers = new HttpHeaders();
176         headers.set("Accept", MediaType.APPLICATION_JSON);
177         headers.set("Content-Type", MediaType.APPLICATION_JSON);
178         HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
179         UriComponentsBuilder builder = UriComponentsBuilder
180                 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/" + testRequestId))
181                 .queryParam("format", "simple");
182
183         ResponseEntity<GetOrchestrationResponse> response =
184                 restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, GetOrchestrationResponse.class);
185
186         assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
187         assertThat(response.getBody(), sameBeanAs(testResponse).ignoring("request.startTime")
188                 .ignoring("request.finishTime").ignoring("request.requestStatus.timeStamp"));
189         assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
190         assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
191         assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
192         assertEquals("7.0.0", response.getHeaders().get("X-LatestVersion").get(0));
193         assertEquals("00032ab7-1a18-42e5-965d-8ea592502018", response.getHeaders().get("X-TransactionID").get(0));
194         assertNotNull(response.getBody().getRequest().getFinishTime());
195     }
196
197     @Test
198     public void testGetOrchestrationRequestInstanceGroup() throws Exception {
199         setupTestGetOrchestrationRequestInstanceGroup();
200         // TEST VALID REQUEST
201         GetOrchestrationResponse testResponse = new GetOrchestrationResponse();
202
203         Request request = ORCHESTRATION_LIST.getRequestList().get(8).getRequest();
204         testResponse.setRequest(request);
205         testResponse.getRequest().setRequestProcessingData(new ArrayList<RequestProcessingData>());
206         RequestProcessingData e = new RequestProcessingData();
207         e.setGroupingId("7d2e8c07-4d10-456d-bddc-37abf38ca714");
208         e.setTag("pincFabricConfigRequest");
209         List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
210         HashMap<String, String> data1 = new HashMap<String, String>();
211         data1.put("requestAction", "assign");
212         data.add(data1);
213         e.setDataPairs(data);
214         testResponse.getRequest().getRequestProcessingData().add(e);
215         String testRequestId = request.getRequestId();
216         HttpHeaders headers = new HttpHeaders();
217         headers.set("Accept", MediaType.APPLICATION_JSON);
218         headers.set("Content-Type", MediaType.APPLICATION_JSON);
219         HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
220
221         UriComponentsBuilder builder = UriComponentsBuilder
222                 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/" + testRequestId));
223
224         ResponseEntity<GetOrchestrationResponse> response =
225                 restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, GetOrchestrationResponse.class);
226
227         assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
228         assertThat(response.getBody(), sameBeanAs(testResponse).ignoring("request.startTime")
229                 .ignoring("request.finishTime").ignoring("request.requestStatus.timeStamp"));
230     }
231
232     @Test
233     public void testGetOrchestrationRequestWithOpenstackDetails() throws Exception {
234         setupTestGetOrchestrationRequestOpenstackDetails("00032ab7-3fb3-42e5-965d-8ea592502017", "COMPLETED");
235         // Test request with modelInfo request body
236         GetOrchestrationResponse testResponse = new GetOrchestrationResponse();
237
238         Request request = ORCHESTRATION_LIST.getRequestList().get(0).getRequest();
239         List<CloudRequestData> cloudRequestData = new ArrayList<>();
240         CloudRequestData cloudData = new CloudRequestData();
241         cloudData.setCloudIdentifier("heatstackName/123123");
242         ObjectMapper mapper = new ObjectMapper();
243         Object reqData = mapper.readValue(
244                 "{\r\n  \"test\": \"00032ab7-3fb3-42e5-965d-8ea592502016\",\r\n  \"test2\": \"deleteInstance\",\r\n  \"test3\": \"COMPLETE\",\r\n  \"test4\": \"Vf Module has been deleted successfully.\",\r\n  \"test5\": 100\r\n}",
245                 Object.class);
246         cloudData.setCloudRequest(reqData);
247         cloudRequestData.add(cloudData);
248         request.setCloudRequestData(cloudRequestData);
249         testResponse.setRequest(request);
250         String testRequestId = request.getRequestId();
251         testResponse.getRequest().setRequestProcessingData(new ArrayList<RequestProcessingData>());
252         RequestProcessingData e = new RequestProcessingData();
253         e.setGroupingId("7d2e8c07-4d10-456d-bddc-37abf38ca714");
254         e.setTag("pincFabricConfigRequest");
255         List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
256         HashMap<String, String> data1 = new HashMap<String, String>();
257         data1.put("requestAction", "assign");
258         data.add(data1);
259         e.setDataPairs(data);
260         testResponse.getRequest().getRequestProcessingData().add(e);
261
262         HttpHeaders headers = new HttpHeaders();
263         headers.set("Accept", MediaType.APPLICATION_JSON);
264         headers.set("Content-Type", MediaType.APPLICATION_JSON);
265         HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
266
267         UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(
268                 "/onap/so/infra/orchestrationRequests/v7/" + testRequestId + "?includeCloudRequest=true"));
269
270         ResponseEntity<GetOrchestrationResponse> response =
271                 restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, GetOrchestrationResponse.class);
272         System.out.println("Response :" + response.getBody().toString());
273         assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
274
275         assertThat(response.getBody(), sameBeanAs(testResponse).ignoring("request.startTime")
276                 .ignoring("request.finishTime").ignoring("request.requestStatus.timeStamp"));
277         assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
278         assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
279         assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
280         assertEquals("7.0.0", response.getHeaders().get("X-LatestVersion").get(0));
281         assertEquals("00032ab7-3fb3-42e5-965d-8ea592502017", response.getHeaders().get("X-TransactionID").get(0));
282     }
283
284     @Test
285     public void testGetOrchestrationRequestNoRequestID() {
286         HttpHeaders headers = new HttpHeaders();
287         headers.set("Accept", "application/json; charset=UTF-8");
288         headers.set("Content-Type", "application/json; charset=UTF-8");
289         HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
290         UriComponentsBuilder builder =
291                 UriComponentsBuilder.fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v6/"));
292
293         ResponseEntity<GetOrchestrationListResponse> response = restTemplate.exchange(builder.toUriString(),
294                 HttpMethod.GET, entity, GetOrchestrationListResponse.class);
295         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
296     }
297
298     @Test
299     public void testGetOrchestrationRequestInvalidRequestID() throws Exception {
300         setupTestGetOrchestrationRequest();
301         // TEST INVALID REQUESTID
302         GetOrchestrationResponse testResponse = new GetOrchestrationResponse();
303
304         Request request = ORCHESTRATION_LIST.getRequestList().get(1).getRequest();
305         testResponse.setRequest(request);
306         String testRequestId = "00032ab7-pfb3-42e5-965d-8ea592502016";
307         HttpHeaders headers = new HttpHeaders();
308         headers.set("Accept", MediaType.APPLICATION_JSON);
309         headers.set("Content-Type", MediaType.APPLICATION_JSON);
310         HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
311
312         UriComponentsBuilder builder = UriComponentsBuilder
313                 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/" + testRequestId));
314
315         ResponseEntity<GetOrchestrationResponse> response =
316                 restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, GetOrchestrationResponse.class);
317
318         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
319     }
320
321     @Test
322     public void testGetOrchestrationRequestFilter() throws Exception {
323         setupTestGetOrchestrationRequestFilter();
324         List<String> values = new ArrayList<>();
325         values.add("EQUALS");
326         values.add("vfModule");
327
328         ObjectMapper mapper = new ObjectMapper();
329         GetOrchestrationListResponse testResponse =
330                 mapper.readValue(new File("src/test/resources/OrchestrationRequest/OrchestrationFilterResponse.json"),
331                         GetOrchestrationListResponse.class);
332
333         Map<String, List<String>> orchestrationMap = new HashMap<>();
334         orchestrationMap.put("modelType", values);
335         List<GetOrchestrationResponse> testResponses = new ArrayList<>();
336
337         List<InfraActiveRequests> requests = requestsDbClient.getOrchestrationFiltersFromInfraActive(orchestrationMap);
338         HttpHeaders headers = new HttpHeaders();
339         headers.set("Accept", MediaType.APPLICATION_JSON);
340         HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
341
342         UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(
343                 createURLWithPort("/onap/so/infra/orchestrationRequests/v6?filter=modelType:EQUALS:vfModule"));
344
345         ResponseEntity<GetOrchestrationListResponse> response = restTemplate.exchange(builder.toUriString(),
346                 HttpMethod.GET, entity, GetOrchestrationListResponse.class);
347         assertThat(response.getBody(), sameBeanAs(testResponse).ignoring("requestList.request.startTime")
348                 .ignoring("requestList.request.finishTime").ignoring("requestList.request.requestStatus.timeStamp"));
349         assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
350         assertEquals(requests.size(), response.getBody().getRequestList().size());
351
352     }
353
354     @Test
355     public void testUnlockOrchestrationRequest() throws Exception {
356         setupTestUnlockOrchestrationRequest("0017f68c-eb2d-45bb-b7c7-ec31b37dc349", "UNLOCKED");
357         ObjectMapper mapper = new ObjectMapper();
358         String requestJSON =
359                 new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/Request.json")));
360         HttpHeaders headers = new HttpHeaders();
361         headers.set("Accept", MediaType.APPLICATION_JSON);
362         headers.set("Content-Type", MediaType.APPLICATION_JSON);
363         HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
364
365         UriComponentsBuilder builder;
366         ResponseEntity<String> response;
367         RequestError expectedRequestError;
368         RequestError actualRequestError;
369         ServiceException se;
370
371         // Test invalid JSON
372         expectedRequestError = new RequestError();
373         se = new ServiceException();
374         se.setMessageId(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR);
375         se.setText(
376                 "Orchestration RequestId 0017f68c-eb2d-45bb-b7c7-ec31b37dc349 has a status of UNLOCKED and can not be unlocked");
377         expectedRequestError.setServiceException(se);
378
379         builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(
380                 "/onap/so/infra/orchestrationRequests/v6/0017f68c-eb2d-45bb-b7c7-ec31b37dc349/unlock"));
381
382         response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
383         actualRequestError = mapper.readValue(response.getBody(), RequestError.class);
384
385         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
386         assertThat(actualRequestError, sameBeanAs(expectedRequestError));
387     }
388
389     @Test
390     public void testUnlockOrchestrationRequest_invalid_Json() throws Exception {
391         setupTestUnlockOrchestrationRequest_invalid_Json();
392         ObjectMapper mapper = new ObjectMapper();
393         String requestJSON =
394                 new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/Request.json")));
395         HttpHeaders headers = new HttpHeaders();
396         headers.set("Accept", MediaType.APPLICATION_JSON);
397         headers.set("Content-Type", MediaType.APPLICATION_JSON);
398         HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
399
400         UriComponentsBuilder builder;
401         ResponseEntity<String> response;
402         RequestError expectedRequestError;
403         RequestError actualRequestError;
404         ServiceException se;
405
406         // Test invalid requestId
407         expectedRequestError = new RequestError();
408         se = new ServiceException();
409         se.setMessageId(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR);
410         se.setText("Null response from RequestDB when searching by RequestId " + INVALID_REQUEST_ID);
411         expectedRequestError.setServiceException(se);
412
413         builder = UriComponentsBuilder.fromHttpUrl(
414                 createURLWithPort("/onap/so/infra/orchestrationRequests/v6/" + INVALID_REQUEST_ID + "/unlock"));
415
416         response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
417         actualRequestError = mapper.readValue(response.getBody(), RequestError.class);
418
419         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatusCode().value());
420         assertThat(expectedRequestError, sameBeanAs(actualRequestError));
421     }
422
423     @Test
424     public void testUnlockOrchestrationRequest_Valid_Status()
425             throws JsonParseException, JsonMappingException, IOException, ValidationException {
426         setupTestUnlockOrchestrationRequest_Valid_Status("5ffbabd6-b793-4377-a1ab-082670fbc7ac", "PENDING");
427         ObjectMapper mapper = new ObjectMapper();
428         String requestJSON =
429                 new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/Request.json")));
430         HttpHeaders headers = new HttpHeaders();
431         headers.set("Accept", MediaType.APPLICATION_JSON);
432         headers.set("Content-Type", MediaType.APPLICATION_JSON);
433         HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
434
435         UriComponentsBuilder builder;
436         ResponseEntity<String> response;
437         Request request;
438
439         // Test valid status
440         request = ORCHESTRATION_LIST.getRequestList().get(1).getRequest();
441         builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(
442                 "/onap/so/infra/orchestrationRequests/v7/" + "5ffbabd6-b793-4377-a1ab-082670fbc7ac" + "/unlock"));
443
444         response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
445         // Cannot assert anything further here, already have a wiremock in place
446         // which ensures that the post was
447         // properly called to update.
448     }
449
450     @Test
451     public void mapRequestProcessingDataTest() throws JsonParseException, JsonMappingException, IOException {
452         RequestProcessingData entry = new RequestProcessingData();
453         RequestProcessingData secondEntry = new RequestProcessingData();
454         List<HashMap<String, String>> expectedList = new ArrayList<>();
455         HashMap<String, String> expectedMap = new HashMap<>();
456         List<HashMap<String, String>> secondExpectedList = new ArrayList<>();
457         HashMap<String, String> secondExpectedMap = new HashMap<>();
458         List<RequestProcessingData> expectedDataList = new ArrayList<>();
459         entry.setGroupingId("7d2e8c07-4d10-456d-bddc-37abf38ca714");
460         entry.setTag("pincFabricConfigRequest");
461         expectedMap.put("requestAction", "assign");
462         expectedMap.put("pincFabricId", "testId");
463         expectedList.add(expectedMap);
464         entry.setDataPairs(expectedList);
465         secondEntry.setGroupingId("7d2e8c07-4d10-456d-bddc-37abf38ca715");
466         secondEntry.setTag("pincFabricConfig");
467         secondExpectedMap.put("requestAction", "unassign");
468         secondExpectedList.add(secondExpectedMap);
469         secondEntry.setDataPairs(secondExpectedList);
470         expectedDataList.add(entry);
471         expectedDataList.add(secondEntry);
472
473         List<org.onap.so.db.request.beans.RequestProcessingData> processingData = new ArrayList<>();
474         List<RequestProcessingData> actualProcessingData = new ArrayList<>();
475         ObjectMapper mapper = new ObjectMapper();
476         processingData =
477                 mapper.readValue(new File("src/test/resources/OrchestrationRequest/RequestProcessingData.json"),
478                         new TypeReference<List<org.onap.so.db.request.beans.RequestProcessingData>>() {});
479         actualProcessingData = orchReq.mapRequestProcessingData(processingData);
480         assertThat(actualProcessingData, sameBeanAs(expectedDataList));
481     }
482
483
484     public void setupTestGetOrchestrationRequest() throws Exception {
485         // For testGetOrchestrationRequest
486         wireMockServer.stubFor(any(urlPathEqualTo("/infraActiveRequests/00032ab7-1a18-42e5-965d-8ea592502018"))
487                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
488                         .withBody(new String(Files.readAllBytes(
489                                 Paths.get("src/test/resources/OrchestrationRequest/getOrchestrationRequest.json"))))
490                         .withStatus(HttpStatus.SC_OK)));
491         wireMockServer
492                 .stubFor(get(urlPathEqualTo("/requestProcessingData/search/findBySoRequestIdOrderByGroupingIdDesc/"))
493                         .withQueryParam("SO_REQUEST_ID", equalTo("00032ab7-1a18-42e5-965d-8ea592502018"))
494                         .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
495                                 .withBody(new String(Files.readAllBytes(Paths
496                                         .get("src/test/resources/OrchestrationRequest/getRequestProcessingData.json"))))
497                                 .withStatus(HttpStatus.SC_OK)));
498     }
499
500     public void setupTestGetOrchestrationRequestInstanceGroup() throws Exception {
501         // For testGetOrchestrationRequest
502         wireMockServer.stubFor(any(urlPathEqualTo("/infraActiveRequests/00032ab7-1a18-42e5-965d-8ea592502018"))
503                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
504                         .withBody(new String(Files.readAllBytes(Paths.get(
505                                 "src/test/resources/OrchestrationRequest/getOrchestrationRequestInstanceGroup.json"))))
506                         .withStatus(HttpStatus.SC_OK)));
507         wireMockServer
508                 .stubFor(get(urlPathEqualTo("/requestProcessingData/search/findBySoRequestIdOrderByGroupingIdDesc/"))
509                         .withQueryParam("SO_REQUEST_ID", equalTo("00032ab7-1a18-42e5-965d-8ea592502018"))
510                         .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
511                                 .withBody(new String(Files.readAllBytes(Paths
512                                         .get("src/test/resources/OrchestrationRequest/getRequestProcessingData.json"))))
513                                 .withStatus(HttpStatus.SC_OK)));
514     }
515
516     private void setupTestGetOrchestrationRequestRequestDetails(String requestId, String status) throws Exception {
517         wireMockServer.stubFor(get(urlPathEqualTo(getTestUrl(requestId))).willReturn(aResponse()
518                 .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
519                 .withBody(new String(Files.readAllBytes(
520                         Paths.get("src/test/resources/OrchestrationRequest/getOrchestrationRequestDetails.json"))))
521                 .withStatus(HttpStatus.SC_OK)));
522     }
523
524     private void setupTestGetOrchestrationRequestOpenstackDetails(String requestId, String status) throws Exception {
525         wireMockServer.stubFor(get(urlPathEqualTo(getTestUrl(requestId))).willReturn(aResponse()
526                 .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
527                 .withBody(new String(Files.readAllBytes(Paths
528                         .get("src/test/resources/OrchestrationRequest/getOrchestrationOpenstackRequestDetails.json"))))
529                 .withStatus(HttpStatus.SC_OK)));
530     }
531
532     private void setupTestUnlockOrchestrationRequest(String requestId, String status) {
533         wireMockServer.stubFor(get(urlPathEqualTo(getTestUrl(requestId)))
534                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
535                         .withBody(String.format(getResponseTemplate, requestId, status)).withStatus(HttpStatus.SC_OK)));
536     }
537
538     private void setupTestUnlockOrchestrationRequest_invalid_Json() {
539         wireMockServer.stubFor(get(urlPathEqualTo(getTestUrl(INVALID_REQUEST_ID))).willReturn(aResponse()
540                 .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).withStatus(HttpStatus.SC_NOT_FOUND)));
541
542     }
543
544     private void setupTestUnlockOrchestrationRequest_Valid_Status(String requestID, String status) {
545         wireMockServer.stubFor(get(urlPathEqualTo(getTestUrl(requestID)))
546                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
547                         .withBody(String.format(getResponseTemplate, requestID, status)).withStatus(HttpStatus.SC_OK)));
548
549         wireMockServer.stubFor(post(urlPathEqualTo(getTestUrl("")))
550                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
551                         .withBody(String.format(infraActivePost, requestID)).withStatus(HttpStatus.SC_OK)));
552     }
553
554     private void setupTestGetOrchestrationRequestFilter() throws Exception {
555         // for testGetOrchestrationRequestFilter();
556         wireMockServer.stubFor(any(urlPathEqualTo("/infraActiveRequests/getOrchestrationFiltersFromInfraActive/"))
557                 .withRequestBody(equalToJson("{\"modelType\":[\"EQUALS\",\"vfModule\"]}"))
558                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
559                         .withBody(new String(Files.readAllBytes(
560                                 Paths.get("src/test/resources/OrchestrationRequest/getRequestDetailsFilter.json"))))
561                         .withStatus(HttpStatus.SC_OK)));
562     }
563 }