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.emptyBodyCall;
45 import static org.onap.usecaseui.server.util.CallStub.failedCall;
46 import static org.onap.usecaseui.server.util.CallStub.successfulCall;
48 public class DefaultPackageDistributionServiceTest {
51 public void itCanRetrievePackageFromSDCAndAAI() {
52 List<SDCServiceTemplate> serviceTemplate = Collections.singletonList(new SDCServiceTemplate("1", "1", "service", "V1","", ""));
54 o.setInvariantUUID("2");
57 List<Vnf> vnf = Collections.singletonList(o);
58 SDCCatalogService sdcService = newSDCService(serviceTemplate, vnf);
60 List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
61 AAIService aaiService = newAAIService(vim);
63 PackageDistributionService service = new DefaultPackageDistributionService(sdcService, null);
65 Assert.assertThat(service.retrievePackageInfo(), equalTo(new VfNsPackageInfo(serviceTemplate, vnf)));
68 private AAIService newAAIService(List<VimInfo> vim) {
69 AAIService aaiService = mock(AAIService.class);
70 VimInfoRsp rsp = new VimInfoRsp();
71 rsp.setCloudRegion(vim);
72 Call<VimInfoRsp> vimCall = successfulCall(rsp);
73 when(aaiService.listVimInfo()).thenReturn(vimCall);
77 private SDCCatalogService newSDCService(List<SDCServiceTemplate> serviceTemplate, List<Vnf> vnf) {
78 SDCCatalogService sdcService = mock(SDCCatalogService.class);
80 Call<List<SDCServiceTemplate>> serviceCall = successfulCall(serviceTemplate);
81 when(sdcService.listServices(CATEGORY_NS, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(serviceCall);
83 Call<List<Vnf>> vnfCall = successfulCall(vnf);
84 when(sdcService.listResources(RESOURCETYPE_VF)).thenReturn(vnfCall);
88 @Test(expected = SDCCatalogException.class)
89 public void retrievePackageWillThrowExceptionWhenSDCIsNotAvailable() {
90 SDCCatalogService sdcService = mock(SDCCatalogService.class);
91 Call<List<SDCServiceTemplate>> serviceCall = failedCall("SDC is not available!");
92 when(sdcService.listServices(CATEGORY_NS, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(serviceCall);
94 List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
95 AAIService aaiService = newAAIService(vim);
97 PackageDistributionService service = new DefaultPackageDistributionService(sdcService, null);
98 service.retrievePackageInfo();
102 public void retrievePackageWillBeEmptyWhenNoNsServiceAndVfInSDC() {
103 SDCCatalogService sdcService = mock(SDCCatalogService.class);
104 Call<List<SDCServiceTemplate>> serviceCall = emptyBodyCall();
105 when(sdcService.listServices(CATEGORY_NS, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(serviceCall);
107 Call<List<Vnf>> resourceCall = emptyBodyCall();
108 when(sdcService.listResources(RESOURCETYPE_VF)).thenReturn(resourceCall);
110 PackageDistributionService service = new DefaultPackageDistributionService(sdcService, null);
111 VfNsPackageInfo vfNsPackageInfo = service.retrievePackageInfo();
113 Assert.assertTrue("ns should be empty!", vfNsPackageInfo.getNsPackage().isEmpty());
114 Assert.assertTrue("vf should be empty!", vfNsPackageInfo.getVnfPackages().isEmpty());
118 public void itCanPostNsPackageToVFC() {
119 VfcService vfcService = mock(VfcService.class);
120 Csar csar = new Csar();
121 DistributionResult result = new DistributionResult();
122 result.setStatus("status");
123 result.setStatusDescription("description");
124 result.setErrorCode("errorcode");
125 when(vfcService.distributeNsPackage(csar)).thenReturn(successfulCall(result));
126 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
128 Assert.assertSame(result, service.postNsPackage(csar));
131 @Test(expected = VfcException.class)
132 public void postNsPackageWillThrowExceptionWhenVFCIsNotAvailable() {
133 VfcService vfcService = mock(VfcService.class);
134 Csar csar = new Csar();
135 when(vfcService.distributeNsPackage(csar)).thenReturn(failedCall("VFC is not available!"));
136 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
137 service.postNsPackage(csar);
140 @Test(expected = VfcException.class)
141 public void postNsPackageWillThrowExceptionWhenVFCResponseError() {
142 VfcService vfcService = mock(VfcService.class);
143 Csar csar = new Csar();
144 when(vfcService.distributeNsPackage(csar)).thenReturn(emptyBodyCall());
145 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
146 service.postNsPackage(csar);
150 public void itCanPostVnfPackageToVFC() {
151 VfcService vfcService = mock(VfcService.class);
152 Csar csar = new Csar();
154 when(vfcService.distributeVnfPackage(csar)).thenReturn(successfulCall(job));
155 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
157 Assert.assertSame(job, service.postVfPackage(csar));
160 @Test(expected = VfcException.class)
161 public void postVnfPackageWillThrowExceptionWhenVFCIsNotAvailable() {
162 VfcService vfcService = mock(VfcService.class);
163 Csar csar = new Csar();
164 when(vfcService.distributeVnfPackage(csar)).thenReturn(failedCall("VFC is not available!"));
165 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
166 service.postVfPackage(csar);
169 @Test(expected = VfcException.class)
170 public void postVnfPackageWillThrowExceptionWhenVFCResponseError() {
171 VfcService vfcService = mock(VfcService.class);
172 Csar csar = new Csar();
173 when(vfcService.distributeVnfPackage(csar)).thenReturn(emptyBodyCall());
174 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
175 service.postVfPackage(csar);
179 public void itCanGetJobStatusFromVFC() {
180 VfcService vfcService = mock(VfcService.class);
182 String responseId = "1";
183 JobStatus jobStatus = new JobStatus();
184 when(vfcService.getJobStatus(jobId, responseId)).thenReturn(successfulCall(jobStatus));
185 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
187 Assert.assertSame(jobStatus, service.getJobStatus(jobId, responseId));
190 @Test(expected = VfcException.class)
191 public void getJobStatusWillThrowExceptionWhenVFCIsNotAvailable() {
192 VfcService vfcService = mock(VfcService.class);
194 String responseId = "1";
195 when(vfcService.getJobStatus(jobId, responseId)).thenReturn(failedCall("VFC is not available!"));
196 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
197 service.getJobStatus(jobId, responseId);
200 @Test(expected = VfcException.class)
201 public void getJobStatusWillThrowExceptionWhenVFCResponseError() {
202 VfcService vfcService = mock(VfcService.class);
204 String responseId = "1";
205 when(vfcService.getJobStatus(jobId, responseId)).thenReturn(emptyBodyCall());
206 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
207 service.getJobStatus(jobId, responseId);
211 public void itCanDeleteNsPackage() {
213 DistributionResult result = new DistributionResult();
214 VfcService vfcService = mock(VfcService.class);
215 when(vfcService.deleteNsPackage(csarId)).thenReturn(successfulCall(result));
216 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
218 Assert.assertSame(result, service.deleteNsPackage(csarId));
221 @Test(expected = VfcException.class)
222 public void deleteNsPackageWillThrowExceptionWhenVFCIsNotAvailable() {
224 VfcService vfcService = mock(VfcService.class);
225 when(vfcService.deleteNsPackage(csarId)).thenReturn(failedCall("VFC is not available!"));
226 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
227 service.deleteNsPackage(csarId);
230 @Test(expected = VfcException.class)
231 public void deleteNsPackageWillThrowExceptionWhenVFCResponseError() {
233 VfcService vfcService = mock(VfcService.class);
234 when(vfcService.deleteNsPackage(csarId)).thenReturn(emptyBodyCall());
235 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
236 service.deleteNsPackage(csarId);
240 public void itCanDeleteVFPackage() {
243 VfcService vfcService = mock(VfcService.class);
244 when(vfcService.deleteVnfPackage(csarId)).thenReturn(successfulCall(job));
245 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
247 Assert.assertSame(job, service.deleteVfPackage(csarId));
250 @Test(expected = VfcException.class)
251 public void deleteVfPackageWillThrowExceptionWhenVFCIsNotAvailable() {
253 VfcService vfcService = mock(VfcService.class);
254 when(vfcService.deleteVnfPackage(csarId)).thenReturn(failedCall("VFC is not available!"));
255 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
256 service.deleteVfPackage(csarId);
259 @Test(expected = VfcException.class)
260 public void deleteVfPackageWillThrowExceptionWhenVFCResponseError() {
262 VfcService vfcService = mock(VfcService.class);
263 when(vfcService.deleteVnfPackage(csarId)).thenReturn(emptyBodyCall());
264 PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
265 service.deleteVfPackage(csarId);