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