add artifacts support in TOSCA exported yml file
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / utils / YamlTemplateParsingHandlerTest.java
1 /*-
2  * ============LICENSE_START===============================================
3  * ONAP SDC
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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=================================================
19  */
20
21 package org.openecomp.sdc.be.components.impl.utils;
22
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;
29
30 import java.io.IOException;
31 import java.net.URISyntaxException;
32 import java.util.*;
33 import java.util.stream.Collectors;
34
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;
44
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;
55
56 import java.io.File;
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;
62
63 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.*;
64 @RunWith(MockitoJUnitRunner.class)
65 public class YamlTemplateParsingHandlerTest {
66
67     @Mock
68     private GroupTypeBusinessLogic groupTypeBusinessLogic;
69     @Mock
70     private AnnotationTypeOperations annotationTypeOperations;
71     @Mock
72     private AnnotationValidator annotationValidator;
73     @Mock
74     private JanusGraphDao janusGraphDao;
75     @Mock
76     private User user;
77
78     private YamlTemplateParsingHandler handler;
79
80     private static Map<String, byte[]> csar;
81     private static String resourceYml;
82
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";
92
93     private static final String FILE_NAME = "MainServiceTemplate.yaml";
94
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";
101
102     @InjectMocks
103     YamlTemplateParsingHandler testSubject;
104
105     @BeforeClass()
106     public static void prepareData() throws IOException, URISyntaxException {
107         csar = ZipUtil.readData(CSAR_FILE_PATH);
108
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);
112
113         resourceYml = new String(mainTemplateService);
114     }
115
116     @Before
117     public void setup() {
118
119         AnnotationBusinessLogic annotationBusinessLogic = new AnnotationBusinessLogic(annotationTypeOperations,
120                 annotationValidator);
121         handler = new YamlTemplateParsingHandler(janusGraphDao, groupTypeBusinessLogic, annotationBusinessLogic);
122         stubGetGroupType();
123     }
124
125     @Test
126     public void parseResourceInfoFromOneNodeTest() {
127
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);
131
132         ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
133                 csarInfo.extractNodeTypesInfo(), NODE_NAME);
134
135         validateParsedYaml(parsedYaml, NESTED_GROUP_NAME,
136                 Lists.newArrayList("heat_file", "description"));
137     }
138
139     @Test
140     public void parseResourceInfoFromYAMLTest() {
141
142         ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
143                 new HashMap<>(), "");
144         validateParsedYamlWithCapability(parsedYaml);
145     }
146
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();
151
152         assertThat(parsedYaml.getGroups().get(group).getProperties()).isNotNull();
153         assertThat(parsedYaml.getGroups().get(group).getProperties()
154                 .stream()
155                 .map(PropertyDataDefinition::getName)
156                 .collect(Collectors.toList()))
157                 .containsAll(expectedProp);
158
159         assertThat(parsedYaml.getGroups().get(group).getMembers()).isNotNull();
160     }
161
162         private void validateParsedYamlWithCapability(ParsedToscaYamlInfo parsedYaml) {
163
164         final List<String> expectedProp = Lists.newArrayList("vfc_parent_port_role",
165                 "network_collection_function", "vfc_instance_group_function", "subinterface_role");
166
167         validateParsedYaml(parsedYaml, MAIN_GROUP_NAME, expectedProp);
168
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();
173         }
174
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);
179     }
180
181     private static GroupTypeDefinition buildRootGroupType() {
182         return createGroupTypeDefinition(ROOT_GROUP_TYPE, null,
183                 "The TOSCA Group Type all other TOSCA Group Types derive from");
184         }
185
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");
189
190         GroupProperty property1 = createGroupProperty("heat_file",
191                 "Heat file which associate to this group/heat stack", "SUPPORTED");
192
193         GroupProperty property2 = createGroupProperty("description",
194                 "Group description", "SUPPORTED");
195
196         groupType.setProperties(Lists.newArrayList(property1, property2));
197         return groupType;
198         }
199
200         private static GroupTypeDefinition buildVfcInstanceGroupType() {
201         GroupTypeDefinition groupType = createGroupTypeDefinition(VFC_GROUP_TYPE, "tosca.groups.Root",
202                 "Groups of VFCs with same parent port role");
203
204         GroupProperty property1 = createGroupProperty("vfc_instance_group_function",
205                 "Function of this VFC group", null);
206
207         GroupProperty property2 = createGroupProperty("vfc_parent_port_role",
208                 "Common role of parent ports of VFCs in this group", null);
209
210         GroupProperty property3 = createGroupProperty("network_collection_function",
211                 "Network collection function assigned to this group", null);
212
213         GroupProperty property4 = createGroupProperty("subinterface_role",
214                 "Common role of subinterfaces of VFCs in this group, criteria the group is created", null);
215
216         groupType.setProperties(Lists.newArrayList(property1, property2, property3, property4));
217
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));
225
226         Map<String, CapabilityDefinition> capabilityMap = new HashMap<>();
227         capabilityMap.put(CAPABILITY_NAME, capability);
228         groupType.setCapabilities(capabilityMap);
229         return groupType;
230     }
231
232     private static GroupTypeDefinition createGroupTypeDefinition(String type, String derivedFrom, String description){
233         GroupTypeDefinition property = new GroupTypeDefinition();
234
235         if (type != null)
236             property.setType(type);
237
238         if (derivedFrom != null) {
239             property.setDerivedFrom(derivedFrom);
240         }
241
242         if (description != null) {
243             property.setDescription(description);
244         }
245
246         return property;
247     }
248     private static GroupProperty createGroupProperty(String name, String description,
249                                                      String status){
250         GroupProperty property = new GroupProperty();
251         if (name != null)
252             property.setName(name);
253
254         if (description != null) {
255             property.setDescription(description);
256         }
257
258         if (status != null) {
259             property.setStatus(status);
260         }
261
262         property.setType("string");
263         property.setRequired(true);
264
265         return property;
266     }
267     @Test
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);
277     }
278
279     @Test
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"));
294     }
295
296     @Test
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"));
307     }
308
309     @Test
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"));
319     }
320
321     @Test
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"));
331     }
332 }