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.AAIClient;
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.mockito.ArgumentMatchers.anyString;
49 import static org.mockito.Mockito.mock;
50 import static org.mockito.Mockito.when;
51 import static org.onap.usecaseui.server.service.lcm.domain.sdc.consts.SDCConsts.CATEGORY_E2E_SERVICE;
52 import static org.onap.usecaseui.server.service.lcm.domain.sdc.consts.SDCConsts.DISTRIBUTION_STATUS_DISTRIBUTED;
53 import static org.onap.usecaseui.server.util.CallStub.emptyBodyCall;
54 import static org.onap.usecaseui.server.util.CallStub.failedCall;
55 import static org.onap.usecaseui.server.util.CallStub.successfulCall;
57 public class DefaultServiceTemplateServiceTest {
60 public void itCanListDistributedServiceTemplate() {
61 List<SDCServiceTemplate> templates = Collections.singletonList(new SDCServiceTemplate("uuid", "uuid", "name", "V1","url", "category"));
62 SDCCatalogService sdcService = mock(SDCCatalogService.class);
63 when(sdcService.listServices(CATEGORY_E2E_SERVICE, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(successfulCall(templates));
65 ServiceTemplateService service = new DefaultServiceTemplateService(sdcService,null);
67 Assert.assertSame(templates, service.listDistributedServiceTemplate());
70 @Test(expected = SDCCatalogException.class)
71 public void retrieveServiceWillThrowExceptionWhenSDCIsNotAvailable() {
72 SDCCatalogService sdcService = mock(SDCCatalogService.class);
73 when(sdcService.listServices(CATEGORY_E2E_SERVICE, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(failedCall("SDC is not available!"));
75 ServiceTemplateService service = new DefaultServiceTemplateService(sdcService,null);
76 service.listDistributedServiceTemplate();
80 public void itWillRetrieveEmptyWhenNoServiceTemplateCanGet() {
81 SDCCatalogService sdcService = mock(SDCCatalogService.class);
82 when(sdcService.listServices(CATEGORY_E2E_SERVICE, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(emptyBodyCall());
84 ServiceTemplateService service = new DefaultServiceTemplateService(sdcService,null);
85 List<SDCServiceTemplate> sdcServiceTemplates = service.listDistributedServiceTemplate();
87 Assert.assertTrue("service templates should be empty.", sdcServiceTemplates.isEmpty());
91 public void itCanRetrieveInputsFromServiceTemplate() throws IOException {
92 final String uuid = "1";
93 String modelPath = "model_path";
94 String nodeUUID = "2";
96 SDCCatalogService sdcService = newSdcCatalogService(nodeUUID);
98 List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
99 AAIClient aaiClient = newAAIService(vim);
101 ServiceTemplateService service = newServiceTemplateService(uuid, nodeUUID, sdcService, aaiClient);
103 Assert.assertNotNull(service.fetchServiceTemplateInput(uuid, modelPath)); }
105 private DefaultServiceTemplateService newServiceTemplateService(String uuid, String nodeUUID, SDCCatalogService sdcService, AAIClient aaiClient) {
106 return new DefaultServiceTemplateService(sdcService, aaiClient) {
109 protected void downloadFile(String templateUrl, String toPath) throws IOException {
110 // download successfully...
114 protected ToscaTemplate translateToToscaTemplate(String toPath) throws JToscaException {
115 if (toPath.contains(uuid)) {
116 return e2eToscaTemplate(nodeUUID);
118 return nodeToscaTemplate(nodeUUID);
123 private SDCCatalogService newSdcCatalogService(String nodeUUID) throws IOException {
124 SDCCatalogService sdcService = mock(SDCCatalogService.class);
125 when(sdcService.getService(nodeUUID)).thenReturn(successfulCall(new SDCServiceTemplate(nodeUUID, nodeUUID, "node", "V1", "nodeModelUrl", "service")));
129 private ServiceTemplateInput expectedServiceInputs(String uuid, String nodeUUID) {
130 ServiceTemplateInput e2eServiceTemplateInput = new ServiceTemplateInput(
131 uuid, uuid, "VoLTE", "service","", "VoLTE", "service", "","", Collections.EMPTY_LIST);
132 TemplateInput templateInput = new TemplateInput("field_name","field_type", "field_description", "true", "field_default");
133 ServiceTemplateInput nodeTemplateInput = new ServiceTemplateInput(
134 nodeUUID, nodeUUID, "", "", "","", "service", "","", Collections.singletonList(templateInput));
135 // e2eServiceTemplateInput.addNestedTemplate(nodeTemplateInput);
136 return e2eServiceTemplateInput;
139 private ToscaTemplate e2eToscaTemplate(String nodeUUID) {
140 ToscaTemplate toscaTemplate = mock(ToscaTemplate.class);
141 Map<String, Object> e2eAttributes = new HashMap<>();
142 e2eAttributes.put("invariantUUID", "1");
143 e2eAttributes.put("UUID", "1");
144 e2eAttributes.put("name", "VoLTE");
145 e2eAttributes.put("type", "service");
146 e2eAttributes.put("description", "VoLTE");
147 e2eAttributes.put("category", "service");
148 e2eAttributes.put("subcategory", "");
149 e2eAttributes.put("version", "");
150 when(toscaTemplate.getMetaData()).thenReturn(new Metadata(e2eAttributes));
151 when(toscaTemplate.getInputs()).thenReturn(new ArrayList<>());
152 NodeTemplate nodeTemplate = mock(NodeTemplate.class);
154 Map<String, Object> nodeUUIDAttr = new HashMap<>();
156 nodeUUIDAttr.put("UUID", nodeUUID);
157 when(nodeTemplate.getMetaData()).thenReturn(new Metadata(nodeUUIDAttr));
159 ArrayList<NodeTemplate> nodeTemplates = new ArrayList<>();
160 // nodeTemplates.add(nodeTemplate);
161 when(toscaTemplate.getNodeTemplates()).thenReturn(nodeTemplates);
163 return toscaTemplate;
166 private ToscaTemplate nodeToscaTemplate(String nodeUUID) {
167 ToscaTemplate toscaTemplate = mock(ToscaTemplate.class);
168 Map<String, Object> Attributes = new HashMap<>();
169 Attributes.put("invariantUUID", nodeUUID);
170 Attributes.put("UUID", nodeUUID);
171 Attributes.put("name", "");
172 Attributes.put("type", "");
173 Attributes.put("description", "");
174 Attributes.put("category", "service");
175 Attributes.put("subcategory", "");
176 when(toscaTemplate.getMetaData()).thenReturn(new Metadata(Attributes));
178 Input input = mock(Input.class);
179 when(input.getName()).thenReturn("field_name");
180 when(input.getDescription()).thenReturn("field_description");
181 when(input.getType()).thenReturn("field_type");
182 when(input.getDefault()).thenReturn("field_default");
183 when(input.isRequired()).thenReturn(true);
185 ArrayList<Input> inputs = new ArrayList<>();
187 when(toscaTemplate.getInputs()).thenReturn(inputs);
188 when(toscaTemplate.getNodeTemplates()).thenReturn(new ArrayList<>());
190 return toscaTemplate;
193 private AAIClient newAAIService(List<VimInfo> vim) {
194 AAIClient aaiClient = mock(AAIClient.class);
195 VimInfoRsp rsp = new VimInfoRsp();
196 rsp.setCloudRegion(vim);
197 Call<VimInfoRsp> vimCall = successfulCall(rsp);
198 when(aaiClient.listVimInfo()).thenReturn(vimCall);
202 @Test(expected = SDCCatalogException.class)
203 public void retrieveInputsWillThrowExceptionWhenDownloadFailed() {
204 ServiceTemplateService service = new DefaultServiceTemplateService(null, null) {
206 protected void downloadFile(String templateUrl, String toPath) throws IOException {
207 throw new IOException("download failed!");
210 service.fetchServiceTemplateInput("1", "url");
213 @Test(expected = SDCCatalogException.class)
214 public void retrieveInputsWillThrowExceptionWhenParsingToscaTemplateFailed() {
215 ServiceTemplateService service = new DefaultServiceTemplateService(null, null) {
217 protected void downloadFile(String templateUrl, String toPath) throws IOException {
218 // download successfully...
222 protected ToscaTemplate translateToToscaTemplate(String toPath) throws JToscaException {
223 throw new JToscaException("parse tosca template failed!", "123");
226 service.fetchServiceTemplateInput("1", "url");
230 public void itCanListVim() {
231 List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "region"));
232 VimInfoRsp rsp = new VimInfoRsp();
233 rsp.setCloudRegion(vim);
234 AAIClient aaiClient = mock(AAIClient.class);
235 when(aaiClient.listVimInfo()).thenReturn(successfulCall(rsp));
237 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiClient);
239 Assert.assertSame(vim, service.listVim());
243 public void itCanRetrieveEmptyListWhenNoVimInfoInAAI() {
244 AAIClient aaiClient = mock(AAIClient.class);
245 when(aaiClient.listVimInfo()).thenReturn(emptyBodyCall());
247 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiClient);
248 List<VimInfo> vimInfos = service.listVim();
250 Assert.assertTrue("vim should be empty.", vimInfos.isEmpty());
253 @Test(expected = AAIException.class)
254 public void itCanThrowExceptionWhenAAIServiceIsNotAvailable() {
255 AAIClient aaiClient = mock(AAIClient.class);
256 when(aaiClient.listVimInfo()).thenReturn(failedCall("AAI is not available!"));
258 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiClient);
263 public void itCanListSDNController() {
264 List<SDNCController> controllers = Collections.singletonList(new SDNCController());
265 SDNCControllerRsp rsp = new SDNCControllerRsp();
266 rsp.setEsrThirdpartySdncList(controllers);
267 AAIClient aaiClient = mock(AAIClient.class);
268 when(aaiClient.listSdncControllers()).thenReturn(successfulCall(rsp));
270 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiClient);
272 Assert.assertSame(controllers, service.listSDNCControllers());
276 public void itCanRetrieveEmptyListWhenNoSDNControllerInAAI() {
277 AAIClient aaiClient = mock(AAIClient.class);
278 when(aaiClient.listSdncControllers()).thenReturn(emptyBodyCall());
280 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiClient);
281 List<SDNCController> controllers = service.listSDNCControllers();
283 Assert.assertTrue("sdn controller should be empty.", controllers.isEmpty());
286 @Test(expected = AAIException.class)
287 public void itListSDNControllerThrowExceptionWhenAAIServiceIsNotAvailable() {
288 AAIClient aaiClient = mock(AAIClient.class);
289 when(aaiClient.listSdncControllers()).thenReturn(failedCall("AAI is not available!"));
291 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiClient);
292 service.listSDNCControllers();
295 public void testDownloadFile() throws IOException {
296 SDCCatalogService sdcService = mock(SDCCatalogService.class);
297 ResponseBody result= new ResponseBody() {
300 public MediaType contentType() {
301 return MediaType.parse("application/json; charset=utf-8");
305 public long contentLength() {
311 public BufferedSource source() {
316 DefaultServiceTemplateService dsts = new DefaultServiceTemplateService(sdcService,null);
317 when(sdcService.downloadCsar(anyString())).thenReturn(successfulCall(result));
318 dsts.downloadFile("toscaModelPath", "toPath");