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