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.process;
 
  23 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
 
  24 import static com.github.tomakehurst.wiremock.client.WireMock.containing;
 
  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.put;
 
  28 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
 
  29 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
 
  30 import static org.junit.Assert.assertEquals;
 
  32 import java.nio.file.Files;
 
  33 import java.util.List;
 
  34 import java.util.UUID;
 
  35 import javax.ws.rs.core.HttpHeaders;
 
  36 import javax.ws.rs.core.MediaType;
 
  37 import org.apache.http.HttpStatus;
 
  38 import org.junit.Before;
 
  39 import org.junit.Test;
 
  40 import org.onap.aai.domain.yang.OperationalEnvironment;
 
  41 import org.onap.so.apihandlerinfra.BaseTest;
 
  42 import org.onap.so.apihandlerinfra.exceptions.ApiException;
 
  43 import org.onap.so.apihandlerinfra.tenantisolation.CloudOrchestrationRequest;
 
  44 import org.onap.so.client.aai.AAIVersion;
 
  45 import org.onap.so.client.grm.beans.Property;
 
  46 import org.onap.so.client.grm.beans.ServiceEndPointList;
 
  47 import org.onap.so.db.request.beans.InfraActiveRequests;
 
  48 import org.springframework.beans.factory.annotation.Autowired;
 
  49 import com.fasterxml.jackson.core.JsonProcessingException;
 
  50 import com.fasterxml.jackson.databind.ObjectMapper;
 
  53 public class CreateVnfOperationalEnvironmentTest extends BaseTest {
 
  55     private CloudOrchestrationRequest request;
 
  56     private ServiceEndPointList serviceEndpoints;
 
  57     private ServiceEndPointList serviceEndpoints2;
 
  60     private CreateVnfOperationalEnvironment createVnfOpEnv;
 
  63     public void testSetUp() throws Exception {
 
  64         ObjectMapper mapper = new ObjectMapper();
 
  65         String jsonRequest = getFileContentsAsString("__files/vnfoperenv/createVnfOperationalEnvironmentRequest.json");
 
  66         request = mapper.readValue(jsonRequest, CloudOrchestrationRequest.class);
 
  67         String jsonServiceEndpoints = getFileContentsAsString("__files/vnfoperenv/endpoints.json");
 
  68         serviceEndpoints = mapper.readValue(jsonServiceEndpoints, ServiceEndPointList.class);
 
  69         String jsonServiceEndpoints2 = getFileContentsAsString("__files/vnfoperenv/endpoints2.json");
 
  70         serviceEndpoints2 = mapper.readValue(jsonServiceEndpoints2, ServiceEndPointList.class);
 
  74     public void testGetEcompManagingEnvironmentId() throws Exception {
 
  75         createVnfOpEnv.setRequest(request);
 
  76         assertEquals("ff305d54-75b4-431b-adb2-eb6b9e5ff000", createVnfOpEnv.getEcompManagingEnvironmentId());
 
  80     public void testGetTenantContext() throws Exception {
 
  81         createVnfOpEnv.setRequest(request);
 
  82         assertEquals("Test", createVnfOpEnv.getTenantContext());
 
  86     public void testGetEnvironmentName() {
 
  87         createVnfOpEnv.setRequest(request);
 
  88         List<Property> props = serviceEndpoints.getServiceEndPointList().get(0).getProperties();
 
  89         assertEquals("DEV", createVnfOpEnv.getEnvironmentName(props));
 
  93     public void testGetEnvironmentNameEndpointListBeginWithUpperCase() {
 
  94         createVnfOpEnv.setRequest(request);
 
  95         List<Property> props = serviceEndpoints2.getServiceEndPointList().get(0).getProperties();
 
  96         assertEquals("DEV", createVnfOpEnv.getEnvironmentName(props));
 
 100     public void testBuildServiceNameForVnf() throws Exception {
 
 101         createVnfOpEnv.setRequest(request);
 
 102         assertEquals("Test.VNF_E2E-IST.Inventory", createVnfOpEnv.buildServiceNameForVnf("TEST.ECOMP_PSL.Inventory"));
 
 106     public void testGetSearchKey() {
 
 107         createVnfOpEnv.setRequest(request);
 
 108         OperationalEnvironment ecompEnv = new OperationalEnvironment();
 
 109         ecompEnv.setTenantContext("Test");
 
 110         ecompEnv.setWorkloadContext("ECOMPL_PSL");
 
 111         assertEquals("Test.ECOMPL_PSL.*", createVnfOpEnv.getSearchKey(ecompEnv));
 
 114     public String getFileContentsAsString(String fileName) {
 
 117             ClassLoader classLoader = this.getClass().getClassLoader();
 
 118             File file = new File(classLoader.getResource(fileName).getFile());
 
 119             content = new String(Files.readAllBytes(file.toPath()));
 
 120         } catch (Exception e) {
 
 122             System.out.println("Exception encountered reading " + fileName + ". Error: " + e.getMessage());
 
 128     public void testExecute() throws ApiException, JsonProcessingException {
 
 129         wireMockServer.stubFor(
 
 130                 get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
 
 131                         .willReturn(aResponse().withHeader("Content-Type", "application/json")
 
 132                                 .withBodyFile("vnfoperenv/ecompOperationalEnvironment.json")
 
 133                                 .withStatus(HttpStatus.SC_ACCEPTED)));
 
 134         wireMockServer.stubFor(post(urlPathMatching("/GRMLWPService/v1/serviceEndPoint/findRunning"))
 
 135                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
 
 136                         .withBodyFile("vnfoperenv/endpoints.json").withStatus(HttpStatus.SC_ACCEPTED)));
 
 137         wireMockServer.stubFor(post(urlPathMatching("/GRMLWPService/v1/serviceEndPoint/add")).willReturn(
 
 138                 aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
 
 139         wireMockServer.stubFor(
 
 140                 put(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
 
 141                         .willReturn(aResponse().withHeader("Content-Type", "application/json")
 
 142                                 .withBodyFile("vnfoperenv/ecompOperationalEnvironment.json")
 
 143                                 .withStatus(HttpStatus.SC_ACCEPTED)));
 
 144         String requestId = UUID.randomUUID().toString();
 
 145         InfraActiveRequests iar = new InfraActiveRequests();
 
 146         iar.setRequestId(requestId);
 
 147         iar.setOperationalEnvName("myOpEnv");
 
 148         iar.setRequestScope("create");
 
 149         iar.setRequestStatus("PENDING");
 
 150         iar.setRequestAction("UNKNOWN");
 
 151         ObjectMapper mapper = new ObjectMapper();
 
 152         wireMockServer.stubFor(post(urlPathEqualTo("/infraActiveRequests/"))
 
 153                 .withRequestBody(containing("{\"requestId\":\"" + requestId
 
 154                         + "\",\"requestStatus\":\"COMPLETE\",\"statusMessage\":\"SUCCESSFUL"))
 
 155                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
 
 156                         .withStatus(HttpStatus.SC_OK)));
 
 158         wireMockServer.stubFor(get(urlPathEqualTo("/infraActiveRequests/" + requestId))
 
 159                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
 
 160                         .withBody(mapper.writeValueAsString(iar)).withStatus(HttpStatus.SC_OK)));
 
 161         createVnfOpEnv.execute(requestId, request);
 
 165     public void testExecuteEndpointsListBeginWithUppercase() throws ApiException, JsonProcessingException {
 
 166         wireMockServer.stubFor(
 
 167                 get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
 
 168                         .willReturn(aResponse().withHeader("Content-Type", "application/json")
 
 169                                 .withBodyFile("vnfoperenv/ecompOperationalEnvironment.json")
 
 170                                 .withStatus(HttpStatus.SC_ACCEPTED)));
 
 171         wireMockServer.stubFor(post(urlPathMatching("/GRMLWPService/v1/serviceEndPoint/findRunning"))
 
 172                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
 
 173                         .withBodyFile("vnfoperenv/endpoints2.json").withStatus(HttpStatus.SC_ACCEPTED)));
 
 174         wireMockServer.stubFor(post(urlPathMatching("/GRMLWPService/v1/serviceEndPoint/add")).willReturn(
 
 175                 aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
 
 176         wireMockServer.stubFor(
 
 177                 put(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
 
 178                         .willReturn(aResponse().withHeader("Content-Type", "application/json")
 
 179                                 .withBodyFile("vnfoperenv/ecompOperationalEnvironment.json")
 
 180                                 .withStatus(HttpStatus.SC_ACCEPTED)));
 
 181         String requestId = UUID.randomUUID().toString();
 
 182         InfraActiveRequests iar = new InfraActiveRequests();
 
 183         iar.setRequestId(requestId);
 
 184         iar.setOperationalEnvName("myOpEnv");
 
 185         iar.setRequestScope("create");
 
 186         iar.setRequestStatus("PENDING");
 
 187         iar.setRequestAction("UNKNOWN");
 
 188         ObjectMapper mapper = new ObjectMapper();
 
 189         wireMockServer.stubFor(post(urlPathEqualTo("/infraActiveRequests/"))
 
 190                 .withRequestBody(containing("{\"requestId\":\"" + requestId
 
 191                         + "\",\"requestStatus\":\"COMPLETE\",\"statusMessage\":\"SUCCESSFUL"))
 
 192                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
 
 193                         .withStatus(HttpStatus.SC_OK)));
 
 195         wireMockServer.stubFor(get(urlPathEqualTo("/infraActiveRequests/" + requestId))
 
 196                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
 
 197                         .withBody(mapper.writeValueAsString(iar)).withStatus(HttpStatus.SC_OK)));
 
 198         createVnfOpEnv.execute(requestId, request);