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