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