cc3fd14e70c4f4b8871342fda6ca4ffdb2cf696c
[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 com.fasterxml.jackson.core.JsonParseException;
24 import com.fasterxml.jackson.databind.DeserializationFeature;
25 import com.fasterxml.jackson.databind.JsonMappingException;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import org.apache.http.HttpStatus;
28 import org.junit.Ignore;
29 import org.junit.Test;
30 import org.onap.so.apihandler.common.ErrorNumbers;
31 import org.onap.so.db.request.beans.InfraActiveRequests;
32 import org.onap.so.db.request.data.repository.InfraActiveRequestsRepository;
33 import org.onap.so.exceptions.ValidationException;
34 import org.onap.so.serviceinstancebeans.*;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.http.HttpEntity;
37 import org.springframework.http.HttpMethod;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.web.util.UriComponentsBuilder;
40
41 import javax.ws.rs.core.HttpHeaders;
42 import javax.ws.rs.core.MediaType;
43 import javax.ws.rs.core.Response;
44 import java.io.File;
45 import java.io.IOException;
46 import java.nio.file.Files;
47 import java.nio.file.Paths;
48 import java.util.ArrayList;
49 import java.util.HashMap;
50 import java.util.List;
51 import java.util.Map;
52
53 import static com.github.tomakehurst.wiremock.client.WireMock.*;
54 import static com.shazam.shazamcrest.MatcherAssert.assertThat;
55 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
56 import static org.junit.Assert.assertEquals;
57
58 public class OrchestrationRequestsTest extends BaseTest {
59     @Autowired
60     private InfraActiveRequestsRepository iar;
61
62     @Autowired
63     private RequestsDbClient requestsDbClient;
64
65     private static final String CHECK_HTML = "<!DOCTYPE html><html><head><meta charset=\"ISO-8859-1\"><title></title></head><body></body></html>";
66     private static final GetOrchestrationListResponse ORCHESTRATION_LIST = generateOrchestrationList();
67     private static final String INVALID_REQUEST_ID = "invalid-request-id";
68
69     private static GetOrchestrationListResponse generateOrchestrationList() {
70         GetOrchestrationListResponse list = null;
71         try {
72             ObjectMapper mapper = new ObjectMapper();
73             list = mapper.readValue(new File("src/test/resources/OrchestrationRequest/OrchestrationList.json"),
74                     GetOrchestrationListResponse.class);
75         } catch (JsonParseException jpe) {
76             jpe.printStackTrace();
77         } catch (JsonMappingException jme) {
78             jme.printStackTrace();
79         } catch (IOException ioe) {
80             ioe.printStackTrace();
81         }
82         return list;
83     }
84
85     @Test
86     public void testGetOrchestrationRequest() throws Exception {
87         setupTestGetOrchestrationRequest();
88         // TEST VALID REQUEST
89         GetOrchestrationResponse testResponse = new GetOrchestrationResponse();
90
91         Request request = ORCHESTRATION_LIST.getRequestList().get(1).getRequest();
92         testResponse.setRequest(request);
93         String testRequestId = request.getRequestId();
94
95         headers.set("Accept", MediaType.APPLICATION_JSON);
96         headers.set("Content-Type", MediaType.APPLICATION_JSON);
97         HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
98
99         UriComponentsBuilder builder = UriComponentsBuilder
100                 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/" + testRequestId));
101
102         ResponseEntity<GetOrchestrationResponse> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET,
103                 entity, GetOrchestrationResponse.class);
104
105         assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
106         assertThat(response.getBody(),
107                 sameBeanAs(testResponse).ignoring("request.startTime").ignoring("request.requestStatus.finishTime"));
108         assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
109         assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
110         assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
111         assertEquals("7.0.0", response.getHeaders().get("X-LatestVersion").get(0));
112         assertEquals("00032ab7-na18-42e5-965d-8ea592502018", response.getHeaders().get("X-TransactionID").get(0));
113     }
114
115     @Test
116     public void testGetOrchestrationRequestRequestDetails() throws Exception {
117         setupTestGetOrchestrationRequestRequestDetails("00032ab7-3fb3-42e5-965d-8ea592502017", "COMPLETED");
118         //Test request with modelInfo request body
119         GetOrchestrationResponse testResponse = new GetOrchestrationResponse();
120
121         Request request = ORCHESTRATION_LIST.getRequestList().get(0).getRequest();
122         testResponse.setRequest(request);
123         String testRequestId = request.getRequestId();
124
125         headers.set("Accept", MediaType.APPLICATION_JSON);
126         headers.set("Content-Type", MediaType.APPLICATION_JSON);
127         HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
128
129         UriComponentsBuilder builder = UriComponentsBuilder
130                 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/" + testRequestId));
131
132         ResponseEntity<GetOrchestrationResponse> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET,
133                 entity, GetOrchestrationResponse.class);
134
135         assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
136         assertThat(response.getBody(),
137                 sameBeanAs(testResponse).ignoring("request.startTime").ignoring("request.requestStatus.finishTime"));
138         assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
139         assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
140         assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
141         assertEquals("7.0.0", response.getHeaders().get("X-LatestVersion").get(0));
142         assertEquals("00032ab7-3fb3-42e5-965d-8ea592502017", response.getHeaders().get("X-TransactionID").get(0));
143     }
144
145     @Test
146     public void testGetOrchestrationRequestNoRequestID() {
147         HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
148         headers.set("Accept", MediaType.APPLICATION_JSON);
149
150         UriComponentsBuilder builder = UriComponentsBuilder
151                 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v6/"));
152
153         ResponseEntity<GetOrchestrationListResponse> response = restTemplate.exchange(builder.toUriString(),
154                 HttpMethod.GET, entity, GetOrchestrationListResponse.class);
155         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
156     }
157
158     @Test
159     public void testGetOrchestrationRequestFilter() throws Exception {
160         setupTestGetOrchestrationRequestFilter();
161         List<String> values = new ArrayList<>();
162         values.add("EQUALS");
163         values.add("vfModule");
164
165         Map<String, List<String>> orchestrationMap = new HashMap<>();
166         orchestrationMap.put("modelType", values);
167
168         List<InfraActiveRequests> requests = requestsDbClient.getOrchestrationFiltersFromInfraActive(orchestrationMap);
169         HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
170         headers.set("Accept", MediaType.APPLICATION_JSON);
171
172         UriComponentsBuilder builder = UriComponentsBuilder
173                 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v6?filter=modelType:EQUALS:vfModule"));
174
175         ResponseEntity<GetOrchestrationListResponse> response = restTemplate.exchange(builder.toUriString(),
176                 HttpMethod.GET, entity, GetOrchestrationListResponse.class);
177         assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
178         assertEquals(requests.size(), response.getBody().getRequestList().size());
179     }
180
181     @Test
182     public void testUnlockOrchestrationRequest() throws Exception {
183         setupTestUnlockOrchestrationRequest("0017f68c-eb2d-45bb-b7c7-ec31b37dc349", "UNLOCKED");
184         ObjectMapper mapper = new ObjectMapper();
185         mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
186         String requestJSON = new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/Request.json")));
187
188         headers.set("Accept", MediaType.APPLICATION_JSON);
189         headers.set("Content-Type", MediaType.APPLICATION_JSON);
190         HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
191
192         UriComponentsBuilder builder;
193         ResponseEntity<String> response;
194         RequestError expectedRequestError;
195         RequestError actualRequestError;
196         ServiceException se;
197
198         // Test invalid JSON
199         expectedRequestError = new RequestError();
200         se = new ServiceException();
201         se.setMessageId(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR);
202         se.setText("Orchestration RequestId 0017f68c-eb2d-45bb-b7c7-ec31b37dc349 has a status of UNLOCKED and can not be unlocked");
203         expectedRequestError.setServiceException(se);
204
205         builder = UriComponentsBuilder.fromHttpUrl(
206                 createURLWithPort("/onap/so/infra/orchestrationRequests/v6/0017f68c-eb2d-45bb-b7c7-ec31b37dc349/unlock"));
207
208         response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
209         actualRequestError = mapper.readValue(response.getBody(), RequestError.class);
210
211         assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
212         assertThat(actualRequestError, sameBeanAs(expectedRequestError));
213     }
214
215     @Test
216     public void testUnlockOrchestrationRequest_invalid_Json() throws Exception {
217         setupTestUnlockOrchestrationRequest_invalid_Json();
218         ObjectMapper mapper = new ObjectMapper();
219         mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
220         String requestJSON = new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/Request.json")));
221
222         headers.set("Accept", MediaType.APPLICATION_JSON);
223         headers.set("Content-Type", MediaType.APPLICATION_JSON);
224         HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
225
226         UriComponentsBuilder builder;
227         ResponseEntity<String> response;
228         RequestError expectedRequestError;
229         RequestError actualRequestError;
230         ServiceException se;
231
232         // Test invalid requestId
233         expectedRequestError = new RequestError();
234         se = new ServiceException();
235         se.setMessageId(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR);
236         se.setText("Null response from RequestDB when searching by RequestId");
237         expectedRequestError.setServiceException(se);
238
239         builder = UriComponentsBuilder.fromHttpUrl(
240                 createURLWithPort("/onap/so/infra/orchestrationRequests/v6/" + INVALID_REQUEST_ID + "/unlock"));
241
242         response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
243         actualRequestError = mapper.readValue(response.getBody(), RequestError.class);
244
245         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatusCode().value());
246         assertThat(actualRequestError, sameBeanAs(expectedRequestError));
247     }
248
249     @Test
250     public void testUnlockOrchestrationRequest_Valid_Status()
251             throws JsonParseException, JsonMappingException, IOException, ValidationException {
252         setupTestUnlockOrchestrationRequest_Valid_Status("5ffbabd6-b793-4377-a1ab-082670fbc7ac", "PENDING");
253         ObjectMapper mapper = new ObjectMapper();
254         String requestJSON = new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/Request.json")));
255
256         headers.set("Accept", MediaType.APPLICATION_JSON);
257         headers.set("Content-Type", MediaType.APPLICATION_JSON);
258         HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
259
260         UriComponentsBuilder builder;
261         ResponseEntity<String> response;
262         Request request;
263
264         // Test valid status
265         request = ORCHESTRATION_LIST.getRequestList().get(1).getRequest();
266         builder = UriComponentsBuilder.fromHttpUrl(
267                 createURLWithPort("/onap/so/infra/orchestrationRequests/v7/" + "5ffbabd6-b793-4377-a1ab-082670fbc7ac" + "/unlock"));
268
269         response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
270         //Cannot assert anything further here, already have a wiremock in place which ensures that the post was properly called to update.
271     }
272
273     @Ignore //What is this testing?
274     @Test
275     public void testGetOrchestrationRequestRequestDetailsWhiteSpace() throws Exception {
276         InfraActiveRequests requests = new InfraActiveRequests();
277         requests.setAction("create");
278         requests.setRequestBody("  ");
279         requests.setRequestId("requestId");
280         requests.setRequestScope("service");
281         requests.setRequestType("createInstance");
282         ObjectMapper mapper = new ObjectMapper();
283         String json = mapper.writeValueAsString(requests);
284
285         requestsDbClient.save(requests);
286
287         headers.set("Accept", MediaType.APPLICATION_JSON);
288         headers.set("Content-Type", MediaType.APPLICATION_JSON);
289         HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
290
291         UriComponentsBuilder builder = UriComponentsBuilder
292                 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/requestId"));
293
294         ResponseEntity<GetOrchestrationResponse> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET,
295                 entity, GetOrchestrationResponse.class);
296
297         assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
298         assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
299         assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
300         assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
301         assertEquals("7.0.0", response.getHeaders().get("X-LatestVersion").get(0));
302         assertEquals("requestId", response.getHeaders().get("X-TransactionID").get(0));
303     }
304
305     @Ignore //What is this testing?
306     @Test
307     public void testGetOrchestrationRequestRequestDetailsAlaCarte() throws IOException {
308         InfraActiveRequests requests = new InfraActiveRequests();
309
310         String requestJSON = new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/AlaCarteRequest.json")));
311
312         requests.setAction("create");
313         requests.setRequestBody(requestJSON);
314         requests.setRequestId("requestId");
315         requests.setRequestScope("service");
316         requests.setRequestType("createInstance");
317         iar.save(requests);
318
319         headers.set("Accept", MediaType.APPLICATION_JSON);
320         headers.set("Content-Type", MediaType.APPLICATION_JSON);
321         HttpEntity<Request> entity = new HttpEntity<Request>(null, headers);
322
323         UriComponentsBuilder builder = UriComponentsBuilder
324                 .fromHttpUrl(createURLWithPort("/onap/so/infra/orchestrationRequests/v7/requestId"));
325
326         ResponseEntity<GetOrchestrationResponse> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET,
327                 entity, GetOrchestrationResponse.class);
328
329         assertEquals(Response.Status.OK.getStatusCode(), response.getStatusCode().value());
330         assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
331         assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
332         assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
333         assertEquals("7.0.0", response.getHeaders().get("X-LatestVersion").get(0));
334         assertEquals("requestId", response.getHeaders().get("X-TransactionID").get(0));
335     }
336
337     public void setupTestGetOrchestrationRequest() throws Exception{
338         //For testGetOrchestrationRequest
339         stubFor(any(urlPathEqualTo("/infraActiveRequests/00032ab7-na18-42e5-965d-8ea592502018")).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
340                 .withBody(new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/getOrchestrationRequest.json"))))
341                 .withStatus(HttpStatus.SC_OK)));
342     }
343
344     private void setupTestGetOrchestrationRequestRequestDetails(String requestId, String status) throws Exception{
345         stubFor(get(urlPathEqualTo(getTestUrl(requestId))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
346                 .withBody(new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/getOrchestrationRequestDetails.json"))))
347                 .withStatus(HttpStatus.SC_OK)));
348     }
349
350     private void setupTestUnlockOrchestrationRequest(String requestId, String status) {
351         stubFor(get(urlPathEqualTo(getTestUrl(requestId))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
352                 .withBody(String.format(getResponseTemplate, requestId, status))
353                 .withStatus(HttpStatus.SC_OK)));
354
355     }
356
357
358
359     private void setupTestUnlockOrchestrationRequest_invalid_Json() {
360         stubFor(get(urlPathEqualTo(getTestUrl(INVALID_REQUEST_ID))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
361                 .withStatus(HttpStatus.SC_NOT_FOUND)));
362
363     }
364
365     private void setupTestUnlockOrchestrationRequest_Valid_Status(String requestID, String status) {
366         stubFor(get(urlPathEqualTo(getTestUrl(requestID))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
367                 .withBody(String.format(getResponseTemplate, requestID, status))
368                 .withStatus(HttpStatus.SC_OK)));
369
370         stubFor(post(urlPathEqualTo(getTestUrl(""))).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
371                 .withBody(String.format(infraActivePost, requestID))
372                 .withStatus(HttpStatus.SC_OK)));
373     }
374
375     private void setupTestGetOrchestrationRequestFilter() throws Exception{
376         //for testGetOrchestrationRequestFilter();
377         stubFor(any(urlPathEqualTo("/getOrchestrationFiltersFromInfraActive/")).withRequestBody(equalToJson("{\"modelType\":[\"EQUALS\",\"vfModule\"]}")).willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
378                 .withBody(new String(Files.readAllBytes(Paths.get("src/test/resources/OrchestrationRequest/getRequestDetailsFilter.json"))))
379                 .withStatus(HttpStatus.SC_OK)));
380     }
381 }