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 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;
37 import javax.ws.rs.core.MediaType;
38 import java.text.ParseException;
40 import static com.github.tomakehurst.wiremock.client.WireMock.*;
41 import static org.junit.Assert.assertEquals;
42 import static org.junit.Assert.assertTrue;
45 public class CloudResourcesOrchestrationTest extends BaseTest{
48 private InfraActiveRequestsRepository iarRepo;
49 private String requestJSON = "{\"requestDetails\":{\"requestInfo\":{\"source\":\"VID\",\"requestorId\":\"xxxxxx\" } } }";
50 private static final String path = "/onap/so/infra/cloudResourcesRequests";
57 HttpHeaders headers = new HttpHeaders();
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)));
63 public void testUnlockFailObjectMapping() {
65 headers.set("Accept", MediaType.APPLICATION_JSON);
66 headers.set("Content-Type", MediaType.APPLICATION_JSON);
67 HttpEntity<String> entity = new HttpEntity<String>(null, headers);
69 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
71 ResponseEntity<String> response = restTemplate.exchange(
72 builder.toUriString(),
73 HttpMethod.POST, entity, String.class);
75 String body = response.getBody();
76 assertTrue(body.contains("Mapping of request to JSON object failed."));
77 assertEquals(400, response.getStatusCodeValue());
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);
87 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
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());
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);
104 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
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());
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);
121 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
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());
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);
139 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/request-id-null-check/unlock");
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());
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);
159 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/requestIdtestUnlock/unlock");
161 ResponseEntity<String> response = restTemplate.exchange(
162 builder.toUriString(),
163 HttpMethod.POST, entity, String.class);
165 assertEquals(204, response.getStatusCodeValue());
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)));
174 headers.set("Accept", MediaType.APPLICATION_JSON);
175 headers.set("Content-Type", MediaType.APPLICATION_JSON);
176 HttpEntity<String> entity = new HttpEntity<>(requestJSON, headers);
178 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/requestIdtestUnlockComplete/unlock");
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());
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);
196 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
198 builder.queryParam("requestId", "not-there");
200 ResponseEntity<String> response = restTemplate.exchange(
201 builder.toUriString(),
202 HttpMethod.GET, entity, String.class);
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());
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);
218 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
220 builder.queryParam("requestId", "90c56827-1c78-4827-bc4d-6afcdb37a51f");
222 ResponseEntity<String> response = restTemplate.exchange(
223 builder.toUriString(),
224 HttpMethod.GET, entity, String.class);
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));
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)));
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)));
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");
250 builder.queryParam("requestId", "requestIdtestGetOperationalEnvFilterSuccess");
251 builder.queryParam("operationalEnvironmentName", "myVnfOpEnv");
253 ResponseEntity<String> response = restTemplate.exchange(
254 builder.toUriString(),
255 HttpMethod.GET, entity, String.class);
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));
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");
278 iarRepo.saveAndFlush(iar);
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");
285 builder.queryParam("filter", "operationalEnvironmentName:EQUALS:myVnfOpEnv");
287 ResponseEntity<String> response = restTemplate.exchange(
288 builder.toUriString(),
289 HttpMethod.GET, entity, String.class);
290 assertEquals(500, response.getStatusCodeValue());
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");
306 iarRepo.saveAndFlush(iar);
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");
313 builder.queryParam("operationalEnvironmentName", "");
315 ResponseEntity<String> response = restTemplate.exchange(
316 builder.toUriString(),
317 HttpMethod.GET, entity, String.class);
320 assertEquals(500, response.getStatusCodeValue());
321 assertTrue(response.getBody().toString().contains("No valid operationalEnvironmentName value is specified"));