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.helpers;
 
  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.put;
 
  27 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
 
  28 import static org.junit.Assert.assertEquals;
 
  29 import static org.junit.Assert.fail;
 
  30 import static org.mockito.ArgumentMatchers.any;
 
  31 import static org.mockito.ArgumentMatchers.anyString;
 
  32 import static org.mockito.Mockito.doNothing;
 
  33 import static org.mockito.Mockito.mock;
 
  34 import static org.mockito.Mockito.times;
 
  35 import static org.mockito.Mockito.verify;
 
  36 import java.util.HashMap;
 
  38 import java.util.Optional;
 
  39 import org.apache.http.HttpStatus;
 
  40 import org.junit.Test;
 
  41 import org.onap.aai.domain.yang.OperationalEnvironment;
 
  42 import org.onap.so.apihandlerinfra.BaseTest;
 
  43 import org.onap.so.client.aai.AAIVersion;
 
  44 import org.onap.so.client.aai.entities.AAIResultWrapper;
 
  45 import org.springframework.beans.factory.annotation.Autowired;
 
  49 public class AAIClientHelperTest extends BaseTest {
 
  52     private AAIClientHelper clientHelper;
 
  55     public void testGetAaiOperationalEnvironmentSuccess() throws Exception {
 
  56         wireMockServer.stubFor(
 
  57                 get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
 
  58                         .willReturn(aResponse().withHeader("Content-Type", "application/json")
 
  59                                 .withBodyFile("vnfoperenv/ecompOperationalEnvironment.json")
 
  60                                 .withStatus(HttpStatus.SC_ACCEPTED)));
 
  62         AAIResultWrapper wrapper = clientHelper.getAaiOperationalEnvironment("EMOE-001");
 
  63         Optional<OperationalEnvironment> aaiOpEnv = wrapper.asBean(OperationalEnvironment.class);
 
  64         assertEquals("EMOE-001", aaiOpEnv.get().getOperationalEnvironmentId());
 
  68     public void testUpdateSuccess() {
 
  69         wireMockServer.stubFor(
 
  70                 post(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
 
  71                         .willReturn(aResponse().withHeader("Content-Type", "application/json")
 
  72                                 .withStatus(HttpStatus.SC_ACCEPTED)));
 
  74         OperationalEnvironment ecompEnv = new OperationalEnvironment();
 
  75         ecompEnv.setTenantContext("Test");
 
  76         ecompEnv.setWorkloadContext("ECOMPL_PSL");
 
  79             AAIClientHelper clientHelper = mock(AAIClientHelper.class);
 
  80             doNothing().when(clientHelper).updateAaiOperationalEnvironment(any(String.class),
 
  81                     any(OperationalEnvironment.class));
 
  82             clientHelper.updateAaiOperationalEnvironment("EMOE-001", ecompEnv);
 
  84             verify(clientHelper, times(1)).updateAaiOperationalEnvironment("EMOE-001", ecompEnv);
 
  85         } catch (Exception e) {
 
  86             fail("shouldn't reach here");
 
  91     public void testUpdateMapSuccess() {
 
  92         wireMockServer.stubFor(
 
  93                 post(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
 
  94                         .willReturn(aResponse().withHeader("Content-Type", "application/json")
 
  95                                 .withStatus(HttpStatus.SC_ACCEPTED)));
 
  97         Map<String, String> payload = new HashMap<String, String>();
 
  98         payload.put("tenant-context", "Test");
 
  99         payload.put("workload-context", "ECOMPL_PSL");
 
 100         payload.put("operational-environment-status", "ACTIVE");
 
 103             AAIClientHelper clientHelper = mock(AAIClientHelper.class);
 
 104             doNothing().when(clientHelper).updateAaiOperationalEnvironment("EMOE-001", payload);
 
 105             clientHelper.updateAaiOperationalEnvironment("EMOE-001", payload);
 
 107             verify(clientHelper, times(1)).updateAaiOperationalEnvironment("EMOE-001", payload);
 
 108         } catch (Exception e) {
 
 109             fail("shouldn't reach here");
 
 114     public void testCreateSuccess() {
 
 115         wireMockServer.stubFor(
 
 116                 put(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
 
 117                         .willReturn(aResponse().withHeader("Content-Type", "application/json")
 
 118                                 .withStatus(HttpStatus.SC_ACCEPTED)));
 
 120         OperationalEnvironment ecompEnv = new OperationalEnvironment();
 
 121         ecompEnv.setOperationalEnvironmentId("opeEvnId");
 
 122         ecompEnv.setTenantContext("Test");
 
 123         ecompEnv.setWorkloadContext("ECOMPL_PSL");
 
 126             AAIClientHelper clientHelper = mock(AAIClientHelper.class);
 
 127             doNothing().when(clientHelper).createOperationalEnvironment(any(OperationalEnvironment.class));
 
 128             clientHelper.createOperationalEnvironment(ecompEnv);
 
 130             verify(clientHelper, times(1)).createOperationalEnvironment(ecompEnv);
 
 131         } catch (Exception e) {
 
 132             fail("shouldn't reach here");
 
 137     public void testcreateRelationshipSuccess() {
 
 138         wireMockServer.stubFor(
 
 139                 put(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
 
 140                         .willReturn(aResponse().withHeader("Content-Type", "application/json")
 
 141                                 .withStatus(HttpStatus.SC_ACCEPTED)));
 
 143         OperationalEnvironment ecompEnv = new OperationalEnvironment();
 
 144         ecompEnv.setTenantContext("Test");
 
 145         ecompEnv.setWorkloadContext("ECOMPL_PSL");
 
 148             AAIClientHelper clientHelper = mock(AAIClientHelper.class);
 
 149             doNothing().when(clientHelper).createRelationship(anyString(), anyString());
 
 150             clientHelper.createRelationship("managingEcomp", "vnfOp");
 
 152             verify(clientHelper, times(1)).createRelationship("managingEcomp", "vnfOp");
 
 153         } catch (Exception e) {
 
 154             fail("shouldn't reach here");