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