260c2379d9ad7f201d21be7a7909f443c4902d11
[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.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;
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", "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(new ServiceTemplateInputRsp(expectedServiceInputs(uuid, nodeUUID),vim)));
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", "nodeModelUrl", "service")));
106         return sdcService;
107     }
108
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);
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 }