2 * Copyright 2016-2017 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.usecaseui.server.service.lcm.impl;
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;
41 import java.io.IOException;
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;
53 public class DefaultServiceTemplateServiceTest {
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));
61 ServiceTemplateService service = new DefaultServiceTemplateService(sdcService,null);
63 Assert.assertSame(templates, service.listDistributedServiceTemplate());
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!"));
71 ServiceTemplateService service = new DefaultServiceTemplateService(sdcService,null);
72 service.listDistributedServiceTemplate();
76 public void itWillRetrieveEmptyWhenNoServiceTemplateCanGet() {
77 SDCCatalogService sdcService = mock(SDCCatalogService.class);
78 when(sdcService.listServices(CATEGORY_E2E_SERVICE, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(emptyBodyCall());
80 ServiceTemplateService service = new DefaultServiceTemplateService(sdcService,null);
81 List<SDCServiceTemplate> sdcServiceTemplates = service.listDistributedServiceTemplate();
83 Assert.assertTrue("service templates should be empty.", sdcServiceTemplates.isEmpty());
87 public void itCanRetrieveInputsFromServiceTemplate() throws IOException {
88 final String uuid = "1";
89 String modelPath = "model_path";
90 String nodeUUID = "2";
92 SDCCatalogService sdcService = newSdcCatalogService(nodeUUID);
94 List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
95 AAIService aaiService = newAAIService(vim);
97 ServiceTemplateService service = newServiceTemplateService(uuid, nodeUUID, sdcService, aaiService);
99 Assert.assertThat(service.fetchServiceTemplateInput(uuid, modelPath), equalTo(expectedServiceInputs(uuid, nodeUUID)));
102 private DefaultServiceTemplateService newServiceTemplateService(String uuid, String nodeUUID, SDCCatalogService sdcService, AAIService aaiService) {
103 return new DefaultServiceTemplateService(sdcService, aaiService) {
106 protected void downloadFile(String templateUrl, String toPath) throws IOException {
107 // download successfully...
111 protected ToscaTemplate translateToToscaTemplate(String toPath) throws JToscaException {
112 if (toPath.contains(uuid)) {
113 return e2eToscaTemplate(nodeUUID);
115 return nodeToscaTemplate(nodeUUID);
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")));
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;
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);
151 Map<String, Object> nodeUUIDAttr = new HashMap<>();
153 nodeUUIDAttr.put("UUID", nodeUUID);
154 when(nodeTemplate.getMetaData()).thenReturn(new Metadata(nodeUUIDAttr));
156 ArrayList<NodeTemplate> nodeTemplates = new ArrayList<>();
157 // nodeTemplates.add(nodeTemplate);
158 when(toscaTemplate.getNodeTemplates()).thenReturn(nodeTemplates);
160 return toscaTemplate;
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));
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);
182 ArrayList<Input> inputs = new ArrayList<>();
184 when(toscaTemplate.getInputs()).thenReturn(inputs);
185 when(toscaTemplate.getNodeTemplates()).thenReturn(new ArrayList<>());
187 return toscaTemplate;
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);
199 @Test(expected = SDCCatalogException.class)
200 public void retrieveInputsWillThrowExceptionWhenDownloadFailed() {
201 ServiceTemplateService service = new DefaultServiceTemplateService(null, null) {
203 protected void downloadFile(String templateUrl, String toPath) throws IOException {
204 throw new IOException("download failed!");
207 service.fetchServiceTemplateInput("1", "url");
210 @Test(expected = SDCCatalogException.class)
211 public void retrieveInputsWillThrowExceptionWhenParsingToscaTemplateFailed() {
212 ServiceTemplateService service = new DefaultServiceTemplateService(null, null) {
214 protected void downloadFile(String templateUrl, String toPath) throws IOException {
215 // download successfully...
219 protected ToscaTemplate translateToToscaTemplate(String toPath) throws JToscaException {
220 throw new JToscaException("parse tosca template failed!", "123");
223 service.fetchServiceTemplateInput("1", "url");
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));
234 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
236 Assert.assertSame(vim, service.listVim());
240 public void itCanRetrieveEmptyListWhenNoVimInfoInAAI() {
241 AAIService aaiService = mock(AAIService.class);
242 when(aaiService.listVimInfo()).thenReturn(emptyBodyCall());
244 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
245 List<VimInfo> vimInfos = service.listVim();
247 Assert.assertTrue("vim should be empty.", vimInfos.isEmpty());
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!"));
255 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
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));
267 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
269 Assert.assertSame(controllers, service.listSDNCControllers());
273 public void itCanRetrieveEmptyListWhenNoSDNControllerInAAI() {
274 AAIService aaiService = mock(AAIService.class);
275 when(aaiService.listSdncControllers()).thenReturn(emptyBodyCall());
277 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
278 List<SDNCController> controllers = service.listSDNCControllers();
280 Assert.assertTrue("sdn controller should be empty.", controllers.isEmpty());
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!"));
288 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
289 service.listSDNCControllers();
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");