2  * Copyright 2016-2017 ZTE 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.usecaseui.server.service.lcm.impl;
 
  18 import org.junit.Assert;
 
  19 import org.junit.Test;
 
  20 import org.onap.usecaseui.server.bean.lcm.VfNsPackageInfo;
 
  21 import org.onap.usecaseui.server.service.lcm.PackageDistributionService;
 
  22 import org.onap.usecaseui.server.service.lcm.domain.aai.AAIService;
 
  23 import org.onap.usecaseui.server.service.lcm.domain.aai.bean.VimInfo;
 
  24 import org.onap.usecaseui.server.service.lcm.domain.sdc.SDCCatalogService;
 
  25 import org.onap.usecaseui.server.service.lcm.domain.sdc.bean.SDCServiceTemplate;
 
  26 import org.onap.usecaseui.server.service.lcm.domain.sdc.bean.Vnf;
 
  27 import org.onap.usecaseui.server.service.lcm.domain.sdc.exceptions.SDCCatalogException;
 
  28 import org.onap.usecaseui.server.service.lcm.domain.vfc.VfcService;
 
  29 import org.onap.usecaseui.server.service.lcm.domain.vfc.beans.Csar;
 
  30 import org.onap.usecaseui.server.service.lcm.domain.vfc.beans.DistributionResult;
 
  31 import org.onap.usecaseui.server.service.lcm.domain.vfc.beans.Job;
 
  32 import org.onap.usecaseui.server.service.lcm.domain.vfc.beans.JobStatus;
 
  33 import org.onap.usecaseui.server.service.lcm.domain.vfc.exceptions.VfcException;
 
  34 import retrofit2.Call;
 
  36 import java.util.Collections;
 
  37 import java.util.List;
 
  39 import static org.hamcrest.CoreMatchers.equalTo;
 
  40 import static org.mockito.Mockito.mock;
 
  41 import static org.mockito.Mockito.when;
 
  42 import static org.onap.usecaseui.server.service.lcm.domain.sdc.consts.SDCConsts.*;
 
  43 import static org.onap.usecaseui.server.util.CallStub.failedCall;
 
  44 import static org.onap.usecaseui.server.util.CallStub.successfulCall;
 
  46 public class DefaultPackageDistributionServiceTest {
 
  49     public void itCanRetrievePackageFromSDCAndAAI() {
 
  50         List<SDCServiceTemplate> serviceTemplate = Collections.singletonList(new SDCServiceTemplate("1", "1", "service", "", ""));
 
  51         List<Vnf> vnf = Collections.singletonList(new Vnf("2","2","vnf"));
 
  52         SDCCatalogService sdcService = newSDCService(serviceTemplate, vnf);
 
  54         List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
 
  55         AAIService aaiService = newAAIService(vim);
 
  57         PackageDistributionService service = new DefaultPackageDistributionService(sdcService,aaiService, null);
 
  59         Assert.assertThat(service.retrievePackageInfo(), equalTo(new VfNsPackageInfo(serviceTemplate, vnf, vim)));
 
  62     private AAIService newAAIService(List<VimInfo> vim) {
 
  63         AAIService aaiService = mock(AAIService.class);
 
  65         Call<List<VimInfo>> vimCall = successfulCall(vim);
 
  66         when(aaiService.listVimInfo()).thenReturn(vimCall);
 
  70     private SDCCatalogService newSDCService(List<SDCServiceTemplate> serviceTemplate, List<Vnf> vnf) {
 
  71         SDCCatalogService sdcService = mock(SDCCatalogService.class);
 
  73         Call<List<SDCServiceTemplate>> serviceCall = successfulCall(serviceTemplate);
 
  74         when(sdcService.listServices(CATEGORY_NS, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(serviceCall);
 
  76         Call<List<Vnf>> vnfCall = successfulCall(vnf);
 
  77         when(sdcService.listResources(RESOURCETYPE_VF, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(vnfCall);
 
  81     @Test(expected = SDCCatalogException.class)
 
  82     public void retrievePackageWillThrowExceptionWhenSDCIsNotAvailable() {
 
  83         SDCCatalogService sdcService = mock(SDCCatalogService.class);
 
  84         Call<List<SDCServiceTemplate>> serviceCall = failedCall("SDC is not available!");
 
  85         when(sdcService.listServices(CATEGORY_NS, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(serviceCall);
 
  87         List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
 
  88         AAIService aaiService = newAAIService(vim);
 
  90         PackageDistributionService service = new DefaultPackageDistributionService(sdcService,aaiService, null);
 
  91         service.retrievePackageInfo();
 
  95     public void itCanPostNsPackageToVFC() {
 
  96         VfcService vfcService = mock(VfcService.class);
 
  97         Csar csar = new Csar();
 
  98         DistributionResult result = new DistributionResult("status", "description", "errorcode");
 
  99         when(vfcService.distributeNsPackage(csar)).thenReturn(successfulCall(result));
 
 100         PackageDistributionService service = new DefaultPackageDistributionService(null, null, vfcService);
 
 102         Assert.assertSame(result, service.postNsPackage(csar));
 
 105     @Test(expected = VfcException.class)
 
 106     public void postNsPackageWillThrowExceptionWhenVFCIsNotAvailable() {
 
 107         VfcService vfcService = mock(VfcService.class);
 
 108         Csar csar = new Csar();
 
 109         when(vfcService.distributeNsPackage(csar)).thenReturn(failedCall("VFC is not available!"));
 
 110         PackageDistributionService service = new DefaultPackageDistributionService(null, null, vfcService);
 
 111         service.postNsPackage(csar);
 
 115     public void itCanPostVnfPackageToVFC() {
 
 116         VfcService vfcService = mock(VfcService.class);
 
 117         Csar csar = new Csar();
 
 119         when(vfcService.distributeVnfPackage(csar)).thenReturn(successfulCall(job));
 
 120         PackageDistributionService service = new DefaultPackageDistributionService(null, null, vfcService);
 
 122         Assert.assertSame(job, service.postVfPackage(csar));
 
 125     @Test(expected = VfcException.class)
 
 126     public void postVnfPackageWillThrowExceptionWhenVFCIsNotAvailable() {
 
 127         VfcService vfcService = mock(VfcService.class);
 
 128         Csar csar = new Csar();
 
 129         when(vfcService.distributeVnfPackage(csar)).thenReturn(failedCall("VFC is not available!"));
 
 130         PackageDistributionService service = new DefaultPackageDistributionService(null, null, vfcService);
 
 131         service.postVfPackage(csar);
 
 135     public void itCanGetJobStatusFromVFC() {
 
 136         VfcService vfcService = mock(VfcService.class);
 
 138         JobStatus jobStatus = new JobStatus();
 
 139         when(vfcService.getJobStatus(jobId)).thenReturn(successfulCall(jobStatus));
 
 140         PackageDistributionService service = new DefaultPackageDistributionService(null, null, vfcService);
 
 142         Assert.assertSame(jobStatus, service.getJobStatus(jobId));
 
 145     @Test(expected = VfcException.class)
 
 146     public void getJobStatusWillThrowExceptionWhenVFCIsNotAvailable() {
 
 147         VfcService vfcService = mock(VfcService.class);
 
 149         when(vfcService.getJobStatus(jobId)).thenReturn(failedCall("VFC is not available!"));
 
 150         PackageDistributionService service = new DefaultPackageDistributionService(null, null, vfcService);
 
 151         service.getJobStatus(jobId);