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