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","", ""));
52 List<Vnf> vnf = Collections.singletonList(new Vnf("2","2","vnf"));
53 SDCCatalogService sdcService = newSDCService(serviceTemplate, vnf);
55 List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
56 AAIService aaiService = newAAIService(vim);
58 PackageDistributionService service = new DefaultPackageDistributionService(sdcService, null);
60 Assert.assertThat(service.retrievePackageInfo(), equalTo(new VfNsPackageInfo(serviceTemplate, vnf)));
63 private AAIService newAAIService(List<VimInfo> vim) {
64 AAIService aaiService = mock(AAIService.class);
65 VimInfoRsp rsp = new VimInfoRsp();
66 rsp.setCloudRegion(vim);
67 Call<VimInfoRsp> vimCall = successfulCall(rsp);
68 when(aaiService.listVimInfo()).thenReturn(vimCall);
72 private SDCCatalogService newSDCService(List<SDCServiceTemplate> serviceTemplate, List<Vnf> vnf) {
73 SDCCatalogService sdcService = mock(SDCCatalogService.class);
75 Call<List<SDCServiceTemplate>> serviceCall = successfulCall(serviceTemplate);
76 when(sdcService.listServices(CATEGORY_NS, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(serviceCall);
78 Call<List<Vnf>> vnfCall = successfulCall(vnf);
79 when(sdcService.listResources(RESOURCETYPE_VF, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(vnfCall);
83 @Test(expected = SDCCatalogException.class)
84 public void retrievePackageWillThrowExceptionWhenSDCIsNotAvailable() {
85 SDCCatalogService sdcService = mock(SDCCatalogService.class);
86 Call<List<SDCServiceTemplate>> serviceCall = failedCall("SDC is not available!");
87 when(sdcService.listServices(CATEGORY_NS, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(serviceCall);
89 List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
90 AAIService aaiService = newAAIService(vim);
92 PackageDistributionService service = new DefaultPackageDistributionService(sdcService, null);
93 service.retrievePackageInfo();
97 public void itCanPostNsPackageToVFC() {
98 VfcService vfcService = mock(VfcService.class);
99 Csar csar = new Csar();
100 DistributionResult result = new DistributionResult("status", "description", "errorcode");
101 when(vfcService.distributeNsPackage(csar)).thenReturn(successfulCall(result));
102 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
104 Assert.assertSame(result, service.postNsPackage(csar));
107 @Test(expected = VfcException.class)
108 public void postNsPackageWillThrowExceptionWhenVFCIsNotAvailable() {
109 VfcService vfcService = mock(VfcService.class);
110 Csar csar = new Csar();
111 when(vfcService.distributeNsPackage(csar)).thenReturn(failedCall("VFC is not available!"));
112 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
113 service.postNsPackage(csar);
117 public void itCanPostVnfPackageToVFC() {
118 VfcService vfcService = mock(VfcService.class);
119 Csar csar = new Csar();
121 when(vfcService.distributeVnfPackage(csar)).thenReturn(successfulCall(job));
122 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
124 Assert.assertSame(job, service.postVfPackage(csar));
127 @Test(expected = VfcException.class)
128 public void postVnfPackageWillThrowExceptionWhenVFCIsNotAvailable() {
129 VfcService vfcService = mock(VfcService.class);
130 Csar csar = new Csar();
131 when(vfcService.distributeVnfPackage(csar)).thenReturn(failedCall("VFC is not available!"));
132 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
133 service.postVfPackage(csar);
137 public void itCanGetJobStatusFromVFC() {
138 VfcService vfcService = mock(VfcService.class);
140 JobStatus jobStatus = new JobStatus();
141 when(vfcService.getJobStatus(jobId)).thenReturn(successfulCall(jobStatus));
142 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
144 Assert.assertSame(jobStatus, service.getJobStatus(jobId));
147 @Test(expected = VfcException.class)
148 public void getJobStatusWillThrowExceptionWhenVFCIsNotAvailable() {
149 VfcService vfcService = mock(VfcService.class);
151 when(vfcService.getJobStatus(jobId)).thenReturn(failedCall("VFC is not available!"));
152 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
153 service.getJobStatus(jobId);