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 com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
 
  31 import static org.hamcrest.MatcherAssert.assertThat;
 
  32 import java.util.UUID;
 
  33 import javax.ws.rs.core.HttpHeaders;
 
  34 import javax.ws.rs.core.MediaType;
 
  35 import org.apache.http.HttpStatus;
 
  36 import org.junit.Test;
 
  37 import org.onap.so.apihandler.common.ErrorNumbers;
 
  38 import org.onap.so.apihandlerinfra.BaseTest;
 
  39 import org.onap.so.apihandlerinfra.exceptions.ApiException;
 
  40 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
 
  41 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
 
  42 import org.onap.so.apihandlerinfra.tenantisolation.CloudOrchestrationRequest;
 
  43 import org.onap.so.apihandlerinfra.tenantisolationbeans.OperationalEnvironment;
 
  44 import org.onap.so.apihandlerinfra.tenantisolationbeans.RequestDetails;
 
  45 import org.onap.so.apihandlerinfra.tenantisolationbeans.RequestInfo;
 
  46 import org.onap.so.apihandlerinfra.tenantisolationbeans.RequestParameters;
 
  47 import org.onap.aaiclient.client.aai.AAIVersion;
 
  48 import org.onap.so.db.request.beans.InfraActiveRequests;
 
  49 import org.onap.logging.filter.base.ErrorCode;
 
  50 import org.onap.so.logger.MessageEnum;
 
  51 import org.springframework.beans.factory.annotation.Autowired;
 
  52 import com.fasterxml.jackson.core.JsonProcessingException;
 
  53 import com.fasterxml.jackson.databind.ObjectMapper;
 
  56 public class CreateEcompOperationalEnvironmentTest extends BaseTest {
 
  59     private CreateEcompOperationalEnvironment createEcompOpEn;
 
  60     private final ObjectMapper mapper = new ObjectMapper();
 
  62     public CloudOrchestrationRequest getCloudOrchestrationRequest() {
 
  63         CloudOrchestrationRequest request = new CloudOrchestrationRequest();
 
  64         RequestDetails reqDetails = new RequestDetails();
 
  65         RequestInfo reqInfo = new RequestInfo();
 
  66         RequestParameters reqParams = new RequestParameters();
 
  67         reqParams.setTenantContext("TEST");
 
  68         reqParams.setWorkloadContext("ECOMP_TEST");
 
  69         reqParams.setOperationalEnvironmentType(OperationalEnvironment.ECOMP);
 
  70         reqInfo.setInstanceName("TEST_ECOMP_ENVIRONMENT");
 
  71         reqDetails.setRequestInfo(reqInfo);
 
  72         reqDetails.setRequestParameters(reqParams);
 
  73         request.setRequestDetails(reqDetails);
 
  74         request.setOperationalEnvironmentId("operationalEnvId");
 
  80     public void testProcess() throws ApiException, JsonProcessingException {
 
  81         wireMockServer.stubFor(
 
  82                 put(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
 
  83                         .willReturn(aResponse().withHeader("Content-Type", "application/json")
 
  84                                 .withStatus(HttpStatus.SC_ACCEPTED)));
 
  85         wireMockServer.stubFor(post(urlPathMatching("/events/.*")).willReturn(
 
  86                 aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
 
  88         InfraActiveRequests iar = new InfraActiveRequests();
 
  89         iar.setRequestId("123");
 
  90         iar.setOperationalEnvName("myOpEnv");
 
  91         iar.setRequestScope("create");
 
  92         iar.setRequestStatus("PENDING");
 
  93         iar.setRequestAction("UNKNOWN");
 
  94         wireMockServer.stubFor(get(urlPathEqualTo("/infraActiveRequests/123"))
 
  95                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
 
  96                         .withBody(mapper.writeValueAsString(iar)).withStatus(HttpStatus.SC_OK)));
 
  97         wireMockServer.stubFor(post(urlPathEqualTo("/infraActiveRequests/")).withRequestBody(containing(
 
  98                 "{\"requestId\":\"123\",\"requestStatus\":\"COMPLETE\",\"statusMessage\":\"SUCCESSFUL, operationalEnvironmentId - operationalEnvId; Success Message: SUCCESSFULLY Created ECOMP OperationalEnvironment.\",\"progress\":100"))
 
  99                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
 
 100                         .withStatus(HttpStatus.SC_OK)));
 
 102         createEcompOpEn.execute("123", getCloudOrchestrationRequest());
 
 106     public void testProcessException() throws JsonProcessingException {
 
 107         wireMockServer.stubFor(
 
 108                 put(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
 
 109                         .willReturn(aResponse().withHeader("Content-Type", "application/json")
 
 110                                 .withStatus(HttpStatus.SC_ACCEPTED)));
 
 111         wireMockServer.stubFor(post(urlPathMatching("/events/.*")).willReturn(
 
 112                 aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_NOT_FOUND)));
 
 113         ErrorLoggerInfo errorLoggerInfo =
 
 114                 new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.DataError).build();
 
 115         ValidateException expectedException = new ValidateException.Builder("Could not publish DMaap",
 
 116                 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(errorLoggerInfo).build();
 
 118         InfraActiveRequests iar = new InfraActiveRequests();
 
 119         String uuid = UUID.randomUUID().toString();
 
 120         iar.setRequestId(uuid);
 
 121         iar.setOperationalEnvName("myOpEnv");
 
 122         iar.setRequestScope("create");
 
 123         iar.setRequestStatus("PENDING");
 
 124         iar.setRequestAction("UNKNOWN");
 
 125         wireMockServer.stubFor(get(urlPathEqualTo("/infraActiveRequests/" + uuid))
 
 126                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
 
 127                         .withBody(mapper.writeValueAsString(iar)).withStatus(HttpStatus.SC_OK)));
 
 128         wireMockServer.stubFor(post(urlPathEqualTo("/infraActiveRequests/"))
 
 129                 .withRequestBody(containing("{\"requestId\":\"" + uuid
 
 130                         + "\",\"requestStatus\":\"FAILED\",\"statusMessage\":\"FAILURE, operationalEnvironmentId - operationalEnvId; Error message:"))
 
 131                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
 
 132                         .withStatus(HttpStatus.SC_OK)));
 
 135             createEcompOpEn.execute(uuid, getCloudOrchestrationRequest());
 
 136         } catch (ApiException e) {
 
 137             assertThat(e, sameBeanAs((ApiException) expectedException).ignoring("cause"));