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.Test;
20 import org.onap.usecaseui.server.bean.lcm.ServiceTemplateInput;
21 import org.onap.usecaseui.server.bean.lcm.TemplateInput;
22 import org.onap.usecaseui.server.service.lcm.ServiceTemplateService;
23 import org.onap.usecaseui.server.service.lcm.domain.aai.AAIService;
24 import org.onap.usecaseui.server.service.lcm.domain.aai.bean.SDNCController;
25 import org.onap.usecaseui.server.service.lcm.domain.aai.bean.SDNCControllerRsp;
26 import org.onap.usecaseui.server.service.lcm.domain.aai.bean.VimInfo;
27 import org.onap.usecaseui.server.service.lcm.domain.aai.bean.VimInfoRsp;
28 import org.onap.usecaseui.server.service.lcm.domain.aai.exceptions.AAIException;
29 import org.onap.usecaseui.server.service.lcm.domain.sdc.SDCCatalogService;
30 import org.onap.usecaseui.server.service.lcm.domain.sdc.bean.SDCServiceTemplate;
31 import org.onap.usecaseui.server.service.lcm.domain.sdc.exceptions.SDCCatalogException;
32 import org.openecomp.sdc.toscaparser.api.NodeTemplate;
33 import org.openecomp.sdc.toscaparser.api.ToscaTemplate;
34 import org.openecomp.sdc.toscaparser.api.common.JToscaException;
35 import org.openecomp.sdc.toscaparser.api.elements.Metadata;
36 import org.openecomp.sdc.toscaparser.api.parameters.Input;
37 import retrofit2.Call;
39 import java.io.IOException;
42 import static org.hamcrest.Matchers.equalTo;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.when;
45 import static org.onap.usecaseui.server.service.lcm.domain.sdc.consts.SDCConsts.CATEGORY_E2E_SERVICE;
46 import static org.onap.usecaseui.server.service.lcm.domain.sdc.consts.SDCConsts.DISTRIBUTION_STATUS_DISTRIBUTED;
47 import static org.onap.usecaseui.server.util.CallStub.emptyBodyCall;
48 import static org.onap.usecaseui.server.util.CallStub.failedCall;
49 import static org.onap.usecaseui.server.util.CallStub.successfulCall;
51 public class DefaultServiceTemplateServiceTest {
54 public void itCanListDistributedServiceTemplate() {
55 List<SDCServiceTemplate> templates = Collections.singletonList(new SDCServiceTemplate("uuid", "uuid", "name", "V1","url", "category"));
56 SDCCatalogService sdcService = mock(SDCCatalogService.class);
57 when(sdcService.listServices(CATEGORY_E2E_SERVICE, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(successfulCall(templates));
59 ServiceTemplateService service = new DefaultServiceTemplateService(sdcService,null);
61 Assert.assertSame(templates, service.listDistributedServiceTemplate());
64 @Test(expected = SDCCatalogException.class)
65 public void retrieveServiceWillThrowExceptionWhenSDCIsNotAvailable() {
66 SDCCatalogService sdcService = mock(SDCCatalogService.class);
67 when(sdcService.listServices(CATEGORY_E2E_SERVICE, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(failedCall("SDC is not available!"));
69 ServiceTemplateService service = new DefaultServiceTemplateService(sdcService,null);
70 service.listDistributedServiceTemplate();
74 public void itWillRetrieveEmptyWhenNoServiceTemplateCanGet() {
75 SDCCatalogService sdcService = mock(SDCCatalogService.class);
76 when(sdcService.listServices(CATEGORY_E2E_SERVICE, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(emptyBodyCall());
78 ServiceTemplateService service = new DefaultServiceTemplateService(sdcService,null);
79 List<SDCServiceTemplate> sdcServiceTemplates = service.listDistributedServiceTemplate();
81 Assert.assertTrue("service templates should be empty.", sdcServiceTemplates.isEmpty());
85 public void itCanRetrieveInputsFromServiceTemplate() throws IOException {
86 final String uuid = "1";
87 String modelPath = "model_path";
88 String nodeUUID = "2";
90 SDCCatalogService sdcService = newSdcCatalogService(nodeUUID);
92 List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
93 AAIService aaiService = newAAIService(vim);
95 ServiceTemplateService service = newServiceTemplateService(uuid, nodeUUID, sdcService, aaiService);
97 Assert.assertThat(service.fetchServiceTemplateInput(uuid, modelPath), equalTo(expectedServiceInputs(uuid, nodeUUID)));
100 private DefaultServiceTemplateService newServiceTemplateService(String uuid, String nodeUUID, SDCCatalogService sdcService, AAIService aaiService) {
101 return new DefaultServiceTemplateService(sdcService, aaiService) {
104 protected void downloadFile(String templateUrl, String toPath) throws IOException {
105 // download successfully...
109 protected ToscaTemplate translateToToscaTemplate(String toPath) throws JToscaException {
110 if (toPath.contains(uuid)) {
111 return e2eToscaTemplate(nodeUUID);
113 return nodeToscaTemplate(nodeUUID);
118 private SDCCatalogService newSdcCatalogService(String nodeUUID) throws IOException {
119 SDCCatalogService sdcService = mock(SDCCatalogService.class);
120 when(sdcService.getService(nodeUUID)).thenReturn(successfulCall(new SDCServiceTemplate(nodeUUID, nodeUUID, "node", "V1", "nodeModelUrl", "service")));
124 private ServiceTemplateInput expectedServiceInputs(String uuid, String nodeUUID) {
125 ServiceTemplateInput e2eServiceTemplateInput = new ServiceTemplateInput(
126 uuid, uuid, "VoLTE", "service","", "VoLTE", "service", "","", Collections.EMPTY_LIST);
127 TemplateInput templateInput = new TemplateInput("field_name","field_type", "field_description", "true", "field_default");
128 ServiceTemplateInput nodeTemplateInput = new ServiceTemplateInput(
129 nodeUUID, nodeUUID, "", "", "","", "service", "","", Collections.singletonList(templateInput));
130 // e2eServiceTemplateInput.addNestedTemplate(nodeTemplateInput);
131 return e2eServiceTemplateInput;
134 private ToscaTemplate e2eToscaTemplate(String nodeUUID) {
135 ToscaTemplate toscaTemplate = mock(ToscaTemplate.class);
136 Map<String, Object> e2eAttributes = new HashMap<>();
137 e2eAttributes.put("invariantUUID", "1");
138 e2eAttributes.put("UUID", "1");
139 e2eAttributes.put("name", "VoLTE");
140 e2eAttributes.put("type", "service");
141 e2eAttributes.put("description", "VoLTE");
142 e2eAttributes.put("category", "service");
143 e2eAttributes.put("subcategory", "");
144 e2eAttributes.put("version", "");
145 when(toscaTemplate.getMetaData()).thenReturn(new Metadata(e2eAttributes));
146 when(toscaTemplate.getInputs()).thenReturn(new ArrayList<>());
147 NodeTemplate nodeTemplate = mock(NodeTemplate.class);
149 Map<String, Object> nodeUUIDAttr = new HashMap<>();
151 nodeUUIDAttr.put("UUID", nodeUUID);
152 when(nodeTemplate.getMetaData()).thenReturn(new Metadata(nodeUUIDAttr));
154 ArrayList<NodeTemplate> nodeTemplates = new ArrayList<>();
155 // nodeTemplates.add(nodeTemplate);
156 when(toscaTemplate.getNodeTemplates()).thenReturn(nodeTemplates);
158 return toscaTemplate;
161 private ToscaTemplate nodeToscaTemplate(String nodeUUID) {
162 ToscaTemplate toscaTemplate = mock(ToscaTemplate.class);
163 Map<String, Object> Attributes = new HashMap<>();
164 Attributes.put("invariantUUID", nodeUUID);
165 Attributes.put("UUID", nodeUUID);
166 Attributes.put("name", "");
167 Attributes.put("type", "");
168 Attributes.put("description", "");
169 Attributes.put("category", "service");
170 Attributes.put("subcategory", "");
171 when(toscaTemplate.getMetaData()).thenReturn(new Metadata(Attributes));
173 Input input = mock(Input.class);
174 when(input.getName()).thenReturn("field_name");
175 when(input.getDescription()).thenReturn("field_description");
176 when(input.getType()).thenReturn("field_type");
177 when(input.getDefault()).thenReturn("field_default");
178 when(input.isRequired()).thenReturn(true);
180 ArrayList<Input> inputs = new ArrayList<>();
182 when(toscaTemplate.getInputs()).thenReturn(inputs);
183 when(toscaTemplate.getNodeTemplates()).thenReturn(new ArrayList<>());
185 return toscaTemplate;
188 private AAIService newAAIService(List<VimInfo> vim) {
189 AAIService aaiService = mock(AAIService.class);
190 VimInfoRsp rsp = new VimInfoRsp();
191 rsp.setCloudRegion(vim);
192 Call<VimInfoRsp> vimCall = successfulCall(rsp);
193 when(aaiService.listVimInfo()).thenReturn(vimCall);
197 @Test(expected = SDCCatalogException.class)
198 public void retrieveInputsWillThrowExceptionWhenDownloadFailed() {
199 ServiceTemplateService service = new DefaultServiceTemplateService(null, null) {
201 protected void downloadFile(String templateUrl, String toPath) throws IOException {
202 throw new IOException("download failed!");
205 service.fetchServiceTemplateInput("1", "url");
208 @Test(expected = SDCCatalogException.class)
209 public void retrieveInputsWillThrowExceptionWhenParsingToscaTemplateFailed() {
210 ServiceTemplateService service = new DefaultServiceTemplateService(null, null) {
212 protected void downloadFile(String templateUrl, String toPath) throws IOException {
213 // download successfully...
217 protected ToscaTemplate translateToToscaTemplate(String toPath) throws JToscaException {
218 throw new JToscaException("parse tosca template failed!", "123");
221 service.fetchServiceTemplateInput("1", "url");
225 public void itCanListVim() {
226 List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "region"));
227 VimInfoRsp rsp = new VimInfoRsp();
228 rsp.setCloudRegion(vim);
229 AAIService aaiService = mock(AAIService.class);
230 when(aaiService.listVimInfo()).thenReturn(successfulCall(rsp));
232 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
234 Assert.assertSame(vim, service.listVim());
238 public void itCanRetrieveEmptyListWhenNoVimInfoInAAI() {
239 AAIService aaiService = mock(AAIService.class);
240 when(aaiService.listVimInfo()).thenReturn(emptyBodyCall());
242 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
243 List<VimInfo> vimInfos = service.listVim();
245 Assert.assertTrue("vim should be empty.", vimInfos.isEmpty());
248 @Test(expected = AAIException.class)
249 public void itCanThrowExceptionWhenAAIServiceIsNotAvailable() {
250 AAIService aaiService = mock(AAIService.class);
251 when(aaiService.listVimInfo()).thenReturn(failedCall("AAI is not available!"));
253 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
258 public void itCanListSDNController() {
259 List<SDNCController> controllers = Collections.singletonList(new SDNCController());
260 SDNCControllerRsp rsp = new SDNCControllerRsp();
261 rsp.setEsrThirdpartySdncList(controllers);
262 AAIService aaiService = mock(AAIService.class);
263 when(aaiService.listSdncControllers()).thenReturn(successfulCall(rsp));
265 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
267 Assert.assertSame(controllers, service.listSDNCControllers());
271 public void itCanRetrieveEmptyListWhenNoSDNControllerInAAI() {
272 AAIService aaiService = mock(AAIService.class);
273 when(aaiService.listSdncControllers()).thenReturn(emptyBodyCall());
275 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
276 List<SDNCController> controllers = service.listSDNCControllers();
278 Assert.assertTrue("sdn controller should be empty.", controllers.isEmpty());
281 @Test(expected = AAIException.class)
282 public void itListSDNControllerThrowExceptionWhenAAIServiceIsNotAvailable() {
283 AAIService aaiService = mock(AAIService.class);
284 when(aaiService.listSdncControllers()).thenReturn(failedCall("AAI is not available!"));
286 ServiceTemplateService service = new DefaultServiceTemplateService(null,aaiService);
287 service.listSDNCControllers();