1 package org.openecomp.core.converter.impl;
3 import org.apache.commons.collections.CollectionUtils;
4 import org.junit.Assert;
6 import org.openecomp.core.converter.ToscaConverter;
7 import org.openecomp.core.impl.ToscaConverterImpl;
8 import org.openecomp.core.utilities.file.FileContentHandler;
9 import org.openecomp.core.utilities.file.FileUtils;
10 import org.openecomp.core.utilities.json.JsonUtil;
11 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
12 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
13 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
14 import org.openecomp.sdc.tosca.datatypes.model.RequirementAssignment;
15 import org.openecomp.sdc.tosca.datatypes.model.RequirementDefinition;
16 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
17 import org.openecomp.sdc.tosca.services.ToscaExtensionYamlUtil;
18 import org.openecomp.sdc.tosca.services.YamlUtil;
21 import java.io.FileInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
25 import java.nio.file.NotDirectoryException;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.ListIterator;
32 import java.util.Objects;
34 import static org.junit.Assert.assertEquals;
35 import static org.openecomp.core.converter.datatypes.Constants.globalStName;
36 import static org.openecomp.core.converter.datatypes.Constants.mainStName;
38 public class ToscaConverterImplTest {
40 private static final ToscaConverter toscaConverter = new ToscaConverterImpl();
41 private static final String VIRTUAL_LINK = "virtualLink";
42 private static final String UNBOUNDED = "UNBOUNDED";
46 public void testConvertMainSt() throws IOException {
47 String inputFilesPath = "/mock/toscaConverter/convertMainSt/in";
48 String outputFilesPath = "/mock/toscaConverter/convertMainSt/out";
50 FileContentHandler fileContentHandler =
51 createFileContentHandlerFromInput(inputFilesPath);
53 Map<String, ServiceTemplate> expectedOutserviceTemplates = new HashMap<>();
54 loadServiceTemplates(outputFilesPath, new ToscaExtensionYamlUtil(),
55 expectedOutserviceTemplates);
57 ToscaServiceModel toscaServiceModel = toscaConverter.convert(fileContentHandler);
58 ServiceTemplate mainSt = toscaServiceModel.getServiceTemplates().get(mainStName);
60 checkSTResults(expectedOutserviceTemplates, null, mainSt);
64 public void testOccurrencesUpperString() {
65 Object[] occurrences = buildOccurrences("0", UNBOUNDED);
66 Assert.assertEquals(occurrences[0], 0);
67 Assert.assertEquals(occurrences[1], UNBOUNDED);
71 public void testOccurrencesAsInts() {
72 Object[] occurrences = buildOccurrences("0", "1");
73 Assert.assertEquals(occurrences[0], 0);
74 Assert.assertEquals(occurrences[1], 1);
78 public void testOccurrencesAsStrings() {
79 String test = "TEST_A";
80 Object[] occurrences = buildOccurrences(UNBOUNDED, test);
81 Assert.assertEquals(occurrences[0], UNBOUNDED);
82 Assert.assertEquals(occurrences[1], test);
86 public void testOccurrencesLowerString() {
87 Object[] occurrences = buildOccurrences(UNBOUNDED, "100");
88 Assert.assertEquals(occurrences[0], UNBOUNDED);
89 Assert.assertEquals(occurrences[1], 100);
93 public void testOccurrencesEmpty() {
94 Object[] occurrences = buildOccurrences();
95 Assert.assertEquals(occurrences.length, 0);
99 public void testOccurrencesMany() {
100 String test = "TEST_B";
101 Object[] occurrences = buildOccurrences("1", "2", test);
102 Assert.assertEquals(occurrences[0], 1);
103 Assert.assertEquals(occurrences[1], 2);
104 Assert.assertEquals(occurrences[2], test);
108 public void testDefaultOccurrences() {
109 Object[] occurrences = buildOccurrences((List<String>) null);
110 Assert.assertEquals(1, occurrences[0]);
111 Assert.assertEquals(1, occurrences[1]);
114 private Object[] buildOccurrences(String... bounds) {
115 return buildOccurrences(Arrays.asList(bounds));
118 private Object[] buildOccurrences(List<String> bounds) {
119 NodeType nodeType = JsonUtil.json2Object("{derived_from=tosca.nodes.Root, description=MME_VFC, " +
120 "properties={vendor={type=string, default=ERICSSON}, " +
121 "csarVersion={type=string, default=v1.0}, csarProvider={type=string, default=ERICSSON}, " +
122 "id={type=string, default=vMME}, version={type=string, default=v1.0}, csarType={type=string, default=NFAR}}, " +
123 "requirements=[{virtualLink={" +
124 (bounds == null ? "" : "occurrences=[" + String.join(", ", bounds) + "], ") +
125 "capability=tosca.capabilities.network.Linkable}}]}", NodeType.class);
126 List<Map<String, RequirementDefinition>> requirements = nodeType.getRequirements();
127 return requirements.get(0).get(VIRTUAL_LINK).getOccurrences();
130 private FileContentHandler createFileContentHandlerFromInput(String inputFilesPath)
132 URL inputFilesUrl = this.getClass().getResource(inputFilesPath);
133 String path = inputFilesUrl.getPath();
134 File directory = new File(path);
135 File[] listFiles = directory.listFiles();
137 FileContentHandler fileContentHandler = new FileContentHandler();
138 insertFilesIntoFileContentHandler(listFiles, fileContentHandler);
139 return fileContentHandler;
142 private void insertFilesIntoFileContentHandler(File[] listFiles,
143 FileContentHandler fileContentHandler)
146 if(CollectionUtils.isEmpty(fileContentHandler.getFileList())) {
147 fileContentHandler.setFiles(new HashMap<>());
150 for (File file : listFiles) {
151 if(!file.isDirectory()) {
152 try (FileInputStream fis = new FileInputStream(file)) {
153 fileContent = FileUtils.toByteArray(fis);
154 fileContentHandler.addFile(file.getPath(), fileContent);
157 File[] currFileList = file.listFiles();
158 insertFilesIntoFileContentHandler(currFileList, fileContentHandler);
164 private void checkSTResults(
165 Map<String, ServiceTemplate> expectedOutserviceTemplates,
166 ServiceTemplate gloablSubstitutionServiceTemplate, ServiceTemplate mainServiceTemplate) {
167 YamlUtil yamlUtil = new YamlUtil();
168 if (Objects.nonNull(gloablSubstitutionServiceTemplate)) {
169 assertEquals("difference global substitution service template: ",
170 yamlUtil.objectToYaml(expectedOutserviceTemplates.get(globalStName)),
171 yamlUtil.objectToYaml(gloablSubstitutionServiceTemplate));
173 if (Objects.nonNull(mainServiceTemplate)) {
174 assertEquals("difference main service template: ",
175 yamlUtil.objectToYaml(expectedOutserviceTemplates.get(mainStName)),
176 yamlUtil.objectToYaml(mainServiceTemplate));
180 public static void loadServiceTemplates(String serviceTemplatesPath,
181 ToscaExtensionYamlUtil toscaExtensionYamlUtil,
182 Map<String, ServiceTemplate> serviceTemplates)
184 URL urlFile = ToscaConverterImplTest.class.getResource(serviceTemplatesPath);
185 if (urlFile != null) {
186 File pathFile = new File(urlFile.getFile());
187 File[] files = pathFile.listFiles();
189 addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
191 throw new NotDirectoryException(serviceTemplatesPath);
194 throw new NotDirectoryException(serviceTemplatesPath);
198 private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
200 ToscaExtensionYamlUtil toscaExtensionYamlUtil) throws IOException {
202 for (File file : files) {
204 try (InputStream yamlFile = new FileInputStream(file)) {
205 ServiceTemplate serviceTemplateFromYaml =
206 toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
207 createConcreteRequirementObjectsInServiceTemplate(serviceTemplateFromYaml, toscaExtensionYamlUtil);
208 serviceTemplates.put(file.getName(), serviceTemplateFromYaml);
213 private static void createConcreteRequirementObjectsInServiceTemplate(ServiceTemplate
214 serviceTemplateFromYaml,
215 ToscaExtensionYamlUtil
216 toscaExtensionYamlUtil) {
218 if (serviceTemplateFromYaml == null
219 || serviceTemplateFromYaml.getTopology_template() == null
220 || serviceTemplateFromYaml.getTopology_template().getNode_templates() == null) {
224 //Creating concrete objects
225 Map<String, NodeTemplate> nodeTemplates =
226 serviceTemplateFromYaml.getTopology_template().getNode_templates();
227 for (Map.Entry<String, NodeTemplate> entry : nodeTemplates.entrySet()) {
228 NodeTemplate nodeTemplate = entry.getValue();
229 List<Map<String, RequirementAssignment>> requirements = nodeTemplate.getRequirements();
230 List<Map<String, RequirementAssignment>> concreteRequirementList = new ArrayList<>();
231 if (requirements != null) {
232 ListIterator<Map<String, RequirementAssignment>> reqListIterator = requirements
234 while (reqListIterator.hasNext()){
235 Map<String, RequirementAssignment> requirement = reqListIterator.next();
236 Map<String, RequirementAssignment> concreteRequirement = new HashMap<>();
237 for (Map.Entry<String, RequirementAssignment> reqEntry : requirement.entrySet()) {
238 RequirementAssignment requirementAssignment = (toscaExtensionYamlUtil
239 .yamlToObject(toscaExtensionYamlUtil.objectToYaml(reqEntry.getValue()),
240 RequirementAssignment.class));
241 concreteRequirement.put(reqEntry.getKey(), requirementAssignment);
242 concreteRequirementList.add(concreteRequirement);
243 reqListIterator.remove();
246 requirements.clear();
247 requirements.addAll(concreteRequirementList);
248 nodeTemplate.setRequirements(requirements);
250 System.out.println();
251 //toscaExtensionYamlUtil.yamlToObject(nodeTemplate, NodeTemplate.class);