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