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 okhttp3.MediaType;
19 import okhttp3.ResponseBody;
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;
45 import java.io.IOException;
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;
58 public class DefaultServiceTemplateServiceTest {
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.assertNotNull(service.fetchServiceTemplateInput(uuid, modelPath)); }
106 private DefaultServiceTemplateService newServiceTemplateService(String uuid, String nodeUUID, SDCCatalogService sdcService, AAIService aaiService) {
107 return new DefaultServiceTemplateService(sdcService, aaiService) {
110 protected void downloadFile(String templateUrl, String toPath) throws IOException {
111 // download successfully...
115 protected ToscaTemplate translateToToscaTemplate(String toPath) throws JToscaException {
116 if (toPath.contains(uuid)) {
117 return e2eToscaTemplate(nodeUUID);
119 return nodeToscaTemplate(nodeUUID);
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")));
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;
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);
155 Map<String, Object> nodeUUIDAttr = new HashMap<>();
157 nodeUUIDAttr.put("UUID", nodeUUID);
158 when(nodeTemplate.getMetaData()).thenReturn(new Metadata(nodeUUIDAttr));
160 ArrayList<NodeTemplate> nodeTemplates = new ArrayList<>();
161 // nodeTemplates.add(nodeTemplate);
162 when(toscaTemplate.getNodeTemplates()).thenReturn(nodeTemplates);
164 return toscaTemplate;
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));
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);
186 ArrayList<Input> inputs = new ArrayList<>();
188 when(toscaTemplate.getInputs()).thenReturn(inputs);
189 when(toscaTemplate.getNodeTemplates()).thenReturn(new ArrayList<>());
191 return toscaTemplate;
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);
203 @Test(expected = SDCCatalogException.class)
204 public void retrieveInputsWillThrowExceptionWhenDownloadFailed() {
205 ServiceTemplateService service = new DefaultServiceTemplateService(null, null) {
207 protected void downloadFile(String templateUrl, String toPath) throws IOException {
208 throw new IOException("download failed!");
211 service.fetchServiceTemplateInput("1", "url");
214 @Test(expected = SDCCatalogException.class)
215 public void retrieveInputsWillThrowExceptionWhenParsingToscaTemplateFailed() {
216 ServiceTemplateService service = new DefaultServiceTemplateService(null, null) {
218 protected void downloadFile(String templateUrl, String toPath) throws IOException {
219 // download successfully...
223 protected ToscaTemplate translateToToscaTemplate(String toPath) throws JToscaException {
224 throw new JToscaException("parse tosca template failed!", "123");
227 service.fetchServiceTemplateInput("1", "url");
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));
238 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
240 Assert.assertSame(vim, service.listVim());
244 public void itCanRetrieveEmptyListWhenNoVimInfoInAAI() {
245 AAIService aaiService = mock(AAIService.class);
246 when(aaiService.listVimInfo()).thenReturn(emptyBodyCall());
248 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
249 List<VimInfo> vimInfos = service.listVim();
251 Assert.assertTrue("vim should be empty.", vimInfos.isEmpty());
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!"));
259 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
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));
271 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
273 Assert.assertSame(controllers, service.listSDNCControllers());
277 public void itCanRetrieveEmptyListWhenNoSDNControllerInAAI() {
278 AAIService aaiService = mock(AAIService.class);
279 when(aaiService.listSdncControllers()).thenReturn(emptyBodyCall());
281 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
282 List<SDNCController> controllers = service.listSDNCControllers();
284 Assert.assertTrue("sdn controller should be empty.", controllers.isEmpty());
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!"));
292 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
293 service.listSDNCControllers();
296 public void testDownloadFile() throws IOException {
297 SDCCatalogService sdcService = mock(SDCCatalogService.class);
298 ResponseBody result= new ResponseBody() {
301 public MediaType contentType() {
302 return MediaType.parse("application/json; charset=utf-8");
306 public long contentLength() {
312 public BufferedSource source() {
317 DefaultServiceTemplateService dsts = new DefaultServiceTemplateService(sdcService,null);
318 when(sdcService.downloadCsar(anyString())).thenReturn(successfulCall(result));
319 dsts.downloadFile("toscaModelPath", "toPath");