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 {
55 DefaultServiceTemplateService dsts = null;
57 public void before() throws Exception {
58 dsts = new DefaultServiceTemplateService();
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));
66 ServiceTemplateService service = new DefaultServiceTemplateService(sdcService,null);
68 Assert.assertSame(templates, service.listDistributedServiceTemplate());
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!"));
76 ServiceTemplateService service = new DefaultServiceTemplateService(sdcService,null);
77 service.listDistributedServiceTemplate();
81 public void itWillRetrieveEmptyWhenNoServiceTemplateCanGet() {
82 SDCCatalogService sdcService = mock(SDCCatalogService.class);
83 when(sdcService.listServices(CATEGORY_E2E_SERVICE, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(emptyBodyCall());
85 ServiceTemplateService service = new DefaultServiceTemplateService(sdcService,null);
86 List<SDCServiceTemplate> sdcServiceTemplates = service.listDistributedServiceTemplate();
88 Assert.assertTrue("service templates should be empty.", sdcServiceTemplates.isEmpty());
92 public void itCanRetrieveInputsFromServiceTemplate() throws IOException {
93 final String uuid = "1";
94 String modelPath = "model_path";
95 String nodeUUID = "2";
97 SDCCatalogService sdcService = newSdcCatalogService(nodeUUID);
99 List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
100 AAIService aaiService = newAAIService(vim);
102 ServiceTemplateService service = newServiceTemplateService(uuid, nodeUUID, sdcService, aaiService);
104 Assert.assertThat(service.fetchServiceTemplateInput(uuid, modelPath), equalTo(expectedServiceInputs(uuid, nodeUUID)));
107 private DefaultServiceTemplateService newServiceTemplateService(String uuid, String nodeUUID, SDCCatalogService sdcService, AAIService aaiService) {
108 return new DefaultServiceTemplateService(sdcService, aaiService) {
111 protected void downloadFile(String templateUrl, String toPath) throws IOException {
112 // download successfully...
116 protected ToscaTemplate translateToToscaTemplate(String toPath) throws JToscaException {
117 if (toPath.contains(uuid)) {
118 return e2eToscaTemplate(nodeUUID);
120 return nodeToscaTemplate(nodeUUID);
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")));
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;
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);
156 Map<String, Object> nodeUUIDAttr = new HashMap<>();
158 nodeUUIDAttr.put("UUID", nodeUUID);
159 when(nodeTemplate.getMetaData()).thenReturn(new Metadata(nodeUUIDAttr));
161 ArrayList<NodeTemplate> nodeTemplates = new ArrayList<>();
162 // nodeTemplates.add(nodeTemplate);
163 when(toscaTemplate.getNodeTemplates()).thenReturn(nodeTemplates);
165 return toscaTemplate;
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));
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);
187 ArrayList<Input> inputs = new ArrayList<>();
189 when(toscaTemplate.getInputs()).thenReturn(inputs);
190 when(toscaTemplate.getNodeTemplates()).thenReturn(new ArrayList<>());
192 return toscaTemplate;
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);
204 @Test(expected = SDCCatalogException.class)
205 public void retrieveInputsWillThrowExceptionWhenDownloadFailed() {
206 ServiceTemplateService service = new DefaultServiceTemplateService(null, null) {
208 protected void downloadFile(String templateUrl, String toPath) throws IOException {
209 throw new IOException("download failed!");
212 service.fetchServiceTemplateInput("1", "url");
215 @Test(expected = SDCCatalogException.class)
216 public void retrieveInputsWillThrowExceptionWhenParsingToscaTemplateFailed() {
217 ServiceTemplateService service = new DefaultServiceTemplateService(null, null) {
219 protected void downloadFile(String templateUrl, String toPath) throws IOException {
220 // download successfully...
224 protected ToscaTemplate translateToToscaTemplate(String toPath) throws JToscaException {
225 throw new JToscaException("parse tosca template failed!", "123");
228 service.fetchServiceTemplateInput("1", "url");
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));
239 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
241 Assert.assertSame(vim, service.listVim());
245 public void itCanRetrieveEmptyListWhenNoVimInfoInAAI() {
246 AAIService aaiService = mock(AAIService.class);
247 when(aaiService.listVimInfo()).thenReturn(emptyBodyCall());
249 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
250 List<VimInfo> vimInfos = service.listVim();
252 Assert.assertTrue("vim should be empty.", vimInfos.isEmpty());
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!"));
260 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
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));
272 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
274 Assert.assertSame(controllers, service.listSDNCControllers());
278 public void itCanRetrieveEmptyListWhenNoSDNControllerInAAI() {
279 AAIService aaiService = mock(AAIService.class);
280 when(aaiService.listSdncControllers()).thenReturn(emptyBodyCall());
282 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
283 List<SDNCController> controllers = service.listSDNCControllers();
285 Assert.assertTrue("sdn controller should be empty.", controllers.isEmpty());
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!"));
293 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
294 service.listSDNCControllers();
297 @Test(expected = AAIException.class)
298 public void testDownloadFile() throws IOException {
299 dsts.downloadFile("toscaModelPath", "toPath");