Fix for list of map type on SELF property
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / csar / YamlTemplateParsingHandlerTest.java
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.csar;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.when;
30 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.ARTIFACTS;
31
32 import java.io.File;
33 import java.net.URISyntaxException;
34 import java.util.Collections;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Optional;
39 import java.util.stream.Collectors;
40 import mockit.Deencapsulation;
41 import org.apache.commons.collections.MapUtils;
42 import org.assertj.core.util.Lists;
43 import org.junit.jupiter.api.BeforeAll;
44 import org.junit.jupiter.api.BeforeEach;
45 import org.junit.jupiter.api.Test;
46 import org.junit.jupiter.api.extension.ExtendWith;
47 import org.mockito.InjectMocks;
48 import org.mockito.Mock;
49 import org.mockito.junit.jupiter.MockitoExtension;
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.impl.PolicyTypeBusinessLogic;
53 import org.openecomp.sdc.be.components.validation.AnnotationValidator;
54 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
55 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
56 import org.openecomp.sdc.be.model.CapabilityDefinition;
57 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
58 import org.openecomp.sdc.be.model.GroupProperty;
59 import org.openecomp.sdc.be.model.GroupTypeDefinition;
60 import org.openecomp.sdc.be.model.ParsedToscaYamlInfo;
61 import org.openecomp.sdc.be.model.PolicyDefinition;
62 import org.openecomp.sdc.be.model.PolicyTypeDefinition;
63 import org.openecomp.sdc.be.model.Resource;
64 import org.openecomp.sdc.be.model.Service;
65 import org.openecomp.sdc.be.model.UploadArtifactInfo;
66 import org.openecomp.sdc.be.model.UploadComponentInstanceInfo;
67 import org.openecomp.sdc.be.model.User;
68 import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations;
69 import org.openecomp.sdc.common.zip.ZipUtils;
70 import org.openecomp.sdc.common.zip.exception.ZipException;
71 import org.springframework.test.util.ReflectionTestUtils;
72
73 @ExtendWith(MockitoExtension.class)
74 public class YamlTemplateParsingHandlerTest {
75
76     @Mock
77     private GroupTypeBusinessLogic groupTypeBusinessLogic;
78     @Mock
79     private AnnotationTypeOperations annotationTypeOperations;
80     @Mock
81     private AnnotationValidator annotationValidator;
82     @Mock
83     private JanusGraphDao janusGraphDao;
84     @Mock
85     private User user;
86     @Mock
87     private PolicyTypeBusinessLogic policyTypeBusinessLogic;
88
89     private YamlTemplateParsingHandler handler;
90
91     private static Map<String, byte[]> csar;
92     private static String resourceYml;
93
94     private final static String VFC_GROUP_TYPE = "org.openecomp.groups.VfcInstanceGroup";
95     private final static String HEAT_GROUP_TYPE = "org.openecomp.groups.heat.HeatStack";
96     private final static String ROOT_GROUP_TYPE = "tosca.groups.Root";
97     private final static String OPENECOMP_ANTILOCATE_POLICY_TYPE = "org.openecomp.policies.placement.Antilocate";
98     private final static String ROOT_POLICIES_TYPE = "tosca.policies.Root";
99     private final static GroupTypeDefinition VfcInstanceGroupType = buildVfcInstanceGroupType();
100     private final static GroupTypeDefinition heatGroupType = buildHeatStackGroupType();
101     private final static GroupTypeDefinition rootGroupType = buildRootGroupType();
102     private static final PolicyTypeDefinition OPENECOMP_POLICY_TYPE = buildOpenecompPolicyType();
103     private final static String OPENECOMP_POLICY_NAME = "vepdg_server_group_policy";
104     private final static String CAPABILITY_TYPE = "org.openecomp.capabilities.VLANAssignment";
105     private final static String CAPABILITY_NAME = "vlan_assignment";
106     private static final String CSAR_FILE_PATH = "csars/with_groups.csar";
107     private static final String FILE_NAME = "MainServiceTemplate.yaml";
108     private static final String CSAR_UUID = "csarUUID";
109     private static final String RESOURCE_NAME = "resourceName";
110     private static final String MAIN_TEMPLATE_NAME = "Definitions/MainServiceTemplate.yaml";
111     private static final String NODE_NAME = "org.openecomp.resource.abstract.nodes.heat.mg";
112     private static final String MAIN_GROUP_NAME = "x_group";
113     private static final String NESTED_GROUP_NAME = "nested_mg_vepdg_group";
114
115     @InjectMocks
116     private YamlTemplateParsingHandler testSubject;
117
118     @BeforeAll
119     public static void prepareData() throws URISyntaxException, ZipException {
120         final File csarFile = new File(
121             YamlTemplateParsingHandlerTest.class.getClassLoader().getResource(CSAR_FILE_PATH).toURI());
122         csar = ZipUtils.readZip(csarFile, false);
123
124         Optional<String> keyOp = csar.keySet().stream().filter(k -> k.endsWith(FILE_NAME)).findAny();
125         byte[] mainTemplateService = keyOp.map(csar::get).orElse(null);
126         assertNotNull(mainTemplateService);
127
128         resourceYml = new String(mainTemplateService);
129     }
130
131     @BeforeEach
132     public void setup() {
133
134         AnnotationBusinessLogic annotationBusinessLogic = new AnnotationBusinessLogic(annotationTypeOperations,
135             annotationValidator);
136         handler = new YamlTemplateParsingHandler(janusGraphDao, groupTypeBusinessLogic, annotationBusinessLogic, policyTypeBusinessLogic);
137         ReflectionTestUtils.setField(handler, "policyTypeBusinessLogic", policyTypeBusinessLogic);
138     }
139
140     @Test
141     void parseResourceInfoFromOneNodeTest() {
142         when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE), any())).thenReturn(heatGroupType);
143
144         String main_template_content = new String(csar.get(MAIN_TEMPLATE_NAME));
145         CsarInfo csarInfo = new CsarInfo(user, CSAR_UUID, csar, RESOURCE_NAME,
146             MAIN_TEMPLATE_NAME, main_template_content, true);
147
148         Resource resource = new Resource();
149         ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
150             csarInfo.extractTypesInfo(), NODE_NAME, resource, getInterfaceTemplateYaml(csarInfo).get());
151
152         validateParsedYaml(parsedYaml, NESTED_GROUP_NAME,
153             Lists.newArrayList("heat_file", "description"));
154     }
155
156     @Test
157     void parseServicePropertiesInfoFromYamlTest() {
158         when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE), any())).thenReturn(heatGroupType);
159         String main_template_content = new String(csar.get(MAIN_TEMPLATE_NAME));
160         CsarInfo csarInfo = new CsarInfo(user, CSAR_UUID, csar, RESOURCE_NAME,
161             MAIN_TEMPLATE_NAME, main_template_content, true);
162
163         Service service = new Service();
164         ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
165             csarInfo.extractTypesInfo(), NODE_NAME, service, getInterfaceTemplateYaml(csarInfo).get());
166
167         assertThat(parsedYaml.getProperties()).isNotNull();
168         assertEquals(5, parsedYaml.getProperties().size());
169         assertTrue(parsedYaml.getProperties().containsKey("skip_post_instantiation_configuration"));
170         assertTrue(parsedYaml.getProperties().containsKey("controller_actor"));
171         assertTrue(parsedYaml.getProperties().containsKey("cds_model_version"));
172         assertTrue(parsedYaml.getProperties().containsKey("cds_model_name"));
173         assertTrue(parsedYaml.getProperties().containsKey("default_software_version"));
174     }
175
176     @Test
177     void parseResourceInfoFromYAMLTest() {
178         stubGetGroupType();
179         stubGetPolicyType();
180
181         Resource resource = new Resource();
182         ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
183             new HashMap<>(), "", resource, null);
184         validateParsedYamlWithCapability(parsedYaml);
185     }
186
187     @Test
188     void testSetArtifacts() {
189         UploadComponentInstanceInfo nodeTemplateInfo = new UploadComponentInstanceInfo();
190         Map<String, Object> nodeTemplateJsonMap = new HashMap<>();
191         Map<String, String> nodeMap = new HashMap<>();
192         nodeMap.put("name", "test_name");
193         nodeMap.put("type", "test_type");
194         nodeTemplateJsonMap.put(ARTIFACTS.getElementName(), nodeMap);
195         Deencapsulation.invoke(testSubject, "setArtifacts", nodeTemplateInfo, nodeTemplateJsonMap);
196         assertNotNull(nodeTemplateInfo.getArtifacts());
197     }
198
199     @Test
200     void testCreateArtifactsModuleFromYaml() {
201         Map<String, Map<String, Map<String, String>>> nodeTemplateJsonMap = new HashMap<>();
202         Map<String, Map<String, String>> map0 = new HashMap<>();
203         Map<String, String> map1 = new HashMap<>();
204         map1.put("file", "test_file");
205         map1.put("type", "test_type");
206         map0.put("test_art", map1);
207         nodeTemplateJsonMap.put(ARTIFACTS.getElementName(), map0);
208         Map<String, Map<String, UploadArtifactInfo>> result;
209         result = Deencapsulation.invoke(testSubject, "createArtifactsModuleFromYaml", nodeTemplateJsonMap);
210         assertTrue(MapUtils.isNotEmpty(result));
211         assertTrue(MapUtils.isNotEmpty(result.get(ARTIFACTS.getElementName())));
212         assertEquals("test_file", result.get(ARTIFACTS.getElementName()).get("test_art").getFile());
213         assertEquals("test_type", result.get(ARTIFACTS.getElementName()).get("test_art").getType());
214     }
215
216     @Test
217     void testAddModuleNodeTemplateArtifacts() {
218         Map<String, Map<String, UploadArtifactInfo>> result = new HashMap<>();
219         Map<String, String> map1 = new HashMap<>();
220         map1.put("file", "test_file");
221         map1.put("type", "test_type");
222         Deencapsulation.invoke(testSubject, "addModuleNodeTemplateArtifacts", result, map1, "test_art");
223         assertTrue(MapUtils.isNotEmpty(result));
224         assertTrue(MapUtils.isNotEmpty(result.get(ARTIFACTS.getElementName())));
225         assertEquals("test_file", result.get(ARTIFACTS.getElementName()).get("test_art").getFile());
226         assertEquals("test_type", result.get(ARTIFACTS.getElementName()).get("test_art").getType());
227     }
228
229     @Test
230     void testBuildModuleNodeTemplateArtifact() {
231         Map<String, String> map1 = new HashMap<>();
232         map1.put("file", "test_file");
233         map1.put("type", "test_type");
234         UploadArtifactInfo result;
235         result = Deencapsulation.invoke(testSubject, "buildModuleNodeTemplateArtifact", map1);
236         assertNotNull(result);
237         assertEquals("test_file", result.getFile());
238         assertEquals("test_type", result.getType());
239     }
240
241     @Test
242     void testFillArtifact() {
243         Map<String, String> map1 = new HashMap<>();
244         map1.put("file", "test_file");
245         map1.put("type", "test_type");
246         UploadArtifactInfo result = new UploadArtifactInfo();
247         Deencapsulation.invoke(testSubject, "fillArtifact", result, map1);
248         assertNotNull(result);
249         assertEquals("test_file", result.getFile());
250         assertEquals("test_type", result.getType());
251     }
252
253     @Test
254     void parseResourceWithPoliciesDefined() {
255         stubGetGroupType();
256         stubGetPolicyType();
257         Resource resource = new Resource();
258         ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
259             new HashMap<>(), "", resource, "");
260         validateParsedYamlWithPolicies(parsedYaml);
261     }
262
263     private void validateParsedYaml(ParsedToscaYamlInfo parsedYaml, String group, List<String> expectedProp) {
264         assertThat(parsedYaml).isNotNull();
265         assertThat(parsedYaml.getGroups()).isNotNull().containsKey(group);
266         assertThat(parsedYaml.getGroups().get(group)).isNotNull();
267
268         assertThat(parsedYaml.getGroups().get(group).getProperties()).isNotNull();
269         assertThat(parsedYaml.getGroups().get(group).getProperties()
270             .stream()
271             .map(PropertyDataDefinition::getName)
272             .collect(Collectors.toList()))
273             .containsAll(expectedProp);
274
275         assertThat(parsedYaml.getGroups().get(group).getMembers()).isNotNull();
276     }
277
278     private void validateParsedYamlWithCapability(ParsedToscaYamlInfo parsedYaml) {
279
280         final List<String> expectedProp = Lists.newArrayList("vfc_parent_port_role",
281             "network_collection_function", "vfc_instance_group_function", "subinterface_role");
282
283         validateParsedYaml(parsedYaml, MAIN_GROUP_NAME, expectedProp);
284
285         assertThat(parsedYaml.getGroups().get(MAIN_GROUP_NAME).getCapabilities()
286             .get(CAPABILITY_TYPE)
287             .get(0).getProperties().get(0).getValue()).isEqualTo("success");
288         assertThat(parsedYaml.getGroups().get(MAIN_GROUP_NAME).getCapabilities()).isNotNull();
289         assertThat(parsedYaml.getSubstitutionMappingNodeType()).isEqualTo("org.openecomp.resource.abstract.nodes.VF");
290     }
291
292     private void stubGetGroupType() {
293         when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(VFC_GROUP_TYPE), any())).thenReturn(VfcInstanceGroupType);
294         when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE), any())).thenReturn(heatGroupType);
295         when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(ROOT_GROUP_TYPE), any())).thenReturn(rootGroupType);
296     }
297
298     private static GroupTypeDefinition buildRootGroupType() {
299         return createGroupTypeDefinition(ROOT_GROUP_TYPE, null,
300             "The TOSCA Group Type all other TOSCA Group Types derive from");
301     }
302
303     private static GroupTypeDefinition buildHeatStackGroupType() {
304         GroupTypeDefinition groupType = createGroupTypeDefinition(HEAT_GROUP_TYPE, "tosca.groups.Root",
305             "Grouped all heat resources which are in the same heat stack");
306
307         GroupProperty property1 = createGroupProperty("heat_file",
308             "Heat file which associate to this group/heat stack", "SUPPORTED");
309
310         GroupProperty property2 = createGroupProperty("description",
311             "Group description", "SUPPORTED");
312
313         groupType.setProperties(Lists.newArrayList(property1, property2));
314         return groupType;
315     }
316
317     private static GroupTypeDefinition buildVfcInstanceGroupType() {
318         GroupTypeDefinition groupType = createGroupTypeDefinition(VFC_GROUP_TYPE, "tosca.groups.Root",
319             "Groups of VFCs with same parent port role");
320
321         GroupProperty property1 = createGroupProperty("vfc_instance_group_function",
322             "Function of this VFC group", null);
323
324         GroupProperty property2 = createGroupProperty("vfc_parent_port_role",
325             "Common role of parent ports of VFCs in this group", null);
326
327         GroupProperty property3 = createGroupProperty("network_collection_function",
328             "Network collection function assigned to this group", null);
329
330         GroupProperty property4 = createGroupProperty("subinterface_role",
331             "Common role of subinterfaces of VFCs in this group, criteria the group is created", null);
332
333         groupType.setProperties(Lists.newArrayList(property1, property2, property3, property4));
334
335         CapabilityDefinition capability = new CapabilityDefinition();
336         capability.setType(CAPABILITY_TYPE);
337         capability.setName(CAPABILITY_NAME);
338         ComponentInstanceProperty capabilityProperty = new ComponentInstanceProperty();
339         capabilityProperty.setName("vfc_instance_group_reference");
340         capabilityProperty.setType("string");
341         capability.setProperties(Collections.singletonList(capabilityProperty));
342
343         Map<String, CapabilityDefinition> capabilityMap = new HashMap<>();
344         capabilityMap.put(CAPABILITY_NAME, capability);
345         groupType.setCapabilities(capabilityMap);
346         return groupType;
347     }
348
349     private static GroupTypeDefinition createGroupTypeDefinition(String type, String derivedFrom, String description) {
350         GroupTypeDefinition property = new GroupTypeDefinition();
351
352         if (type != null) {
353             property.setType(type);
354         }
355
356         if (derivedFrom != null) {
357             property.setDerivedFrom(derivedFrom);
358         }
359
360         if (description != null) {
361             property.setDescription(description);
362         }
363
364         return property;
365     }
366
367     private static GroupProperty createGroupProperty(String name, String description,
368                                                      String status) {
369         GroupProperty property = new GroupProperty();
370         if (name != null) {
371             property.setName(name);
372         }
373
374         if (description != null) {
375             property.setDescription(description);
376         }
377
378         if (status != null) {
379             property.setStatus(status);
380         }
381
382         property.setType("string");
383         property.setRequired(true);
384
385         return property;
386     }
387
388     private void validateParsedYamlWithPolicies(ParsedToscaYamlInfo parsedYaml) {
389         // validate  policies
390         assertThat(parsedYaml.getPolicies()).isNotNull();
391         assertThat(parsedYaml.getPolicies()).containsKey(OPENECOMP_POLICY_NAME);
392         assertThat(parsedYaml.getPolicies().get(OPENECOMP_POLICY_NAME)).isInstanceOf(PolicyDefinition.class);
393     }
394
395     private void stubGetPolicyType() {
396         when(policyTypeBusinessLogic.getLatestPolicyTypeByType(eq(OPENECOMP_ANTILOCATE_POLICY_TYPE), any()))
397             .thenReturn(OPENECOMP_POLICY_TYPE);
398     }
399
400     private static PolicyTypeDefinition buildOpenecompPolicyType() {
401         return createPolicyTypeDefinition(OPENECOMP_POLICY_NAME, OPENECOMP_ANTILOCATE_POLICY_TYPE, ROOT_POLICIES_TYPE,
402             "The Openecomp Antilocate policy");
403     }
404
405     private static PolicyTypeDefinition createPolicyTypeDefinition(String policyName, String policyType, String derivedFrom, String description) {
406         PolicyTypeDefinition policyTypeDefinition = new PolicyTypeDefinition();
407         if (policyName != null && !policyName.isEmpty()) {
408             policyTypeDefinition.setName(policyName);
409         }
410         if (policyType != null) {
411             policyTypeDefinition.setType(policyType);
412         }
413         if (derivedFrom != null) {
414             policyTypeDefinition.setDerivedFrom(derivedFrom);
415         }
416         if (description != null) {
417             policyTypeDefinition.setDescription(description);
418         }
419         return policyTypeDefinition;
420     }
421
422     private Optional<String> getInterfaceTemplateYaml(CsarInfo csarInfo) {
423         String[] yamlFile;
424         String interfaceTemplateYaml = "";
425         if (csarInfo.getMainTemplateName().contains(".yml")) {
426             yamlFile = csarInfo.getMainTemplateName().split(".yml");
427             interfaceTemplateYaml = yamlFile[0] + "-interface.yml";
428         } else if (csarInfo.getMainTemplateName().contains(".yaml")) {
429             yamlFile = csarInfo.getMainTemplateName().split(".yaml");
430             interfaceTemplateYaml = yamlFile[0] + "-interface.yaml";
431         }
432         if (csarInfo.getCsar().containsKey(interfaceTemplateYaml)) {
433             return Optional.of(new String(csarInfo.getCsar().get(interfaceTemplateYaml)));
434         }
435         return Optional.empty();
436     }
437 }