[SDC-29] rebase continue work to align source
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / externalApis / DeploymentValiditaion.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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.externalApis;
22
23 import static org.testng.AssertJUnit.assertTrue;
24
25 import java.io.File;
26 import java.io.FileReader;
27 import java.io.IOException;
28 import java.nio.charset.StandardCharsets;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Collection;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Set;
39 import java.util.stream.Collectors;
40
41 import org.apache.commons.io.FileUtils;
42 import org.junit.Rule;
43 import org.junit.rules.TestName;
44 import org.openecomp.sdc.be.datatypes.enums.AssetTypeEnum;
45 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
46 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
47 import org.openecomp.sdc.be.model.Component;
48 import org.openecomp.sdc.be.model.Resource;
49 import org.openecomp.sdc.be.model.User;
50 import org.openecomp.sdc.be.model.category.CategoryDefinition;
51 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
52 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
53 import org.openecomp.sdc.ci.tests.datatypes.PropertyReqDetails;
54 import org.openecomp.sdc.ci.tests.datatypes.enums.ResourceCategoryEnum;
55 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
56 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
57 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
58 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
59 import org.openecomp.sdc.ci.tests.utils.rest.AssetRestUtils;
60 import org.openecomp.sdc.ci.tests.utils.rest.CatalogRestUtils;
61 import org.openecomp.sdc.ci.tests.utils.rest.CategoryRestUtils;
62 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
63 import org.openecomp.sdc.ci.tests.utils.validation.AuditValidationUtils;
64 import org.openecomp.sdc.common.util.ZipUtil;
65 import org.testng.annotations.BeforeTest;
66 import org.testng.annotations.Test;
67 import org.yaml.snakeyaml.Yaml;
68
69 import com.aventstack.extentreports.Status;
70
71
72
73
74
75
76
77 public class DeploymentValiditaion extends ComponentBaseTest{
78
79
80         
81         
82         @Rule
83         public static TestName name = new TestName();
84
85         public DeploymentValiditaion() {
86                 super(name, DeploymentValiditaion.class.getName());
87         }
88         
89         protected final static String categoryFilterKey = "category";
90         protected final static String subCategoryFilterKey = "subCategory";
91         protected  String attVersionStr ;
92         
93         
94          public static List<File> listf(String directoryName) {
95                 File directory = new File(directoryName);
96
97                 List<File> resultList = new ArrayList<File>();
98
99                 // get all the files from a directory
100                 File[] fList = directory.listFiles();
101                 resultList.addAll(Arrays.asList(fList));
102                 for (File file : fList) {
103                     if (file.isFile()) {
104                         System.out.println(file.getAbsolutePath());
105                     } else if (file.isDirectory()) {
106                         resultList.addAll(listf(file.getAbsolutePath()));
107                     }
108                 }
109                 //System.out.println(fList);
110                 return resultList;
111             } 
112         @BeforeTest
113         public void beforeTest() throws Exception{
114                 RestResponse attVersion = CatalogRestUtils.getOsVersion();
115                 attVersionStr = ResponseParser.getVersionFromResponse(attVersion);
116         }
117         
118         
119         @Test
120         public void pasrseNormativies() throws Exception{
121                 
122                 
123                 String path = "//apps//asdc_kits//catalog-be-" + attVersionStr + "//import//tosca//normative-types";
124                 String path2 = "//apps//asdc_kits//catalog-be-" + attVersionStr + "//import//tosca//heat-types";
125                                 
126 //              String path = "C:\\Git_work\\Git_UGN\\d2-sdnc\\catalog-be\\src\\main\\resources\\import\\tosca\\normative-types";
127 //              String path2 = "C:\\Git_work\\Git_UGN\\d2-sdnc\\catalog-be\\src\\main\\resources\\import\\tosca\\heat-types";
128
129                 
130                 List<File> yamlList1 = getYamlFilesList(path);          
131                 List<String> nodeNamesFromYamlList1 = getNodeNamesFromYamlList(yamlList1);
132                 List<File> yamlList2 = getYamlFilesList(path2);         
133                 List<String> nodeNamesFromYamlList2 = getNodeNamesFromYamlList(yamlList2);
134                 
135                 
136                 List<String> expectedList = new ArrayList<String>();
137                 expectedList.addAll(nodeNamesFromYamlList1);
138                 expectedList.addAll(nodeNamesFromYamlList2);
139                 System.out.println("list of normatives from files:::::::::::");
140                 expectedList.forEach(System.out::println);
141                 getExtendTest().log(Status.INFO, "list of normatives from files:");
142                 getExtendTest().log(Status.INFO,expectedList.toString());
143                 
144                 
145                 String[] filter = { categoryFilterKey + "=" + ResourceCategoryEnum.GENERIC_ABSTRACT.getCategory(), subCategoryFilterKey + "=" + ResourceCategoryEnum.GENERIC_ABSTRACT.getSubCategory() };
146                 RestResponse assetResponse = AssetRestUtils.getComponentListByAssetType(true, AssetTypeEnum.RESOURCES);
147                 Map<String, String> resourceAssetList = AssetRestUtils.getResourceAssetMap(assetResponse);
148                 Map<String, String> resourceListFiltteredByWholeVersion = AssetRestUtils.getResourceListFiltteredByWholeVersion(resourceAssetList);
149                 List<String> resourceToscaNamesList = AssetRestUtils.getResourceObjectByNameAndVersionToscaNamesList(resourceListFiltteredByWholeVersion);
150                 System.out.println("list of normatives from APIs:::::::::::");
151                 resourceToscaNamesList.forEach(System.out::println);
152                 getExtendTest().log(Status.INFO, "list of normatives from APIs:");
153                 getExtendTest().log(Status.INFO, resourceToscaNamesList.toString());
154                 
155                 boolean good = true;
156                 List<String> missingNormatives =  new ArrayList<>();
157
158                 for (int i = 0; i < expectedList.size(); i ++) {
159                     if (!resourceToscaNamesList.contains(expectedList.get(i))) {
160                         good = false;
161                         missingNormatives.add(expectedList.get(i));
162                         
163                         
164                     }
165                 }
166                 
167                 System.out.println("<<<<<<<<<MISSING NORMATIVES>>>>>>");
168                 missingNormatives.forEach(System.out::println);
169                 getExtendTest().log(Status.INFO, "MISSING NORMATIVES:");
170                 getExtendTest().log(Status.INFO, missingNormatives.toString());
171                 
172                 assertTrue("missing normatives ",  good);
173                 
174         }
175
176
177         public List<String> getNodeNamesFromYamlList(List<File> yamlList) throws IOException {
178                 List<String> nodeNameList = new ArrayList<String>();
179
180                 for (File file : yamlList) {
181                     String content = new String(Files.readAllBytes(Paths.get(file.getPath())), StandardCharsets.UTF_8);
182                         Yaml yaml = new Yaml();
183                         Map<String, Object> load = (Map<String, Object>) yaml.load(content);
184                         Map<String, Object> topology_template = (Map<String, Object>) load.get("node_types");
185 //                      String string = topology_template.keySet().toString().replaceAll("tosca.nodes.", "");
186                         String string = topology_template.keySet().iterator().next().toString();
187                         System.out.println(string +" -----> "  +file.getPath());
188                         nodeNameList.add(string);
189                 }
190                 return nodeNameList;
191         }
192
193
194         public List<File> getYamlFilesList(String path) throws IOException {
195                 List<File> yamlList = new ArrayList<File>();
196                 File dir = new File(path);
197                 String[] extensions = new String[] { "yml" };
198                 System.out.println("Getting all .yml files in " + dir.getCanonicalPath()
199                                 + " including those in subdirectories");
200                 List<File> files = (List<File>) FileUtils.listFiles(dir, extensions, true);
201                 for (File file : files) {
202                         System.out.println("file: " + file.getCanonicalPath());
203                         yamlList.add(file);
204                 }
205                 return yamlList;
206         }
207         
208
209         
210         
211
212         
213         @Test (enabled=false)
214         public void testYaml() throws IOException{
215                 
216                 System.out.println("");
217                 
218                 File file = new File("\\\\Comp-1\\FileIO\\Stop.txt");
219                 
220                 
221                 //read file
222
223
224                 Map<String, byte[]> readZip = null;
225                 Path path = Paths.get("C:\\Users\\ys9693\\Documents\\csar\\attributesWithProporties\\attributesWithProporties.csar");
226                 byte[] data = Files.readAllBytes(path);
227                 if (data != null && data.length > 0) {
228                         readZip = ZipUtil.readZip(data);
229
230                 }
231
232                 byte[] artifactsBs = readZip.get("Definitions/VF_RI2_G6.yaml");
233                 String str = new String(artifactsBs, StandardCharsets.UTF_8);
234                 
235                                 
236
237                 
238                 Yaml yaml = new Yaml();
239                 Map<String, Object> load = (Map<String, Object>) yaml.load(str);
240                 Map<String, Object> topology_template = (Map<String, Object>) load.get("topology_template");
241                 Map<String, Object> node_templates = (Map<String, Object>) topology_template.get("node_templates");
242                 
243                 Set<String> keySet = node_templates.keySet();
244         }
245         
246         
247         
248         @Test
249         public void pasrseDataTypes() throws Exception{
250                 
251 //              String path = "C:\\Git_work\\Git_UGN\\d2-sdnc\\catalog-be\\src\\main\\resources\\import\\tosca\\data-types\\dataTypes.yml";
252                 String path = "//apps//asdc_kits//catalog-be-" + attVersionStr + "//import//tosca//data-types//dataTypes.yml";
253                 
254             String content = new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8);
255                 
256                                 
257                 Yaml yaml = new Yaml();
258                 Map<String, Object> load = (Map<String, Object>) yaml.load(content);
259                 List<String> listOfDataTypes = new ArrayList<String>();
260                 listOfDataTypes.addAll(load.keySet());
261                 System.out.println("<<<<<<<< List of Data Types >>>>>>>>>");
262                 listOfDataTypes.forEach(System.out::println);
263                 getExtendTest().log(Status.INFO, "List of Data Types:");
264                 getExtendTest().log(Status.INFO, listOfDataTypes.toString());
265                                 
266                 Resource resource = AtomicOperationUtils.createResourceByType(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, true).left().value();
267                 PropertyReqDetails defaultProperty = ElementFactory.getDefaultListProperty();
268                 
269 //              SchemaDefinition schemaDefinition = new SchemaDefinition();
270                 
271                 
272                 
273         
274                 defaultProperty.setPropertyDefaultValue(null);
275 //              defaultProperty.setSchema(schemaDefinition);
276                 
277                 
278 //              listOfDataTypes.remove("tosca.datatypes.Root");
279                 for (String dataType : listOfDataTypes) {
280 //                      if (dataType.equals("map") ||  dataType.equals("list")){ 
281                         defaultProperty.setPropertyType(dataType);
282                         defaultProperty.setName(dataType);
283                         System.out.println("Adding proporty with data type: ----> " + dataType);
284                         getExtendTest().log(Status.INFO, "Adding proporty with data type: ----> " + dataType);
285                         AtomicOperationUtils.addCustomPropertyToResource(defaultProperty, resource, UserRoleEnum.DESIGNER, true);
286 //                      }
287                 }
288
289                 
290                 listOfDataTypes.forEach(System.out::println);
291         
292         }
293                 
294
295
296
297
298
299         @Test
300         public void pasrseCategories() throws Exception{
301                 
302 //              String path = "C:\\Git_work\\Git_UGN\\d2-sdnc\\catalog-be\\src\\main\\resources\\import\\tosca\\categories\\categoryTypes.yml";
303                 String path = "//apps//asdc_kits//catalog-be-" + attVersionStr + "//import//tosca//categories//categoryTypes.yml";
304                 
305             String content = new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8);
306                 
307                 List<String> serviceCategories = new ArrayList<String>();
308                 List<String> resourceCategories = new ArrayList<String>();
309                                 
310                 Yaml yaml = new Yaml();
311                 Map<String, Object> load = (Map<String, Object>) yaml.load(content);
312                 Map<String, Map> services = (Map<String, Map>) load.get("services");
313                 Map<String, Map> resources = (Map<String, Map>) load.get("resources");
314                 
315                 Map<String, List<String>> resourcesListFromFile = new HashMap<String, List<String>>() ;
316                                 
317                 //retrieve subcategories
318                 for ( String resourceCategoryName : resources.keySet()) {
319                         Map<String, Map> subcategory = (Map) resources.get(resourceCategoryName).get("subcategories");
320                                 
321                         resourceCategories = new ArrayList<String>();
322                         for (String subcategoryName : subcategory.keySet()) {   
323                                 String name = (String) subcategory.get(subcategoryName).get("name");
324 //                              resourceCategories.add(name.replaceAll("\\s",""));
325                                 resourceCategories.add(name);
326                                                         
327                         }
328                         resourcesListFromFile.put(resources.get(resourceCategoryName).get("name").toString(), resourceCategories);      
329                 }               
330                 
331                         System.out.println(resourcesListFromFile.toString());
332                         getExtendTest().log(Status.INFO, "Expected categories:");
333                         getExtendTest().log(Status.INFO, resourcesListFromFile.toString());
334                         
335                 //retrieve service categories   
336 //              for ( String serviceCategoryName : services.keySet()) {
337 //                      String name = (String) services.get(serviceCategoryName).get("name");
338 //                      serviceCategories.add(name);
339 //                              }       
340 //              serviceCategories.forEach(System.out::println);
341                         
342                         
343                         //retrieve resource list from URL
344                         
345                         Map<String, List<CategoryDefinition>> categoriesMap = getCategories();
346                         List<CategoryDefinition> resourceSubCategories = categoriesMap.get(ComponentTypeEnum.RESOURCE_PARAM_NAME);
347                         List<SubCategoryDefinition> subcategories;
348                         for (CategoryDefinition categoryDefinition : resourceSubCategories) {
349                                 subcategories =  categoryDefinition.getSubcategories();
350                         }
351 //                      subcategories.stream().collect(toMap(i -> i, i -> items.get(i)));
352                         
353 //                      resourceSubCategories.stream().collect(
354 //                      Collectors.groupingBy(CategoryDefinition::getName, Collectors.groupingBy(SubCategoryDefinition::getName)));
355                         
356 //                      resourceSubCategories.stream().filter(p->p.getSubcategories()).map(m->m.getName()).collect(Collectors.toList()).collect(Collectors.toMap(CategoryDefinition::getName,m));
357                         
358                         
359                         Map<String, List<String>> resourceMapFromUrl = resourceSubCategories.stream().collect(Collectors.toMap( e -> e.getName() , e -> e.getSubcategories().stream().map(e1 -> e1.getName()).collect(Collectors.toList())));
360                         
361                         getExtendTest().log(Status.INFO, "Actual categories:");
362                         getExtendTest().log(Status.INFO, resourceMapFromUrl.toString());
363                         
364                         
365                         assertTrue("missing categories ", resourceMapFromUrl.keySet().containsAll(resourcesListFromFile.keySet()));
366                         
367                         
368
369                         
370                         
371
372         }
373         
374         
375         public Map<String, List<CategoryDefinition>> getCategories() throws Exception {
376                 
377                 User defaultAdminUser = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
378                 
379                 Map<String,List<CategoryDefinition>> map = new HashMap<String,List<CategoryDefinition>>();
380                                 
381                 
382                 RestResponse allResourceCategories = CategoryRestUtils.getAllCategories(defaultAdminUser, ComponentTypeEnum.RESOURCE_PARAM_NAME);
383                 RestResponse allServiceCategories = CategoryRestUtils.getAllCategories(defaultAdminUser, ComponentTypeEnum.SERVICE_PARAM_NAME);
384
385                 List<CategoryDefinition> parsedResourceCategories = ResponseParser.parseCategories(allResourceCategories);
386                 List<CategoryDefinition> parsedServiceCategories = ResponseParser.parseCategories(allServiceCategories);
387                 
388                 map.put(ComponentTypeEnum.RESOURCE_PARAM_NAME, parsedResourceCategories);
389                 map.put(ComponentTypeEnum.SERVICE_PARAM_NAME, parsedServiceCategories);
390                 
391                 return map;
392         }
393         
394
395
396         @Test (enabled=false)
397         public void pasrseCategoriesClass2() throws IOException{
398                 
399                 String path = "C:\\Git_work\\Git_UGN\\d2-sdnc\\catalog-be\\src\\main\\resources\\import\\tosca\\categories\\categoryTypes.yml";
400
401                 FileReader reader = new FileReader(path);
402                 Yaml yaml=new Yaml();
403                 
404                 
405                 Map<?, ?> map = (Map<?, ?>) yaml.load(reader);
406                 
407                 Collection<Map> values = (Collection<Map>) map.values();
408                 for (Map map2 : values) {
409                         Collection values2 = map2.values();
410                         for (Object object : values2) {
411                                 
412                                 
413                         }
414                 }
415                         
416                 {
417                         
418                 }
419                 List<Object> collect = values.stream().map(e -> e.get("name")).collect(Collectors.toList());
420                 
421 //              resourcesArrayList.stream().filter(s -> s.getName().toLowerCase().startsWith("ci") && !s.getName().toLowerCase().equals("cindervolume")).map(e -> e.getUniqueId()).collect(Collectors.toList()).forEach((i)
422                 
423                 
424                 
425                 
426         }
427
428 }