2  * Copyright 2016-2017, Nokia Corporation
 
   4  * Licensed under the Apache License, Version 2.0 (the "License");
 
   5  * you may not use this file except in compliance with the License.
 
   6  * You may obtain a copy of the License at
 
   8  *     http://www.apache.org/licenses/LICENSE-2.0
 
  10  * Unless required by applicable law or agreed to in writing, software
 
  11  * distributed under the License is distributed on an "AS IS" BASIS,
 
  12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  13  * See the License for the specific language governing permissions and
 
  14  * limitations under the License.
 
  16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.notification;
 
  18 import com.nokia.cbam.lcm.v32.model.AffectedVnfc;
 
  19 import com.nokia.cbam.lcm.v32.model.ResourceHandle;
 
  20 import io.reactivex.Observable;
 
  21 import java.util.ArrayList;
 
  22 import java.util.NoSuchElementException;
 
  23 import org.junit.Before;
 
  24 import org.junit.Test;
 
  25 import org.mockito.ArgumentCaptor;
 
  26 import org.mockito.Mock;
 
  27 import org.onap.aai.api.NetworkApi;
 
  28 import org.onap.aai.model.Vnfc;
 
  29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider;
 
  30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
 
  32 import static junit.framework.TestCase.assertEquals;
 
  33 import static org.mockito.Matchers.eq;
 
  34 import static org.mockito.Mockito.verify;
 
  35 import static org.mockito.Mockito.when;
 
  36 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.notification.AbstractManager.buildRelationshipData;
 
  37 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.notification.TestGenericVnfManager.assertRelation;
 
  38 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager.getCloudOwner;
 
  39 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager.getRegionName;
 
  40 import static org.springframework.test.util.ReflectionTestUtils.setField;
 
  42 public class TestVnfcManager extends TestBase {
 
  43     private ArgumentCaptor<Vnfc> payload = ArgumentCaptor.forClass(Vnfc.class);
 
  46     private AAIRestApiProvider aaiRestApiProvider;
 
  47     private VnfcManager vnfcManager;
 
  49     private NetworkApi networkApi;
 
  53         vnfcManager = new VnfcManager(aaiRestApiProvider, cbamRestApiProvider, driverProperties);
 
  54         setField(VnfcManager.class, "logger", logger);
 
  55         when(aaiRestApiProvider.getNetworkApi()).thenReturn(networkApi);
 
  63     public void testCreate() throws Exception {
 
  64         AffectedVnfc affectedVnfc = new AffectedVnfc();
 
  65         affectedVnfc.setComputeResource(new ResourceHandle());
 
  66         affectedVnfc.getComputeResource().setResourceId("serverProviderId");
 
  67         affectedVnfc.setId("vnfcId");
 
  68         when(networkApi.getNetworkVnfcsVnfc("myVnfId_vnfcId", null, null, null, null, null, null, null, null, null)).thenReturn(Observable.error(new NoSuchElementException()));
 
  69         when(networkApi.createOrUpdateNetworkVnfcsVnfc(eq("myVnfId_vnfcId"), payload.capture())).thenReturn(VOID_OBSERVABLE.value());
 
  71         vnfcManager.update(VIM_ID, "myTenantPrivderId", VNF_ID, affectedVnfc, true);
 
  73         Vnfc vnfc = payload.getValue();
 
  74         assertEquals("myVnfId_vnfcId", vnfc.getVnfcName());
 
  75         assertEquals("vnfcId", vnfc.getNfcFunction());
 
  76         assertEquals("vnfcId", vnfc.getNfcNamingCode());
 
  77         assertRelation(payload.getValue().getRelationshipList(), "generic-vnf", buildRelationshipData("generic-vnf.vnf-id", VNF_ID));
 
  79         assertRelation(vnfc.getRelationshipList(), "vserver",
 
  80                 buildRelationshipData("cloud-region.cloud-owner", getCloudOwner(VIM_ID)),
 
  81                 buildRelationshipData("cloud-region.cloud-region-id", getRegionName(VIM_ID)),
 
  82                 buildRelationshipData("tenant.tenant-id", "myTenantPrivderId"),
 
  83                 buildRelationshipData("vserver.vserver-id", "serverProviderId"));
 
  84         assertEquals(2, vnfc.getRelationshipList().size());
 
  85         VOID_OBSERVABLE.assertCalled();
 
  92     public void testUpdate() throws Exception {
 
  93         AffectedVnfc affectedVnfc = new AffectedVnfc();
 
  94         affectedVnfc.setComputeResource(new ResourceHandle());
 
  95         affectedVnfc.getComputeResource().setResourceId("serverProviderId");
 
  96         affectedVnfc.setId("vnfcId");
 
  97         Vnfc existingVnfc = new Vnfc();
 
  98         existingVnfc.setRelationshipList(new ArrayList<>());
 
  99         existingVnfc.getRelationshipList().add(GenericVnfManager.linkTo("any"));
 
 100         when(networkApi.getNetworkVnfcsVnfc("myVnfId_vnfcId", null, null, null, null, null, null, null, null, null)).thenReturn(buildObservable(existingVnfc));
 
 101         when(networkApi.createOrUpdateNetworkVnfcsVnfc(eq("myVnfId_vnfcId"), payload.capture())).thenReturn(VOID_OBSERVABLE.value());
 
 103         vnfcManager.update(VIM_ID, "myTenantPrivderId", VNF_ID, affectedVnfc, true);
 
 105         Vnfc vnfc = payload.getValue();
 
 106         assertEquals("myVnfId_vnfcId", vnfc.getVnfcName());
 
 107         assertEquals("vnfcId", vnfc.getNfcFunction());
 
 108         assertEquals("vnfcId", vnfc.getNfcNamingCode());
 
 109         assertRelation(payload.getValue().getRelationshipList(), "generic-vnf", buildRelationshipData("generic-vnf.vnf-id", VNF_ID));
 
 111         assertRelation(vnfc.getRelationshipList(), "vserver",
 
 112                 buildRelationshipData("cloud-region.cloud-owner", getCloudOwner(VIM_ID)),
 
 113                 buildRelationshipData("cloud-region.cloud-region-id", getRegionName(VIM_ID)),
 
 114                 buildRelationshipData("tenant.tenant-id", "myTenantPrivderId"),
 
 115                 buildRelationshipData("vserver.vserver-id", "serverProviderId"));
 
 116         assertEquals(2, vnfc.getRelationshipList().size());
 
 117         VOID_OBSERVABLE.assertCalled();
 
 124     public void testDelete() throws Exception {
 
 125         AffectedVnfc affectedVnfc = new AffectedVnfc();
 
 126         affectedVnfc.setComputeResource(new ResourceHandle());
 
 127         affectedVnfc.getComputeResource().setResourceId("serverProviderId");
 
 128         affectedVnfc.setId("vnfcId");
 
 129         Vnfc existingVnfc = new Vnfc();
 
 130         existingVnfc.setResourceVersion("v3");
 
 131         existingVnfc.setVnfcName("myVnfId_vnfcId");
 
 132         when(networkApi.getNetworkVnfcsVnfc("myVnfId_vnfcId", null, null, null, null, null, null, null, null, null)).thenReturn(buildObservable(existingVnfc));
 
 133         when(networkApi.deleteNetworkVnfcsVnfc("myVnfId_vnfcId", "v3")).thenReturn(VOID_OBSERVABLE.value());
 
 135         vnfcManager.delete(VNF_ID, affectedVnfc);
 
 137         verify(networkApi).deleteNetworkVnfcsVnfc("myVnfId_vnfcId", "v3");
 
 138         VOID_OBSERVABLE.assertCalled();
 
 145     public void testInheritence() {
 
 146         assertEquals(logger, vnfcManager.getLogger());