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;
30 import java.io.IOException;
31 import java.net.URISyntaxException;
33 import java.util.stream.Collectors;
35 import org.assertj.core.util.Lists;
36 import org.junit.Assert;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.InjectMocks;
42 import org.mockito.Mock;
43 import org.mockito.junit.MockitoJUnitRunner;
45 import org.openecomp.sdc.ZipUtil;
46 import org.openecomp.sdc.be.components.csar.CsarInfo;
47 import org.openecomp.sdc.be.components.csar.YamlTemplateParsingHandler;
48 import org.openecomp.sdc.be.components.impl.AnnotationBusinessLogic;
49 import org.openecomp.sdc.be.components.impl.GroupTypeBusinessLogic;
50 import org.openecomp.sdc.be.components.validation.AnnotationValidator;
51 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
52 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
53 import org.openecomp.sdc.be.model.*;
54 import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations;
57 import java.io.IOException;
58 import java.io.StringReader;
59 import java.nio.file.Files;
60 import java.nio.file.Path;
61 import java.nio.file.Paths;
63 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.*;
64 @RunWith(MockitoJUnitRunner.class)
65 public class YamlTemplateParsingHandlerTest {
68 private GroupTypeBusinessLogic groupTypeBusinessLogic;
70 private AnnotationTypeOperations annotationTypeOperations;
72 private AnnotationValidator annotationValidator;
74 private JanusGraphDao janusGraphDao;
78 private YamlTemplateParsingHandler handler;
80 private static Map<String, byte[]> csar;
81 private static String resourceYml;
83 private final static String VFC_GROUP_TYPE = "org.openecomp.groups.VfcInstanceGroup";
84 private final static String HEAT_GROUP_TYPE = "org.openecomp.groups.heat.HeatStack";
85 private final static String ROOT_GROUP_TYPE = "tosca.groups.Root";
86 private final static GroupTypeDefinition VfcInstanceGroupType = buildVfcInstanceGroupType();
87 private final static GroupTypeDefinition heatGroupType = buildHeatStackGroupType();
88 private final static GroupTypeDefinition rootGroupType = buildRootGroupType();
89 private final static String CAPABILITY_TYPE = "org.openecomp.capabilities.VLANAssignment";
90 private final static String CAPABILITY_NAME = "vlan_assignment";
91 private static final String CSAR_FILE_PATH = "/csars/with_groups.csar";
93 private static final String FILE_NAME = "MainServiceTemplate.yaml";
95 private static final String CSAR_UUID = "csarUUID";
96 private static final String RESOURCE_NAME = "resourceName";
97 private static final String MAIN_TEMPLATE_NAME = "Definitions/MainServiceTemplate.yaml";
98 private static final String NODE_NAME = "org.openecomp.resource.abstract.nodes.heat.mg";
99 private static final String MAIN_GROUP_NAME = "x_group";
100 private static final String NESTED_GROUP_NAME = "nested_mg_vepdg_group";
103 YamlTemplateParsingHandler testSubject;
106 public static void prepareData() throws IOException, URISyntaxException {
107 csar = ZipUtil.readData(CSAR_FILE_PATH);
109 Optional<String> keyOp = csar.keySet().stream().filter(k -> k.endsWith(FILE_NAME)).findAny();
110 byte[] mainTemplateService = keyOp.map(csar::get).orElse(null);
111 assertNotNull(mainTemplateService);
113 resourceYml = new String(mainTemplateService);
117 public void setup() {
119 AnnotationBusinessLogic annotationBusinessLogic = new AnnotationBusinessLogic(annotationTypeOperations,
120 annotationValidator);
121 handler = new YamlTemplateParsingHandler(janusGraphDao, groupTypeBusinessLogic, annotationBusinessLogic);
126 public void parseResourceInfoFromOneNodeTest() {
128 String main_template_content = new String(csar.get(MAIN_TEMPLATE_NAME));
129 CsarInfo csarInfo = new CsarInfo(user, CSAR_UUID, csar, RESOURCE_NAME,
130 MAIN_TEMPLATE_NAME, main_template_content, true);
132 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
133 csarInfo.extractNodeTypesInfo(), NODE_NAME);
135 validateParsedYaml(parsedYaml, NESTED_GROUP_NAME,
136 Lists.newArrayList("heat_file", "description"));
140 public void parseResourceInfoFromYAMLTest() {
142 ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
143 new HashMap<>(), "");
144 validateParsedYamlWithCapability(parsedYaml);
147 private void validateParsedYaml(ParsedToscaYamlInfo parsedYaml, String group, List<String> expectedProp) {
148 assertThat(parsedYaml).isNotNull();
149 assertThat(parsedYaml.getGroups()).isNotNull().containsKey(group);
150 assertThat(parsedYaml.getGroups().get(group)).isNotNull();
152 assertThat(parsedYaml.getGroups().get(group).getProperties()).isNotNull();
153 assertThat(parsedYaml.getGroups().get(group).getProperties()
155 .map(PropertyDataDefinition::getName)
156 .collect(Collectors.toList()))
157 .containsAll(expectedProp);
159 assertThat(parsedYaml.getGroups().get(group).getMembers()).isNotNull();
162 private void validateParsedYamlWithCapability(ParsedToscaYamlInfo parsedYaml) {
164 final List<String> expectedProp = Lists.newArrayList("vfc_parent_port_role",
165 "network_collection_function", "vfc_instance_group_function", "subinterface_role");
167 validateParsedYaml(parsedYaml, MAIN_GROUP_NAME, expectedProp);
169 assertThat(parsedYaml.getGroups().get(MAIN_GROUP_NAME).getCapabilities()
170 .get(CAPABILITY_TYPE)
171 .get(0).getProperties().get(0).getValue()).isEqualTo("success");
172 assertThat(parsedYaml.getGroups().get(MAIN_GROUP_NAME).getCapabilities()).isNotNull();
175 private void stubGetGroupType() {
176 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(VFC_GROUP_TYPE))).thenReturn(VfcInstanceGroupType);
177 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE))).thenReturn(heatGroupType);
178 when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(ROOT_GROUP_TYPE))).thenReturn(rootGroupType);
181 private static GroupTypeDefinition buildRootGroupType() {
182 return createGroupTypeDefinition(ROOT_GROUP_TYPE, null,
183 "The TOSCA Group Type all other TOSCA Group Types derive from");
186 private static GroupTypeDefinition buildHeatStackGroupType() {
187 GroupTypeDefinition groupType = createGroupTypeDefinition(HEAT_GROUP_TYPE, "tosca.groups.Root",
188 "Grouped all heat resources which are in the same heat stack");
190 GroupProperty property1 = createGroupProperty("heat_file",
191 "Heat file which associate to this group/heat stack", "SUPPORTED");
193 GroupProperty property2 = createGroupProperty("description",
194 "Group description", "SUPPORTED");
196 groupType.setProperties(Lists.newArrayList(property1, property2));
200 private static GroupTypeDefinition buildVfcInstanceGroupType() {
201 GroupTypeDefinition groupType = createGroupTypeDefinition(VFC_GROUP_TYPE, "tosca.groups.Root",
202 "Groups of VFCs with same parent port role");
204 GroupProperty property1 = createGroupProperty("vfc_instance_group_function",
205 "Function of this VFC group", null);
207 GroupProperty property2 = createGroupProperty("vfc_parent_port_role",
208 "Common role of parent ports of VFCs in this group", null);
210 GroupProperty property3 = createGroupProperty("network_collection_function",
211 "Network collection function assigned to this group", null);
213 GroupProperty property4 = createGroupProperty("subinterface_role",
214 "Common role of subinterfaces of VFCs in this group, criteria the group is created", null);
216 groupType.setProperties(Lists.newArrayList(property1, property2, property3, property4));
218 CapabilityDefinition capability = new CapabilityDefinition();
219 capability.setType(CAPABILITY_TYPE);
220 capability.setName(CAPABILITY_NAME);
221 ComponentInstanceProperty capabilityProperty = new ComponentInstanceProperty();
222 capabilityProperty.setName("vfc_instance_group_reference");
223 capabilityProperty.setType("string");
224 capability.setProperties(Collections.singletonList(capabilityProperty));
226 Map<String, CapabilityDefinition> capabilityMap = new HashMap<>();
227 capabilityMap.put(CAPABILITY_NAME, capability);
228 groupType.setCapabilities(capabilityMap);
232 private static GroupTypeDefinition createGroupTypeDefinition(String type, String derivedFrom, String description){
233 GroupTypeDefinition property = new GroupTypeDefinition();
236 property.setType(type);
238 if (derivedFrom != null) {
239 property.setDerivedFrom(derivedFrom);
242 if (description != null) {
243 property.setDescription(description);
248 private static GroupProperty createGroupProperty(String name, String description,
250 GroupProperty property = new GroupProperty();
252 property.setName(name);
254 if (description != null) {
255 property.setDescription(description);
258 if (status != null) {
259 property.setStatus(status);
262 property.setType("string");
263 property.setRequired(true);
268 public void testSetArtifacts() throws Exception {
269 UploadComponentInstanceInfo nodeTemplateInfo = new UploadComponentInstanceInfo();
270 Map<String, Object> nodeTemplateJsonMap = new HashMap<>();
271 Map<String, String> nodeMap = new HashMap<>();
272 nodeMap.put("name","test_name");
273 nodeMap.put("type","test_type");
274 nodeTemplateJsonMap.put(ARTIFACTS.getElementName(), nodeMap);
275 Deencapsulation.invoke(testSubject, "setArtifacts", nodeTemplateInfo, nodeTemplateJsonMap);
276 Assert.assertTrue(nodeTemplateInfo.getArtifacts() != null);
280 public void testCreateArtifactsModuleFromYaml() throws Exception {
281 Map<String, Map<String, Map<String, String>>> nodeTemplateJsonMap = new HashMap<>();
282 Map<String, Map<String,String>> map0 = new HashMap<>();
283 Map<String, String> map1 = new HashMap<>();
284 map1.put("file", "test_file");
285 map1.put("type", "test_type");
286 map0.put("test_art", map1);
287 nodeTemplateJsonMap.put(ARTIFACTS.getElementName(), map0);
288 Map<String, Map<String, UploadArtifactInfo>> result;
289 result = Deencapsulation.invoke(testSubject, "createArtifactsModuleFromYaml", nodeTemplateJsonMap);
290 Assert.assertTrue(MapUtils.isNotEmpty(result));
291 Assert.assertTrue(MapUtils.isNotEmpty(result.get(ARTIFACTS.getElementName())));
292 Assert.assertTrue(result.get(ARTIFACTS.getElementName()).get("test_art").getFile().equals("test_file"));
293 Assert.assertTrue(result.get(ARTIFACTS.getElementName()).get("test_art").getType().equals("test_type"));
297 public void testAddModuleNodeTemplateArtifacts() throws Exception {
298 Map<String, Map<String, UploadArtifactInfo>> result = new HashMap<>();
299 Map<String, String> map1 = new HashMap<>();
300 map1.put("file", "test_file");
301 map1.put("type", "test_type");
302 Deencapsulation.invoke(testSubject, "addModuleNodeTemplateArtifacts", result, map1, "test_art");
303 Assert.assertTrue(MapUtils.isNotEmpty(result));
304 Assert.assertTrue(MapUtils.isNotEmpty(result.get(ARTIFACTS.getElementName())));
305 Assert.assertTrue(result.get(ARTIFACTS.getElementName()).get("test_art").getFile().equals("test_file"));
306 Assert.assertTrue(result.get(ARTIFACTS.getElementName()).get("test_art").getType().equals("test_type"));
310 public void testBuildModuleNodeTemplateArtifact() throws Exception {
311 Map<String, String> map1 = new HashMap<>();
312 map1.put("file", "test_file");
313 map1.put("type", "test_type");
314 UploadArtifactInfo result;
315 result = Deencapsulation.invoke(testSubject, "buildModuleNodeTemplateArtifact", map1);
316 Assert.assertTrue(result != null);
317 Assert.assertTrue(result.getFile().equals("test_file"));
318 Assert.assertTrue(result.getType().equals("test_type"));
322 public void testFillArtifact() throws Exception {
323 Map<String, String> map1 = new HashMap<>();
324 map1.put("file", "test_file");
325 map1.put("type", "test_type");
326 UploadArtifactInfo result = new UploadArtifactInfo();
327 Deencapsulation.invoke(testSubject, "fillArtifact", result, map1);
328 Assert.assertTrue(result != null);
329 Assert.assertTrue(result.getFile().equals("test_file"));
330 Assert.assertTrue(result.getType().equals("test_type"));