fdbe7c219797f5022de488e9c141b3dc45b61a20
[sdc.git] /
1 /*-
2  * ============LICENSE_START===============================================
3  * ONAP SDC
4  * ========================================================================
5  * Modifications Copyright (c) 2019 Samsung
6  * ========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=================================================
19  */
20
21 package org.openecomp.sdc.be.components.impl.utils;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertNotNull;
25 import static org.mockito.ArgumentMatchers.eq;
26 import static org.mockito.Mockito.when;
27 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.ARTIFACTS;
28
29 import java.io.File;
30 import java.net.URISyntaxException;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Optional;
36 import java.util.stream.Collectors;
37 import mockit.Deencapsulation;
38 import org.apache.commons.collections.MapUtils;
39 import org.assertj.core.util.Lists;
40 import org.junit.Assert;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.mockito.InjectMocks;
46 import org.mockito.Mock;
47 import org.mockito.junit.MockitoJUnitRunner;
48 import org.openecomp.sdc.be.components.csar.CsarInfo;
49 import org.openecomp.sdc.be.components.csar.YamlTemplateParsingHandler;
50 import org.openecomp.sdc.be.components.impl.AnnotationBusinessLogic;
51 import org.openecomp.sdc.be.components.impl.GroupTypeBusinessLogic;
52 import org.openecomp.sdc.be.components.validation.AnnotationValidator;
53 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
54 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
55 import org.openecomp.sdc.be.model.CapabilityDefinition;
56 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
57 import org.openecomp.sdc.be.model.GroupProperty;
58 import org.openecomp.sdc.be.model.GroupTypeDefinition;
59 import org.openecomp.sdc.be.model.ParsedToscaYamlInfo;
60 import org.openecomp.sdc.be.model.UploadArtifactInfo;
61 import org.openecomp.sdc.be.model.UploadComponentInstanceInfo;
62 import org.openecomp.sdc.be.model.User;
63 import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations;
64 import org.openecomp.sdc.common.zip.ZipUtils;
65 import org.openecomp.sdc.common.zip.exception.ZipException;
66
67 @RunWith(MockitoJUnitRunner.class)
68 public class YamlTemplateParsingHandlerTest {
69
70     @Mock
71     private GroupTypeBusinessLogic groupTypeBusinessLogic;
72     @Mock
73     private AnnotationTypeOperations annotationTypeOperations;
74     @Mock
75     private AnnotationValidator annotationValidator;
76     @Mock
77     private JanusGraphDao janusGraphDao;
78     @Mock
79     private User user;
80
81     private YamlTemplateParsingHandler handler;
82
83     private static Map<String, byte[]> csar;
84     private static String resourceYml;
85
86     private final static String VFC_GROUP_TYPE = "org.openecomp.groups.VfcInstanceGroup";
87     private final static String HEAT_GROUP_TYPE = "org.openecomp.groups.heat.HeatStack";
88     private final static String ROOT_GROUP_TYPE = "tosca.groups.Root";
89     private final static GroupTypeDefinition VfcInstanceGroupType = buildVfcInstanceGroupType();
90     private final static GroupTypeDefinition heatGroupType = buildHeatStackGroupType();
91     private final static GroupTypeDefinition rootGroupType = buildRootGroupType();
92     private final static String CAPABILITY_TYPE = "org.openecomp.capabilities.VLANAssignment";
93     private final static String CAPABILITY_NAME = "vlan_assignment";
94     private static final String CSAR_FILE_PATH = "csars/with_groups.csar";
95     private static final String FILE_NAME = "MainServiceTemplate.yaml";
96     private static final String CSAR_UUID = "csarUUID";
97     private static final String RESOURCE_NAME = "resourceName";
98     private static final String MAIN_TEMPLATE_NAME = "Definitions/MainServiceTemplate.yaml";
99     private static final String NODE_NAME = "org.openecomp.resource.abstract.nodes.heat.mg";
100     private static final String MAIN_GROUP_NAME = "x_group";
101     private static final String NESTED_GROUP_NAME = "nested_mg_vepdg_group";
102
103     @InjectMocks
104     YamlTemplateParsingHandler testSubject;
105
106     @BeforeClass()
107     public static void prepareData() throws URISyntaxException, ZipException {
108         final File csarFile = new File(
109             YamlTemplateParsingHandlerTest.class.getClassLoader().getResource(CSAR_FILE_PATH).toURI());
110         csar = ZipUtils.readZip(csarFile, false);
111
112         Optional<String> keyOp = csar.keySet().stream().filter(k -> k.endsWith(FILE_NAME)).findAny();
113         byte[] mainTemplateService = keyOp.map(csar::get).orElse(null);
114         assertNotNull(mainTemplateService);
115
116         resourceYml = new String(mainTemplateService);
117     }
118
119     @Before
120     public void setup() {
121
122         AnnotationBusinessLogic annotationBusinessLogic = new AnnotationBusinessLogic(annotationTypeOperations,
123                 annotationValidator);
124         handler = new YamlTemplateParsingHandler(janusGraphDao, groupTypeBusinessLogic, annotationBusinessLogic);
125         stubGetGroupType();
126     }
127
128     @Test
129     public void parseResourceInfoFromOneNodeTest() {
130
131         String main_template_content = new String(csar.get(MAIN_TEMPLATE_NAME));
132         CsarInfo csarInfo = new CsarInfo(user, CSAR_UUID, csar, RESOURCE_NAME,
133                 MAIN_TEMPLATE_NAME, main_template_content, true);
134
135         ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
136                 csarInfo.extractNodeTypesInfo(), NODE_NAME);
137
138         validateParsedYaml(parsedYaml, NESTED_GROUP_NAME,
139                 Lists.newArrayList("heat_file", "description"));
140     }
141
142     @Test
143     public void parseResourceInfoFromYAMLTest() {
144
145         ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
146                 new HashMap<>(), "");
147         validateParsedYamlWithCapability(parsedYaml);
148     }
149
150     private void validateParsedYaml(ParsedToscaYamlInfo parsedYaml, String group, List<String> expectedProp) {
151         assertThat(parsedYaml).isNotNull();
152         assertThat(parsedYaml.getGroups()).isNotNull().containsKey(group);
153         assertThat(parsedYaml.getGroups().get(group)).isNotNull();
154
155         assertThat(parsedYaml.getGroups().get(group).getProperties()).isNotNull();
156         assertThat(parsedYaml.getGroups().get(group).getProperties()
157                 .stream()
158                 .map(PropertyDataDefinition::getName)
159                 .collect(Collectors.toList()))
160                 .containsAll(expectedProp);
161
162         assertThat(parsedYaml.getGroups().get(group).getMembers()).isNotNull();
163     }
164
165         private void validateParsedYamlWithCapability(ParsedToscaYamlInfo parsedYaml) {
166
167         final List<String> expectedProp = Lists.newArrayList("vfc_parent_port_role",
168                 "network_collection_function", "vfc_instance_group_function", "subinterface_role");
169
170         validateParsedYaml(parsedYaml, MAIN_GROUP_NAME, expectedProp);
171
172         assertThat(parsedYaml.getGroups().get(MAIN_GROUP_NAME).getCapabilities()
173                 .get(CAPABILITY_TYPE)
174                 .get(0).getProperties().get(0).getValue()).isEqualTo("success");
175         assertThat(parsedYaml.getGroups().get(MAIN_GROUP_NAME).getCapabilities()).isNotNull();
176         }
177
178         private void stubGetGroupType() {
179                 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(VFC_GROUP_TYPE))).thenReturn(VfcInstanceGroupType);
180                 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE))).thenReturn(heatGroupType);
181         when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(ROOT_GROUP_TYPE))).thenReturn(rootGroupType);
182     }
183
184     private static GroupTypeDefinition buildRootGroupType() {
185         return createGroupTypeDefinition(ROOT_GROUP_TYPE, null,
186                 "The TOSCA Group Type all other TOSCA Group Types derive from");
187         }
188
189         private static GroupTypeDefinition buildHeatStackGroupType() {
190         GroupTypeDefinition groupType = createGroupTypeDefinition(HEAT_GROUP_TYPE, "tosca.groups.Root",
191                 "Grouped all heat resources which are in the same heat stack");
192
193         GroupProperty property1 = createGroupProperty("heat_file",
194                 "Heat file which associate to this group/heat stack", "SUPPORTED");
195
196         GroupProperty property2 = createGroupProperty("description",
197                 "Group description", "SUPPORTED");
198
199         groupType.setProperties(Lists.newArrayList(property1, property2));
200         return groupType;
201         }
202
203         private static GroupTypeDefinition buildVfcInstanceGroupType() {
204         GroupTypeDefinition groupType = createGroupTypeDefinition(VFC_GROUP_TYPE, "tosca.groups.Root",
205                 "Groups of VFCs with same parent port role");
206
207         GroupProperty property1 = createGroupProperty("vfc_instance_group_function",
208                 "Function of this VFC group", null);
209
210         GroupProperty property2 = createGroupProperty("vfc_parent_port_role",
211                 "Common role of parent ports of VFCs in this group", null);
212
213         GroupProperty property3 = createGroupProperty("network_collection_function",
214                 "Network collection function assigned to this group", null);
215
216         GroupProperty property4 = createGroupProperty("subinterface_role",
217                 "Common role of subinterfaces of VFCs in this group, criteria the group is created", null);
218
219         groupType.setProperties(Lists.newArrayList(property1, property2, property3, property4));
220
221         CapabilityDefinition capability = new CapabilityDefinition();
222         capability.setType(CAPABILITY_TYPE);
223         capability.setName(CAPABILITY_NAME);
224         ComponentInstanceProperty capabilityProperty = new ComponentInstanceProperty();
225         capabilityProperty.setName("vfc_instance_group_reference");
226         capabilityProperty.setType("string");
227         capability.setProperties(Collections.singletonList(capabilityProperty));
228
229         Map<String, CapabilityDefinition> capabilityMap = new HashMap<>();
230         capabilityMap.put(CAPABILITY_NAME, capability);
231         groupType.setCapabilities(capabilityMap);
232         return groupType;
233     }
234
235     private static GroupTypeDefinition createGroupTypeDefinition(String type, String derivedFrom, String description){
236         GroupTypeDefinition property = new GroupTypeDefinition();
237
238         if (type != null)
239             property.setType(type);
240
241         if (derivedFrom != null) {
242             property.setDerivedFrom(derivedFrom);
243         }
244
245         if (description != null) {
246             property.setDescription(description);
247         }
248
249         return property;
250     }
251     private static GroupProperty createGroupProperty(String name, String description,
252                                                      String status){
253         GroupProperty property = new GroupProperty();
254         if (name != null)
255             property.setName(name);
256
257         if (description != null) {
258             property.setDescription(description);
259         }
260
261         if (status != null) {
262             property.setStatus(status);
263         }
264
265         property.setType("string");
266         property.setRequired(true);
267
268         return property;
269     }
270     @Test
271     public void testSetArtifacts() {
272         UploadComponentInstanceInfo nodeTemplateInfo = new UploadComponentInstanceInfo();
273         Map<String, Object> nodeTemplateJsonMap = new HashMap<>();
274         Map<String, String> nodeMap = new HashMap<>();
275         nodeMap.put("name","test_name");
276         nodeMap.put("type","test_type");
277         nodeTemplateJsonMap.put(ARTIFACTS.getElementName(), nodeMap);
278         Deencapsulation.invoke(testSubject, "setArtifacts", nodeTemplateInfo, nodeTemplateJsonMap);
279         assertNotNull(nodeTemplateInfo.getArtifacts());
280     }
281
282     @Test
283     public void testCreateArtifactsModuleFromYaml() {
284         Map<String, Map<String, Map<String, String>>> nodeTemplateJsonMap = new HashMap<>();
285         Map<String, Map<String,String>> map0 = new HashMap<>();
286         Map<String, String> map1 = new HashMap<>();
287         map1.put("file", "test_file");
288         map1.put("type", "test_type");
289         map0.put("test_art", map1);
290         nodeTemplateJsonMap.put(ARTIFACTS.getElementName(), map0);
291         Map<String, Map<String, UploadArtifactInfo>> result;
292         result = Deencapsulation.invoke(testSubject, "createArtifactsModuleFromYaml", nodeTemplateJsonMap);
293         Assert.assertTrue(MapUtils.isNotEmpty(result));
294         Assert.assertTrue(MapUtils.isNotEmpty(result.get(ARTIFACTS.getElementName())));
295         Assert.assertEquals("test_file", result.get(ARTIFACTS.getElementName()).get("test_art").getFile());
296         Assert.assertEquals("test_type", result.get(ARTIFACTS.getElementName()).get("test_art").getType());
297     }
298
299     @Test
300     public void testAddModuleNodeTemplateArtifacts() {
301         Map<String, Map<String, UploadArtifactInfo>> result = new HashMap<>();
302         Map<String, String> map1 = new HashMap<>();
303         map1.put("file", "test_file");
304         map1.put("type", "test_type");
305         Deencapsulation.invoke(testSubject, "addModuleNodeTemplateArtifacts", result, map1, "test_art");
306         Assert.assertTrue(MapUtils.isNotEmpty(result));
307         Assert.assertTrue(MapUtils.isNotEmpty(result.get(ARTIFACTS.getElementName())));
308         Assert.assertEquals("test_file", result.get(ARTIFACTS.getElementName()).get("test_art").getFile());
309         Assert.assertEquals("test_type", result.get(ARTIFACTS.getElementName()).get("test_art").getType());
310     }
311
312     @Test
313     public void testBuildModuleNodeTemplateArtifact() {
314         Map<String, String> map1 = new HashMap<>();
315         map1.put("file", "test_file");
316         map1.put("type", "test_type");
317         UploadArtifactInfo result;
318         result = Deencapsulation.invoke(testSubject, "buildModuleNodeTemplateArtifact", map1);
319         assertNotNull(result);
320         Assert.assertEquals("test_file", result.getFile());
321         Assert.assertEquals("test_type", result.getType());
322     }
323
324     @Test
325     public void testFillArtifact() {
326         Map<String, String> map1 = new HashMap<>();
327         map1.put("file", "test_file");
328         map1.put("type", "test_type");
329         UploadArtifactInfo result = new UploadArtifactInfo();
330         Deencapsulation.invoke(testSubject, "fillArtifact", result, map1);
331         assertNotNull(result);
332         Assert.assertEquals("test_file", result.getFile());
333         Assert.assertEquals("test_type", result.getType());
334     }
335 }