9696a77e13170606497a3e52b1ee89de8349467c
[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.sdc.SDCCatalogService;
26 import org.onap.usecaseui.server.service.lcm.domain.sdc.bean.SDCServiceTemplate;
27 import org.onap.usecaseui.server.service.lcm.domain.sdc.exceptions.SDCCatalogException;
28 import org.openecomp.sdc.toscaparser.api.NodeTemplate;
29 import org.openecomp.sdc.toscaparser.api.ToscaTemplate;
30 import org.openecomp.sdc.toscaparser.api.common.JToscaException;
31 import org.openecomp.sdc.toscaparser.api.elements.Metadata;
32 import org.openecomp.sdc.toscaparser.api.parameters.Input;
33 import retrofit2.Call;
34
35 import java.io.IOException;
36 import java.util.*;
37
38 import static org.hamcrest.Matchers.equalTo;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.when;
41 import static org.onap.usecaseui.server.service.lcm.domain.sdc.consts.SDCConsts.CATEGORY_E2E_SERVICE;
42 import static org.onap.usecaseui.server.service.lcm.domain.sdc.consts.SDCConsts.DISTRIBUTION_STATUS_DISTRIBUTED;
43 import static org.onap.usecaseui.server.util.CallStub.failedCall;
44 import static org.onap.usecaseui.server.util.CallStub.successfulCall;
45
46 public class DefaultServiceTemplateServiceTest {
47
48     @Test
49     public void itCanListDistributedServiceTemplate() {
50         List<SDCServiceTemplate> templates = Collections.singletonList(new SDCServiceTemplate("uuid", "uuid", "name", "V1","url", "category"));
51         SDCCatalogService sdcService = mock(SDCCatalogService.class);
52         when(sdcService.listServices(CATEGORY_E2E_SERVICE, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(successfulCall(templates));
53
54         ServiceTemplateService service = new DefaultServiceTemplateService(sdcService,null);
55
56         Assert.assertSame(templates, service.listDistributedServiceTemplate());
57     }
58
59     @Test(expected = SDCCatalogException.class)
60     public void retrieveServiceWillThrowExceptionWhenSDCIsNotAvailable() {
61         SDCCatalogService sdcService = mock(SDCCatalogService.class);
62         when(sdcService.listServices(CATEGORY_E2E_SERVICE, DISTRIBUTION_STATUS_DISTRIBUTED)).thenReturn(failedCall("SDC is not available!"));
63
64         ServiceTemplateService service = new DefaultServiceTemplateService(sdcService,null);
65         service.listDistributedServiceTemplate();
66     }
67
68     @Test
69     public void itCanRetrieveInputsFromServiceTemplate() throws IOException {
70         final String uuid = "1";
71         String modelPath = "model_path";
72         String nodeUUID = "2";
73
74         SDCCatalogService sdcService = newSdcCatalogService(nodeUUID);
75
76         List<VimInfo> vim = Collections.singletonList(new VimInfo("owner", "regionId"));
77         AAIService aaiService = newAAIService(vim);
78
79         ServiceTemplateService service = newServiceTemplateService(uuid, nodeUUID, sdcService, aaiService);
80
81         Assert.assertThat(service.fetchServiceTemplateInput(uuid, modelPath), equalTo(expectedServiceInputs(uuid, nodeUUID)));
82     }
83
84     private DefaultServiceTemplateService newServiceTemplateService(String uuid, String nodeUUID, SDCCatalogService sdcService, AAIService aaiService) {
85         return new DefaultServiceTemplateService(sdcService, aaiService) {
86
87             @Override
88             protected void downloadFile(String templateUrl, String toPath) throws IOException {
89                 // download successfully...
90             }
91
92             @Override
93             protected ToscaTemplate translateToToscaTemplate(String toPath) throws JToscaException {
94                 if (toPath.contains(uuid)) {
95                     return e2eToscaTemplate(nodeUUID);
96                 }
97                 return nodeToscaTemplate(nodeUUID);
98             }
99         };
100     }
101
102     private SDCCatalogService newSdcCatalogService(String nodeUUID) throws IOException {
103         SDCCatalogService sdcService = mock(SDCCatalogService.class);
104         when(sdcService.getService(nodeUUID)).thenReturn(successfulCall(new SDCServiceTemplate(nodeUUID, nodeUUID, "node", "V1", "nodeModelUrl", "service")));
105         return sdcService;
106     }
107
108     private ServiceTemplateInput expectedServiceInputs(String uuid, String nodeUUID) {
109         ServiceTemplateInput e2eServiceTemplateInput = new ServiceTemplateInput(
110                 uuid, uuid, "VoLTE", "service","", "VoLTE", "service", "", Collections.EMPTY_LIST);
111         TemplateInput templateInput = new TemplateInput("field_name","field_type", "field_description", "true", "field_default");
112         ServiceTemplateInput nodeTemplateInput = new ServiceTemplateInput(
113                 nodeUUID, nodeUUID, "", "", "","", "service", "", Collections.singletonList(templateInput));
114         e2eServiceTemplateInput.addNestedTemplate(nodeTemplateInput);
115         return e2eServiceTemplateInput;
116     }
117
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);
131
132         Map<String, Object> nodeUUIDAttr = new HashMap<>();
133
134         nodeUUIDAttr.put("UUID", nodeUUID);
135         when(nodeTemplate.getMetaData()).thenReturn(new Metadata(nodeUUIDAttr));
136
137         ArrayList<NodeTemplate> nodeTemplates = new ArrayList<>();
138         nodeTemplates.add(nodeTemplate);
139         when(toscaTemplate.getNodeTemplates()).thenReturn(nodeTemplates);
140
141         return toscaTemplate;
142     }
143
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));
155
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);
162
163         ArrayList<Input> inputs = new ArrayList<>();
164         inputs.add(input);
165         when(toscaTemplate.getInputs()).thenReturn(inputs);
166         when(toscaTemplate.getNodeTemplates()).thenReturn(new ArrayList<>());
167
168         return toscaTemplate;
169     }
170
171     private AAIService newAAIService(List<VimInfo> vim) {
172         AAIService aaiService = mock(AAIService.class);
173
174         Call<List<VimInfo>> vimCall = successfulCall(vim);
175         when(aaiService.listVimInfo()).thenReturn(vimCall);
176         return aaiService;
177     }
178
179     @Test(expected = SDCCatalogException.class)
180     public void retrieveInputsWillThrowExceptionWhenDownloadFailed() {
181         ServiceTemplateService service = new DefaultServiceTemplateService() {
182             @Override
183             protected void downloadFile(String templateUrl, String toPath) throws IOException {
184                 throw new IOException("download failed!");
185             }
186         };
187         service.fetchServiceTemplateInput("1", "url");
188     }
189
190     @Test(expected = SDCCatalogException.class)
191     public void retrieveInputsWillThrowExceptionWhenParsingToscaTemplateFailed() {
192         ServiceTemplateService service = new DefaultServiceTemplateService() {
193             @Override
194             protected void downloadFile(String templateUrl, String toPath) throws IOException {
195                 // download successfully...
196             }
197
198             @Override
199             protected ToscaTemplate translateToToscaTemplate(String toPath) throws JToscaException {
200                 throw new JToscaException("parse tosca template failed!", "123");
201             }
202         };
203         service.fetchServiceTemplateInput("1", "url");
204     }
205 }