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