Add UT for query service tempalte.
[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.mockito.Mockito;
21 import org.onap.usecaseui.server.bean.lcm.VfNsPackageInfo;
22 import org.onap.usecaseui.server.service.lcm.PackageDistributionService;
23 import org.onap.usecaseui.server.service.lcm.domain.aai.AAIService;
24 import org.onap.usecaseui.server.service.lcm.domain.aai.bean.VimInfo;
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.exceptions.VfcException;
33 import org.onap.usecaseui.server.util.CallStub;
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.onap.usecaseui.server.service.lcm.domain.sdc.consts.SDCConsts.*;
41
42 public class DefaultPackageDistributionServiceTest {
43
44     @Test
45     public void itCanRetrievePackageFromSDCAndAAI() {
46         List<SDCServiceTemplate> serviceTemplate = Collections.singletonList(new SDCServiceTemplate("1", "1", "service", "", ""));
47         List<Vnf> vnf = Collections.singletonList(new Vnf("2","2","vnf"));
48         SDCCatalogService sdcService = newSDCService(serviceTemplate, vnf);
49
50         List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
51         AAIService aaiService = newAAIService(vim);
52
53         PackageDistributionService service = new DefaultPackageDistributionService(sdcService,aaiService, null);
54
55         Assert.assertThat(service.retrievePackageInfo(), equalTo(new VfNsPackageInfo(serviceTemplate, vnf, vim)));
56     }
57
58     private AAIService newAAIService(List<VimInfo> vim) {
59         AAIService aaiService = Mockito.mock(AAIService.class);
60
61         Call<List<VimInfo>> vimCall = CallStub.successfulCall(vim);
62         Mockito.when(aaiService.listVimInfo()).thenReturn(vimCall);
63         return aaiService;
64     }
65
66     private SDCCatalogService newSDCService(List<SDCServiceTemplate> serviceTemplate, List<Vnf> vnf) {
67         SDCCatalogService sdcService = Mockito.mock(SDCCatalogService.class);
68
69         Call<List<SDCServiceTemplate>> serviceCall = CallStub.successfulCall(serviceTemplate);
70         Mockito.when(sdcService.listServices(CATEGORY_NS, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(serviceCall);
71
72         Call<List<Vnf>> vnfCall = CallStub.successfulCall(vnf);
73         Mockito.when(sdcService.listResources(RESOURCETYPE_VF, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(vnfCall);
74         return sdcService;
75     }
76
77     @Test(expected = SDCCatalogException.class)
78     public void retrievePackageWillThrowExceptionWhenSDCIsNotAvailable() {
79         SDCCatalogService sdcService = Mockito.mock(SDCCatalogService.class);
80         Call<List<SDCServiceTemplate>> serviceCall = CallStub.failedCall("SDC is not available!");
81         Mockito.when(sdcService.listServices(CATEGORY_NS, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(serviceCall);
82
83         List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
84         AAIService aaiService = newAAIService(vim);
85
86         PackageDistributionService service = new DefaultPackageDistributionService(sdcService,aaiService, null);
87         service.retrievePackageInfo();
88     }
89
90     @Test
91     public void itCanPostNsPackageToVFC() {
92         VfcService vfcService = Mockito.mock(VfcService.class);
93         Csar csar = new Csar();
94         DistributionResult result = new DistributionResult("status", "description", "errorcode");
95         Mockito.when(vfcService.distributeNsPackage(csar)).thenReturn(CallStub.successfulCall(result));
96         PackageDistributionService service = new DefaultPackageDistributionService(null, null, vfcService);
97
98         Assert.assertSame(result, service.postNsPackage(csar));
99     }
100
101     @Test(expected = VfcException.class)
102     public void postNsPackageWillThrowExceptionWhenVFCIsNotAvailable() {
103         VfcService vfcService = Mockito.mock(VfcService.class);
104         Csar csar = new Csar();
105         Mockito.when(vfcService.distributeNsPackage(csar)).thenReturn(CallStub.failedCall("VFC is not available!"));
106         PackageDistributionService service = new DefaultPackageDistributionService(null, null, vfcService);
107         service.postNsPackage(csar);
108     }
109 }