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.ServiceTemplateInputRsp;
22 import org.onap.usecaseui.server.bean.lcm.TemplateInput;
23 import org.onap.usecaseui.server.service.lcm.ServiceTemplateService;
24 import org.onap.usecaseui.server.service.lcm.domain.aai.AAIService;
25 import org.onap.usecaseui.server.service.lcm.domain.aai.bean.VimInfo;
26 import org.onap.usecaseui.server.service.lcm.domain.sdc.SDCCatalogService;
27 import org.onap.usecaseui.server.service.lcm.domain.sdc.bean.SDCServiceTemplate;
28 import org.onap.usecaseui.server.service.lcm.domain.sdc.exceptions.SDCCatalogException;
29 import org.openecomp.sdc.toscaparser.api.NodeTemplate;
30 import org.openecomp.sdc.toscaparser.api.ToscaTemplate;
31 import org.openecomp.sdc.toscaparser.api.common.JToscaException;
32 import org.openecomp.sdc.toscaparser.api.elements.Metadata;
33 import org.openecomp.sdc.toscaparser.api.parameters.Input;
34 import retrofit2.Call;
36 import java.io.IOException;
39 import static org.hamcrest.Matchers.equalTo;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.when;
42 import static org.onap.usecaseui.server.service.lcm.domain.sdc.consts.SDCConsts.CATEGORY_E2E_SERVICE;
43 import static org.onap.usecaseui.server.service.lcm.domain.sdc.consts.SDCConsts.DISTRIBUTION_STATUS_DISTRIBUTED;
44 import static org.onap.usecaseui.server.util.CallStub.failedCall;
45 import static org.onap.usecaseui.server.util.CallStub.successfulCall;
47 public class DefaultServiceTemplateServiceTest {
50 public void itCanListDistributedServiceTemplate() {
51 List<SDCServiceTemplate> templates = Collections.singletonList(new SDCServiceTemplate("uuid", "uuid", "name", "url", "category"));
52 SDCCatalogService sdcService = mock(SDCCatalogService.class);
53 when(sdcService.listServices(CATEGORY_E2E_SERVICE, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(successfulCall(templates));
55 ServiceTemplateService service = new DefaultServiceTemplateService(sdcService,null);
57 Assert.assertSame(templates, service.listDistributedServiceTemplate());
60 @Test(expected = SDCCatalogException.class)
61 public void retrieveServiceWillThrowExceptionWhenSDCIsNotAvailable() {
62 SDCCatalogService sdcService = mock(SDCCatalogService.class);
63 when(sdcService.listServices(CATEGORY_E2E_SERVICE, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(failedCall("SDC is not available!"));
65 ServiceTemplateService service = new DefaultServiceTemplateService(sdcService,null);
66 service.listDistributedServiceTemplate();
70 public void itCanRetrieveInputsFromServiceTemplate() throws IOException {
71 final String uuid = "1";
72 String modelPath = "model_path";
73 String nodeUUID = "2";
75 SDCCatalogService sdcService = newSdcCatalogService(nodeUUID);
77 List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
78 AAIService aaiService = newAAIService(vim);
80 ServiceTemplateService service = newServiceTemplateService(uuid, nodeUUID, sdcService, aaiService);
82 Assert.assertThat(service.fetchServiceTemplateInput(uuid, modelPath), equalTo(new ServiceTemplateInputRsp(expectedServiceInputs(uuid, nodeUUID),vim)));
85 private DefaultServiceTemplateService newServiceTemplateService(String uuid, String nodeUUID, SDCCatalogService sdcService, AAIService aaiService) {
86 return new DefaultServiceTemplateService(sdcService, aaiService) {
89 protected void downloadFile(String templateUrl, String toPath) throws IOException {
90 // download successfully...
94 protected ToscaTemplate translateToToscaTemplate(String toPath) throws JToscaException {
95 if (toPath.contains(uuid)) {
96 return e2eToscaTemplate(nodeUUID);
98 return nodeToscaTemplate(nodeUUID);
103 private SDCCatalogService newSdcCatalogService(String nodeUUID) throws IOException {
104 SDCCatalogService sdcService = mock(SDCCatalogService.class);
105 when(sdcService.getService(nodeUUID)).thenReturn(successfulCall(new SDCServiceTemplate(nodeUUID, nodeUUID, "node", "nodeModelUrl", "service")));
109 private List<ServiceTemplateInput> expectedServiceInputs(String uuid, String nodeUUID) {
110 ServiceTemplateInput e2eServiceTemplateInput = new ServiceTemplateInput(
111 uuid, uuid, "VoLTE", "service", "VoLTE", "service", "", Collections.EMPTY_LIST);
112 TemplateInput templateInput = new TemplateInput("field_name","field_type", "field_description", "true", "field_default");
113 ServiceTemplateInput nodeTemplateInput = new ServiceTemplateInput(
114 nodeUUID, nodeUUID, "", "", "", "service", "", Collections.singletonList(templateInput));
115 return Arrays.asList(e2eServiceTemplateInput, nodeTemplateInput);
118 private ToscaTemplate e2eToscaTemplate(String nodeUUID) {
119 ToscaTemplate toscaTemplate = mock(ToscaTemplate.class);
120 Map<String, Object> e2eAttributes = new HashMap<>();
121 e2eAttributes.put("invariantUUID", "1");
122 e2eAttributes.put("UUID", "1");
123 e2eAttributes.put("name", "VoLTE");
124 e2eAttributes.put("type", "service");
125 e2eAttributes.put("description", "VoLTE");
126 e2eAttributes.put("category", "service");
127 e2eAttributes.put("subcategory", "");
128 when(toscaTemplate.getMetaData()).thenReturn(new Metadata(e2eAttributes));
129 when(toscaTemplate.getInputs()).thenReturn(new ArrayList<>());
130 NodeTemplate nodeTemplate = mock(NodeTemplate.class);
132 Map<String, Object> nodeUUIDAttr = new HashMap<>();
134 nodeUUIDAttr.put("UUID", nodeUUID);
135 when(nodeTemplate.getMetaData()).thenReturn(new Metadata(nodeUUIDAttr));
137 ArrayList<NodeTemplate> nodeTemplates = new ArrayList<>();
138 nodeTemplates.add(nodeTemplate);
139 when(toscaTemplate.getNodeTemplates()).thenReturn(nodeTemplates);
141 return toscaTemplate;
144 private ToscaTemplate nodeToscaTemplate(String nodeUUID) {
145 ToscaTemplate toscaTemplate = mock(ToscaTemplate.class);
146 Map<String, Object> Attributes = new HashMap<>();
147 Attributes.put("invariantUUID", nodeUUID);
148 Attributes.put("UUID", nodeUUID);
149 Attributes.put("name", "");
150 Attributes.put("type", "");
151 Attributes.put("description", "");
152 Attributes.put("category", "service");
153 Attributes.put("subcategory", "");
154 when(toscaTemplate.getMetaData()).thenReturn(new Metadata(Attributes));
156 Input input = mock(Input.class);
157 when(input.getName()).thenReturn("field_name");
158 when(input.getDescription()).thenReturn("field_description");
159 when(input.getType()).thenReturn("field_type");
160 when(input.getDefault()).thenReturn("field_default");
161 when(input.isRequired()).thenReturn(true);
163 ArrayList<Input> inputs = new ArrayList<>();
165 when(toscaTemplate.getInputs()).thenReturn(inputs);
166 when(toscaTemplate.getNodeTemplates()).thenReturn(new ArrayList<>());
168 return toscaTemplate;
171 private AAIService newAAIService(List<VimInfo> vim) {
172 AAIService aaiService = mock(AAIService.class);
174 Call<List<VimInfo>> vimCall = successfulCall(vim);
175 when(aaiService.listVimInfo()).thenReturn(vimCall);
179 @Test(expected = SDCCatalogException.class)
180 public void retrieveInputsWillThrowExceptionWhenDownloadFailed() {
181 ServiceTemplateService service = new DefaultServiceTemplateService() {
183 protected void downloadFile(String templateUrl, String toPath) throws IOException {
184 throw new IOException("download failed!");
187 service.fetchServiceTemplateInput("1", "url");
190 @Test(expected = SDCCatalogException.class)
191 public void retrieveInputsWillThrowExceptionWhenParsingToscaTemplateFailed() {
192 ServiceTemplateService service = new DefaultServiceTemplateService() {
194 protected void downloadFile(String templateUrl, String toPath) throws IOException {
195 // download successfully...
199 protected ToscaTemplate translateToToscaTemplate(String toPath) throws JToscaException {
200 throw new JToscaException("parse tosca template failed!", "123");
203 service.fetchServiceTemplateInput("1", "url");