Containerization feature of SO
[so.git] / mso-api-handlers / mso-api-handler-infra / src / test / java / org / onap / so / apihandlerinfra / tenantisolation / CloudResourcesOrchestrationTest.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.tenantisolation;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
26 import java.sql.Timestamp;
27 import java.text.DateFormat;
28 import java.text.ParseException;
29 import java.text.SimpleDateFormat;
30 import java.util.Date;
31
32 import javax.ws.rs.core.MediaType;
33
34 import org.junit.Test;
35 import org.onap.so.apihandlerinfra.BaseTest;
36 import org.onap.so.db.request.beans.InfraActiveRequests;
37 import org.onap.so.db.request.data.repository.InfraActiveRequestsRepository;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.boot.context.embedded.LocalServerPort;
40 import org.springframework.http.HttpEntity;
41 import org.springframework.http.HttpHeaders;
42 import org.springframework.http.HttpMethod;
43 import org.springframework.http.ResponseEntity;
44 import org.springframework.web.util.UriComponentsBuilder;
45
46
47 public class CloudResourcesOrchestrationTest extends BaseTest{
48         
49         @Autowired
50         private InfraActiveRequestsRepository iarRepo;
51         private String requestJSON = "{\"requestDetails\":{\"requestInfo\":{\"source\":\"VID\",\"requestorId\":\"xxxxxx\" } } }";
52         private static final String path = "/onap/so/infra/cloudResourcesRequests";
53         
54         @LocalServerPort
55         private int port;
56
57
58
59         HttpHeaders headers = new HttpHeaders();
60
61         @Test
62         public void testUnlockFailObjectMapping() {
63                 
64                 headers.set("Accept", MediaType.APPLICATION_JSON);
65                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
66                 HttpEntity<String> entity = new HttpEntity<String>(null, headers);
67         
68                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
69                        
70                 ResponseEntity<String> response = restTemplate.exchange(
71                                 builder.toUriString(),
72                                 HttpMethod.POST, entity, String.class);
73                 
74                 String body = response.getBody();
75                 assertTrue(body.contains("Mapping of request to JSON object failed."));
76                 assertEquals(400, response.getStatusCodeValue());
77         }
78         
79         @Test
80         public void testParseOrchestrationError1() {
81                 String requestJSON = "{\"requestDetails\": null }";
82                 headers.set("Accept", MediaType.APPLICATION_JSON);
83                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
84                 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
85                 
86                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
87                        
88                 ResponseEntity<String> response = restTemplate.exchange(
89                                 builder.toUriString(),
90                                 HttpMethod.POST, entity, String.class);
91                 String body = response.getBody();
92                 assertTrue(body.contains("No valid requestDetails is specified"));
93                 assertEquals(400, response.getStatusCodeValue());
94         }
95         
96         @Test
97         public void testParseOrchestrationError2() {
98                 String requestJSON = "{\"requestDetails\":{\"requestInfo\":{\"source\":\"\",\"requestorId\":\"xxxxxx\" } } }";
99                 headers.set("Accept", MediaType.APPLICATION_JSON);
100                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
101                 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
102
103                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
104                        
105                 ResponseEntity<String> response = restTemplate.exchange(
106                                 builder.toUriString(),
107                                 HttpMethod.POST, entity, String.class);
108                 String body = response.getBody();
109                 assertTrue(body.contains("No valid source is specified"));
110                 assertEquals(400, response.getStatusCodeValue());
111         }
112         
113         @Test
114         public void testParseOrchestrationError3() {
115                 String requestJSON = "{\"requestDetails\":{\"requestInfo\":{\"source\":\"VID\",\"requestorId\":\"\" } } }";
116                 headers.set("Accept", MediaType.APPLICATION_JSON);
117                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
118                 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
119
120                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
121                        
122                 ResponseEntity<String> response = restTemplate.exchange(
123                                 builder.toUriString(),
124                                 HttpMethod.POST, entity, String.class);
125                 String body = response.getBody();
126                 assertTrue(body.contains("No valid requestorId is specified"));
127                 assertEquals(400, response.getStatusCodeValue());
128         }
129         
130         @Test
131         public void testGetInfraActiveRequestNull() {
132                 
133                 headers.set("Accept", MediaType.APPLICATION_JSON);
134                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
135                 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
136
137                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/request-id-null-check/unlock");
138                        
139                 ResponseEntity<String> response = restTemplate.exchange(
140                                 builder.toUriString(),
141                                 HttpMethod.POST, entity, String.class);
142                 String body = response.getBody();
143                 assertTrue(body.contains("Orchestration RequestId request-id-null-check is not found in DB"));
144                 assertEquals(400, response.getStatusCodeValue());
145
146         }
147         
148         @Test
149         public void testUnlockError() {
150                 InfraActiveRequests iar = new InfraActiveRequests();
151                 iar.setRequestId("requestIdtestUnlockError");
152                 iar.setRequestScope("requestScope");
153                 iar.setRequestType("requestType");
154                 iar.setOperationalEnvId("operationalEnvironmentId");
155                 iar.setOperationalEnvName("operationalEnvName");
156                 iar.setRequestorId("xxxxxx");
157                 iar.setRequestBody("");
158                 iar.setRequestStatus("IN_PROGRESS");
159                 iar.setRequestAction("TEST");
160                 
161                 iarRepo.saveAndFlush(iar);
162                 headers.set("Accept", MediaType.APPLICATION_JSON);
163                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
164                 HttpEntity<String> entity = new HttpEntity<>(requestJSON, headers);
165         
166                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/requestId/unlock");
167                        
168                 ResponseEntity<String> response = restTemplate.exchange(
169                                 builder.toUriString(),
170                                 HttpMethod.POST, entity, String.class);
171                 
172                 assertEquals(400, response.getStatusCodeValue());
173         }
174         
175         @Test
176         public void testUnlock() throws ParseException {
177                 InfraActiveRequests iar = new InfraActiveRequests();
178                 iar.setRequestId("requestIdtestUnlock");
179                 iar.setRequestScope("requestScope");
180                 iar.setRequestType("requestType");
181                 iar.setOperationalEnvId("operationalEnvironmentId");
182                 iar.setOperationalEnvName("operationalEnvName");
183                 iar.setRequestorId("xxxxxx");
184                 iar.setRequestBody("{}");
185                 iar.setRequestStatus("IN_PROGRESS");
186                 iar.setRequestAction("TEST");
187                 
188                 DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
189                 Date date = dateFormat.parse("23/09/2007");
190                 long time = date.getTime();
191                 iar.setStartTime(new Timestamp(time));
192                 
193                 iarRepo.saveAndFlush(iar);
194                 headers.set("Accept", MediaType.APPLICATION_JSON);
195                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
196                 HttpEntity<String> entity = new HttpEntity<>(requestJSON, headers);
197         
198                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/requestIdtestUnlock/unlock");
199                        
200                 ResponseEntity<String> response = restTemplate.exchange(
201                                 builder.toUriString(),
202                                 HttpMethod.POST, entity, String.class);
203                 
204                 assertEquals(204, response.getStatusCodeValue());
205         }
206         
207         @Test
208         public void testUnlockComplete() throws ParseException {
209                 InfraActiveRequests iar = new InfraActiveRequests();
210                 iar.setRequestId("requestIdtestUnlockComplete");
211                 iar.setRequestScope("requestScope");
212                 iar.setRequestType("requestType");
213                 iar.setOperationalEnvId("operationalEnvironmentId");
214                 iar.setOperationalEnvName("operationalEnvName");
215                 iar.setRequestorId("xxxxxx");
216                 iar.setRequestBody("{}");
217                 iar.setRequestStatus("COMPLETE");
218                 iar.setRequestAction("TEST");
219                 
220                 DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
221                 Date date = dateFormat.parse("23/09/2007");
222                 long time = date.getTime();
223                 iar.setStartTime(new Timestamp(time));
224                 
225                 iarRepo.saveAndFlush(iar);
226                 headers.set("Accept", MediaType.APPLICATION_JSON);
227                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
228                 HttpEntity<String> entity = new HttpEntity<>(requestJSON, headers);
229         
230                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/requestIdtestUnlockComplete/unlock");
231                        
232                 ResponseEntity<String> response = restTemplate.exchange(
233                                 builder.toUriString(),
234                                 HttpMethod.POST, entity, String.class);
235                 String body = response.getBody().toString();
236                 assertTrue(body.contains("Orchestration RequestId requestIdtestUnlockComplete has a status of COMPLETE and can not be unlocked"));
237                 assertEquals(400, response.getStatusCodeValue());
238         }
239         
240         @Test
241         public void testGetOperationalEnvFilter() {
242                 
243                 headers.set("Accept", MediaType.APPLICATION_JSON);
244                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
245                 HttpEntity<String> entity = new HttpEntity<>(null, headers);
246         
247                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
248                 
249                 builder.queryParam("requestId", "not-there");
250                        
251                 ResponseEntity<String> response = restTemplate.exchange(
252                                 builder.toUriString(),
253                                 HttpMethod.GET, entity, String.class);
254                 
255                 //204s cannot have a body
256                 //assertTrue(response.getBody().contains("Orchestration RequestId not-there is not found in DB"));
257                 assertEquals(204, response.getStatusCodeValue());
258         }
259         
260         @Test
261         public void testGetOperationalEnvSuccess() throws ParseException {
262                 InfraActiveRequests iar = new InfraActiveRequests();
263                 iar.setRequestId("90c56827-1c78-4827-bc4d-6afcdb37a51f");
264                 iar.setRequestScope("requestScope");
265                 iar.setRequestType("requestType");
266                 iar.setOperationalEnvId("operationalEnvironmentId");
267                 iar.setOperationalEnvName("operationalEnvName");
268                 iar.setRequestorId("xxxxxx");
269                 iar.setRequestBody("{}");
270                 iar.setRequestStatus("COMPLETE");
271                 iar.setRequestAction("TEST");
272                 
273                 DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
274                 Date date = dateFormat.parse("23/09/2007");
275                 long time = date.getTime();
276                 iar.setStartTime(new Timestamp(time));
277                 
278                 iarRepo.saveAndFlush(iar);
279                 headers.set("Accept", MediaType.APPLICATION_JSON);
280                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
281                 HttpEntity<String> entity = new HttpEntity<>("", headers);
282         
283                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
284                 
285                 builder.queryParam("requestId", "90c56827-1c78-4827-bc4d-6afcdb37a51f");
286                        
287                 ResponseEntity<String> response = restTemplate.exchange(
288                                 builder.toUriString(),
289                                 HttpMethod.GET, entity, String.class);
290                 
291                 assertEquals(200, response.getStatusCodeValue());
292         assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
293         assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
294         assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
295         assertEquals("1.0.0", response.getHeaders().get("X-LatestVersion").get(0));
296         assertEquals("90c56827-1c78-4827-bc4d-6afcdb37a51f", response.getHeaders().get("X-TransactionID").get(0));
297         }
298         
299         @Test
300         public void testGetOperationalEnvFilterSuccess() throws ParseException {
301                 InfraActiveRequests iar = new InfraActiveRequests();
302                 iar.setRequestId("requestIdtestGetOperationalEnvFilterSuccess");
303                 iar.setRequestScope("requestScope");
304                 iar.setRequestType("requestType");
305                 iar.setOperationalEnvId("operationalEnvironmentId");
306                 iar.setOperationalEnvName("myVnfOpEnv");
307                 iar.setRequestorId("xxxxxx");
308                 iar.setRequestBody("");
309                 iar.setRequestStatus("COMPLETE");
310                 iar.setStatusMessage("status Message");
311                 iar.setProgress(20L);
312                 iar.setRequestAction("TEST");
313                 
314                 DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
315                 Date date = dateFormat.parse("23/09/2007");
316                 long time = date.getTime();
317                 iar.setStartTime(new Timestamp(time));
318                 iar.setEndTime(new Timestamp(time));
319                 
320                 iarRepo.saveAndFlush(iar);
321
322                 headers.set("Accept", MediaType.APPLICATION_JSON);
323                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
324                 HttpEntity<String> entity = new HttpEntity<>(null, headers);
325                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
326                 
327                 builder.queryParam("requestId", "requestIdtestGetOperationalEnvFilterSuccess");
328                 builder.queryParam("operationalEnvironmentName", "myVnfOpEnv");
329                        
330                 ResponseEntity<String> response = restTemplate.exchange(
331                                 builder.toUriString(),
332                                 HttpMethod.GET, entity, String.class);
333                 
334                 assertEquals(200, response.getStatusCodeValue());
335         assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
336         assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
337         assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
338         assertEquals("1.0.0", response.getHeaders().get("X-LatestVersion").get(0));
339
340         }
341         
342         @Test
343         public void testGetOperationalEnvFilterException1() throws ParseException {
344                 InfraActiveRequests iar = new InfraActiveRequests();
345                 iar.setRequestId("requestId-getOpEnvFilterEx1");
346                 iar.setRequestScope("requestScope");
347                 iar.setRequestType("requestType");
348                 iar.setOperationalEnvId("operationalEnvironmentId");
349                 iar.setOperationalEnvName("operationalEnvName");
350                 iar.setRequestorId("xxxxxx");
351                 iar.setRequestBody("");
352                 iar.setRequestStatus("COMPLETE");
353                 iar.setRequestAction("TEST");
354                 
355                 iarRepo.saveAndFlush(iar);
356
357                 headers.set("Accept", MediaType.APPLICATION_JSON);
358                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
359                 HttpEntity<String> entity = new HttpEntity<>("", headers);
360                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
361                 
362                 builder.queryParam("filter", "operationalEnvironmentName:EQUALS:myVnfOpEnv");
363                        
364                 ResponseEntity<String> response = restTemplate.exchange(
365                                 builder.toUriString(),
366                                 HttpMethod.GET, entity, String.class);
367                 assertEquals(500, response.getStatusCodeValue());
368         }
369         
370         @Test
371         public void testGetOperationalEnvFilterException2() throws ParseException {
372                 InfraActiveRequests iar = new InfraActiveRequests();
373                 iar.setRequestId("requestIdFilterException2");
374                 iar.setRequestScope("requestScope");
375                 iar.setRequestType("requestType");
376                 iar.setOperationalEnvId("operationalEnvId");
377                 iar.setOperationalEnvName("operationalEnvName");
378                 iar.setRequestorId("xxxxxx");
379                 iar.setRequestBody("");
380                 iar.setRequestStatus("COMPLETE");
381                 iar.setRequestAction("TEST");
382                 
383                 iarRepo.saveAndFlush(iar);
384
385                 headers.set("Accept", MediaType.APPLICATION_JSON);
386                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
387                 HttpEntity<String> entity = new HttpEntity<>(null, headers);
388                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
389                 
390                 builder.queryParam("operationalEnvironmentName", "");
391                        
392                 ResponseEntity<String> response = restTemplate.exchange(
393                                 builder.toUriString(),
394                                 HttpMethod.GET, entity, String.class);
395                 
396                 
397                 assertEquals(500, response.getStatusCodeValue());
398                 assertTrue(response.getBody().toString().contains("No valid operationalEnvironmentName value is specified"));
399         }
400
401 }