2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.externalApis;
23 import static org.testng.AssertJUnit.assertTrue;
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;
39 import java.util.stream.Collectors;
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;
69 import com.aventstack.extentreports.Status;
77 public class DeploymentValiditaion extends ComponentBaseTest{
83 public static TestName name = new TestName();
85 public DeploymentValiditaion() {
86 super(name, DeploymentValiditaion.class.getName());
89 protected final static String categoryFilterKey = "category";
90 protected final static String subCategoryFilterKey = "subCategory";
91 protected String attVersionStr ;
94 public static List<File> listf(String directoryName) {
95 File directory = new File(directoryName);
97 List<File> resultList = new ArrayList<File>();
99 // get all the files from a directory
100 File[] fList = directory.listFiles();
101 resultList.addAll(Arrays.asList(fList));
102 for (File file : fList) {
104 System.out.println(file.getAbsolutePath());
105 } else if (file.isDirectory()) {
106 resultList.addAll(listf(file.getAbsolutePath()));
109 //System.out.println(fList);
113 public void beforeTest() throws Exception{
114 RestResponse attVersion = CatalogRestUtils.getOsVersion();
115 attVersionStr = ResponseParser.getVersionFromResponse(attVersion);
120 public void pasrseNormativies() throws Exception{
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";
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";
130 List<File> yamlList1 = getYamlFilesList(path);
131 List<String> nodeNamesFromYamlList1 = getNodeNamesFromYamlList(yamlList1);
132 List<File> yamlList2 = getYamlFilesList(path2);
133 List<String> nodeNamesFromYamlList2 = getNodeNamesFromYamlList(yamlList2);
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());
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());
156 List<String> missingNormatives = new ArrayList<>();
158 for (int i = 0; i < expectedList.size(); i ++) {
159 if (!resourceToscaNamesList.contains(expectedList.get(i))) {
161 missingNormatives.add(expectedList.get(i));
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());
172 assertTrue("missing normatives ", good);
177 public List<String> getNodeNamesFromYamlList(List<File> yamlList) throws IOException {
178 List<String> nodeNameList = new ArrayList<String>();
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);
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());
213 @Test (enabled=false)
214 public void testYaml() throws IOException{
216 System.out.println("");
218 File file = new File("\\\\Comp-1\\FileIO\\Stop.txt");
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);
232 byte[] artifactsBs = readZip.get("Definitions/VF_RI2_G6.yaml");
233 String str = new String(artifactsBs, StandardCharsets.UTF_8);
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");
243 Set<String> keySet = node_templates.keySet();
249 public void pasrseDataTypes() throws Exception{
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";
254 String content = new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8);
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());
266 Resource resource = AtomicOperationUtils.createResourceByType(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, true).left().value();
267 PropertyReqDetails defaultProperty = ElementFactory.getDefaultListProperty();
269 // SchemaDefinition schemaDefinition = new SchemaDefinition();
274 defaultProperty.setPropertyDefaultValue(null);
275 // defaultProperty.setSchema(schemaDefinition);
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);
290 listOfDataTypes.forEach(System.out::println);
300 public void pasrseCategories() throws Exception{
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";
305 String content = new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8);
307 List<String> serviceCategories = new ArrayList<String>();
308 List<String> resourceCategories = new ArrayList<String>();
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");
315 Map<String, List<String>> resourcesListFromFile = new HashMap<String, List<String>>() ;
317 //retrieve subcategories
318 for ( String resourceCategoryName : resources.keySet()) {
319 Map<String, Map> subcategory = (Map) resources.get(resourceCategoryName).get("subcategories");
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);
328 resourcesListFromFile.put(resources.get(resourceCategoryName).get("name").toString(), resourceCategories);
331 System.out.println(resourcesListFromFile.toString());
332 getExtendTest().log(Status.INFO, "Expected categories:");
333 getExtendTest().log(Status.INFO, resourcesListFromFile.toString());
335 //retrieve service categories
336 // for ( String serviceCategoryName : services.keySet()) {
337 // String name = (String) services.get(serviceCategoryName).get("name");
338 // serviceCategories.add(name);
340 // serviceCategories.forEach(System.out::println);
343 //retrieve resource list from URL
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();
351 // subcategories.stream().collect(toMap(i -> i, i -> items.get(i)));
353 // resourceSubCategories.stream().collect(
354 // Collectors.groupingBy(CategoryDefinition::getName, Collectors.groupingBy(SubCategoryDefinition::getName)));
356 // resourceSubCategories.stream().filter(p->p.getSubcategories()).map(m->m.getName()).collect(Collectors.toList()).collect(Collectors.toMap(CategoryDefinition::getName,m));
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())));
361 getExtendTest().log(Status.INFO, "Actual categories:");
362 getExtendTest().log(Status.INFO, resourceMapFromUrl.toString());
365 assertTrue("missing categories ", resourceMapFromUrl.keySet().containsAll(resourcesListFromFile.keySet()));
375 public Map<String, List<CategoryDefinition>> getCategories() throws Exception {
377 User defaultAdminUser = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
379 Map<String,List<CategoryDefinition>> map = new HashMap<String,List<CategoryDefinition>>();
382 RestResponse allResourceCategories = CategoryRestUtils.getAllCategories(defaultAdminUser, ComponentTypeEnum.RESOURCE_PARAM_NAME);
383 RestResponse allServiceCategories = CategoryRestUtils.getAllCategories(defaultAdminUser, ComponentTypeEnum.SERVICE_PARAM_NAME);
385 List<CategoryDefinition> parsedResourceCategories = ResponseParser.parseCategories(allResourceCategories);
386 List<CategoryDefinition> parsedServiceCategories = ResponseParser.parseCategories(allServiceCategories);
388 map.put(ComponentTypeEnum.RESOURCE_PARAM_NAME, parsedResourceCategories);
389 map.put(ComponentTypeEnum.SERVICE_PARAM_NAME, parsedServiceCategories);
396 @Test (enabled=false)
397 public void pasrseCategoriesClass2() throws IOException{
399 String path = "C:\\Git_work\\Git_UGN\\d2-sdnc\\catalog-be\\src\\main\\resources\\import\\tosca\\categories\\categoryTypes.yml";
401 FileReader reader = new FileReader(path);
402 Yaml yaml=new Yaml();
405 Map<?, ?> map = (Map<?, ?>) yaml.load(reader);
407 Collection<Map> values = (Collection<Map>) map.values();
408 for (Map map2 : values) {
409 Collection values2 = map2.values();
410 for (Object object : values2) {
419 List<Object> collect = values.stream().map(e -> e.get("name")).collect(Collectors.toList());
421 // resourcesArrayList.stream().filter(s -> s.getName().toLowerCase().startsWith("ci") && !s.getName().toLowerCase().equals("cindervolume")).map(e -> e.getUniqueId()).collect(Collectors.toList()).forEach((i)