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