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.aai.bean.VimInfoRsp;
25 import org.onap.usecaseui.server.service.lcm.domain.sdc.SDCCatalogService;
26 import org.onap.usecaseui.server.service.lcm.domain.sdc.bean.SDCServiceTemplate;
27 import org.onap.usecaseui.server.service.lcm.domain.sdc.bean.Vnf;
28 import org.onap.usecaseui.server.service.lcm.domain.sdc.exceptions.SDCCatalogException;
29 import org.onap.usecaseui.server.service.lcm.domain.vfc.VfcService;
30 import org.onap.usecaseui.server.service.lcm.domain.vfc.beans.Csar;
31 import org.onap.usecaseui.server.service.lcm.domain.vfc.beans.DistributionResult;
32 import org.onap.usecaseui.server.service.lcm.domain.vfc.beans.Job;
33 import org.onap.usecaseui.server.service.lcm.domain.vfc.beans.JobStatus;
34 import org.onap.usecaseui.server.service.lcm.domain.vfc.exceptions.VfcException;
35 import retrofit2.Call;
37 import java.util.Collections;
38 import java.util.List;
40 import static org.hamcrest.CoreMatchers.equalTo;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.when;
43 import static org.onap.usecaseui.server.service.lcm.domain.sdc.consts.SDCConsts.*;
44 import static org.onap.usecaseui.server.util.CallStub.failedCall;
45 import static org.onap.usecaseui.server.util.CallStub.successfulCall;
47 public class DefaultPackageDistributionServiceTest {
50 public void itCanRetrievePackageFromSDCAndAAI() {
51 List<SDCServiceTemplate> serviceTemplate = Collections.singletonList(new SDCServiceTemplate("1", "1", "service", "V1","", ""));
53 o.setInvariantUUID("2");
56 List<Vnf> vnf = Collections.singletonList(o);
57 SDCCatalogService sdcService = newSDCService(serviceTemplate, vnf);
59 List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
60 AAIService aaiService = newAAIService(vim);
62 PackageDistributionService service = new DefaultPackageDistributionService(sdcService, null);
64 Assert.assertThat(service.retrievePackageInfo(), equalTo(new VfNsPackageInfo(serviceTemplate, vnf)));
67 private AAIService newAAIService(List<VimInfo> vim) {
68 AAIService aaiService = mock(AAIService.class);
69 VimInfoRsp rsp = new VimInfoRsp();
70 rsp.setCloudRegion(vim);
71 Call<VimInfoRsp> vimCall = successfulCall(rsp);
72 when(aaiService.listVimInfo()).thenReturn(vimCall);
76 private SDCCatalogService newSDCService(List<SDCServiceTemplate> serviceTemplate, List<Vnf> vnf) {
77 SDCCatalogService sdcService = mock(SDCCatalogService.class);
79 Call<List<SDCServiceTemplate>> serviceCall = successfulCall(serviceTemplate);
80 when(sdcService.listServices(CATEGORY_NS, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(serviceCall);
82 Call<List<Vnf>> vnfCall = successfulCall(vnf);
83 when(sdcService.listResources(RESOURCETYPE_VF)).thenReturn(vnfCall);
87 @Test(expected = SDCCatalogException.class)
88 public void retrievePackageWillThrowExceptionWhenSDCIsNotAvailable() {
89 SDCCatalogService sdcService = mock(SDCCatalogService.class);
90 Call<List<SDCServiceTemplate>> serviceCall = failedCall("SDC is not available!");
91 when(sdcService.listServices(CATEGORY_NS, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(serviceCall);
93 List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
94 AAIService aaiService = newAAIService(vim);
96 PackageDistributionService service = new DefaultPackageDistributionService(sdcService, null);
97 service.retrievePackageInfo();
101 public void itCanPostNsPackageToVFC() {
102 VfcService vfcService = mock(VfcService.class);
103 Csar csar = new Csar();
104 DistributionResult result = new DistributionResult("status", "description", "errorcode");
105 when(vfcService.distributeNsPackage(csar)).thenReturn(successfulCall(result));
106 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
108 Assert.assertSame(result, service.postNsPackage(csar));
111 @Test(expected = VfcException.class)
112 public void postNsPackageWillThrowExceptionWhenVFCIsNotAvailable() {
113 VfcService vfcService = mock(VfcService.class);
114 Csar csar = new Csar();
115 when(vfcService.distributeNsPackage(csar)).thenReturn(failedCall("VFC is not available!"));
116 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
117 service.postNsPackage(csar);
121 public void itCanPostVnfPackageToVFC() {
122 VfcService vfcService = mock(VfcService.class);
123 Csar csar = new Csar();
125 when(vfcService.distributeVnfPackage(csar)).thenReturn(successfulCall(job));
126 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
128 Assert.assertSame(job, service.postVfPackage(csar));
131 @Test(expected = VfcException.class)
132 public void postVnfPackageWillThrowExceptionWhenVFCIsNotAvailable() {
133 VfcService vfcService = mock(VfcService.class);
134 Csar csar = new Csar();
135 when(vfcService.distributeVnfPackage(csar)).thenReturn(failedCall("VFC is not available!"));
136 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
137 service.postVfPackage(csar);
141 public void itCanGetJobStatusFromVFC() {
142 VfcService vfcService = mock(VfcService.class);
144 JobStatus jobStatus = new JobStatus();
145 when(vfcService.getJobStatus(jobId)).thenReturn(successfulCall(jobStatus));
146 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
148 Assert.assertSame(jobStatus, service.getJobStatus(jobId));
151 @Test(expected = VfcException.class)
152 public void getJobStatusWillThrowExceptionWhenVFCIsNotAvailable() {
153 VfcService vfcService = mock(VfcService.class);
155 when(vfcService.getJobStatus(jobId)).thenReturn(failedCall("VFC is not available!"));
156 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
157 service.getJobStatus(jobId);