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.ci.tests.utils;
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertNotNull;
25 import static org.testng.AssertJUnit.assertTrue;
28 import java.io.FileInputStream;
29 import java.io.FileNotFoundException;
30 import java.io.InputStream;
31 import java.text.ParseException;
32 import java.text.SimpleDateFormat;
33 import java.util.ArrayList;
34 import java.util.List;
36 import java.util.Map.Entry;
38 import org.apache.commons.lang3.StringUtils;
39 import org.apache.log4j.Logger;
40 import org.openecomp.sdc.ci.tests.config.Config;
41 import org.openecomp.sdc.ci.tests.datatypes.enums.ArtifactTypeEnum;
42 import org.openecomp.sdc.common.api.ToscaNodeTypeInfo;
43 import org.openecomp.sdc.common.api.YamlConstants;
44 import org.yaml.snakeyaml.Yaml;
46 import com.google.gson.Gson;
47 import com.google.gson.JsonElement;
48 import com.google.gson.JsonObject;
49 import com.google.gson.JsonParser;
51 public final class Utils {
53 Gson gson = new Gson();
55 static Logger logger = Logger.getLogger(Utils.class.getName());
57 String contentTypeHeaderData = "application/json";
58 String acceptHeaderDate = "application/json";
64 * StartTest.enableLogger(); logger =
65 * Logger.getLogger(Utils.class.getName());
70 // public String serviceTopologyPattern = "/topology/topology/%s";
71 // public String serviceTopologyTemplatePattern =
72 // "/topologytemplate/topologytemplate/%s";
74 // public String serviceTopologySearchPattern =
75 // "topology/topology/_search?q=%s";
76 // public String serviceTopologyTemplateSearchPattern =
77 // "topologytemplate/topologytemplate/_search?q=%s";
79 // public ArtifactTypeEnum getFileTypeByExtension(String fileName) {
81 // String fileExtension = null;
82 // if (fileName.matches("(.*)\\.(.*)")) {
83 // System.out.println(fileName.substring(fileName.lastIndexOf(".") + 1));
84 // fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1);
87 // switch (fileExtension) {
89 // return ArtifactTypeEnum.SHELL_SCRIPT;
91 // return ArtifactTypeEnum.ICON;
93 // return ArtifactTypeEnum.PUPPET;
95 // return ArtifactTypeEnum.YANG;
97 // return ArtifactTypeEnum.UNKNOWN;
102 // public ArrayList<String> getScriptList (List<UploadArtifactInfo>
105 // ArrayList<String> scriptNameArray = new ArrayList<>();
106 // if (artifactsList != null){
107 // for (UploadArtifactInfo fileInArtifactsList : artifactsList){
108 // String artifactFileName = fileInArtifactsList.getArtifactName();
109 // ArtifactTypeEnum artifactFileType =
110 // fileInArtifactsList.getArtifactType();
111 // if (! artifactFileType.equals(ArtifactTypeEnum.ICON)){
112 // scriptNameArray.add(artifactFileName);
116 // return scriptNameArray;
122 // public String getYamlFileLocation(File testResourcesPath) {
123 // File[] files = testResourcesPath.listFiles();
124 // if (files.length == 0){
127 // for (int i = 0; i < files.length; i++){
128 // if (files[i].isFile()){
129 // return files[i].getAbsoluteFile().toString();
136 // public String readFileContentToString (String fileName) throws
139 // Path path = Paths.get(fileName);
140 // String stringFromFile = new String(Files.readAllBytes(path));
141 // return stringFromFile;
146 @SuppressWarnings("unchecked")
147 public ToscaNodeTypeInfo parseToscaNodeYaml(String fileContent) {
149 ToscaNodeTypeInfo result = new ToscaNodeTypeInfo();
150 Object templateVersion = null;
151 Object templateName = null;
153 if (fileContent != null) {
154 Yaml yaml = new Yaml();
156 Map<String, Object> yamlObject = (Map<String, Object>) yaml.load(fileContent);
158 templateVersion = yamlObject.get(YamlConstants.TEMPLATE_VERSION);
159 if (templateVersion != null) {
160 result.setTemplateVersion(templateVersion.toString());
162 templateName = yamlObject.get(YamlConstants.TEMPLATE_NAME);
163 if (templateName != null) {
164 result.setTemplateName(templateName.toString());
166 Object nodeTypes = yamlObject.get(YamlConstants.NODE_TYPES);
168 if (nodeTypes != null) {
169 Map<String, Object> nodeTypesMap = (Map<String, Object>) nodeTypes;
170 for (Entry<String, Object> entry : nodeTypesMap.entrySet()) {
172 String nodeName = entry.getKey();
173 if (nodeName != null) {
174 result.setNodeName(nodeName);
189 // public ArtifactsMetadata getArtifactsMetadata(String response){
190 // ArtifactsMetadata artifactsMetadata = new ArtifactsMetadata();
192 // artifactsMetadata.setId(getJsonObjectValueByKey(response, "id"));
193 // artifactsMetadata.setName(getJsonObjectValueByKey(response, "name"));
194 // artifactsMetadata.setType(getJsonObjectValueByKey(response, "type"));
196 // artifactsMetadata.setCreator(getJsonObjectValueByKey(response,
198 // artifactsMetadata.setCreationTime(getJsonObjectValueByKey(response,
200 // artifactsMetadata.setLastUpdateTime(getJsonObjectValueByKey(response,
201 // "lastUpdateTime"));
202 // artifactsMetadata.setChecksum(getJsonObjectValueByKey(response,
204 // artifactsMetadata.setDescription(getJsonObjectValueByKey(response,
206 // artifactsMetadata.setLastUpdater(getJsonObjectValueByKey(response,
209 // return artifactsMetadata;
212 public static String getJsonObjectValueByKey(String metadata, String key) {
213 JsonElement jelement = new JsonParser().parse(metadata);
215 JsonObject jobject = jelement.getAsJsonObject();
216 Object obj = jobject.get(key);
221 value = (String) jobject.get(key).getAsString();
226 public static Config getConfig() throws FileNotFoundException {
227 Config config = Config.instance();
231 // public void uploadNormativeTypes() throws IOException{
232 // Config config = getConfig();
233 // String[] normativeTypes = {"root", "compute", "blockStorage",
234 // "softwareComponent", "DBMS", "database", "network", "objectStorage",
235 // "webServer", "webApplication"};
236 // for( String normativeType : normativeTypes ){
237 // uploadComponent(config.getComponentsConfigDir()+File.separator+"normativeTypes"+File.separator+normativeType);
242 // public void uploadApacheComponent() throws IOException{
243 // Config config = getConfig();
244 // uploadComponent(config.getComponentsConfigDir()+File.separator+"apache");
247 // public void uploadComponent(String componentDir) throws IOException{
249 // //*********************************************upload*************************************************************
250 // Config config = getConfig();
251 // ZipDirectory zipDirectory = new ZipDirectory();
252 // System.out.println(config.getEsHost());
254 // List<UploadArtifactInfo> artifactsList = new
255 // ArrayList<UploadArtifactInfo>();
257 //// read test resources and zip it as byte array
258 // byte[] zippedAsByteArray = zipDirectory.zip(componentDir, artifactsList);
260 //// encode zipped directory using base64
261 // String payload = Decoder.encode(zippedAsByteArray);
263 //// zip name build as testName with ".zip" extension
264 // String payloadZipName = getPayloadZipName(componentDir);
267 // UploadResourceInfo resourceInfo = new UploadResourceInfo(payload,
268 // payloadZipName, "description", "category/mycategory", null,
270 // String json = new Gson().toJson(resourceInfo);
272 //// calculate md5 on the content of json
274 // org.apache.commons.codec.digest.DigestUtils.md5Hex(json);
276 //// encode the md5 to base64, sent as header in post http request
277 // String encodedMd5 = Decoder.encode(jsonMd5.getBytes());
279 //// upload component to Elastic Search DB
280 // String url = null;
281 // HttpRequest http = new HttpRequest();
283 // url = String.format(Urls.UPLOAD_ZIP_URL, config.getCatalogFeHost(),
284 // config.getCatalogFePort());
286 //// Prepare headers to post upload component request
287 // HeaderData headerData = new HeaderData(encodedMd5, "application/json",
288 // "att", "test", "testIvanovich", "RoyalSeal", "Far_Far_Away",
289 // "getResourceArtifactListTest");
291 // MustHeaders headers = new MustHeaders(headerData);
292 // System.out.println("headers:"+headers.getMap());
294 // RestResponse response = http.httpSendPost(url, json, headers.getMap());
296 // assertEquals("upload component failed with code " +
297 // response.getErrorCode().intValue(),response.getErrorCode().intValue(),
301 // private String getPayloadZipName(String componentDir) {
302 // String payloadName;
303 // if( componentDir.contains( File.separator) ){
304 // String delimiter = null;
305 // if( File.separator.equals("\\")){
306 // delimiter ="\\\\";
309 // delimiter = File.separator;
311 // String[] split = componentDir.split(delimiter);
312 // payloadName = split[split.length-1];
315 // payloadName = componentDir;
317 // return payloadName+".zip";
322 // public List<UploadArtifactInfo> createArtifactsList(String srcDir) {
324 // List<UploadArtifactInfo> artifactsList = new
325 // ArrayList<UploadArtifactInfo>();
326 // File srcFile = new File(srcDir);
327 // addFileToList(srcFile, artifactsList);
329 // return artifactsList;
332 // public void addFileToList(File srcFile, List<UploadArtifactInfo>
335 // File[] files = srcFile.listFiles();
337 // for (int i = 0; i < files.length; i++) {
338 // // if the file is directory, use recursion
339 // if (files[i].isDirectory()) {
340 // addFileToList(files[i], artifactsList);
344 // String fileName = files[i].getName();
345 // String artifactPath = fileName;
347 // if ( ! files[i].getName().matches("(.*)\\.y(?)ml($)")) {
348 // UploadArtifactInfo uploadArtifactInfo = new UploadArtifactInfo();
349 // uploadArtifactInfo.setArtifactName(files[i].getName());
350 // String parent = files[i].getParent();
352 // if (parent != null) {
353 // System.out.println(parent);
354 // int lastSepartor = parent.lastIndexOf(File.separator);
355 // if (lastSepartor > -1) {
356 // String actualParent = parent.substring(lastSepartor + 1);
357 // artifactPath = actualParent + "/" + artifactPath;
361 // uploadArtifactInfo.setArtifactPath(artifactPath);
362 // uploadArtifactInfo.setArtifactType(getFileTypeByExtension(fileName));
363 // uploadArtifactInfo.setArtifactDescription("description");
364 // artifactsList.add(uploadArtifactInfo);
366 // System.out.println("artifact list: " + artifactsList);
374 // public String buildArtifactListUrl (String nodesType, String
375 // templateVersion, String artifactName) throws FileNotFoundException{
376 // //"http://172.20.43.132/sdc2/v1/catalog/resources/tosca.nodes.Root/1.0.0.wd03-SNAPSHOT/artifacts/wxs_baseline_compare.sh"
377 // Config config = getConfig();
378 // return "\"http://" + config.getCatalogBeHost() + ":" +
379 // config.getCatalogBePort() + "/sdc2/v1/catalog/resources/" +nodesType +
380 // "/" + templateVersion + "/artifacts/" + artifactName +"\"";
384 // public void addTopologyToES(String testFolder, String
385 // serviceTopologyPattern) throws IOException{
386 // Config config = getConfig();
387 // String url = String.format(Urls.ES_URL, config.getEsHost(),
388 // config.getEsPort()) + serviceTopologyPattern;
389 // String sourceDir =
390 // config.getResourceConfigDir()+File.separator+testFolder;
391 // Path filePath = FileSystems.getDefault().getPath(sourceDir,
393 // postFileContentsToUrl(url, filePath);
396 // public void addTopologyTemplateToES(String testFolder, String
397 // serviceTopologyTemplatePattern) throws IOException{
398 // Config config = getConfig();
399 // String url = String.format(Urls.ES_URL, config.getEsHost(),
400 // config.getEsPort()) + serviceTopologyTemplatePattern;
401 // String sourceDir =
402 // config.getResourceConfigDir()+File.separator+testFolder;
403 // Path filePath = FileSystems.getDefault().getPath(sourceDir,
404 // "topologyTemplate.txt");
405 // postFileContentsToUrl(url, filePath);
409 // public void postFileContentsToUrl(String url, Path filePath) throws
411 // HttpClientContext localContext = HttpClientContext.create();
412 // CloseableHttpResponse response = null;
414 // byte[] fileContent = Files.readAllBytes(filePath);
416 // try(CloseableHttpClient httpClient = HttpClients.createDefault()){
417 // HttpPost httpPost = new HttpPost(url);
418 // StringEntity entity = new StringEntity(new String(fileContent) ,
419 // ContentType.APPLICATION_JSON);
420 // httpPost.setEntity(entity);
421 // response = httpClient.execute(httpPost, localContext);
432 //// public boolean isPatternInEsDb(String patternToSearch)throws
434 //// Config config = getConfig();
435 //// String url = String.format(Urls.GET_SEARCH_DATA_FROM_ES,
436 // config.getEsHost(), config.getEsPort(),patternToSearch);
437 //// HttpRequest httpRequest = new HttpRequest();
438 //// RestResponse restResponse = httpRequest.httpSendGet(url);
439 //// if (restResponse.getErrorCode() == 200){
442 //// if (restResponse.getErrorCode() == 404){
449 // public static RestResponse deleteAllDataFromEs() throws IOException{
450 // return deleteFromEsDbByPattern("_all");
455 // public List<String> buildIdArrayListByTypesIndex (String index, String
456 // types) throws IOException{
458 // Config config = getConfig();
459 // HttpRequest http = new HttpRequest();
460 // RestResponse getResponce =
461 // http.httpSendGet(String.format(Urls.GET_ID_LIST_BY_INDEX_FROM_ES,
462 // config.getEsHost(), config.getEsPort(), index, types), null);
464 // List <String> idArray = new ArrayList<String>();
466 // JsonElement jelement = new JsonParser().parse(getResponce.getResponse());
467 // JsonObject jobject = jelement.getAsJsonObject();
468 // JsonObject hitsObject = (JsonObject) jobject.get("hits");
469 // JsonArray hitsArray = (JsonArray) hitsObject.get("hits");
470 // for (int i = 0; i < hitsArray.size(); i ++){
471 // JsonObject idObject = (JsonObject) hitsArray.get(i);
472 // String id = idObject.get("_id").toString();
473 // id = id.replace("\"", "");
480 // public List<String> buildCategoriesTagsListFromJson(String
481 // categoriesTagsJson){
483 // ArrayList<String> categoriesTagsArray = new ArrayList<>();
484 // JsonElement jelement = new JsonParser().parse(categoriesTagsJson);
485 // JsonArray jArray = jelement.getAsJsonArray();
486 // for (int i = 0; i < jArray.size(); i ++){
487 // JsonObject categoriesTagsObject = (JsonObject) jArray.get(i);
488 // String categories = categoriesTagsObject.get("name").toString();
489 // categoriesTagsArray.add(categories);
492 // return categoriesTagsArray;
495 // public ArrayList <String> getCategoriesFromDb() throws Exception{
497 // ArrayList<String> categoriesFromDbArrayList = new ArrayList<>();
498 // RestResponse restResponse = new RestResponse();
499 // String contentTypeHeaderData = "application/json";
500 // String acceptHeaderDate = "application/json";
502 // Map<String, String> headersMap = new HashMap<String,String>();
503 // headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(),contentTypeHeaderData);
504 // headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
506 // HttpRequest httpRequest = new HttpRequest();
507 // String url = String.format(Urls.QUERY_NEO4J,
508 // Config.instance().getNeoHost(), Config.instance().getNeoPort());
509 // String body = "{\"statements\" : [ { \"statement\" : \"MATCH
510 // (category:category) return (category)\"} ]}";
511 // restResponse = httpRequest.httpSendPostWithAuth(url, body, headersMap,
512 // Config.instance().getNeoDBusername(),
513 // Config.instance().getNeoDBpassword());
515 // if (restResponse.getResponse()==null){
516 // return categoriesFromDbArrayList;
518 // JsonElement jelement = new
519 // JsonParser().parse(restResponse.getResponse());
520 // JsonObject jobject = jelement.getAsJsonObject();
521 // JsonArray resultsArray = (JsonArray) jobject.get("results");
522 // JsonObject resObject = (JsonObject) resultsArray.get(0);
523 // JsonArray dataArray = (JsonArray) resObject.get("data");
524 // for (int i = 0; i < dataArray.size(); i ++){
525 // JsonObject rowObject = (JsonObject) dataArray.get(i);
526 // JsonArray rowArray = (JsonArray) rowObject.get("row");
527 // JsonObject nameObject = (JsonObject) rowArray.get(0);
528 // String name = nameObject.get("name").toString();
529 //// name = name.replace("\"", "");
530 // categoriesFromDbArrayList.add(name);
536 // return categoriesFromDbArrayList;
539 public static void compareArrayLists(List<String> actualArraylList, List<String> expectedArrayList,
542 ArrayList<String> actual = new ArrayList<String>(actualArraylList);
543 ArrayList<String> expected = new ArrayList<String>(expectedArrayList);
544 // assertEquals(message + " count got by rest API not match to " +
545 // message + " expected count", expected.size(),actual.size());
546 expected.removeAll(actual);
547 assertEquals(message + " content got by rest API not match to " + message + " actual content", 0,
551 public static Object parseYamlConfig(String pattern) throws FileNotFoundException {
553 Yaml yaml = new Yaml();
554 Config config = getConfig();
555 String configurationFile = config.getConfigurationFile();
556 File file = new File(configurationFile);
558 // File("../catalog-be/src/main/resources/config/configuration.yaml");
559 InputStream inputStream = new FileInputStream(file);
560 Map<?, ?> map = (Map<?, ?>) yaml.load(inputStream);
561 Object patternMap = (Object) map.get(pattern);
566 public static String getDepArtLabelFromConfig(ArtifactTypeEnum artifactTypeEnum) throws FileNotFoundException {
568 @SuppressWarnings("unchecked")
569 Map<String, Object> mapOfDepResArtTypesObjects = (Map<String, Object>) parseYamlConfig(
570 "deploymentResourceArtifacts");
571 for (Map.Entry<String, Object> iter : mapOfDepResArtTypesObjects.entrySet()) {
572 if (iter.getValue().toString().contains(artifactTypeEnum.getType())) {
573 return iter.getKey().toLowerCase();
577 return "defaultLabelName";
581 public static String multipleChar(String ch, int repeat) {
582 return StringUtils.repeat(ch, repeat);
585 public static List<String> getListOfDepResArtLabels(Boolean isLowerCase) throws FileNotFoundException {
587 List<String> listOfResDepArtTypesFromConfig = new ArrayList<String>();
588 @SuppressWarnings("unchecked")
589 Map<String, Object> resourceDeploymentArtifacts = (Map<String, Object>) parseYamlConfig(
590 "deploymentResourceArtifacts");
591 if (resourceDeploymentArtifacts != null) {
594 for (Map.Entry<String, Object> iter : resourceDeploymentArtifacts.entrySet()) {
595 listOfResDepArtTypesFromConfig.add(iter.getKey().toLowerCase());
599 for (Map.Entry<String, Object> iter : resourceDeploymentArtifacts.entrySet()) {
600 listOfResDepArtTypesFromConfig.add(iter.getKey());
604 return listOfResDepArtTypesFromConfig;
607 public static List<String> getListOfToscaArtLabels(Boolean isLowerCase) throws FileNotFoundException {
609 List<String> listOfToscaArtTypesFromConfig = new ArrayList<String>();
610 @SuppressWarnings("unchecked")
611 Map<String, Object> toscaArtifacts = (Map<String, Object>) parseYamlConfig("toscaArtifacts");
612 if (toscaArtifacts != null) {
615 for (Map.Entry<String, Object> iter : toscaArtifacts.entrySet()) {
616 listOfToscaArtTypesFromConfig.add(iter.getKey().toLowerCase());
619 for (Map.Entry<String, Object> iter : toscaArtifacts.entrySet()) {
620 listOfToscaArtTypesFromConfig.add(iter.getKey());
624 return listOfToscaArtTypesFromConfig;
628 // public static List<String> getListOfResDepArtTypes() throws
629 // FileNotFoundException {
631 // List<String> listOfResDepArtTypesFromConfig = new ArrayList<String>();
632 // @SuppressWarnings("unchecked")
633 // Map<String, Object> resourceDeploymentArtifacts = (Map<String, Object>)
634 // parseYamlConfig("resourceDeploymentArtifacts");
635 // for (Map.Entry<String, Object> iter :
636 // resourceDeploymentArtifacts.entrySet()) {
637 // listOfResDepArtTypesFromConfig.add(iter.getKey());
640 // return listOfResDepArtTypesFromConfig;
643 // public static List<String> getListOfDepResInstArtTypes() throws
644 // FileNotFoundException {
646 // List<String> listOfResInstDepArtTypesFromConfig = new
647 // ArrayList<String>();
648 // @SuppressWarnings("unchecked")
649 // Map<String, Object> resourceDeploymentArtifacts = (Map<String, Object>)
650 // parseYamlConfig("deploymentResourceInstanceArtifacts");
651 // for (Map.Entry<String, Object> iter :
652 // resourceDeploymentArtifacts.entrySet()) {
653 // listOfResInstDepArtTypesFromConfig.add(iter.getKey().toLowerCase());
656 // return listOfResInstDepArtTypesFromConfig;
659 public static List<String> getListOfResPlaceHoldersDepArtTypes() throws FileNotFoundException {
660 List<String> listResDepArtTypesFromConfig = new ArrayList<String>();
661 List<String> listOfResDepArtLabelsFromConfig = getListOfDepResArtLabels(false);
662 assertNotNull("deployment artifact types list is null", listOfResDepArtLabelsFromConfig);
663 Object parseYamlConfig = Utils.parseYamlConfig("deploymentResourceArtifacts");
664 Map<String, Object> mapOfDepResArtTypesObjects = (Map<String, Object>) Utils
665 .parseYamlConfig("deploymentResourceArtifacts");
667 // assertNotNull("deployment artifact types list is null",
668 // mapOfDepResArtTypesObjects);
669 if (listOfResDepArtLabelsFromConfig != null) {
670 for (String resDepArtType : listOfResDepArtLabelsFromConfig) {
671 Object object = mapOfDepResArtTypesObjects.get(resDepArtType);
672 if (object instanceof Map<?, ?>) {
673 Map<String, Object> map = (Map<String, Object>) object;
674 listResDepArtTypesFromConfig.add((String) map.get("type"));
676 assertTrue("return object does not instance of map", false);
680 return listResDepArtTypesFromConfig;
683 public static Long getEpochTimeFromUTC(String time) throws ParseException {
684 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS zzz");
685 java.util.Date date = df.parse(time);
686 long epoch = date.getTime();