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.VimInfo;
25 import org.onap.usecaseui.server.service.lcm.domain.aai.bean.VimInfoRsp;
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", "V1","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(expectedServiceInputs(uuid, nodeUUID)));
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", "V1", "nodeModelUrl", "service")));
109 private 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 e2eServiceTemplateInput.addNestedTemplate(nodeTemplateInput);
116 return e2eServiceTemplateInput;
119 private ToscaTemplate e2eToscaTemplate(String nodeUUID) {
120 ToscaTemplate toscaTemplate = mock(ToscaTemplate.class);
121 Map<String, Object> e2eAttributes = new HashMap<>();
122 e2eAttributes.put("invariantUUID", "1");
123 e2eAttributes.put("UUID", "1");
124 e2eAttributes.put("name", "VoLTE");
125 e2eAttributes.put("type", "service");
126 e2eAttributes.put("description", "VoLTE");
127 e2eAttributes.put("category", "service");
128 e2eAttributes.put("subcategory", "");
129 when(toscaTemplate.getMetaData()).thenReturn(new Metadata(e2eAttributes));
130 when(toscaTemplate.getInputs()).thenReturn(new ArrayList<>());
131 NodeTemplate nodeTemplate = mock(NodeTemplate.class);
133 Map<String, Object> nodeUUIDAttr = new HashMap<>();
135 nodeUUIDAttr.put("UUID", nodeUUID);
136 when(nodeTemplate.getMetaData()).thenReturn(new Metadata(nodeUUIDAttr));
138 ArrayList<NodeTemplate> nodeTemplates = new ArrayList<>();
139 nodeTemplates.add(nodeTemplate);
140 when(toscaTemplate.getNodeTemplates()).thenReturn(nodeTemplates);
142 return toscaTemplate;
145 private ToscaTemplate nodeToscaTemplate(String nodeUUID) {
146 ToscaTemplate toscaTemplate = mock(ToscaTemplate.class);
147 Map<String, Object> Attributes = new HashMap<>();
148 Attributes.put("invariantUUID", nodeUUID);
149 Attributes.put("UUID", nodeUUID);
150 Attributes.put("name", "");
151 Attributes.put("type", "");
152 Attributes.put("description", "");
153 Attributes.put("category", "service");
154 Attributes.put("subcategory", "");
155 when(toscaTemplate.getMetaData()).thenReturn(new Metadata(Attributes));
157 Input input = mock(Input.class);
158 when(input.getName()).thenReturn("field_name");
159 when(input.getDescription()).thenReturn("field_description");
160 when(input.getType()).thenReturn("field_type");
161 when(input.getDefault()).thenReturn("field_default");
162 when(input.isRequired()).thenReturn(true);
164 ArrayList<Input> inputs = new ArrayList<>();
166 when(toscaTemplate.getInputs()).thenReturn(inputs);
167 when(toscaTemplate.getNodeTemplates()).thenReturn(new ArrayList<>());
169 return toscaTemplate;
172 private AAIService newAAIService(List<VimInfo> vim) {
173 AAIService aaiService = mock(AAIService.class);
174 VimInfoRsp rsp = new VimInfoRsp();
175 rsp.setCloudRegion(vim);
176 Call<VimInfoRsp> vimCall = successfulCall(rsp);
177 when(aaiService.listVimInfo()).thenReturn(vimCall);
181 @Test(expected = SDCCatalogException.class)
182 public void retrieveInputsWillThrowExceptionWhenDownloadFailed() {
183 ServiceTemplateService service = new DefaultServiceTemplateService() {
185 protected void downloadFile(String templateUrl, String toPath) throws IOException {
186 throw new IOException("download failed!");
189 service.fetchServiceTemplateInput("1", "url");
192 @Test(expected = SDCCatalogException.class)
193 public void retrieveInputsWillThrowExceptionWhenParsingToscaTemplateFailed() {
194 ServiceTemplateService service = new DefaultServiceTemplateService() {
196 protected void downloadFile(String templateUrl, String toPath) throws IOException {
197 // download successfully...
201 protected ToscaTemplate translateToToscaTemplate(String toPath) throws JToscaException {
202 throw new JToscaException("parse tosca template failed!", "123");
205 service.fetchServiceTemplateInput("1", "url");