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 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;
44 public class CloudResourcesOrchestrationTest extends BaseTest {
46 private String requestJSON =
47 "{\"requestDetails\":{\"requestInfo\":{\"source\":\"VID\",\"requestorId\":\"xxxxxx\" } } }";
48 private static final String path = "/onap/so/infra/cloudResourcesRequests";
50 HttpHeaders headers = new HttpHeaders();
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)));
60 public void testUnlockFailObjectMapping() {
62 headers.set("Accept", MediaType.APPLICATION_JSON);
63 headers.set("Content-Type", MediaType.APPLICATION_JSON);
64 HttpEntity<String> entity = new HttpEntity<String>(null, headers);
66 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
68 ResponseEntity<String> response =
69 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
71 String body = response.getBody();
72 assertTrue(body.contains("Mapping of request to JSON object failed."));
73 assertEquals(400, response.getStatusCodeValue());
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);
83 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
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());
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);
99 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
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());
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);
115 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
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());
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);
133 UriComponentsBuilder builder =
134 UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/request-id-null-check/unlock");
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());
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);
154 UriComponentsBuilder builder =
155 UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/requestIdtestUnlock/unlock");
157 ResponseEntity<String> response =
158 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
160 assertEquals(204, response.getStatusCodeValue());
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)));
170 headers.set("Accept", MediaType.APPLICATION_JSON);
171 headers.set("Content-Type", MediaType.APPLICATION_JSON);
172 HttpEntity<String> entity = new HttpEntity<>(requestJSON, headers);
174 UriComponentsBuilder builder =
175 UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/requestIdtestUnlockComplete/unlock");
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());
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);
194 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
196 builder.queryParam("requestId", "not-there");
198 ResponseEntity<String> response =
199 restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, String.class);
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());
207 public void testGetOperationalEnvSuccess() throws ParseException {
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);
219 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
221 builder.queryParam("requestId", "90c56827-1c78-4827-bc4d-6afcdb37a51f");
223 ResponseEntity<String> response =
224 restTemplate.exchange(builder.toUriString(), 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("e0e0e749-c9e2-48c3-8c4c-d51bf65a86c9", response.getHeaders().get("X-TransactionID").get(0));
235 public void testGetOperationalEnvFilterSuccess() throws ParseException {
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)));
243 wireMockServer.stubFor(
244 post(urlPathEqualTo(getTestUrl("getCloudOrchestrationFiltersFromInfraActive"))).willReturn(aResponse()
245 .withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
247 "{\"requestId\":\"getCloudOrchestrationFiltersFromInfraActive\", \"operationalEnvironmentName\":\"myVnfOpEnv\"}")
248 .withBody("[" + String.format(getResponseTemplateNoBody,
249 "requestIdtestGetOperationalEnvFilterSuccess", "COMPLETE") + "]")
250 .withStatus(HttpStatus.SC_OK)));
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");
257 builder.queryParam("requestId", "requestIdtestGetOperationalEnvFilterSuccess");
258 builder.queryParam("operationalEnvironmentName", "myVnfOpEnv");
260 ResponseEntity<String> response =
261 restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, String.class);
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));
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");
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");
288 builder.queryParam("filter", "operationalEnvironmentName:EQUALS:myVnfOpEnv");
290 ResponseEntity<String> response =
291 restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, String.class);
292 assertEquals(500, response.getStatusCodeValue());
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");
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");
312 builder.queryParam("operationalEnvironmentName", "");
314 ResponseEntity<String> response =
315 restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, String.class);
318 assertEquals(500, response.getStatusCodeValue());
319 assertTrue(response.getBody().toString().contains("No valid operationalEnvironmentName value is specified"));