Fix compile issue.
[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.failedCall;
45 import static org.onap.usecaseui.server.util.CallStub.successfulCall;
46
47 public class DefaultPackageDistributionServiceTest {
48
49     @Test
50     public void itCanRetrievePackageFromSDCAndAAI() {
51         List<SDCServiceTemplate> serviceTemplate = Collections.singletonList(new SDCServiceTemplate("1", "1", "service", "V1","", ""));
52         Vnf o = new Vnf();
53         o.setInvariantUUID("2");
54         o.setUuid("2");
55         o.setName("vnf");
56         List<Vnf> vnf = Collections.singletonList(o);
57         SDCCatalogService sdcService = newSDCService(serviceTemplate, vnf);
58
59         List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
60         AAIService aaiService = newAAIService(vim);
61
62         PackageDistributionService service = new DefaultPackageDistributionService(sdcService, null);
63
64         Assert.assertThat(service.retrievePackageInfo(), equalTo(new VfNsPackageInfo(serviceTemplate, vnf)));
65     }
66
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);
73         return aaiService;
74     }
75
76     private SDCCatalogService newSDCService(List<SDCServiceTemplate> serviceTemplate, List<Vnf> vnf) {
77         SDCCatalogService sdcService = mock(SDCCatalogService.class);
78
79         Call<List<SDCServiceTemplate>> serviceCall = successfulCall(serviceTemplate);
80         when(sdcService.listServices(CATEGORY_NS, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(serviceCall);
81
82         Call<List<Vnf>> vnfCall = successfulCall(vnf);
83         when(sdcService.listResources(RESOURCETYPE_VF)).thenReturn(vnfCall);
84         return sdcService;
85     }
86
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);
92
93         List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
94         AAIService aaiService = newAAIService(vim);
95
96         PackageDistributionService service = new DefaultPackageDistributionService(sdcService, null);
97         service.retrievePackageInfo();
98     }
99
100     @Test
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);
110
111         Assert.assertSame(result, service.postNsPackage(csar));
112     }
113
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);
121     }
122
123     @Test
124     public void itCanPostVnfPackageToVFC() {
125         VfcService vfcService = mock(VfcService.class);
126         Csar csar = new Csar();
127         Job job = new Job();
128         when(vfcService.distributeVnfPackage(csar)).thenReturn(successfulCall(job));
129         PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
130
131         Assert.assertSame(job, service.postVfPackage(csar));
132     }
133
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);
141     }
142
143     @Test
144     public void itCanGetJobStatusFromVFC() {
145         VfcService vfcService = mock(VfcService.class);
146         String jobId = "1";
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);
151
152         Assert.assertSame(jobStatus, service.getJobStatus(jobId, responseId));
153     }
154
155     @Test(expected = VfcException.class)
156     public void getJobStatusWillThrowExceptionWhenVFCIsNotAvailable() {
157         VfcService vfcService = mock(VfcService.class);
158         String jobId = "1";
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);
163     }
164 }