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();
105 result.setStatus("status");
106 result.setStatusDescription("description");
107 result.setErrorCode("errorcode");
108 when(vfcService.distributeNsPackage(csar)).thenReturn(successfulCall(result));
109 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
111 Assert.assertSame(result, service.postNsPackage(csar));
114 @Test(expected = VfcException.class)
115 public void postNsPackageWillThrowExceptionWhenVFCIsNotAvailable() {
116 VfcService vfcService = mock(VfcService.class);
117 Csar csar = new Csar();
118 when(vfcService.distributeNsPackage(csar)).thenReturn(failedCall("VFC is not available!"));
119 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
120 service.postNsPackage(csar);
124 public void itCanPostVnfPackageToVFC() {
125 VfcService vfcService = mock(VfcService.class);
126 Csar csar = new Csar();
128 when(vfcService.distributeVnfPackage(csar)).thenReturn(successfulCall(job));
129 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
131 Assert.assertSame(job, service.postVfPackage(csar));
134 @Test(expected = VfcException.class)
135 public void postVnfPackageWillThrowExceptionWhenVFCIsNotAvailable() {
136 VfcService vfcService = mock(VfcService.class);
137 Csar csar = new Csar();
138 when(vfcService.distributeVnfPackage(csar)).thenReturn(failedCall("VFC is not available!"));
139 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
140 service.postVfPackage(csar);
144 public void itCanGetJobStatusFromVFC() {
145 VfcService vfcService = mock(VfcService.class);
147 String responseId = "1";
148 JobStatus jobStatus = new JobStatus();
149 when(vfcService.getJobStatus(jobId, responseId)).thenReturn(successfulCall(jobStatus));
150 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
152 Assert.assertSame(jobStatus, service.getJobStatus(jobId, responseId));
155 @Test(expected = VfcException.class)
156 public void getJobStatusWillThrowExceptionWhenVFCIsNotAvailable() {
157 VfcService vfcService = mock(VfcService.class);
159 String responseId = "1";
160 when(vfcService.getJobStatus(jobId, responseId)).thenReturn(failedCall("VFC is not available!"));
161 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
162 service.getJobStatus(jobId, responseId);