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.equalTo;
 
  25 import static com.github.tomakehurst.wiremock.client.WireMock.get;
 
  26 import static com.github.tomakehurst.wiremock.client.WireMock.post;
 
  27 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
 
  28 import static org.junit.Assert.assertEquals;
 
  29 import static org.junit.Assert.assertTrue;
 
  31 import java.io.IOException;
 
  32 import javax.ws.rs.core.MediaType;
 
  33 import org.apache.http.HttpStatus;
 
  34 import org.junit.Before;
 
  35 import org.junit.Ignore;
 
  36 import org.junit.Test;
 
  37 import org.onap.so.apihandlerinfra.BaseTest;
 
  38 import org.onap.so.apihandlerinfra.tenantisolationbeans.Action;
 
  39 import org.onap.so.apihandlerinfra.tenantisolationbeans.TenantIsolationRequest;
 
  40 import org.onap.so.constants.Status;
 
  41 import org.onap.so.db.request.beans.InfraActiveRequests;
 
  42 import org.springframework.http.HttpEntity;
 
  43 import org.springframework.http.HttpHeaders;
 
  44 import org.springframework.http.HttpMethod;
 
  45 import org.springframework.http.ResponseEntity;
 
  46 import org.springframework.web.util.UriComponentsBuilder;
 
  47 import com.fasterxml.jackson.databind.ObjectMapper;
 
  50 public class CloudOrchestrationTest extends BaseTest {
 
  52     private static final String path = "/onap/so/infra/cloudResources/v1";
 
  54     private HttpHeaders headers = new HttpHeaders();
 
  57     public void setupTestClass() throws Exception {
 
  58         wireMockServer.stubFor(post(urlPathEqualTo(getTestUrl(""))).willReturn(
 
  59                 aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
 
  60                         .withStatus(HttpStatus.SC_CREATED)));
 
  64     public void testCreateOpEnvObjectMapperError() throws IOException {
 
  66         headers.set("Accept", MediaType.APPLICATION_JSON);
 
  67         headers.set("Content-Type", MediaType.APPLICATION_JSON);
 
  68         HttpEntity<String> entity = new HttpEntity<String>(null, headers);
 
  70         UriComponentsBuilder builder =
 
  71                 UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
 
  72         ResponseEntity<String> response =
 
  73                 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
 
  75         String body = response.getBody().toString();
 
  76         assertTrue(body.contains("Mapping of request to JSON object failed."));
 
  77         assertEquals(400, response.getStatusCodeValue());
 
  81     public void testCreateOpEnvError() throws IOException {
 
  84                 "{\"requestDetails\":{\"requestInfo\":{\"resourceType\":\"operationalEnvironment\",\"instanceName\": \"myOpEnv\",\"source\": \"VID\",\"requestorId\": \"xxxxxx\"},"
 
  85                         + "     \"requestParameters\": {\"tenantContext\": \"Test\",\"workloadContext\": \"ECOMP_E2E-IST\"}}}";
 
  86         headers.set("Accept", MediaType.APPLICATION_JSON);
 
  87         headers.set("Content-Type", MediaType.APPLICATION_JSON);
 
  88         HttpEntity<String> entity = new HttpEntity<String>(request, headers);
 
  90         UriComponentsBuilder builder =
 
  91                 UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
 
  93         ResponseEntity<String> response =
 
  94                 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
 
  96         String body = response.getBody().toString();
 
  97         assertTrue(body.contains("Mapping of request to JSON object failed"));
 
  98         assertEquals(400, response.getStatusCodeValue());
 
 102     public void testCreateOpEnvReqRecordDuplicateCheck() throws IOException {
 
 103         wireMockServer.stubFor(post(urlPathEqualTo(getTestUrl("checkInstanceNameDuplicate"))).withRequestBody(equalTo(
 
 104                 "{\"instanceIdMap\":null,\"instanceName\":\"myOpEnv\",\"requestScope\":\"operationalEnvironment\"}"))
 
 105                 .willReturn(aResponse()
 
 106                         .withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
 
 107                         .withBody(String.format(getResponseTemplate, "123", "PENDING")).withStatus(HttpStatus.SC_OK)));
 
 108         ObjectMapper mapper = new ObjectMapper();
 
 109         TenantIsolationRequest request =
 
 110                 mapper.readValue(new File("src/test/resources/TenantIsolation/ECOMPOperationEnvironmentCreate.json"),
 
 111                         TenantIsolationRequest.class);
 
 112         headers.set("Accept", MediaType.APPLICATION_JSON);
 
 113         headers.set("Content-Type", MediaType.APPLICATION_JSON);
 
 114         HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
 
 117         UriComponentsBuilder builder =
 
 118                 UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
 
 120         ResponseEntity<String> response =
 
 121                 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
 
 123         assertEquals(409, response.getStatusCodeValue());
 
 127     public void testCreateOperationalEnvironment() throws IOException {
 
 128         ObjectMapper mapper = new ObjectMapper();
 
 129         TenantIsolationRequest request =
 
 130                 mapper.readValue(new File("src/test/resources/TenantIsolation/ECOMPOperationEnvironmentCreate.json"),
 
 131                         TenantIsolationRequest.class);
 
 132         headers.set("Accept", MediaType.APPLICATION_JSON);
 
 133         headers.set("Content-Type", MediaType.APPLICATION_JSON);
 
 134         headers.set("X-TransactionID", "987654321");
 
 135         HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
 
 137         UriComponentsBuilder builder =
 
 138                 UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
 
 139         wireMockServer.stubFor(post(urlPathEqualTo(getTestUrl("checkInstanceNameDuplicate"))).withRequestBody(equalTo(
 
 140                 "{\"instanceIdMap\":null,\"instanceName\":\"myOpEnv\",\"requestScope\":\"operationalEnvironment\"}"))
 
 142                         aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
 
 143                                 .withStatus(HttpStatus.SC_NOT_FOUND)));
 
 144         ResponseEntity<String> response =
 
 145                 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
 
 146         assertEquals(200, response.getStatusCodeValue());
 
 150     public void testCreateVNFDuplicateCheck() throws IOException {
 
 151         wireMockServer.stubFor(post(urlPathEqualTo(getTestUrl("checkInstanceNameDuplicate"))).withRequestBody(equalTo(
 
 152                 "{\"instanceIdMap\":null,\"instanceName\":\"myVnfOpEnv\",\"requestScope\":\"operationalEnvironment\"}"))
 
 153                 .willReturn(aResponse()
 
 154                         .withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
 
 155                         .withBody(String.format(getResponseTemplate, "requestId", Status.IN_PROGRESS.toString()))
 
 156                         .withStatus(HttpStatus.SC_OK)));
 
 157         ObjectMapper mapper = new ObjectMapper();
 
 158         TenantIsolationRequest request =
 
 159                 mapper.readValue(new File("src/test/resources/TenantIsolation/VNFOperationEnvironmentCreate.json"),
 
 160                         TenantIsolationRequest.class);
 
 161         headers.set("Accept", MediaType.APPLICATION_JSON);
 
 162         headers.set("Content-Type", MediaType.APPLICATION_JSON);
 
 163         HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
 
 165         UriComponentsBuilder builder =
 
 166                 UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
 
 168         ResponseEntity<String> response =
 
 169                 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
 
 170         assertEquals(409, response.getStatusCodeValue());
 
 174     public void testCreateVNF() throws IOException {
 
 175         ObjectMapper mapper = new ObjectMapper();
 
 176         TenantIsolationRequest request =
 
 177                 mapper.readValue(new File("src/test/resources/TenantIsolation/VNFOperationEnvironmentCreate.json"),
 
 178                         TenantIsolationRequest.class);
 
 179         headers.set("Accept", MediaType.APPLICATION_JSON);
 
 180         headers.set("Content-Type", MediaType.APPLICATION_JSON);
 
 181         HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
 
 183         UriComponentsBuilder builder =
 
 184                 UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
 
 185         wireMockServer.stubFor(post(urlPathEqualTo(getTestUrl("checkInstanceNameDuplicate"))).withRequestBody(equalTo(
 
 186                 "{\"instanceIdMap\":null,\"instanceName\":\"myVnfOpEnv\",\"requestScope\":\"operationalEnvironment\"}"))
 
 188                         aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
 
 189                                 .withStatus(HttpStatus.SC_NOT_FOUND)));
 
 191         ResponseEntity<String> response =
 
 192                 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
 
 193         assertEquals(200, response.getStatusCodeValue());
 
 197     public void testActivate() throws IOException {
 
 198         ObjectMapper mapper = new ObjectMapper();
 
 199         TenantIsolationRequest request =
 
 200                 mapper.readValue(new File("src/test/resources/TenantIsolation/ActivateOperationEnvironment.json"),
 
 201                         TenantIsolationRequest.class);
 
 202         headers.set("Accept", MediaType.APPLICATION_JSON);
 
 203         headers.set("Content-Type", MediaType.APPLICATION_JSON);
 
 204         HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
 
 206         UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(
 
 207                 createURLWithPort(path) + "/operationalEnvironments/ff3514e3-5a33-55df-13ab-12abad84e7ff/activate");
 
 208         wireMockServer.stubFor(post(urlPathEqualTo(getTestUrl("checkInstanceNameDuplicate"))).withRequestBody(equalTo(
 
 209                 "{\"instanceIdMap\":{\"operationalEnvironmentId\":\"ff3514e3-5a33-55df-13ab-12abad84e7ff\"},\"instanceName\":\"myVnfOpEnv\",\"requestScope\":\"operationalEnvironment\"}"))
 
 211                         aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
 
 212                                 .withStatus(HttpStatus.SC_NOT_FOUND)));
 
 214         wireMockServer.stubFor(
 
 215                 get(urlPathEqualTo(getTestUrl("checkVnfIdStatus/ff3514e3-5a33-55df-13ab-12abad84e7ff"))).willReturn(
 
 216                         aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
 
 217                                 .withStatus(HttpStatus.SC_OK)));
 
 219         ResponseEntity<String> response =
 
 220                 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
 
 222         assertEquals(200, response.getStatusCodeValue());
 
 226     public void testDeactivate() throws IOException {
 
 227         ObjectMapper mapper = new ObjectMapper();
 
 228         TenantIsolationRequest request =
 
 229                 mapper.readValue(new File("src/test/resources/TenantIsolation/DeactivateOperationEnvironment.json"),
 
 230                         TenantIsolationRequest.class);
 
 232         headers.set("Accept", MediaType.APPLICATION_JSON);
 
 233         headers.set("Content-Type", MediaType.APPLICATION_JSON);
 
 234         HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
 
 237         // wireMockServer.stubFor(post(urlPathEqualTo(getTestUrl("checkInstanceNameDuplicate"))).withRequestBody(equalTo("{\"instanceIdMap\":{\"operationalEnvironmentId\":\"ff3514e3-5a33-55df-13ab-12abad84e7fa\"},\"instanceName\":null,\"requestScope\":\"operationalEnvironment\"}")).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE,
 
 238         // MediaType.APPLICATION_JSON)
 
 239         // .withBodyFile((String.format(getResponseTemplate, "ff3514e3-5a33-55df-13ab-12abad84e7fa",
 
 240         // Status.COMPLETE.toString()))).withStatus(HttpStatus.SC_OK)));
 
 242         wireMockServer.stubFor(
 
 243                 get(urlPathEqualTo(getTestUrl("checkVnfIdStatus/ff3514e3-5a33-55df-13ab-12abad84e7fa"))).willReturn(
 
 244                         aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
 
 245                                 .withStatus(HttpStatus.SC_OK)));
 
 247         UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(
 
 248                 createURLWithPort(path) + "/operationalEnvironments/ff3514e3-5a33-55df-13ab-12abad84e7fa/deactivate");
 
 250         ResponseEntity<String> response =
 
 251                 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
 
 253         assertEquals(200, response.getStatusCodeValue());
 
 259     public void testDeactivateDupCheck() throws IOException {
 
 261         InfraActiveRequests iar = new InfraActiveRequests();
 
 262         iar.setRequestId("requestId");
 
 263         iar.setOperationalEnvName("myVnfOpEnv");
 
 264         iar.setRequestStatus(Status.IN_PROGRESS.toString());
 
 265         iar.setAction(Action.create.toString());
 
 266         iar.setRequestAction(Action.create.toString());
 
 267         iar.setRequestScope("UNKNOWN");
 
 268         // iarRepo.saveAndFlush(iar);
 
 269         ObjectMapper mapper = new ObjectMapper();
 
 270         String request = mapper.readValue(
 
 271                 new File("src/test/resources/TenantIsolation/DeactivateOperationEnvironment.json"), String.class);
 
 273         headers.set("Accept", MediaType.APPLICATION_JSON);
 
 274         headers.set("Content-Type", MediaType.APPLICATION_JSON);
 
 275         HttpEntity<String> entity = new HttpEntity<String>(request, headers);
 
 277         UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(
 
 278                 createURLWithPort(path) + "/operationalEnvironments/ff3514e3-5a33-55df-13ab-12abad84e7ff/deactivate");
 
 280         ResponseEntity<String> response =
 
 281                 restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
 
 282         assertEquals(409, response.getStatusCodeValue());