1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
/*-
* ============LICENSE_START===============================================
* ONAP SDC
* ========================================================================
* Modifications Copyright (c) 2019 Samsung
* ========================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=================================================
*/
package org.openecomp.sdc.be.components.impl.utils;
import mockit.Deencapsulation;
import org.apache.commons.collections.MapUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
import java.io.File;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import org.assertj.core.util.Lists;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openecomp.sdc.be.components.csar.CsarInfo;
import org.openecomp.sdc.be.components.csar.YamlTemplateParsingHandler;
import org.openecomp.sdc.be.components.impl.AnnotationBusinessLogic;
import org.openecomp.sdc.be.components.impl.GroupTypeBusinessLogic;
import org.openecomp.sdc.be.components.validation.AnnotationValidator;
import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
import org.openecomp.sdc.be.model.CapabilityDefinition;
import org.openecomp.sdc.be.model.ComponentInstanceProperty;
import org.openecomp.sdc.be.model.GroupProperty;
import org.openecomp.sdc.be.model.GroupTypeDefinition;
import org.openecomp.sdc.be.model.ParsedToscaYamlInfo;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations;
import org.openecomp.sdc.common.zip.ZipUtils;
import org.openecomp.sdc.common.zip.exception.ZipException;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.*;
@RunWith(MockitoJUnitRunner.class)
public class YamlTemplateParsingHandlerTest {
@Mock
private GroupTypeBusinessLogic groupTypeBusinessLogic;
@Mock
private AnnotationTypeOperations annotationTypeOperations;
@Mock
private AnnotationValidator annotationValidator;
@Mock
private JanusGraphDao janusGraphDao;
@Mock
private User user;
private YamlTemplateParsingHandler handler;
private static Map<String, byte[]> csar;
private static String resourceYml;
private final static String VFC_GROUP_TYPE = "org.openecomp.groups.VfcInstanceGroup";
private final static String HEAT_GROUP_TYPE = "org.openecomp.groups.heat.HeatStack";
private final static String ROOT_GROUP_TYPE = "tosca.groups.Root";
private final static GroupTypeDefinition VfcInstanceGroupType = buildVfcInstanceGroupType();
private final static GroupTypeDefinition heatGroupType = buildHeatStackGroupType();
private final static GroupTypeDefinition rootGroupType = buildRootGroupType();
private final static String CAPABILITY_TYPE = "org.openecomp.capabilities.VLANAssignment";
private final static String CAPABILITY_NAME = "vlan_assignment";
private static final String CSAR_FILE_PATH = "csars/with_groups.csar";
private static final String FILE_NAME = "MainServiceTemplate.yaml";
private static final String CSAR_UUID = "csarUUID";
private static final String RESOURCE_NAME = "resourceName";
private static final String MAIN_TEMPLATE_NAME = "Definitions/MainServiceTemplate.yaml";
private static final String NODE_NAME = "org.openecomp.resource.abstract.nodes.heat.mg";
private static final String MAIN_GROUP_NAME = "x_group";
private static final String NESTED_GROUP_NAME = "nested_mg_vepdg_group";
@InjectMocks
YamlTemplateParsingHandler testSubject;
@BeforeClass()
public static void prepareData() throws URISyntaxException, ZipException {
final File csarFile = new File(
YamlTemplateParsingHandlerTest.class.getClassLoader().getResource(CSAR_FILE_PATH).toURI());
csar = ZipUtils.readZip(csarFile, false);
Optional<String> keyOp = csar.keySet().stream().filter(k -> k.endsWith(FILE_NAME)).findAny();
byte[] mainTemplateService = keyOp.map(csar::get).orElse(null);
assertNotNull(mainTemplateService);
resourceYml = new String(mainTemplateService);
}
@Before
public void setup() {
AnnotationBusinessLogic annotationBusinessLogic = new AnnotationBusinessLogic(annotationTypeOperations,
annotationValidator);
handler = new YamlTemplateParsingHandler(janusGraphDao, groupTypeBusinessLogic, annotationBusinessLogic);
stubGetGroupType();
}
@Test
public void parseResourceInfoFromOneNodeTest() {
String main_template_content = new String(csar.get(MAIN_TEMPLATE_NAME));
CsarInfo csarInfo = new CsarInfo(user, CSAR_UUID, csar, RESOURCE_NAME,
MAIN_TEMPLATE_NAME, main_template_content, true);
ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
csarInfo.extractNodeTypesInfo(), NODE_NAME);
validateParsedYaml(parsedYaml, NESTED_GROUP_NAME,
Lists.newArrayList("heat_file", "description"));
}
@Test
public void parseResourceInfoFromYAMLTest() {
ParsedToscaYamlInfo parsedYaml = handler.parseResourceInfoFromYAML(FILE_NAME, resourceYml, new HashMap<>(),
new HashMap<>(), "");
validateParsedYamlWithCapability(parsedYaml);
}
private void validateParsedYaml(ParsedToscaYamlInfo parsedYaml, String group, List<String> expectedProp) {
assertThat(parsedYaml).isNotNull();
assertThat(parsedYaml.getGroups()).isNotNull().containsKey(group);
assertThat(parsedYaml.getGroups().get(group)).isNotNull();
assertThat(parsedYaml.getGroups().get(group).getProperties()).isNotNull();
assertThat(parsedYaml.getGroups().get(group).getProperties()
.stream()
.map(PropertyDataDefinition::getName)
.collect(Collectors.toList()))
.containsAll(expectedProp);
assertThat(parsedYaml.getGroups().get(group).getMembers()).isNotNull();
}
private void validateParsedYamlWithCapability(ParsedToscaYamlInfo parsedYaml) {
final List<String> expectedProp = Lists.newArrayList("vfc_parent_port_role",
"network_collection_function", "vfc_instance_group_function", "subinterface_role");
validateParsedYaml(parsedYaml, MAIN_GROUP_NAME, expectedProp);
assertThat(parsedYaml.getGroups().get(MAIN_GROUP_NAME).getCapabilities()
.get(CAPABILITY_TYPE)
.get(0).getProperties().get(0).getValue()).isEqualTo("success");
assertThat(parsedYaml.getGroups().get(MAIN_GROUP_NAME).getCapabilities()).isNotNull();
}
private void stubGetGroupType() {
when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(VFC_GROUP_TYPE))).thenReturn(VfcInstanceGroupType);
when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(HEAT_GROUP_TYPE))).thenReturn(heatGroupType);
when(groupTypeBusinessLogic.getLatestGroupTypeByType(eq(ROOT_GROUP_TYPE))).thenReturn(rootGroupType);
}
private static GroupTypeDefinition buildRootGroupType() {
return createGroupTypeDefinition(ROOT_GROUP_TYPE, null,
"The TOSCA Group Type all other TOSCA Group Types derive from");
}
private static GroupTypeDefinition buildHeatStackGroupType() {
GroupTypeDefinition groupType = createGroupTypeDefinition(HEAT_GROUP_TYPE, "tosca.groups.Root",
"Grouped all heat resources which are in the same heat stack");
GroupProperty property1 = createGroupProperty("heat_file",
"Heat file which associate to this group/heat stack", "SUPPORTED");
GroupProperty property2 = createGroupProperty("description",
"Group description", "SUPPORTED");
groupType.setProperties(Lists.newArrayList(property1, property2));
return groupType;
}
private static GroupTypeDefinition buildVfcInstanceGroupType() {
GroupTypeDefinition groupType = createGroupTypeDefinition(VFC_GROUP_TYPE, "tosca.groups.Root",
"Groups of VFCs with same parent port role");
GroupProperty property1 = createGroupProperty("vfc_instance_group_function",
"Function of this VFC group", null);
GroupProperty property2 = createGroupProperty("vfc_parent_port_role",
"Common role of parent ports of VFCs in this group", null);
GroupProperty property3 = createGroupProperty("network_collection_function",
"Network collection function assigned to this group", null);
GroupProperty property4 = createGroupProperty("subinterface_role",
"Common role of subinterfaces of VFCs in this group, criteria the group is created", null);
groupType.setProperties(Lists.newArrayList(property1, property2, property3, property4));
CapabilityDefinition capability = new CapabilityDefinition();
capability.setType(CAPABILITY_TYPE);
capability.setName(CAPABILITY_NAME);
ComponentInstanceProperty capabilityProperty = new ComponentInstanceProperty();
capabilityProperty.setName("vfc_instance_group_reference");
capabilityProperty.setType("string");
capability.setProperties(Collections.singletonList(capabilityProperty));
Map<String, CapabilityDefinition> capabilityMap = new HashMap<>();
capabilityMap.put(CAPABILITY_NAME, capability);
groupType.setCapabilities(capabilityMap);
return groupType;
}
private static GroupTypeDefinition createGroupTypeDefinition(String type, String derivedFrom, String description){
GroupTypeDefinition property = new GroupTypeDefinition();
if (type != null)
property.setType(type);
if (derivedFrom != null) {
property.setDerivedFrom(derivedFrom);
}
if (description != null) {
property.setDescription(description);
}
return property;
}
private static GroupProperty createGroupProperty(String name, String description,
String status){
GroupProperty property = new GroupProperty();
if (name != null)
property.setName(name);
if (description != null) {
property.setDescription(description);
}
if (status != null) {
property.setStatus(status);
}
property.setType("string");
property.setRequired(true);
return property;
}
@Test
public void testSetArtifacts() throws Exception {
UploadComponentInstanceInfo nodeTemplateInfo = new UploadComponentInstanceInfo();
Map<String, Object> nodeTemplateJsonMap = new HashMap<>();
Map<String, String> nodeMap = new HashMap<>();
nodeMap.put("name","test_name");
nodeMap.put("type","test_type");
nodeTemplateJsonMap.put(ARTIFACTS.getElementName(), nodeMap);
Deencapsulation.invoke(testSubject, "setArtifacts", nodeTemplateInfo, nodeTemplateJsonMap);
Assert.assertTrue(nodeTemplateInfo.getArtifacts() != null);
}
@Test
public void testCreateArtifactsModuleFromYaml() throws Exception {
Map<String, Map<String, Map<String, String>>> nodeTemplateJsonMap = new HashMap<>();
Map<String, Map<String,String>> map0 = new HashMap<>();
Map<String, String> map1 = new HashMap<>();
map1.put("file", "test_file");
map1.put("type", "test_type");
map0.put("test_art", map1);
nodeTemplateJsonMap.put(ARTIFACTS.getElementName(), map0);
Map<String, Map<String, UploadArtifactInfo>> result;
result = Deencapsulation.invoke(testSubject, "createArtifactsModuleFromYaml", nodeTemplateJsonMap);
Assert.assertTrue(MapUtils.isNotEmpty(result));
Assert.assertTrue(MapUtils.isNotEmpty(result.get(ARTIFACTS.getElementName())));
Assert.assertTrue(result.get(ARTIFACTS.getElementName()).get("test_art").getFile().equals("test_file"));
Assert.assertTrue(result.get(ARTIFACTS.getElementName()).get("test_art").getType().equals("test_type"));
}
@Test
public void testAddModuleNodeTemplateArtifacts() throws Exception {
Map<String, Map<String, UploadArtifactInfo>> result = new HashMap<>();
Map<String, String> map1 = new HashMap<>();
map1.put("file", "test_file");
map1.put("type", "test_type");
Deencapsulation.invoke(testSubject, "addModuleNodeTemplateArtifacts", result, map1, "test_art");
Assert.assertTrue(MapUtils.isNotEmpty(result));
Assert.assertTrue(MapUtils.isNotEmpty(result.get(ARTIFACTS.getElementName())));
Assert.assertTrue(result.get(ARTIFACTS.getElementName()).get("test_art").getFile().equals("test_file"));
Assert.assertTrue(result.get(ARTIFACTS.getElementName()).get("test_art").getType().equals("test_type"));
}
@Test
public void testBuildModuleNodeTemplateArtifact() throws Exception {
Map<String, String> map1 = new HashMap<>();
map1.put("file", "test_file");
map1.put("type", "test_type");
UploadArtifactInfo result;
result = Deencapsulation.invoke(testSubject, "buildModuleNodeTemplateArtifact", map1);
Assert.assertTrue(result != null);
Assert.assertTrue(result.getFile().equals("test_file"));
Assert.assertTrue(result.getType().equals("test_type"));
}
@Test
public void testFillArtifact() throws Exception {
Map<String, String> map1 = new HashMap<>();
map1.put("file", "test_file");
map1.put("type", "test_type");
UploadArtifactInfo result = new UploadArtifactInfo();
Deencapsulation.invoke(testSubject, "fillArtifact", result, map1);
Assert.assertTrue(result != null);
Assert.assertTrue(result.getFile().equals("test_file"));
Assert.assertTrue(result.getType().equals("test_type"));
}
}
|