Fix query para is wrong.
[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         List<Vnf> vnf = Collections.singletonList(new Vnf("2","2","vnf"));
53         SDCCatalogService sdcService = newSDCService(serviceTemplate, vnf);
54
55         List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
56         AAIService aaiService = newAAIService(vim);
57
58         PackageDistributionService service = new DefaultPackageDistributionService(sdcService, null);
59
60         Assert.assertThat(service.retrievePackageInfo(), equalTo(new VfNsPackageInfo(serviceTemplate, vnf)));
61     }
62
63     private AAIService newAAIService(List<VimInfo> vim) {
64         AAIService aaiService = mock(AAIService.class);
65         VimInfoRsp rsp = new VimInfoRsp();
66         rsp.setCloudRegion(vim);
67         Call<VimInfoRsp> vimCall = successfulCall(rsp);
68         when(aaiService.listVimInfo()).thenReturn(vimCall);
69         return aaiService;
70     }
71
72     private SDCCatalogService newSDCService(List<SDCServiceTemplate> serviceTemplate, List<Vnf> vnf) {
73         SDCCatalogService sdcService = mock(SDCCatalogService.class);
74
75         Call<List<SDCServiceTemplate>> serviceCall = successfulCall(serviceTemplate);
76         when(sdcService.listServices(CATEGORY_NS, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(serviceCall);
77
78         Call<List<Vnf>> vnfCall = successfulCall(vnf);
79         when(sdcService.listResources(RESOURCETYPE_VF)).thenReturn(vnfCall);
80         return sdcService;
81     }
82
83     @Test(expected = SDCCatalogException.class)
84     public void retrievePackageWillThrowExceptionWhenSDCIsNotAvailable() {
85         SDCCatalogService sdcService = mock(SDCCatalogService.class);
86         Call<List<SDCServiceTemplate>> serviceCall = failedCall("SDC is not available!");
87         when(sdcService.listServices(CATEGORY_NS, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(serviceCall);
88
89         List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
90         AAIService aaiService = newAAIService(vim);
91
92         PackageDistributionService service = new DefaultPackageDistributionService(sdcService, null);
93         service.retrievePackageInfo();
94     }
95
96     @Test
97     public void itCanPostNsPackageToVFC() {
98         VfcService vfcService = mock(VfcService.class);
99         Csar csar = new Csar();
100         DistributionResult result = new DistributionResult("status", "description", "errorcode");
101         when(vfcService.distributeNsPackage(csar)).thenReturn(successfulCall(result));
102         PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
103
104         Assert.assertSame(result, service.postNsPackage(csar));
105     }
106
107     @Test(expected = VfcException.class)
108     public void postNsPackageWillThrowExceptionWhenVFCIsNotAvailable() {
109         VfcService vfcService = mock(VfcService.class);
110         Csar csar = new Csar();
111         when(vfcService.distributeNsPackage(csar)).thenReturn(failedCall("VFC is not available!"));
112         PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
113         service.postNsPackage(csar);
114     }
115
116     @Test
117     public void itCanPostVnfPackageToVFC() {
118         VfcService vfcService = mock(VfcService.class);
119         Csar csar = new Csar();
120         Job job = new Job();
121         when(vfcService.distributeVnfPackage(csar)).thenReturn(successfulCall(job));
122         PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
123
124         Assert.assertSame(job, service.postVfPackage(csar));
125     }
126
127     @Test(expected = VfcException.class)
128     public void postVnfPackageWillThrowExceptionWhenVFCIsNotAvailable() {
129         VfcService vfcService = mock(VfcService.class);
130         Csar csar = new Csar();
131         when(vfcService.distributeVnfPackage(csar)).thenReturn(failedCall("VFC is not available!"));
132         PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
133         service.postVfPackage(csar);
134     }
135
136     @Test
137     public void itCanGetJobStatusFromVFC() {
138         VfcService vfcService = mock(VfcService.class);
139         String jobId = "1";
140         JobStatus jobStatus = new JobStatus();
141         when(vfcService.getJobStatus(jobId)).thenReturn(successfulCall(jobStatus));
142         PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
143
144         Assert.assertSame(jobStatus, service.getJobStatus(jobId));
145     }
146
147     @Test(expected = VfcException.class)
148     public void getJobStatusWillThrowExceptionWhenVFCIsNotAvailable() {
149         VfcService vfcService = mock(VfcService.class);
150         String jobId = "1";
151         when(vfcService.getJobStatus(jobId)).thenReturn(failedCall("VFC is not available!"));
152         PackageDistributionService service = new DefaultPackageDistributionService(null, vfcService);
153         service.getJobStatus(jobId);
154     }
155 }