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 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;
30 import java.net.URISyntaxException;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.List;
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;
67 @RunWith(MockitoJUnitRunner.class)
68 public class YamlTemplateParsingHandlerTest {
71 private GroupTypeBusinessLogic groupTypeBusinessLogic;
73 private AnnotationTypeOperations annotationTypeOperations;
75 private AnnotationValidator annotationValidator;
77 private JanusGraphDao janusGraphDao;
81 private YamlTemplateParsingHandler handler;
83 private static Map<String, byte[]> csar;
84 private static String resourceYml;
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";
104 YamlTemplateParsingHandler testSubject;
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);
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);
116 resourceYml = new String(mainTemplateService);
120 public void setup() {
122 AnnotationBusinessLogic annotationBusinessLogic = new AnnotationBusinessLogic(annotationTypeOperations,
123 annotationValidator);
124 handler = new YamlTemplateParsingHandler(janusGraphDao, groupTypeBusinessLogic, annotationBusinessLogic);
129 public void parseResourceInfoFromOneNodeTest() {
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);
135 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
136 csarInfo.extractNodeTypesInfo(), NODE_NAME);
138 validateParsedYaml(parsedYaml, NESTED_GROUP_NAME,
139 Lists.newArrayList("heat_file", "description"));
143 public void parseResourceInfoFromYAMLTest() {
145 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
146 new HashMap<>(), "");
147 validateParsedYamlWithCapability(parsedYaml);
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();
155 assertThat(parsedYaml.getGroups().get(group).getProperties()).isNotNull();
156 assertThat(parsedYaml.getGroups().get(group).getProperties()
158 .map(PropertyDataDefinition::getName)
159 .collect(Collectors.toList()))
160 .containsAll(expectedProp);
162 assertThat(parsedYaml.getGroups().get(group).getMembers()).isNotNull();
165 private void validateParsedYamlWithCapability(ParsedToscaYamlInfo parsedYaml) {
167 final List<String> expectedProp = Lists.newArrayList("vfc_parent_port_role",
168 "network_collection_function", "vfc_instance_group_function", "subinterface_role");
170 validateParsedYaml(parsedYaml, MAIN_GROUP_NAME, expectedProp);
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();
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);
184 private static GroupTypeDefinition buildRootGroupType() {
185 return createGroupTypeDefinition(ROOT_GROUP_TYPE, null,
186 "The TOSCA Group Type all other TOSCA Group Types derive from");
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");
193 GroupProperty property1 = createGroupProperty("heat_file",
194 "Heat file which associate to this group/heat stack", "SUPPORTED");
196 GroupProperty property2 = createGroupProperty("description",
197 "Group description", "SUPPORTED");
199 groupType.setProperties(Lists.newArrayList(property1, property2));
203 private static GroupTypeDefinition buildVfcInstanceGroupType() {
204 GroupTypeDefinition groupType = createGroupTypeDefinition(VFC_GROUP_TYPE, "tosca.groups.Root",
205 "Groups of VFCs with same parent port role");
207 GroupProperty property1 = createGroupProperty("vfc_instance_group_function",
208 "Function of this VFC group", null);
210 GroupProperty property2 = createGroupProperty("vfc_parent_port_role",
211 "Common role of parent ports of VFCs in this group", null);
213 GroupProperty property3 = createGroupProperty("network_collection_function",
214 "Network collection function assigned to this group", null);
216 GroupProperty property4 = createGroupProperty("subinterface_role",
217 "Common role of subinterfaces of VFCs in this group, criteria the group is created", null);
219 groupType.setProperties(Lists.newArrayList(property1, property2, property3, property4));
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));
229 Map<String, CapabilityDefinition> capabilityMap = new HashMap<>();
230 capabilityMap.put(CAPABILITY_NAME, capability);
231 groupType.setCapabilities(capabilityMap);
235 private static GroupTypeDefinition createGroupTypeDefinition(String type, String derivedFrom, String description){
236 GroupTypeDefinition property = new GroupTypeDefinition();
239 property.setType(type);
241 if (derivedFrom != null) {
242 property.setDerivedFrom(derivedFrom);
245 if (description != null) {
246 property.setDescription(description);
251 private static GroupProperty createGroupProperty(String name, String description,
253 GroupProperty property = new GroupProperty();
255 property.setName(name);
257 if (description != null) {
258 property.setDescription(description);
261 if (status != null) {
262 property.setStatus(status);
265 property.setType("string");
266 property.setRequired(true);
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());
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());
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());
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());
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());