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