ac0d26948b6e432e40bb7f6699b33d3506798931
[usecase-ui/server.git] /
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.ServiceTemplateInput;
21 import org.onap.usecaseui.server.bean.lcm.TemplateInput;
22 import org.onap.usecaseui.server.service.lcm.ServiceTemplateService;
23 import org.onap.usecaseui.server.service.lcm.domain.aai.AAIService;
24 import org.onap.usecaseui.server.service.lcm.domain.aai.bean.SDNCController;
25 import org.onap.usecaseui.server.service.lcm.domain.aai.bean.SDNCControllerRsp;
26 import org.onap.usecaseui.server.service.lcm.domain.aai.bean.VimInfo;
27 import org.onap.usecaseui.server.service.lcm.domain.aai.bean.VimInfoRsp;
28 import org.onap.usecaseui.server.service.lcm.domain.aai.exceptions.AAIException;
29 import org.onap.usecaseui.server.service.lcm.domain.sdc.SDCCatalogService;
30 import org.onap.usecaseui.server.service.lcm.domain.sdc.bean.SDCServiceTemplate;
31 import org.onap.usecaseui.server.service.lcm.domain.sdc.exceptions.SDCCatalogException;
32 import org.openecomp.sdc.toscaparser.api.NodeTemplate;
33 import org.openecomp.sdc.toscaparser.api.ToscaTemplate;
34 import org.openecomp.sdc.toscaparser.api.common.JToscaException;
35 import org.openecomp.sdc.toscaparser.api.elements.Metadata;
36 import org.openecomp.sdc.toscaparser.api.parameters.Input;
37 import retrofit2.Call;
38
39 import java.io.IOException;
40 import java.util.*;
41
42 import static org.hamcrest.Matchers.equalTo;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.when;
45 import static org.onap.usecaseui.server.service.lcm.domain.sdc.consts.SDCConsts.CATEGORY_E2E_SERVICE;
46 import static org.onap.usecaseui.server.service.lcm.domain.sdc.consts.SDCConsts.DISTRIBUTION_STATUS_DISTRIBUTED;
47 import static org.onap.usecaseui.server.util.CallStub.emptyBodyCall;
48 import static org.onap.usecaseui.server.util.CallStub.failedCall;
49 import static org.onap.usecaseui.server.util.CallStub.successfulCall;
50
51 public class DefaultServiceTemplateServiceTest {
52
53     @Test
54     public void itCanListDistributedServiceTemplate() {
55         List<SDCServiceTemplate> templates = Collections.singletonList(new SDCServiceTemplate("uuid", "uuid", "name", "V1","url", "category"));
56         SDCCatalogService sdcService = mock(SDCCatalogService.class);
57         when(sdcService.listServices(CATEGORY_E2E_SERVICE, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(successfulCall(templates));
58
59         ServiceTemplateService service = new DefaultServiceTemplateService(sdcService,null);
60
61         Assert.assertSame(templates, service.listDistributedServiceTemplate());
62     }
63
64     @Test(expected = SDCCatalogException.class)
65     public void retrieveServiceWillThrowExceptionWhenSDCIsNotAvailable() {
66         SDCCatalogService sdcService = mock(SDCCatalogService.class);
67         when(sdcService.listServices(CATEGORY_E2E_SERVICE, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(failedCall("SDC is not available!"));
68
69         ServiceTemplateService service = new DefaultServiceTemplateService(sdcService,null);
70         service.listDistributedServiceTemplate();
71     }
72
73     @Test
74     public void itWillRetrieveEmptyWhenNoServiceTemplateCanGet() {
75         SDCCatalogService sdcService = mock(SDCCatalogService.class);
76         when(sdcService.listServices(CATEGORY_E2E_SERVICE, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(emptyBodyCall());
77
78         ServiceTemplateService service = new DefaultServiceTemplateService(sdcService,null);
79         List<SDCServiceTemplate> sdcServiceTemplates = service.listDistributedServiceTemplate();
80
81         Assert.assertTrue("service templates should be empty.", sdcServiceTemplates.isEmpty());
82     }
83
84     @Test
85     public void itCanRetrieveInputsFromServiceTemplate() throws IOException {
86         final String uuid = "1";
87         String modelPath = "model_path";
88         String nodeUUID = "2";
89
90         SDCCatalogService sdcService = newSdcCatalogService(nodeUUID);
91
92         List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
93         AAIService aaiService = newAAIService(vim);
94
95         ServiceTemplateService service = newServiceTemplateService(uuid, nodeUUID, sdcService, aaiService);
96
97         Assert.assertThat(service.fetchServiceTemplateInput(uuid, modelPath), equalTo(expectedServiceInputs(uuid, nodeUUID)));
98     }
99
100     private DefaultServiceTemplateService newServiceTemplateService(String uuid, String nodeUUID, SDCCatalogService sdcService, AAIService aaiService) {
101         return new DefaultServiceTemplateService(sdcService, aaiService) {
102
103             @Override
104             protected void downloadFile(String templateUrl, String toPath) throws IOException {
105                 // download successfully...
106             }
107
108             @Override
109             protected ToscaTemplate translateToToscaTemplate(String toPath) throws JToscaException {
110                 if (toPath.contains(uuid)) {
111                     return e2eToscaTemplate(nodeUUID);
112                 }
113                 return nodeToscaTemplate(nodeUUID);
114             }
115         };
116     }
117
118     private SDCCatalogService newSdcCatalogService(String nodeUUID) throws IOException {
119         SDCCatalogService sdcService = mock(SDCCatalogService.class);
120         when(sdcService.getService(nodeUUID)).thenReturn(successfulCall(new SDCServiceTemplate(nodeUUID, nodeUUID, "node", "V1", "nodeModelUrl", "service")));
121         return sdcService;
122     }
123
124     private ServiceTemplateInput expectedServiceInputs(String uuid, String nodeUUID) {
125         ServiceTemplateInput e2eServiceTemplateInput = new ServiceTemplateInput(
126                 uuid, uuid, "VoLTE", "service","", "VoLTE", "service", "","", Collections.EMPTY_LIST);
127         TemplateInput templateInput = new TemplateInput("field_name","field_type", "field_description", "true", "field_default");
128         ServiceTemplateInput nodeTemplateInput = new ServiceTemplateInput(
129                 nodeUUID, nodeUUID, "", "", "","", "service", "","", Collections.singletonList(templateInput));
130 //        e2eServiceTemplateInput.addNestedTemplate(nodeTemplateInput);
131         return e2eServiceTemplateInput;
132     }
133
134     private ToscaTemplate e2eToscaTemplate(String nodeUUID) {
135         ToscaTemplate toscaTemplate = mock(ToscaTemplate.class);
136         Map<String, Object> e2eAttributes = new HashMap<>();
137         e2eAttributes.put("invariantUUID", "1");
138         e2eAttributes.put("UUID", "1");
139         e2eAttributes.put("name", "VoLTE");
140         e2eAttributes.put("type", "service");
141         e2eAttributes.put("description", "VoLTE");
142         e2eAttributes.put("category", "service");
143         e2eAttributes.put("subcategory", "");
144         e2eAttributes.put("version", "");
145         when(toscaTemplate.getMetaData()).thenReturn(new Metadata(e2eAttributes));
146         when(toscaTemplate.getInputs()).thenReturn(new ArrayList<>());
147         NodeTemplate nodeTemplate = mock(NodeTemplate.class);
148
149         Map<String, Object> nodeUUIDAttr = new HashMap<>();
150
151         nodeUUIDAttr.put("UUID", nodeUUID);
152         when(nodeTemplate.getMetaData()).thenReturn(new Metadata(nodeUUIDAttr));
153
154         ArrayList<NodeTemplate> nodeTemplates = new ArrayList<>();
155 //        nodeTemplates.add(nodeTemplate);
156         when(toscaTemplate.getNodeTemplates()).thenReturn(nodeTemplates);
157
158         return toscaTemplate;
159     }
160
161     private ToscaTemplate nodeToscaTemplate(String nodeUUID) {
162         ToscaTemplate toscaTemplate = mock(ToscaTemplate.class);
163         Map<String, Object> Attributes = new HashMap<>();
164         Attributes.put("invariantUUID", nodeUUID);
165         Attributes.put("UUID", nodeUUID);
166         Attributes.put("name", "");
167         Attributes.put("type", "");
168         Attributes.put("description", "");
169         Attributes.put("category", "service");
170         Attributes.put("subcategory", "");
171         when(toscaTemplate.getMetaData()).thenReturn(new Metadata(Attributes));
172
173         Input input = mock(Input.class);
174         when(input.getName()).thenReturn("field_name");
175         when(input.getDescription()).thenReturn("field_description");
176         when(input.getType()).thenReturn("field_type");
177         when(input.getDefault()).thenReturn("field_default");
178         when(input.isRequired()).thenReturn(true);
179
180         ArrayList<Input> inputs = new ArrayList<>();
181         inputs.add(input);
182         when(toscaTemplate.getInputs()).thenReturn(inputs);
183         when(toscaTemplate.getNodeTemplates()).thenReturn(new ArrayList<>());
184
185         return toscaTemplate;
186     }
187
188     private AAIService newAAIService(List<VimInfo> vim) {
189         AAIService aaiService = mock(AAIService.class);
190         VimInfoRsp rsp = new VimInfoRsp();
191         rsp.setCloudRegion(vim);
192         Call<VimInfoRsp> vimCall = successfulCall(rsp);
193         when(aaiService.listVimInfo()).thenReturn(vimCall);
194         return aaiService;
195     }
196
197     @Test(expected = SDCCatalogException.class)
198     public void retrieveInputsWillThrowExceptionWhenDownloadFailed() {
199         ServiceTemplateService service = new DefaultServiceTemplateService(null, null) {
200             @Override
201             protected void downloadFile(String templateUrl, String toPath) throws IOException {
202                 throw new IOException("download failed!");
203             }
204         };
205         service.fetchServiceTemplateInput("1", "url");
206     }
207
208     @Test(expected = SDCCatalogException.class)
209     public void retrieveInputsWillThrowExceptionWhenParsingToscaTemplateFailed() {
210         ServiceTemplateService service = new DefaultServiceTemplateService(null, null) {
211             @Override
212             protected void downloadFile(String templateUrl, String toPath) throws IOException {
213                 // download successfully...
214             }
215
216             @Override
217             protected ToscaTemplate translateToToscaTemplate(String toPath) throws JToscaException {
218                 throw new JToscaException("parse tosca template failed!", "123");
219             }
220         };
221         service.fetchServiceTemplateInput("1", "url");
222     }
223
224     @Test
225     public void itCanListVim() {
226         List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "region"));
227         VimInfoRsp rsp = new VimInfoRsp();
228         rsp.setCloudRegion(vim);
229         AAIService aaiService = mock(AAIService.class);
230         when(aaiService.listVimInfo()).thenReturn(successfulCall(rsp));
231
232         ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
233
234         Assert.assertSame(vim, service.listVim());
235     }
236
237     @Test
238     public void itCanRetrieveEmptyListWhenNoVimInfoInAAI() {
239         AAIService aaiService = mock(AAIService.class);
240         when(aaiService.listVimInfo()).thenReturn(emptyBodyCall());
241
242         ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
243         List<VimInfo> vimInfos = service.listVim();
244
245         Assert.assertTrue("vim should be empty.", vimInfos.isEmpty());
246     }
247
248     @Test(expected = AAIException.class)
249     public void itCanThrowExceptionWhenAAIServiceIsNotAvailable() {
250         AAIService aaiService = mock(AAIService.class);
251         when(aaiService.listVimInfo()).thenReturn(failedCall("AAI is not available!"));
252
253         ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
254         service.listVim();
255     }
256
257     @Test
258     public void itCanListSDNController() {
259         List<SDNCController> controllers = Collections.singletonList(new SDNCController());
260         SDNCControllerRsp rsp = new SDNCControllerRsp();
261         rsp.setEsrThirdpartySdncList(controllers);
262         AAIService aaiService = mock(AAIService.class);
263         when(aaiService.listSdncControllers()).thenReturn(successfulCall(rsp));
264
265         ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
266
267         Assert.assertSame(controllers, service.listSDNCControllers());
268     }
269
270     @Test
271     public void itCanRetrieveEmptyListWhenNoSDNControllerInAAI() {
272         AAIService aaiService = mock(AAIService.class);
273         when(aaiService.listSdncControllers()).thenReturn(emptyBodyCall());
274
275         ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
276         List<SDNCController> controllers = service.listSDNCControllers();
277
278         Assert.assertTrue("sdn controller should be empty.", controllers.isEmpty());
279     }
280
281     @Test(expected = AAIException.class)
282     public void itListSDNControllerThrowExceptionWhenAAIServiceIsNotAvailable() {
283         AAIService aaiService = mock(AAIService.class);
284         when(aaiService.listSdncControllers()).thenReturn(failedCall("AAI is not available!"));
285
286         ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
287         service.listSDNCControllers();
288     }
289 }