acb129578e91b121cc20bdb560e1be50db796c7a
[usecase-ui/server.git] /
1 /**
2  * Copyright 2016-2017 ZTE Corporation.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.usecaseui.server.service.lcm.impl;
17
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;
35
36 import java.io.IOException;
37 import java.util.*;
38
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;
46
47 public class DefaultServiceTemplateServiceTest {
48
49     @Test
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));
54
55         ServiceTemplateService service = new DefaultServiceTemplateService(sdcService,null);
56
57         Assert.assertSame(templates, service.listDistributedServiceTemplate());
58     }
59
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!"));
64
65         ServiceTemplateService service = new DefaultServiceTemplateService(sdcService,null);
66         service.listDistributedServiceTemplate();
67     }
68
69     @Test
70     public void itCanRetrieveInputsFromServiceTemplate() throws IOException {
71         final String uuid = "1";
72         String modelPath = "model_path";
73         String nodeUUID = "2";
74
75         SDCCatalogService sdcService = newSdcCatalogService(nodeUUID);
76
77         List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
78         AAIService aaiService = newAAIService(vim);
79
80         ServiceTemplateService service = newServiceTemplateService(uuid, nodeUUID, sdcService, aaiService);
81
82         Assert.assertThat(service.fetchServiceTemplateInput(uuid, modelPath), equalTo(expectedServiceInputs(uuid, nodeUUID)));
83     }
84
85     private DefaultServiceTemplateService newServiceTemplateService(String uuid, String nodeUUID, SDCCatalogService sdcService, AAIService aaiService) {
86         return new DefaultServiceTemplateService(sdcService, aaiService) {
87
88             @Override
89             protected void downloadFile(String templateUrl, String toPath) throws IOException {
90                 // download successfully...
91             }
92
93             @Override
94             protected ToscaTemplate translateToToscaTemplate(String toPath) throws JToscaException {
95                 if (toPath.contains(uuid)) {
96                     return e2eToscaTemplate(nodeUUID);
97                 }
98                 return nodeToscaTemplate(nodeUUID);
99             }
100         };
101     }
102
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")));
106         return sdcService;
107     }
108
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;
117     }
118
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);
132
133         Map<String, Object> nodeUUIDAttr = new HashMap<>();
134
135         nodeUUIDAttr.put("UUID", nodeUUID);
136         when(nodeTemplate.getMetaData()).thenReturn(new Metadata(nodeUUIDAttr));
137
138         ArrayList<NodeTemplate> nodeTemplates = new ArrayList<>();
139         nodeTemplates.add(nodeTemplate);
140         when(toscaTemplate.getNodeTemplates()).thenReturn(nodeTemplates);
141
142         return toscaTemplate;
143     }
144
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));
156
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);
163
164         ArrayList<Input> inputs = new ArrayList<>();
165         inputs.add(input);
166         when(toscaTemplate.getInputs()).thenReturn(inputs);
167         when(toscaTemplate.getNodeTemplates()).thenReturn(new ArrayList<>());
168
169         return toscaTemplate;
170     }
171
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);
178         return aaiService;
179     }
180
181     @Test(expected = SDCCatalogException.class)
182     public void retrieveInputsWillThrowExceptionWhenDownloadFailed() {
183         ServiceTemplateService service = new DefaultServiceTemplateService() {
184             @Override
185             protected void downloadFile(String templateUrl, String toPath) throws IOException {
186                 throw new IOException("download failed!");
187             }
188         };
189         service.fetchServiceTemplateInput("1", "url");
190     }
191
192     @Test(expected = SDCCatalogException.class)
193     public void retrieveInputsWillThrowExceptionWhenParsingToscaTemplateFailed() {
194         ServiceTemplateService service = new DefaultServiceTemplateService() {
195             @Override
196             protected void downloadFile(String templateUrl, String toPath) throws IOException {
197                 // download successfully...
198             }
199
200             @Override
201             protected ToscaTemplate translateToToscaTemplate(String toPath) throws JToscaException {
202                 throw new JToscaException("parse tosca template failed!", "123");
203             }
204         };
205         service.fetchServiceTemplateInput("1", "url");
206     }
207 }