Add UT for service 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.sdc.SDCCatalogService;
25 import org.onap.usecaseui.server.service.lcm.domain.sdc.bean.SDCServiceTemplate;
26 import org.onap.usecaseui.server.service.lcm.domain.sdc.bean.Vnf;
27 import org.onap.usecaseui.server.service.lcm.domain.sdc.exceptions.SDCCatalogException;
28 import org.onap.usecaseui.server.service.lcm.domain.vfc.VfcService;
29 import org.onap.usecaseui.server.service.lcm.domain.vfc.beans.Csar;
30 import org.onap.usecaseui.server.service.lcm.domain.vfc.beans.DistributionResult;
31 import org.onap.usecaseui.server.service.lcm.domain.vfc.beans.Job;
32 import org.onap.usecaseui.server.service.lcm.domain.vfc.beans.JobStatus;
33 import org.onap.usecaseui.server.service.lcm.domain.vfc.exceptions.VfcException;
34 import retrofit2.Call;
35
36 import java.util.Collections;
37 import java.util.List;
38
39 import static org.hamcrest.CoreMatchers.equalTo;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.when;
42 import static org.onap.usecaseui.server.service.lcm.domain.sdc.consts.SDCConsts.*;
43 import static org.onap.usecaseui.server.util.CallStub.failedCall;
44 import static org.onap.usecaseui.server.util.CallStub.successfulCall;
45
46 public class DefaultPackageDistributionServiceTest {
47
48     @Test
49     public void itCanRetrievePackageFromSDCAndAAI() {
50         List<SDCServiceTemplate> serviceTemplate = Collections.singletonList(new SDCServiceTemplate("1", "1", "service", "", ""));
51         List<Vnf> vnf = Collections.singletonList(new Vnf("2","2","vnf"));
52         SDCCatalogService sdcService = newSDCService(serviceTemplate, vnf);
53
54         List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
55         AAIService aaiService = newAAIService(vim);
56
57         PackageDistributionService service = new DefaultPackageDistributionService(sdcService,aaiService, null);
58
59         Assert.assertThat(service.retrievePackageInfo(), equalTo(new VfNsPackageInfo(serviceTemplate, vnf, vim)));
60     }
61
62     private AAIService newAAIService(List<VimInfo> vim) {
63         AAIService aaiService = mock(AAIService.class);
64
65         Call<List<VimInfo>> vimCall = successfulCall(vim);
66         when(aaiService.listVimInfo()).thenReturn(vimCall);
67         return aaiService;
68     }
69
70     private SDCCatalogService newSDCService(List<SDCServiceTemplate> serviceTemplate, List<Vnf> vnf) {
71         SDCCatalogService sdcService = mock(SDCCatalogService.class);
72
73         Call<List<SDCServiceTemplate>> serviceCall = successfulCall(serviceTemplate);
74         when(sdcService.listServices(CATEGORY_NS, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(serviceCall);
75
76         Call<List<Vnf>> vnfCall = successfulCall(vnf);
77         when(sdcService.listResources(RESOURCETYPE_VF, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(vnfCall);
78         return sdcService;
79     }
80
81     @Test(expected = SDCCatalogException.class)
82     public void retrievePackageWillThrowExceptionWhenSDCIsNotAvailable() {
83         SDCCatalogService sdcService = mock(SDCCatalogService.class);
84         Call<List<SDCServiceTemplate>> serviceCall = failedCall("SDC is not available!");
85         when(sdcService.listServices(CATEGORY_NS, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(serviceCall);
86
87         List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
88         AAIService aaiService = newAAIService(vim);
89
90         PackageDistributionService service = new DefaultPackageDistributionService(sdcService,aaiService, null);
91         service.retrievePackageInfo();
92     }
93
94     @Test
95     public void itCanPostNsPackageToVFC() {
96         VfcService vfcService = mock(VfcService.class);
97         Csar csar = new Csar();
98         DistributionResult result = new DistributionResult("status", "description", "errorcode");
99         when(vfcService.distributeNsPackage(csar)).thenReturn(successfulCall(result));
100         PackageDistributionService service = new DefaultPackageDistributionService(null, null, vfcService);
101
102         Assert.assertSame(result, service.postNsPackage(csar));
103     }
104
105     @Test(expected = VfcException.class)
106     public void postNsPackageWillThrowExceptionWhenVFCIsNotAvailable() {
107         VfcService vfcService = mock(VfcService.class);
108         Csar csar = new Csar();
109         when(vfcService.distributeNsPackage(csar)).thenReturn(failedCall("VFC is not available!"));
110         PackageDistributionService service = new DefaultPackageDistributionService(null, null, vfcService);
111         service.postNsPackage(csar);
112     }
113
114     @Test
115     public void itCanPostVnfPackageToVFC() {
116         VfcService vfcService = mock(VfcService.class);
117         Csar csar = new Csar();
118         Job job = new Job();
119         when(vfcService.distributeVnfPackage(csar)).thenReturn(successfulCall(job));
120         PackageDistributionService service = new DefaultPackageDistributionService(null, null, vfcService);
121
122         Assert.assertSame(job, service.postVfPackage(csar));
123     }
124
125     @Test(expected = VfcException.class)
126     public void postVnfPackageWillThrowExceptionWhenVFCIsNotAvailable() {
127         VfcService vfcService = mock(VfcService.class);
128         Csar csar = new Csar();
129         when(vfcService.distributeVnfPackage(csar)).thenReturn(failedCall("VFC is not available!"));
130         PackageDistributionService service = new DefaultPackageDistributionService(null, null, vfcService);
131         service.postVfPackage(csar);
132     }
133
134     @Test
135     public void itCanGetJobStatusFromVFC() {
136         VfcService vfcService = mock(VfcService.class);
137         String jobId = "1";
138         JobStatus jobStatus = new JobStatus();
139         when(vfcService.getJobStatus(jobId)).thenReturn(successfulCall(jobStatus));
140         PackageDistributionService service = new DefaultPackageDistributionService(null, null, vfcService);
141
142         Assert.assertSame(jobStatus, service.getJobStatus(jobId));
143     }
144
145     @Test(expected = VfcException.class)
146     public void getJobStatusWillThrowExceptionWhenVFCIsNotAvailable() {
147         VfcService vfcService = mock(VfcService.class);
148         String jobId = "1";
149         when(vfcService.getJobStatus(jobId)).thenReturn(failedCall("VFC is not available!"));
150         PackageDistributionService service = new DefaultPackageDistributionService(null, null, vfcService);
151         service.getJobStatus(jobId);
152     }
153 }