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