2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.apihandlerinfra.tenantisolation;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
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;
32 import javax.ws.rs.core.MediaType;
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;
47 public class CloudResourcesOrchestrationTest extends BaseTest{
50 private InfraActiveRequestsRepository iarRepo;
51 private String requestJSON = "{\"requestDetails\":{\"requestInfo\":{\"source\":\"VID\",\"requestorId\":\"xxxxxx\" } } }";
52 private static final String path = "/onap/so/infra/cloudResourcesRequests";
59 HttpHeaders headers = new HttpHeaders();
62 public void testUnlockFailObjectMapping() {
64 headers.set("Accept", MediaType.APPLICATION_JSON);
65 headers.set("Content-Type", MediaType.APPLICATION_JSON);
66 HttpEntity<String> entity = new HttpEntity<String>(null, headers);
68 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
70 ResponseEntity<String> response = restTemplate.exchange(
71 builder.toUriString(),
72 HttpMethod.POST, entity, String.class);
74 String body = response.getBody();
75 assertTrue(body.contains("Mapping of request to JSON object failed."));
76 assertEquals(400, response.getStatusCodeValue());
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);
86 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
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());
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);
103 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
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());
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);
120 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
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());
131 public void testGetInfraActiveRequestNull() {
133 headers.set("Accept", MediaType.APPLICATION_JSON);
134 headers.set("Content-Type", MediaType.APPLICATION_JSON);
135 HttpEntity<String> entity = new HttpEntity<String>(requestJSON, headers);
137 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/request-id-null-check/unlock");
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());
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");
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);
166 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/requestId/unlock");
168 ResponseEntity<String> response = restTemplate.exchange(
169 builder.toUriString(),
170 HttpMethod.POST, entity, String.class);
172 assertEquals(400, response.getStatusCodeValue());
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");
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));
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);
198 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/requestIdtestUnlock/unlock");
200 ResponseEntity<String> response = restTemplate.exchange(
201 builder.toUriString(),
202 HttpMethod.POST, entity, String.class);
204 assertEquals(204, response.getStatusCodeValue());
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");
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));
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);
230 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/requestIdtestUnlockComplete/unlock");
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());
241 public void testGetOperationalEnvFilter() {
243 headers.set("Accept", MediaType.APPLICATION_JSON);
244 headers.set("Content-Type", MediaType.APPLICATION_JSON);
245 HttpEntity<String> entity = new HttpEntity<>(null, headers);
247 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
249 builder.queryParam("requestId", "not-there");
251 ResponseEntity<String> response = restTemplate.exchange(
252 builder.toUriString(),
253 HttpMethod.GET, entity, String.class);
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());
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");
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));
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);
283 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
285 builder.queryParam("requestId", "90c56827-1c78-4827-bc4d-6afcdb37a51f");
287 ResponseEntity<String> response = restTemplate.exchange(
288 builder.toUriString(),
289 HttpMethod.GET, entity, String.class);
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));
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");
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));
320 iarRepo.saveAndFlush(iar);
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");
327 builder.queryParam("requestId", "requestIdtestGetOperationalEnvFilterSuccess");
328 builder.queryParam("operationalEnvironmentName", "myVnfOpEnv");
330 ResponseEntity<String> response = restTemplate.exchange(
331 builder.toUriString(),
332 HttpMethod.GET, entity, String.class);
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));
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");
355 iarRepo.saveAndFlush(iar);
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");
362 builder.queryParam("filter", "operationalEnvironmentName:EQUALS:myVnfOpEnv");
364 ResponseEntity<String> response = restTemplate.exchange(
365 builder.toUriString(),
366 HttpMethod.GET, entity, String.class);
367 assertEquals(500, response.getStatusCodeValue());
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");
383 iarRepo.saveAndFlush(iar);
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");
390 builder.queryParam("operationalEnvironmentName", "");
392 ResponseEntity<String> response = restTemplate.exchange(
393 builder.toUriString(),
394 HttpMethod.GET, entity, String.class);
397 assertEquals(500, response.getStatusCodeValue());
398 assertTrue(response.getBody().toString().contains("No valid operationalEnvironmentName value is specified"));