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.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;
 
  43 public class CloudResourcesOrchestrationTest extends BaseTest {
 
  45     private String requestJSON =
 
  46             "{\"requestDetails\":{\"requestInfo\":{\"source\":\"VID\",\"requestorId\":\"xxxxxx\" } } }";
 
  47     private static final String path = "/onap/so/infra/cloudResourcesRequests";
 
  49     HttpHeaders headers = new HttpHeaders();
 
  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)));
 
  59     public void testUnlockFailObjectMapping() {
 
  61         headers.set("Accept", MediaType.APPLICATION_JSON);
 
  62         headers.set("Content-Type", MediaType.APPLICATION_JSON);
 
  63         HttpEntity<String> entity = new HttpEntity<String>(null, headers);
 
  65         UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
 
  67         ResponseEntity<String> response =
 
  68                 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
 
  70         String body = response.getBody();
 
  71         assertTrue(body.contains("Mapping of request to JSON object failed."));
 
  72         assertEquals(400, response.getStatusCodeValue());
 
  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);
 
  82         UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
 
  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());
 
  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);
 
  98         UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
 
 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());
 
 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);
 
 114         UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/test/unlock");
 
 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());
 
 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);
 
 132         UriComponentsBuilder builder =
 
 133                 UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/request-id-null-check/unlock");
 
 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());
 
 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);
 
 153         UriComponentsBuilder builder =
 
 154                 UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/requestIdtestUnlock/unlock");
 
 156         ResponseEntity<String> response =
 
 157                 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
 
 159         assertEquals(204, response.getStatusCodeValue());
 
 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)));
 
 169         headers.set("Accept", MediaType.APPLICATION_JSON);
 
 170         headers.set("Content-Type", MediaType.APPLICATION_JSON);
 
 171         HttpEntity<String> entity = new HttpEntity<>(requestJSON, headers);
 
 173         UriComponentsBuilder builder =
 
 174                 UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1/requestIdtestUnlockComplete/unlock");
 
 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());
 
 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);
 
 193         UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
 
 195         builder.queryParam("requestId", "not-there");
 
 197         ResponseEntity<String> response =
 
 198                 restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, String.class);
 
 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());
 
 206     public void testGetOperationalEnvSuccess() throws ParseException {
 
 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);
 
 217         UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
 
 219         builder.queryParam("requestId", "90c56827-1c78-4827-bc4d-6afcdb37a51f");
 
 221         ResponseEntity<String> response =
 
 222                 restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, String.class);
 
 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));
 
 233     public void testGetOperationalEnvFilterSuccess() throws ParseException {
 
 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)));
 
 241         wireMockServer.stubFor(
 
 242                 post(urlPathEqualTo(getTestUrl("getCloudOrchestrationFiltersFromInfraActive"))).willReturn(aResponse()
 
 243                         .withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
 
 245                                 "{\"requestId\":\"getCloudOrchestrationFiltersFromInfraActive\", \"operationalEnvironmentName\":\"myVnfOpEnv\"}")
 
 246                         .withBody("[" + String.format(getResponseTemplateNoBody,
 
 247                                 "requestIdtestGetOperationalEnvFilterSuccess", "COMPLETE") + "]")
 
 248                         .withStatus(HttpStatus.SC_OK)));
 
 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");
 
 255         builder.queryParam("requestId", "requestIdtestGetOperationalEnvFilterSuccess");
 
 256         builder.queryParam("operationalEnvironmentName", "myVnfOpEnv");
 
 258         ResponseEntity<String> response =
 
 259                 restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, String.class);
 
 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));
 
 270     public void testGetOperationalEnvFilterException1() throws ParseException {
 
 271         InfraActiveRequests iar = new InfraActiveRequests();
 
 272         iar.setRequestId("requestId-getOpEnvFilterEx1");
 
 273         iar.setRequestScope("requestScope");
 
 274         iar.setRequestType("requestType");
 
 275         iar.setOperationalEnvId("operationalEnvironmentId");
 
 276         iar.setOperationalEnvName("operationalEnvName");
 
 277         iar.setRequestorId("xxxxxx");
 
 278         iar.setRequestBody("");
 
 279         iar.setRequestStatus("COMPLETE");
 
 280         iar.setRequestAction("TEST");
 
 282         headers.set("Accept", MediaType.APPLICATION_JSON);
 
 283         headers.set("Content-Type", MediaType.APPLICATION_JSON);
 
 284         HttpEntity<String> entity = new HttpEntity<>("", headers);
 
 285         UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/v1");
 
 287         builder.queryParam("filter", "operationalEnvironmentName:EQUALS:myVnfOpEnv");
 
 289         ResponseEntity<String> response =
 
 290                 restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, String.class);
 
 291         assertEquals(500, response.getStatusCodeValue());
 
 295     public void testGetOperationalEnvFilterException2() throws ParseException {
 
 296         InfraActiveRequests iar = new InfraActiveRequests();
 
 297         iar.setRequestId("requestIdFilterException2");
 
 298         iar.setRequestScope("requestScope");
 
 299         iar.setRequestType("requestType");
 
 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"));