471e3891c790223db0318129595b91c2020744e6
[sdc.git] /
1 package org.openecomp.core.converter.impl;
2
3 import org.apache.commons.collections.CollectionUtils;
4 import org.junit.Assert;
5 import org.junit.Test;
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;
19
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.net.URL;
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;
31 import java.util.Map;
32 import java.util.Objects;
33
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;
37
38 public class ToscaConverterImplTest {
39
40   private static final ToscaConverter toscaConverter = new ToscaConverterImpl();
41   private static final String VIRTUAL_LINK = "virtualLink";
42   private static final String UNBOUNDED = "UNBOUNDED";
43
44
45   @Test
46   public void testConvertMainSt() throws IOException {
47     String inputFilesPath = "/mock/toscaConverter/convertMainSt/in";
48     String outputFilesPath = "/mock/toscaConverter/convertMainSt/out";
49
50     FileContentHandler fileContentHandler =
51         createFileContentHandlerFromInput(inputFilesPath);
52
53     Map<String, ServiceTemplate> expectedOutserviceTemplates = new HashMap<>();
54     loadServiceTemplates(outputFilesPath, new ToscaExtensionYamlUtil(),
55             expectedOutserviceTemplates);
56
57     ToscaServiceModel toscaServiceModel = toscaConverter.convert(fileContentHandler);
58     ServiceTemplate mainSt = toscaServiceModel.getServiceTemplates().get(mainStName);
59
60     checkSTResults(expectedOutserviceTemplates, null, mainSt);
61   }
62
63   @Test
64   public void testOccurrencesUpperString() {
65     Object[] occurrences = buildOccurrences("0", UNBOUNDED);
66     Assert.assertEquals(occurrences[0], 0);
67     Assert.assertEquals(occurrences[1], UNBOUNDED);
68   }
69
70   @Test
71   public void testOccurrencesAsInts() {
72     Object[] occurrences = buildOccurrences("0", "1");
73     Assert.assertEquals(occurrences[0], 0);
74     Assert.assertEquals(occurrences[1], 1);
75   }
76
77   @Test
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);
83   }
84
85   @Test
86   public void testOccurrencesLowerString() {
87     Object[] occurrences = buildOccurrences(UNBOUNDED, "100");
88     Assert.assertEquals(occurrences[0], UNBOUNDED);
89     Assert.assertEquals(occurrences[1], 100);
90   }
91
92   @Test
93   public void testOccurrencesEmpty() {
94     Object[] occurrences = buildOccurrences();
95     Assert.assertEquals(occurrences.length, 0);
96   }
97
98   @Test
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);
105   }
106
107   @Test
108   public void testDefaultOccurrences() {
109     Object[] occurrences = buildOccurrences((List<String>) null);
110     Assert.assertEquals(1, occurrences[0]);
111     Assert.assertEquals(1, occurrences[1]);
112   }
113
114   private Object[] buildOccurrences(String... bounds) {
115     return buildOccurrences(Arrays.asList(bounds));
116   }
117
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();
128   }
129
130   private FileContentHandler createFileContentHandlerFromInput(String inputFilesPath)
131       throws IOException {
132     URL inputFilesUrl = this.getClass().getResource(inputFilesPath);
133     String path = inputFilesUrl.getPath();
134     File directory = new File(path);
135     File[] listFiles = directory.listFiles();
136
137     FileContentHandler fileContentHandler = new FileContentHandler();
138     insertFilesIntoFileContentHandler(listFiles, fileContentHandler);
139     return fileContentHandler;
140   }
141
142   private void insertFilesIntoFileContentHandler(File[] listFiles,
143                                                  FileContentHandler fileContentHandler)
144       throws IOException {
145     byte[] fileContent;
146     if(CollectionUtils.isEmpty(fileContentHandler.getFileList())) {
147       fileContentHandler.setFiles(new HashMap<>());
148     }
149
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);
155         }
156       }else{
157         File[] currFileList = file.listFiles();
158         insertFilesIntoFileContentHandler(currFileList, fileContentHandler);
159       }
160
161     }
162   }
163
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));
172     }
173     if (Objects.nonNull(mainServiceTemplate)) {
174       assertEquals("difference main service template: ",
175           yamlUtil.objectToYaml(expectedOutserviceTemplates.get(mainStName)),
176           yamlUtil.objectToYaml(mainServiceTemplate));
177     }
178   }
179
180   public static void loadServiceTemplates(String serviceTemplatesPath,
181                                           ToscaExtensionYamlUtil toscaExtensionYamlUtil,
182                                           Map<String, ServiceTemplate> serviceTemplates)
183       throws IOException {
184     URL urlFile = ToscaConverterImplTest.class.getResource(serviceTemplatesPath);
185     if (urlFile != null) {
186       File pathFile = new File(urlFile.getFile());
187       File[] files = pathFile.listFiles();
188       if (files != null) {
189         addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
190       } else {
191         throw new NotDirectoryException(serviceTemplatesPath);
192       }
193     } else {
194       throw new NotDirectoryException(serviceTemplatesPath);
195     }
196   }
197
198   private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
199                                               File[] files,
200                                               ToscaExtensionYamlUtil toscaExtensionYamlUtil) throws IOException {
201
202     for (File file : files) {
203
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);
209       }
210     }
211   }
212
213   private static void createConcreteRequirementObjectsInServiceTemplate(ServiceTemplate
214                                                                             serviceTemplateFromYaml,
215                                                                         ToscaExtensionYamlUtil
216                                                                             toscaExtensionYamlUtil) {
217
218     if (serviceTemplateFromYaml == null
219         || serviceTemplateFromYaml.getTopology_template() == null
220         || serviceTemplateFromYaml.getTopology_template().getNode_templates() == null) {
221       return;
222     }
223
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
233             .listIterator();
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();
244           }
245         }
246         requirements.clear();
247         requirements.addAll(concreteRequirementList);
248         nodeTemplate.setRequirements(requirements);
249       }
250       System.out.println();
251       //toscaExtensionYamlUtil.yamlToObject(nodeTemplate, NodeTemplate.class);
252     }
253   }
254 }