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 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.impl.PolicyTypeBusinessLogic;
39 import org.openecomp.sdc.be.components.validation.AnnotationValidator;
40 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
41 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
42 import org.openecomp.sdc.be.model.CapabilityDefinition;
43 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
44 import org.openecomp.sdc.be.model.GroupProperty;
45 import org.openecomp.sdc.be.model.GroupTypeDefinition;
46 import org.openecomp.sdc.be.model.ParsedToscaYamlInfo;
47 import org.openecomp.sdc.be.model.PolicyDefinition;
48 import org.openecomp.sdc.be.model.PolicyTypeDefinition;
49 import org.openecomp.sdc.be.model.UploadArtifactInfo;
50 import org.openecomp.sdc.be.model.UploadComponentInstanceInfo;
51 import org.openecomp.sdc.be.model.User;
52 import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations;
53 import org.openecomp.sdc.common.zip.ZipUtils;
54 import org.openecomp.sdc.common.zip.exception.ZipException;
57 import java.net.URISyntaxException;
58 import java.util.Collections;
59 import java.util.HashMap;
60 import java.util.List;
62 import java.util.Optional;
63 import java.util.stream.Collectors;
64 import org.springframework.test.util.ReflectionTestUtils;
66 import static org.assertj.core.api.Assertions.assertThat;
67 import static org.junit.Assert.assertNotNull;
68 import static org.mockito.ArgumentMatchers.eq;
69 import static org.mockito.Mockito.when;
70 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.ARTIFACTS;
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 PolicyTypeBusinessLogic policyTypeBusinessLogic;
88 private YamlTemplateParsingHandler handler;
90 private static Map<String, byte[]> csar;
91 private static String resourceYml;
93 private final static String VFC_GROUP_TYPE = "org.openecomp.groups.VfcInstanceGroup";
94 private final static String HEAT_GROUP_TYPE = "org.openecomp.groups.heat.HeatStack";
95 private final static String ROOT_GROUP_TYPE = "tosca.groups.Root";
96 private final static String OPENECOMP_ANTILOCATE_POLICY_TYPE = "org.openecomp.policies.placement.Antilocate";
97 private final static String ROOT_POLICIES_TYPE = "tosca.policies.Root";
98 private final static GroupTypeDefinition VfcInstanceGroupType = buildVfcInstanceGroupType();
99 private final static GroupTypeDefinition heatGroupType = buildHeatStackGroupType();
100 private final static GroupTypeDefinition rootGroupType = buildRootGroupType();
101 private static final PolicyTypeDefinition OPENECOMP_POLICY_TYPE = buildOpenecompPolicyType();
102 private final static String OPENECOMP_POLICY_NAME = "vepdg_server_group_policy";
103 private final static String CAPABILITY_TYPE = "org.openecomp.capabilities.VLANAssignment";
104 private final static String CAPABILITY_NAME = "vlan_assignment";
105 private static final String CSAR_FILE_PATH = "csars/with_groups.csar";
106 private static final String FILE_NAME = "MainServiceTemplate.yaml";
107 private static final String CSAR_UUID = "csarUUID";
108 private static final String RESOURCE_NAME = "resourceName";
109 private static final String MAIN_TEMPLATE_NAME = "Definitions/MainServiceTemplate.yaml";
110 private static final String NODE_NAME = "org.openecomp.resource.abstract.nodes.heat.mg";
111 private static final String MAIN_GROUP_NAME = "x_group";
112 private static final String NESTED_GROUP_NAME = "nested_mg_vepdg_group";
115 YamlTemplateParsingHandler testSubject;
118 public static void prepareData() throws URISyntaxException, ZipException {
119 final File csarFile = new File(
120 YamlTemplateParsingHandlerTest.class.getClassLoader().getResource(CSAR_FILE_PATH).toURI());
121 csar = ZipUtils.readZip(csarFile, false);
123 Optional<String> keyOp = csar.keySet().stream().filter(k -> k.endsWith(FILE_NAME)).findAny();
124 byte[] mainTemplateService = keyOp.map(csar::get).orElse(null);
125 assertNotNull(mainTemplateService);
127 resourceYml = new String(mainTemplateService);
131 public void setup() {
133 AnnotationBusinessLogic annotationBusinessLogic = new AnnotationBusinessLogic(annotationTypeOperations,
134 annotationValidator);
135 handler = new YamlTemplateParsingHandler(janusGraphDao, groupTypeBusinessLogic, annotationBusinessLogic, policyTypeBusinessLogic);
136 ReflectionTestUtils.setField(handler, "policyTypeBusinessLogic", policyTypeBusinessLogic);
142 public void parseResourceInfoFromOneNodeTest() {
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);
148 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
149 csarInfo.extractNodeTypesInfo(), NODE_NAME);
151 validateParsedYaml(parsedYaml, NESTED_GROUP_NAME,
152 Lists.newArrayList("heat_file", "description"));
156 public void parseResourceInfoFromYAMLTest() {
158 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
159 new HashMap<>(), "");
160 validateParsedYamlWithCapability(parsedYaml);
164 public void testSetArtifacts() {
165 UploadComponentInstanceInfo nodeTemplateInfo = new UploadComponentInstanceInfo();
166 Map<String, Object> nodeTemplateJsonMap = new HashMap<>();
167 Map<String, String> nodeMap = new HashMap<>();
168 nodeMap.put("name","test_name");
169 nodeMap.put("type","test_type");
170 nodeTemplateJsonMap.put(ARTIFACTS.getElementName(), nodeMap);
171 Deencapsulation.invoke(testSubject, "setArtifacts", nodeTemplateInfo, nodeTemplateJsonMap);
172 assertNotNull(nodeTemplateInfo.getArtifacts());
176 public void testCreateArtifactsModuleFromYaml() {
177 Map<String, Map<String, Map<String, String>>> nodeTemplateJsonMap = new HashMap<>();
178 Map<String, Map<String,String>> map0 = new HashMap<>();
179 Map<String, String> map1 = new HashMap<>();
180 map1.put("file", "test_file");
181 map1.put("type", "test_type");
182 map0.put("test_art", map1);
183 nodeTemplateJsonMap.put(ARTIFACTS.getElementName(), map0);
184 Map<String, Map<String, UploadArtifactInfo>> result;
185 result = Deencapsulation.invoke(testSubject, "createArtifactsModuleFromYaml", nodeTemplateJsonMap);
186 Assert.assertTrue(MapUtils.isNotEmpty(result));
187 Assert.assertTrue(MapUtils.isNotEmpty(result.get(ARTIFACTS.getElementName())));
188 Assert.assertEquals("test_file", result.get(ARTIFACTS.getElementName()).get("test_art").getFile());
189 Assert.assertEquals("test_type", result.get(ARTIFACTS.getElementName()).get("test_art").getType());
193 public void testAddModuleNodeTemplateArtifacts() {
194 Map<String, Map<String, UploadArtifactInfo>> result = new HashMap<>();
195 Map<String, String> map1 = new HashMap<>();
196 map1.put("file", "test_file");
197 map1.put("type", "test_type");
198 Deencapsulation.invoke(testSubject, "addModuleNodeTemplateArtifacts", result, map1, "test_art");
199 Assert.assertTrue(MapUtils.isNotEmpty(result));
200 Assert.assertTrue(MapUtils.isNotEmpty(result.get(ARTIFACTS.getElementName())));
201 Assert.assertEquals("test_file", result.get(ARTIFACTS.getElementName()).get("test_art").getFile());
202 Assert.assertEquals("test_type", result.get(ARTIFACTS.getElementName()).get("test_art").getType());
206 public void testBuildModuleNodeTemplateArtifact() {
207 Map<String, String> map1 = new HashMap<>();
208 map1.put("file", "test_file");
209 map1.put("type", "test_type");
210 UploadArtifactInfo result;
211 result = Deencapsulation.invoke(testSubject, "buildModuleNodeTemplateArtifact", map1);
212 assertNotNull(result);
213 Assert.assertEquals("test_file", result.getFile());
214 Assert.assertEquals("test_type", result.getType());
218 public void testFillArtifact() {
219 Map<String, String> map1 = new HashMap<>();
220 map1.put("file", "test_file");
221 map1.put("type", "test_type");
222 UploadArtifactInfo result = new UploadArtifactInfo();
223 Deencapsulation.invoke(testSubject, "fillArtifact", result, map1);
224 assertNotNull(result);
225 Assert.assertEquals("test_file", result.getFile());
226 Assert.assertEquals("test_type", result.getType());
230 public void parseResourceWithPoliciesDefined() {
231 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
232 new HashMap<>(), "");
233 validateParsedYamlWithPolicies(parsedYaml);
236 private void validateParsedYaml(ParsedToscaYamlInfo parsedYaml, String group, List<String> expectedProp) {
237 assertThat(parsedYaml).isNotNull();
238 assertThat(parsedYaml.getGroups()).isNotNull().containsKey(group);
239 assertThat(parsedYaml.getGroups().get(group)).isNotNull();
241 assertThat(parsedYaml.getGroups().get(group).getProperties()).isNotNull();
242 assertThat(parsedYaml.getGroups().get(group).getProperties()
244 .map(PropertyDataDefinition::getName)
245 .collect(Collectors.toList()))
246 .containsAll(expectedProp);
248 assertThat(parsedYaml.getGroups().get(group).getMembers()).isNotNull();
251 private void validateParsedYamlWithCapability(ParsedToscaYamlInfo parsedYaml) {
253 final List<String> expectedProp = Lists.newArrayList("vfc_parent_port_role",
254 "network_collection_function", "vfc_instance_group_function", "subinterface_role");
256 validateParsedYaml(parsedYaml, MAIN_GROUP_NAME, expectedProp);
258 assertThat(parsedYaml.getGroups().get(MAIN_GROUP_NAME).getCapabilities()
259 .get(CAPABILITY_TYPE)
260 .get(0).getProperties().get(0).getValue()).isEqualTo("success");
261 assertThat(parsedYaml.getGroups().get(MAIN_GROUP_NAME).getCapabilities()).isNotNull();
264 private void stubGetGroupType() {
265 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(VFC_GROUP_TYPE))).thenReturn(VfcInstanceGroupType);
266 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE))).thenReturn(heatGroupType);
267 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(ROOT_GROUP_TYPE))).thenReturn(rootGroupType);
270 private static GroupTypeDefinition buildRootGroupType() {
271 return createGroupTypeDefinition(ROOT_GROUP_TYPE, null,
272 "The TOSCA Group Type all other TOSCA Group Types derive from");
275 private static GroupTypeDefinition buildHeatStackGroupType() {
276 GroupTypeDefinition groupType = createGroupTypeDefinition(HEAT_GROUP_TYPE, "tosca.groups.Root",
277 "Grouped all heat resources which are in the same heat stack");
279 GroupProperty property1 = createGroupProperty("heat_file",
280 "Heat file which associate to this group/heat stack", "SUPPORTED");
282 GroupProperty property2 = createGroupProperty("description",
283 "Group description", "SUPPORTED");
285 groupType.setProperties(Lists.newArrayList(property1, property2));
289 private static GroupTypeDefinition buildVfcInstanceGroupType() {
290 GroupTypeDefinition groupType = createGroupTypeDefinition(VFC_GROUP_TYPE, "tosca.groups.Root",
291 "Groups of VFCs with same parent port role");
293 GroupProperty property1 = createGroupProperty("vfc_instance_group_function",
294 "Function of this VFC group", null);
296 GroupProperty property2 = createGroupProperty("vfc_parent_port_role",
297 "Common role of parent ports of VFCs in this group", null);
299 GroupProperty property3 = createGroupProperty("network_collection_function",
300 "Network collection function assigned to this group", null);
302 GroupProperty property4 = createGroupProperty("subinterface_role",
303 "Common role of subinterfaces of VFCs in this group, criteria the group is created", null);
305 groupType.setProperties(Lists.newArrayList(property1, property2, property3, property4));
307 CapabilityDefinition capability = new CapabilityDefinition();
308 capability.setType(CAPABILITY_TYPE);
309 capability.setName(CAPABILITY_NAME);
310 ComponentInstanceProperty capabilityProperty = new ComponentInstanceProperty();
311 capabilityProperty.setName("vfc_instance_group_reference");
312 capabilityProperty.setType("string");
313 capability.setProperties(Collections.singletonList(capabilityProperty));
315 Map<String, CapabilityDefinition> capabilityMap = new HashMap<>();
316 capabilityMap.put(CAPABILITY_NAME, capability);
317 groupType.setCapabilities(capabilityMap);
321 private static GroupTypeDefinition createGroupTypeDefinition(String type, String derivedFrom, String description){
322 GroupTypeDefinition property = new GroupTypeDefinition();
325 property.setType(type);
327 if (derivedFrom != null) {
328 property.setDerivedFrom(derivedFrom);
331 if (description != null) {
332 property.setDescription(description);
337 private static GroupProperty createGroupProperty(String name, String description,
339 GroupProperty property = new GroupProperty();
341 property.setName(name);
343 if (description != null) {
344 property.setDescription(description);
347 if (status != null) {
348 property.setStatus(status);
351 property.setType("string");
352 property.setRequired(true);
357 private void validateParsedYamlWithPolicies(ParsedToscaYamlInfo parsedYaml) {
359 assertThat(parsedYaml.getPolicies()).isNotNull();
360 assertThat(parsedYaml.getPolicies()).containsKey(OPENECOMP_POLICY_NAME);
361 assertThat(parsedYaml.getPolicies().get(OPENECOMP_POLICY_NAME)).isInstanceOf(PolicyDefinition.class);
364 private void stubGetPolicyType () {
365 when(policyTypeBusinessLogic.getLatestPolicyTypeByType(eq(OPENECOMP_ANTILOCATE_POLICY_TYPE))).thenReturn(
366 OPENECOMP_POLICY_TYPE);
369 private static PolicyTypeDefinition buildOpenecompPolicyType() {
370 return createPolicyTypeDefinition(OPENECOMP_POLICY_NAME, OPENECOMP_ANTILOCATE_POLICY_TYPE, ROOT_POLICIES_TYPE,
371 "The Openecomp Antilocate policy");
374 private static PolicyTypeDefinition createPolicyTypeDefinition(String policyName, String policyType, String derivedFrom, String description) {
375 PolicyTypeDefinition policyTypeDefinition = new PolicyTypeDefinition();
376 if (policyName != null && !policyName.isEmpty()) {
377 policyTypeDefinition.setName(policyName);
379 if (policyType != null) {
380 policyTypeDefinition.setType(policyType);
382 if (derivedFrom != null) {
383 policyTypeDefinition.setDerivedFrom(derivedFrom);
385 if (description != null) {
386 policyTypeDefinition.setDescription(description);
388 return policyTypeDefinition;