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 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;
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;
54 import java.util.stream.Collectors;
56 import static org.testng.AssertJUnit.assertTrue;
58 public class DeploymentValiditaion extends ComponentBaseTest{
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());
67 protected final static String categoryFilterKey = "category";
68 protected final static String subCategoryFilterKey = "subCategory";
69 protected String attVersionStr ;
70 protected String pathPrefix;
73 public static List<File> listf(String directoryName) {
74 File directory = new File(directoryName);
76 List<File> resultList = new ArrayList<>();
78 // get all the files from a directory
79 File[] fList = directory.listFiles();
80 resultList.addAll(Arrays.asList(fList));
81 for (File file : fList) {
83 System.out.println(file.getAbsolutePath());
84 } else if (file.isDirectory()) {
85 resultList.addAll(listf(file.getAbsolutePath()));
88 //System.out.println(fList);
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;
99 public void pasrseNormativies() throws Exception{
102 String path = pathPrefix+"normative-types";
103 String path2 = pathPrefix+"heat-types";
105 List<File> yamlList1 = getYamlFilesList(path);
106 List<String> nodeNamesFromYamlList1 = getNodeNamesFromYamlList(yamlList1);
107 List<File> yamlList2 = getYamlFilesList(path2);
108 List<String> nodeNamesFromYamlList2 = getNodeNamesFromYamlList(yamlList2);
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());
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());
130 List<String> missingNormatives = new ArrayList<>();
132 for (int i = 0; i < expectedList.size(); i ++) {
133 if (!resourceToscaNamesList.contains(expectedList.get(i))) {
135 missingNormatives.add(expectedList.get(i));
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());
144 assertTrue("missing normatives ", good);
149 public List<String> getNodeNamesFromYamlList(List<File> yamlList) throws IOException {
150 List<String> nodeNameList = new ArrayList<>();
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);
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());
180 @Test (enabled=false)
181 public void testYaml() throws IOException{
183 System.out.println("");
185 File file = new File("\\\\Comp-1\\FileIO\\Stop.txt");
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);
195 byte[] artifactsBs = readZip.get("Definitions/VF_RI2_G6.yaml");
196 String str = new String(artifactsBs, StandardCharsets.UTF_8);
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");
203 Set<String> keySet = node_templates.keySet();
209 public void pasrseDataTypes() throws Exception{
211 String path = pathPrefix+"data-types"+File.separator+"dataTypes.yml";
212 String content = new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8);
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());
223 Resource resource = AtomicOperationUtils.createResourceByType(ResourceTypeEnum.VF, UserRoleEnum.DESIGNER, true).left().value();
224 PropertyReqDetails defaultProperty = ElementFactory.getDefaultListProperty();
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);
235 listOfDataTypes.forEach(System.out::println);
240 public void pasrseCategories() throws Exception{
242 String path = pathPrefix+"categories"+File.separator+"categoryTypes.yml";
243 String content = new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8);
245 List<String> serviceCategories = new ArrayList<>();
246 List<String> resourceCategories = new ArrayList<>();
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");
253 Map<String, List<String>> resourcesListFromFile = new HashMap<>() ;
255 //retrieve subcategories
256 for ( String resourceCategoryName : resources.keySet()) {
257 Map<String, Map> subcategory = (Map) resources.get(resourceCategoryName).get("subcategories");
259 resourceCategories = new ArrayList<String>();
260 for (String subcategoryName : subcategory.keySet()) {
261 String name = (String) subcategory.get(subcategoryName).get("name");
262 resourceCategories.add(name);
264 resourcesListFromFile.put(resources.get(resourceCategoryName).get("name").toString(), resourceCategories);
267 System.out.println(resourcesListFromFile.toString());
268 getExtendTest().log(Status.INFO, "Expected categories:");
269 getExtendTest().log(Status.INFO, resourcesListFromFile.toString());
271 //retrieve service categories
272 // for ( String serviceCategoryName : services.keySet()) {
273 // String name = (String) services.get(serviceCategoryName).get("name");
274 // serviceCategories.add(name);
276 // serviceCategories.forEach(System.out::println);
278 //retrieve resource list from URL
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();
286 // subcategories.stream().collect(toMap(i -> i, i -> items.get(i)));
288 // resourceSubCategories.stream().collect(
289 // Collectors.groupingBy(CategoryDefinition::getName, Collectors.groupingBy(SubCategoryDefinition::getName)));
291 // resourceSubCategories.stream().filter(p->p.getSubcategories()).map(m->m.getName()).collect(Collectors.toList()).collect(Collectors.toMap(CategoryDefinition::getName,m));
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())));
296 getExtendTest().log(Status.INFO, "Actual categories:");
297 getExtendTest().log(Status.INFO, resourceMapFromUrl.toString());
300 assertTrue("missing categories ", resourceMapFromUrl.keySet().containsAll(resourcesListFromFile.keySet()));
305 public Map<String, List<CategoryDefinition>> getCategories() throws Exception {
307 User defaultAdminUser = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
309 Map<String,List<CategoryDefinition>> map = new HashMap<String,List<CategoryDefinition>>();
312 RestResponse allResourceCategories = CategoryRestUtils.getAllCategories(defaultAdminUser, ComponentTypeEnum.RESOURCE_PARAM_NAME);
313 RestResponse allServiceCategories = CategoryRestUtils.getAllCategories(defaultAdminUser, ComponentTypeEnum.SERVICE_PARAM_NAME);
315 List<CategoryDefinition> parsedResourceCategories = ResponseParser.parseCategories(allResourceCategories);
316 List<CategoryDefinition> parsedServiceCategories = ResponseParser.parseCategories(allServiceCategories);
318 map.put(ComponentTypeEnum.RESOURCE_PARAM_NAME, parsedResourceCategories);
319 map.put(ComponentTypeEnum.SERVICE_PARAM_NAME, parsedServiceCategories);
326 /* @Test (enabled=false)
327 public void pasrseCategoriesClass2() throws IOException{
329 String path = "C:\\Git_work\\Git_UGN\\d2-sdnc\\catalog-be\\src\\main\\resources\\import\\tosca\\categories\\categoryTypes.yml";
331 FileReader reader = new FileReader(path);
332 Yaml yaml=new Yaml();
335 Map<?, ?> map = (Map<?, ?>) yaml.load(reader);
337 Collection<Map> values = (Collection<Map>) map.values();
338 for (Map map2 : values) {
339 Collection values2 = map2.values();
340 for (Object object : values2) {
346 List<Object> collect = values.stream().map(e -> e.get("name")).collect(Collectors.toList());
348 // resourcesArrayList.stream().filter(s -> s.getName().toLowerCase().startsWith("ci") && !s.getName().toLowerCase().equals("cindervolume")).map(e -> e.getUniqueId()).collect(Collectors.toList()).forEach((i)