save csar file path add directory version
[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 org.apache.http.HttpStatus;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.onap.so.apihandlerinfra.BaseTest;
27 import org.onap.so.db.request.beans.InfraActiveRequests;
28 import org.onap.so.db.request.data.repository.InfraActiveRequestsRepository;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.boot.context.embedded.LocalServerPort;
31 import org.springframework.http.HttpEntity;
32 import org.springframework.http.HttpHeaders;
33 import org.springframework.http.HttpMethod;
34 import org.springframework.http.ResponseEntity;
35 import org.springframework.web.util.UriComponentsBuilder;
36
37 import javax.ws.rs.core.MediaType;
38 import java.text.ParseException;
39
40 import static com.github.tomakehurst.wiremock.client.WireMock.*;
41 import static org.junit.Assert.assertEquals;
42 import static org.junit.Assert.assertTrue;
43
44
45 public class CloudResourcesOrchestrationTest extends BaseTest{
46         
47         @Autowired
48         private InfraActiveRequestsRepository iarRepo;
49         private String requestJSON = "{\"requestDetails\":{\"requestInfo\":{\"source\":\"VID\",\"requestorId\":\"xxxxxx\" } } }";
50         private static final String path = "/onap/so/infra/cloudResourcesRequests";
51         
52         @LocalServerPort
53         private int port;
54
55
56
57         HttpHeaders headers = new HttpHeaders();
58         @Before
59         public void setupTestClass() throws Exception{
60                 stubFor(post(urlPathEqualTo(getTestUrl(""))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).withStatus(HttpStatus.SC_CREATED)));
61         }
62         @Test
63         public void testUnlockFailObjectMapping() {
64                 
65                 headers.set("Accept", MediaType.APPLICATION_JSON);
66                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
67                 HttpEntity<String> entity = new HttpEntity<String>(null, headers);
68         
69                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
70                        
71                 ResponseEntity<String> response = restTemplate.exchange(
72                                 builder.toUriString(),
73                                 HttpMethod.POST, entity, String.class);
74                 
75                 String body = response.getBody();
76                 assertTrue(body.contains("Mapping of request to JSON object failed."));
77                 assertEquals(400, response.getStatusCodeValue());
78         }
79         
80         @Test
81         public void testParseOrchestrationError1() {
82                 String requestJSON = "{\"requestDetails\": null }";
83                 headers.set("Accept", MediaType.APPLICATION_JSON);
84                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
85                 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
86                 
87                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
88                        
89                 ResponseEntity<String> response = restTemplate.exchange(
90                                 builder.toUriString(),
91                                 HttpMethod.POST, entity, String.class);
92                 String body = response.getBody();
93                 assertTrue(body.contains("No valid requestDetails is specified"));
94                 assertEquals(400, response.getStatusCodeValue());
95         }
96         
97         @Test
98         public void testParseOrchestrationError2() {
99                 String requestJSON = "{\"requestDetails\":{\"requestInfo\":{\"source\":\"\",\"requestorId\":\"xxxxxx\" } } }";
100                 headers.set("Accept", MediaType.APPLICATION_JSON);
101                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
102                 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
103
104                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
105                        
106                 ResponseEntity<String> response = restTemplate.exchange(
107                                 builder.toUriString(),
108                                 HttpMethod.POST, entity, String.class);
109                 String body = response.getBody();
110                 assertTrue(body.contains("No valid source is specified"));
111                 assertEquals(400, response.getStatusCodeValue());
112         }
113         
114         @Test
115         public void testParseOrchestrationError3() {
116                 String requestJSON = "{\"requestDetails\":{\"requestInfo\":{\"source\":\"VID\",\"requestorId\":\"\" } } }";
117                 headers.set("Accept", MediaType.APPLICATION_JSON);
118                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
119                 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
120
121                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
122                        
123                 ResponseEntity<String> response = restTemplate.exchange(
124                                 builder.toUriString(),
125                                 HttpMethod.POST, entity, String.class);
126                 String body = response.getBody();
127                 assertTrue(body.contains("No valid requestorId is specified"));
128                 assertEquals(400, response.getStatusCodeValue());
129         }
130         
131         @Test
132         public void testGetInfraActiveRequestNull() {
133                 stubFor(get(urlPathEqualTo(getTestUrl("request-id-null-check"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
134                                 .withStatus(HttpStatus.SC_OK)));
135                 headers.set("Accept", MediaType.APPLICATION_JSON);
136                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
137                 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
138
139                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/request-id-null-check/unlock");
140                        
141                 ResponseEntity<String> response = restTemplate.exchange(
142                                 builder.toUriString(),
143                                 HttpMethod.POST, entity, String.class);
144                 String body = response.getBody();
145                 assertTrue(body.contains("Orchestration RequestId request-id-null-check is not found in DB"));
146                 assertEquals(400, response.getStatusCodeValue());
147
148         }
149
150         @Test
151         public void testUnlock() throws ParseException {
152                 stubFor(get(urlPathEqualTo(getTestUrl("requestIdtestUnlock"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
153                                 .withBody(String.format(getResponseTemplate, "requestIdtestUnlock", "IN_PROGRESS"))
154                                 .withStatus(HttpStatus.SC_OK)));
155                 headers.set("Accept", MediaType.APPLICATION_JSON);
156                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
157                 HttpEntity<String> entity = new HttpEntity<>(requestJSON, headers);
158         
159                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/requestIdtestUnlock/unlock");
160                        
161                 ResponseEntity<String> response = restTemplate.exchange(
162                                 builder.toUriString(),
163                                 HttpMethod.POST, entity, String.class);
164                 
165                 assertEquals(204, response.getStatusCodeValue());
166         }
167         
168         @Test
169         public void testUnlockComplete() throws ParseException {
170                 stubFor(get(urlPathEqualTo(getTestUrl("requestIdtestUnlockComplete"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
171                                 .withBody(String.format(getResponseTemplate, "requestIdtestUnlockComplete", "COMPLETE"))
172                                 .withStatus(HttpStatus.SC_OK)));
173
174                 headers.set("Accept", MediaType.APPLICATION_JSON);
175                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
176                 HttpEntity<String> entity = new HttpEntity<>(requestJSON, headers);
177         
178                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/requestIdtestUnlockComplete/unlock");
179                        
180                 ResponseEntity<String> response = restTemplate.exchange(
181                                 builder.toUriString(),
182                                 HttpMethod.POST, entity, String.class);
183                 String body = response.getBody().toString();
184                 assertTrue(body.contains("Orchestration RequestId requestIdtestUnlockComplete has a status of COMPLETE and can not be unlocked"));
185                 assertEquals(400, response.getStatusCodeValue());
186         }
187         
188         @Test
189         public void testGetOperationalEnvFilter() {
190                 stubFor(get(urlPathEqualTo(getTestUrl("not-there"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
191                                 .withStatus(HttpStatus.SC_OK)));
192                 headers.set("Accept", MediaType.APPLICATION_JSON);
193                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
194                 HttpEntity<String> entity = new HttpEntity<>(null, headers);
195         
196                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
197                 
198                 builder.queryParam("requestId", "not-there");
199                        
200                 ResponseEntity<String> response = restTemplate.exchange(
201                                 builder.toUriString(),
202                                 HttpMethod.GET, entity, String.class);
203                 
204                 //204s cannot have a body
205                 //assertTrue(response.getBody().contains("Orchestration RequestId not-there is not found in DB"));
206                 assertEquals(204, response.getStatusCodeValue());
207         }
208         
209         @Test
210         public void testGetOperationalEnvSuccess() throws ParseException {
211                 stubFor(get(urlPathEqualTo(getTestUrl("90c56827-1c78-4827-bc4d-6afcdb37a51f"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
212                                 .withBody(String.format(getResponseTemplateNoBody, "90c56827-1c78-4827-bc4d-6afcdb37a51f", "COMPLETE"))
213                                 .withStatus(HttpStatus.SC_OK)));
214                 headers.set("Accept", MediaType.APPLICATION_JSON);
215                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
216                 HttpEntity<String> entity = new HttpEntity<>("", headers);
217         
218                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
219                 
220                 builder.queryParam("requestId", "90c56827-1c78-4827-bc4d-6afcdb37a51f");
221                        
222                 ResponseEntity<String> response = restTemplate.exchange(
223                                 builder.toUriString(),
224                                 HttpMethod.GET, entity, String.class);
225                 
226                 assertEquals(200, response.getStatusCodeValue());
227         assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
228         assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
229         assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
230         assertEquals("1.0.0", response.getHeaders().get("X-LatestVersion").get(0));
231         assertEquals("90c56827-1c78-4827-bc4d-6afcdb37a51f", response.getHeaders().get("X-TransactionID").get(0));
232         }
233         
234         @Test
235         public void testGetOperationalEnvFilterSuccess() throws ParseException {
236                 stubFor(get(urlPathEqualTo(getTestUrl("requestIdtestGetOperationalEnvFilterSuccess"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
237                                 .withBody(String.format(getResponseTemplate, "requestIdtestGetOperationalEnvFilterSuccess", "COMPLETE"))
238                                 .withStatus(HttpStatus.SC_OK)));
239
240                 stubFor(post(urlPathEqualTo(getTestUrl("getCloudOrchestrationFiltersFromInfraActive"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
241                                 .withBody("{\"requestId\":\"getCloudOrchestrationFiltersFromInfraActive\", \"operationalEnvironmentName\":\"myVnfOpEnv\"}")
242                                 .withBody("["+String.format(getResponseTemplateNoBody, "requestIdtestGetOperationalEnvFilterSuccess", "COMPLETE")+"]")
243                                 .withStatus(HttpStatus.SC_OK)));
244
245                 headers.set("Accept", MediaType.APPLICATION_JSON);
246                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
247                 HttpEntity<String> entity = new HttpEntity<>(null, headers);
248                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
249                 
250                 builder.queryParam("requestId", "requestIdtestGetOperationalEnvFilterSuccess");
251                 builder.queryParam("operationalEnvironmentName", "myVnfOpEnv");
252                        
253                 ResponseEntity<String> response = restTemplate.exchange(
254                                 builder.toUriString(),
255                                 HttpMethod.GET, entity, String.class);
256                 
257                 assertEquals(200, response.getStatusCodeValue());
258         assertEquals("application/json", response.getHeaders().get(HttpHeaders.CONTENT_TYPE).get(0));
259         assertEquals("0", response.getHeaders().get("X-MinorVersion").get(0));
260         assertEquals("0", response.getHeaders().get("X-PatchVersion").get(0));
261         assertEquals("1.0.0", response.getHeaders().get("X-LatestVersion").get(0));
262
263         }
264         
265         @Test
266         public void testGetOperationalEnvFilterException1() throws ParseException {
267                 InfraActiveRequests iar = new InfraActiveRequests();
268                 iar.setRequestId("requestId-getOpEnvFilterEx1");
269                 iar.setRequestScope("requestScope");
270                 iar.setRequestType("requestType");
271                 iar.setOperationalEnvId("operationalEnvironmentId");
272                 iar.setOperationalEnvName("operationalEnvName");
273                 iar.setRequestorId("xxxxxx");
274                 iar.setRequestBody("");
275                 iar.setRequestStatus("COMPLETE");
276                 iar.setRequestAction("TEST");
277                 
278                 iarRepo.saveAndFlush(iar);
279
280                 headers.set("Accept", MediaType.APPLICATION_JSON);
281                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
282                 HttpEntity<String> entity = new HttpEntity<>("", headers);
283                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
284                 
285                 builder.queryParam("filter", "operationalEnvironmentName:EQUALS:myVnfOpEnv");
286                        
287                 ResponseEntity<String> response = restTemplate.exchange(
288                                 builder.toUriString(),
289                                 HttpMethod.GET, entity, String.class);
290                 assertEquals(500, response.getStatusCodeValue());
291         }
292         
293         @Test
294         public void testGetOperationalEnvFilterException2() throws ParseException {
295                 InfraActiveRequests iar = new InfraActiveRequests();
296                 iar.setRequestId("requestIdFilterException2");
297                 iar.setRequestScope("requestScope");
298                 iar.setRequestType("requestType");
299                 iar.setOperationalEnvId("operationalEnvId");
300                 iar.setOperationalEnvName("operationalEnvName");
301                 iar.setRequestorId("xxxxxx");
302                 iar.setRequestBody("");
303                 iar.setRequestStatus("COMPLETE");
304                 iar.setRequestAction("TEST");
305                 
306                 iarRepo.saveAndFlush(iar);
307
308                 headers.set("Accept", MediaType.APPLICATION_JSON);
309                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
310                 HttpEntity<String> entity = new HttpEntity<>(null, headers);
311                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
312                 
313                 builder.queryParam("operationalEnvironmentName", "");
314                        
315                 ResponseEntity<String> response = restTemplate.exchange(
316                                 builder.toUriString(),
317                                 HttpMethod.GET, entity, String.class);
318                 
319                 
320                 assertEquals(500, response.getStatusCodeValue());
321                 assertTrue(response.getBody().toString().contains("No valid operationalEnvironmentName value is specified"));
322         }
323
324 }