1 package org.openecomp.sdc.be.components.impl.utils;
3 import org.assertj.core.util.Lists;
4 import org.junit.Before;
6 import org.junit.runner.RunWith;
7 import org.mockito.Mock;
8 import org.mockito.junit.MockitoJUnitRunner;
9 import org.openecomp.sdc.be.components.csar.YamlTemplateParsingHandler;
10 import org.openecomp.sdc.be.components.impl.AnnotationBusinessLogic;
11 import org.openecomp.sdc.be.components.impl.GroupTypeBusinessLogic;
12 import org.openecomp.sdc.be.components.validation.AnnotationValidator;
13 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
14 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
15 import org.openecomp.sdc.be.impl.ComponentsUtils;
16 import org.openecomp.sdc.be.model.*;
17 import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations;
18 import org.openecomp.sdc.common.util.ZipUtil;
21 import java.io.IOException;
22 import java.io.StringReader;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.nio.file.Paths;
27 import java.util.stream.Collectors;
29 import static org.assertj.core.api.Assertions.assertThat;
30 import static org.mockito.ArgumentMatchers.eq;
31 import static org.mockito.Mockito.when;
32 @RunWith(MockitoJUnitRunner.class)
33 public class YamlTemplateParsingHandlerTest {
35 private final static String VFC_GROUP_TYPE = "org.openecomp.groups.VfcInstanceGroup";
36 private final static String HEAT_GROUP_TYPE = "org.openecomp.groups.heat.HeatStack";
37 private final static String ROOT_GROUP_TYPE = "tosca.groups.Root";
38 private final static GroupTypeDefinition VfcInstanceGroupType = buildVfcInstanceGroupType();
39 private final static GroupTypeDefinition heatGroupType = buildHeatStackGroupType();
40 private final static GroupTypeDefinition rootGroupType = buildRootGroupType();
41 private final static String CAPABILITY_TYPE = "org.openecomp.capabilities.VLANAssignment";
42 private final static String CAPABILITY_NAME = "vlan_assignment";
43 public static final String csarsFilePath = System.getProperty("user.dir") + File.separator + "src" + File.separator + "test" + File.separator + "resources" + File.separator + "csars" ;
45 private YamlTemplateParsingHandler handler;
46 private AnnotationBusinessLogic annotationBusinessLogic;
48 private ComponentsUtils componentsUtils;
50 private GroupTypeBusinessLogic groupTypeBusinessLogic;
52 private AnnotationTypeOperations annotationTypeOperations;
54 private AnnotationValidator annotationValidator;
56 private TitanDao titanDao;
60 annotationBusinessLogic = new AnnotationBusinessLogic(annotationTypeOperations, annotationValidator);
61 handler = new YamlTemplateParsingHandler(titanDao, groupTypeBusinessLogic, annotationBusinessLogic);
65 public void parseResourceInfoFromYAMLTest(){
66 Path path = Paths.get(csarsFilePath + File.separator + "with_groups.csar");
68 Map<String, byte[]> csar = ZipUtil.readZip(Files.readAllBytes(path));
69 String fileName = "MainServiceTemplate.yaml";
70 Optional<String> keyOp = csar.keySet().stream().filter(k -> k.endsWith(fileName)).findAny();
71 byte[] mainTemplateService = csar.get(keyOp.get());
72 Properties props = new Properties();
73 String resourceYml = new String(mainTemplateService);
74 props.load(new StringReader(resourceYml.replace("\\","\\\\")));
75 Resource resource = new Resource();
79 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(fileName, resourceYml, new HashMap<>(), new HashMap<>(), "");
81 validateParsedYaml(parsedYaml);
83 } catch (IOException e) {
90 private void validateParsedYaml(ParsedToscaYamlInfo parsedYaml) {
91 assertThat(parsedYaml).isNotNull();
92 assertThat(parsedYaml.getGroups()).isNotNull().containsKey("x_group");
93 assertThat(parsedYaml.getGroups().get("x_group")).isNotNull();
94 assertThat(parsedYaml.getGroups().get("x_group").getProperties()).isNotNull();
95 assertThat(parsedYaml.getGroups().get("x_group").getProperties()
97 .map(PropertyDataDefinition::getName)
98 .collect(Collectors.toList()))
99 .containsAll(Lists.newArrayList("vfc_parent_port_role", "network_collection_function", "vfc_instance_group_function", "subinterface_role"));
100 assertThat(parsedYaml.getGroups().get("x_group").getCapabilities()
101 .get(CAPABILITY_TYPE)
102 .get(0).getProperties().get(0).getValue()).isEqualTo("success");
103 assertThat(parsedYaml.getGroups().get("x_group").getProperties()
105 .map(PropertyDataDefinition::getName)
106 .collect(Collectors.toList()))
107 .containsAll(Lists.newArrayList("vfc_parent_port_role", "network_collection_function", "vfc_instance_group_function", "subinterface_role"));
108 assertThat(parsedYaml.getGroups().get("x_group").getCapabilities()).isNotNull();
109 assertThat(parsedYaml.getGroups().get("x_group").getMembers()).isNotNull();
112 private void stubGetGroupType() {
113 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(VFC_GROUP_TYPE))).thenReturn(VfcInstanceGroupType);
114 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE))).thenReturn(heatGroupType);
115 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(ROOT_GROUP_TYPE))).thenReturn(rootGroupType);
116 // when(annotationBusinessLogic.validateAndMergeAnnotationsAndAssignToInput(any(Map.class))).thenReturn(null);
119 private static GroupTypeDefinition buildRootGroupType() {
120 GroupTypeDefinition groupType = new GroupTypeDefinition();
121 groupType.setType(ROOT_GROUP_TYPE);
122 groupType.setDescription("The TOSCA Group Type all other TOSCA Group Types derive from");
126 private static GroupTypeDefinition buildHeatStackGroupType() {
127 GroupTypeDefinition groupType = new GroupTypeDefinition();
128 groupType.setType(HEAT_GROUP_TYPE);
129 groupType.setDerivedFrom("tosca.groups.Root");
130 groupType.setDescription("Grouped all heat resources which are in the same heat stack");
132 GroupProperty property1 = new GroupProperty();
133 property1.setName("heat_file");
134 property1.setType("string");
135 property1.setRequired(true);
136 property1.setDescription("Heat file which associate to this group/heat stack");
137 property1.setStatus("SUPPORTED");
139 GroupProperty property2 = new GroupProperty();
140 property2.setName("description");
141 property2.setType("string");
142 property2.setRequired(true);
143 property2.setDescription("group description");
144 property2.setStatus("SUPPORTED");
145 groupType.setProperties(Lists.newArrayList(property1, property2));
149 private static GroupTypeDefinition buildVfcInstanceGroupType() {
150 GroupTypeDefinition groupType = new GroupTypeDefinition();
151 groupType.setType(VFC_GROUP_TYPE);
152 groupType.setDerivedFrom("tosca.groups.Root");
153 groupType.setDescription("groups VFCs with same parent port role");
154 GroupProperty property1 = new GroupProperty();
155 property1.setName("vfc_instance_group_function");
156 property1.setType("string");
157 property1.setRequired(true);
158 property1.setDescription("function of this VFC group");
160 GroupProperty property2 = new GroupProperty();
161 property2.setName("vfc_parent_port_role");
162 property2.setType("string");
163 property2.setRequired(true);
164 property2.setDescription("common role of parent ports of VFCs in this group");
166 GroupProperty property3 = new GroupProperty();
167 property3.setName("network_collection_function");
168 property3.setType("string");
169 property3.setRequired(true);
170 property3.setDescription("network collection function assigned to this group");
172 GroupProperty property4 = new GroupProperty();
173 property4.setName("subinterface_role");
174 property4.setType("string");
175 property4.setRequired(true);
176 property4.setDescription("common role of subinterfaces of VFCs in this group, criteria the group is created");
178 groupType.setProperties(Lists.newArrayList(property1, property2, property3, property4));
180 CapabilityDefinition capability = new CapabilityDefinition();
181 capability.setType(CAPABILITY_TYPE);
182 capability.setName(CAPABILITY_NAME);
183 ComponentInstanceProperty capabilityProperty = new ComponentInstanceProperty();
184 capabilityProperty.setName("vfc_instance_group_reference");
185 capabilityProperty.setType("string");
186 capability.setProperties(Arrays.asList(capabilityProperty));
188 Map<String, CapabilityDefinition> capabilityMap = new HashMap<>();
189 capabilityMap.put(CAPABILITY_NAME, capability);
190 groupType.setCapabilities(capabilityMap);