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