Add unit test for uui lcm
[usecase-ui/server.git] / server / src / test / java / org / onap / usecaseui / server / service / lcm / impl / DefaultPackageDistributionServiceTest.java
1 /**
2  * Copyright 2016-2017 ZTE Corporation.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.usecaseui.server.service.lcm.impl;
17
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;
36
37 import java.util.Collections;
38 import java.util.List;
39
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;
47
48 public class DefaultPackageDistributionServiceTest {
49
50     @Test
51     public void itCanRetrievePackageFromSDCAndAAI() {
52         List<SDCServiceTemplate> serviceTemplate = Collections.singletonList(new SDCServiceTemplate("1", "1", "service", "V1","", ""));
53         Vnf o = new Vnf();
54         o.setInvariantUUID("2");
55         o.setUuid("2");
56         o.setName("vnf");
57         List<Vnf> vnf = Collections.singletonList(o);
58         SDCCatalogService sdcService = newSDCService(serviceTemplate, vnf);
59
60         List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
61         AAIService aaiService = newAAIService(vim);
62
63         PackageDistributionService service = new DefaultPackageDistributionService(sdcService, null);
64
65         Assert.assertThat(service.retrievePackageInfo(), equalTo(new VfNsPackageInfo(serviceTemplate, vnf)));
66     }
67
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);
74         return aaiService;
75     }
76
77     private SDCCatalogService newSDCService(List<SDCServiceTemplate> serviceTemplate, List<Vnf> vnf) {
78         SDCCatalogService sdcService = mock(SDCCatalogService.class);
79
80         Call<List<SDCServiceTemplate>> serviceCall = successfulCall(serviceTemplate);
81         when(sdcService.listServices(CATEGORY_NS, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(serviceCall);
82
83         Call<List<Vnf>> vnfCall = successfulCall(vnf);
84         when(sdcService.listResources(RESOURCETYPE_VF)).thenReturn(vnfCall);
85         return sdcService;
86     }
87
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);
93
94         List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
95         AAIService aaiService = newAAIService(vim);
96
97         PackageDistributionService service = new DefaultPackageDistributionService(sdcService, null);
98         service.retrievePackageInfo();
99     }
100
101     @Test
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);
106
107         Call<List<Vnf>> resourceCall = emptyBodyCall();
108         when(sdcService.listResources(RESOURCETYPE_VF)).thenReturn(resourceCall);
109
110         PackageDistributionService service = new DefaultPackageDistributionService(sdcService, null);
111         VfNsPackageInfo vfNsPackageInfo = service.retrievePackageInfo();
112
113         Assert.assertTrue("ns should be empty!", vfNsPackageInfo.getNsPackage().isEmpty());
114         Assert.assertTrue("vf should be empty!", vfNsPackageInfo.getVnfPackages().isEmpty());
115     }
116
117     @Test
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);
127
128         Assert.assertSame(result, service.postNsPackage(csar));
129     }
130
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);
138     }
139
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);
147     }
148
149     @Test
150     public void itCanPostVnfPackageToVFC() {
151         VfcService vfcService = mock(VfcService.class);
152         Csar csar = new Csar();
153         Job job = new Job();
154         when(vfcService.distributeVnfPackage(csar)).thenReturn(successfulCall(job));
155         PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
156
157         Assert.assertSame(job, service.postVfPackage(csar));
158     }
159
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);
167     }
168
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);
176     }
177
178     @Test
179     public void itCanGetJobStatusFromVFC() {
180         VfcService vfcService = mock(VfcService.class);
181         String jobId = "1";
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);
186
187         Assert.assertSame(jobStatus, service.getJobStatus(jobId, responseId));
188     }
189
190     @Test(expected = VfcException.class)
191     public void getJobStatusWillThrowExceptionWhenVFCIsNotAvailable() {
192         VfcService vfcService = mock(VfcService.class);
193         String jobId = "1";
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);
198     }
199
200     @Test(expected = VfcException.class)
201     public void getJobStatusWillThrowExceptionWhenVFCResponseError() {
202         VfcService vfcService = mock(VfcService.class);
203         String jobId = "1";
204         String responseId = "1";
205         when(vfcService.getJobStatus(jobId, responseId)).thenReturn(emptyBodyCall());
206         PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
207         service.getJobStatus(jobId, responseId);
208     }
209
210     @Test
211     public void itCanDeleteNsPackage() {
212         String csarId = "1";
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);
217
218         Assert.assertSame(result, service.deleteNsPackage(csarId));
219     }
220
221     @Test(expected = VfcException.class)
222     public void deleteNsPackageWillThrowExceptionWhenVFCIsNotAvailable() {
223         String csarId = "1";
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);
228     }
229
230     @Test(expected = VfcException.class)
231     public void deleteNsPackageWillThrowExceptionWhenVFCResponseError() {
232         String csarId = "1";
233         VfcService vfcService = mock(VfcService.class);
234         when(vfcService.deleteNsPackage(csarId)).thenReturn(emptyBodyCall());
235         PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
236         service.deleteNsPackage(csarId);
237     }
238
239     @Test
240     public void itCanDeleteVFPackage() {
241         String csarId = "1";
242         Job job = new Job();
243         VfcService vfcService = mock(VfcService.class);
244         when(vfcService.deleteVnfPackage(csarId)).thenReturn(successfulCall(job));
245         PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
246
247         Assert.assertSame(job, service.deleteVfPackage(csarId));
248     }
249
250     @Test(expected = VfcException.class)
251     public void deleteVfPackageWillThrowExceptionWhenVFCIsNotAvailable() {
252         String csarId = "1";
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);
257     }
258
259     @Test(expected = VfcException.class)
260     public void deleteVfPackageWillThrowExceptionWhenVFCResponseError() {
261         String csarId = "1";
262         VfcService vfcService = mock(VfcService.class);
263         when(vfcService.deleteVnfPackage(csarId)).thenReturn(emptyBodyCall());
264         PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
265         service.deleteVfPackage(csarId);
266     }
267 }