0534838aa5bfa77e46b3f36e47e6cfddb0d9e48a
[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         DefaultServiceTemplateService dsts = null;
56         @Before
57         public void before() throws Exception {
58                 dsts = new DefaultServiceTemplateService();
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.assertThat(service.fetchServiceTemplateInput(uuid, modelPath), equalTo(expectedServiceInputs(uuid, nodeUUID)));
105     }
106
107     private DefaultServiceTemplateService newServiceTemplateService(String uuid, String nodeUUID, SDCCatalogService sdcService, AAIService aaiService) {
108         return new DefaultServiceTemplateService(sdcService, aaiService) {
109
110             @Override
111             protected void downloadFile(String templateUrl, String toPath) throws IOException {
112                 // download successfully...
113             }
114
115             @Override
116             protected ToscaTemplate translateToToscaTemplate(String toPath) throws JToscaException {
117                 if (toPath.contains(uuid)) {
118                     return e2eToscaTemplate(nodeUUID);
119                 }
120                 return nodeToscaTemplate(nodeUUID);
121             }
122         };
123     }
124
125     private SDCCatalogService newSdcCatalogService(String nodeUUID) throws IOException {
126         SDCCatalogService sdcService = mock(SDCCatalogService.class);
127         when(sdcService.getService(nodeUUID)).thenReturn(successfulCall(new SDCServiceTemplate(nodeUUID, nodeUUID, "node", "V1", "nodeModelUrl", "service")));
128         return sdcService;
129     }
130
131     private ServiceTemplateInput expectedServiceInputs(String uuid, String nodeUUID) {
132         ServiceTemplateInput e2eServiceTemplateInput = new ServiceTemplateInput(
133                 uuid, uuid, "VoLTE", "service","", "VoLTE", "service", "","", Collections.EMPTY_LIST);
134         TemplateInput templateInput = new TemplateInput("field_name","field_type", "field_description", "true", "field_default");
135         ServiceTemplateInput nodeTemplateInput = new ServiceTemplateInput(
136                 nodeUUID, nodeUUID, "", "", "","", "service", "","", Collections.singletonList(templateInput));
137 //        e2eServiceTemplateInput.addNestedTemplate(nodeTemplateInput);
138         return e2eServiceTemplateInput;
139     }
140
141     private ToscaTemplate e2eToscaTemplate(String nodeUUID) {
142         ToscaTemplate toscaTemplate = mock(ToscaTemplate.class);
143         Map<String, Object> e2eAttributes = new HashMap<>();
144         e2eAttributes.put("invariantUUID", "1");
145         e2eAttributes.put("UUID", "1");
146         e2eAttributes.put("name", "VoLTE");
147         e2eAttributes.put("type", "service");
148         e2eAttributes.put("description", "VoLTE");
149         e2eAttributes.put("category", "service");
150         e2eAttributes.put("subcategory", "");
151         e2eAttributes.put("version", "");
152         when(toscaTemplate.getMetaData()).thenReturn(new Metadata(e2eAttributes));
153         when(toscaTemplate.getInputs()).thenReturn(new ArrayList<>());
154         NodeTemplate nodeTemplate = mock(NodeTemplate.class);
155
156         Map<String, Object> nodeUUIDAttr = new HashMap<>();
157
158         nodeUUIDAttr.put("UUID", nodeUUID);
159         when(nodeTemplate.getMetaData()).thenReturn(new Metadata(nodeUUIDAttr));
160
161         ArrayList<NodeTemplate> nodeTemplates = new ArrayList<>();
162 //        nodeTemplates.add(nodeTemplate);
163         when(toscaTemplate.getNodeTemplates()).thenReturn(nodeTemplates);
164
165         return toscaTemplate;
166     }
167
168     private ToscaTemplate nodeToscaTemplate(String nodeUUID) {
169         ToscaTemplate toscaTemplate = mock(ToscaTemplate.class);
170         Map<String, Object> Attributes = new HashMap<>();
171         Attributes.put("invariantUUID", nodeUUID);
172         Attributes.put("UUID", nodeUUID);
173         Attributes.put("name", "");
174         Attributes.put("type", "");
175         Attributes.put("description", "");
176         Attributes.put("category", "service");
177         Attributes.put("subcategory", "");
178         when(toscaTemplate.getMetaData()).thenReturn(new Metadata(Attributes));
179
180         Input input = mock(Input.class);
181         when(input.getName()).thenReturn("field_name");
182         when(input.getDescription()).thenReturn("field_description");
183         when(input.getType()).thenReturn("field_type");
184         when(input.getDefault()).thenReturn("field_default");
185         when(input.isRequired()).thenReturn(true);
186
187         ArrayList<Input> inputs = new ArrayList<>();
188         inputs.add(input);
189         when(toscaTemplate.getInputs()).thenReturn(inputs);
190         when(toscaTemplate.getNodeTemplates()).thenReturn(new ArrayList<>());
191
192         return toscaTemplate;
193     }
194
195     private AAIService newAAIService(List<VimInfo> vim) {
196         AAIService aaiService = mock(AAIService.class);
197         VimInfoRsp rsp = new VimInfoRsp();
198         rsp.setCloudRegion(vim);
199         Call<VimInfoRsp> vimCall = successfulCall(rsp);
200         when(aaiService.listVimInfo()).thenReturn(vimCall);
201         return aaiService;
202     }
203
204     @Test(expected = SDCCatalogException.class)
205     public void retrieveInputsWillThrowExceptionWhenDownloadFailed() {
206         ServiceTemplateService service = new DefaultServiceTemplateService(null, null) {
207             @Override
208             protected void downloadFile(String templateUrl, String toPath) throws IOException {
209                 throw new IOException("download failed!");
210             }
211         };
212         service.fetchServiceTemplateInput("1", "url");
213     }
214
215     @Test(expected = SDCCatalogException.class)
216     public void retrieveInputsWillThrowExceptionWhenParsingToscaTemplateFailed() {
217         ServiceTemplateService service = new DefaultServiceTemplateService(null, null) {
218             @Override
219             protected void downloadFile(String templateUrl, String toPath) throws IOException {
220                 // download successfully...
221             }
222
223             @Override
224             protected ToscaTemplate translateToToscaTemplate(String toPath) throws JToscaException {
225                 throw new JToscaException("parse tosca template failed!", "123");
226             }
227         };
228         service.fetchServiceTemplateInput("1", "url");
229     }
230
231     @Test
232     public void itCanListVim() {
233         List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "region"));
234         VimInfoRsp rsp = new VimInfoRsp();
235         rsp.setCloudRegion(vim);
236         AAIService aaiService = mock(AAIService.class);
237         when(aaiService.listVimInfo()).thenReturn(successfulCall(rsp));
238
239         ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
240
241         Assert.assertSame(vim, service.listVim());
242     }
243
244     @Test
245     public void itCanRetrieveEmptyListWhenNoVimInfoInAAI() {
246         AAIService aaiService = mock(AAIService.class);
247         when(aaiService.listVimInfo()).thenReturn(emptyBodyCall());
248
249         ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
250         List<VimInfo> vimInfos = service.listVim();
251
252         Assert.assertTrue("vim should be empty.", vimInfos.isEmpty());
253     }
254
255     @Test(expected = AAIException.class)
256     public void itCanThrowExceptionWhenAAIServiceIsNotAvailable() {
257         AAIService aaiService = mock(AAIService.class);
258         when(aaiService.listVimInfo()).thenReturn(failedCall("AAI is not available!"));
259
260         ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
261         service.listVim();
262     }
263
264     @Test
265     public void itCanListSDNController() {
266         List<SDNCController> controllers = Collections.singletonList(new SDNCController());
267         SDNCControllerRsp rsp = new SDNCControllerRsp();
268         rsp.setEsrThirdpartySdncList(controllers);
269         AAIService aaiService = mock(AAIService.class);
270         when(aaiService.listSdncControllers()).thenReturn(successfulCall(rsp));
271
272         ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
273
274         Assert.assertSame(controllers, service.listSDNCControllers());
275     }
276
277     @Test
278     public void itCanRetrieveEmptyListWhenNoSDNControllerInAAI() {
279         AAIService aaiService = mock(AAIService.class);
280         when(aaiService.listSdncControllers()).thenReturn(emptyBodyCall());
281
282         ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
283         List<SDNCController> controllers = service.listSDNCControllers();
284
285         Assert.assertTrue("sdn controller should be empty.", controllers.isEmpty());
286     }
287
288     @Test(expected = AAIException.class)
289     public void itListSDNControllerThrowExceptionWhenAAIServiceIsNotAvailable() {
290         AAIService aaiService = mock(AAIService.class);
291         when(aaiService.listSdncControllers()).thenReturn(failedCall("AAI is not available!"));
292
293         ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
294         service.listSDNCControllers();
295     }
296     
297     @Test(expected = AAIException.class)
298     public void testDownloadFile() throws IOException {
299         dsts.downloadFile("toscaModelPath", "toPath");
300     }
301 }