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;
37 import java.util.concurrent.TimeUnit;
39 import org.apache.commons.lang3.StringUtils;
40 import org.apache.log4j.Logger;
41 import org.openecomp.sdc.ci.tests.config.Config;
42 import org.openecomp.sdc.ci.tests.datatypes.enums.ArtifactTypeEnum;
43 import org.openecomp.sdc.common.api.ToscaNodeTypeInfo;
44 import org.openecomp.sdc.common.api.YamlConstants;
45 import org.yaml.snakeyaml.Yaml;
47 import com.google.gson.Gson;
48 import com.google.gson.JsonElement;
49 import com.google.gson.JsonObject;
50 import com.google.gson.JsonParser;
52 public final class Utils {
54 Gson gson = new Gson();
56 static Logger logger = Logger.getLogger(Utils.class.getName());
58 String contentTypeHeaderData = "application/json";
59 String acceptHeaderDate = "application/json";
65 * StartTest.enableLogger(); logger =
66 * Logger.getLogger(Utils.class.getName());
71 // public String serviceTopologyPattern = "/topology/topology/%s";
72 // public String serviceTopologyTemplatePattern =
73 // "/topologytemplate/topologytemplate/%s";
75 // public String serviceTopologySearchPattern =
76 // "topology/topology/_search?q=%s";
77 // public String serviceTopologyTemplateSearchPattern =
78 // "topologytemplate/topologytemplate/_search?q=%s";
80 // public ArtifactTypeEnum getFileTypeByExtension(String fileName) {
82 // String fileExtension = null;
83 // if (fileName.matches("(.*)\\.(.*)")) {
84 // System.out.println(fileName.substring(fileName.lastIndexOf(".") + 1));
85 // fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1);
88 // switch (fileExtension) {
90 // return ArtifactTypeEnum.SHELL_SCRIPT;
92 // return ArtifactTypeEnum.ICON;
94 // return ArtifactTypeEnum.PUPPET;
96 // return ArtifactTypeEnum.YANG;
98 // return ArtifactTypeEnum.UNKNOWN;
103 // public ArrayList<String> getScriptList (List<UploadArtifactInfo>
106 // ArrayList<String> scriptNameArray = new ArrayList<>();
107 // if (artifactsList != null){
108 // for (UploadArtifactInfo fileInArtifactsList : artifactsList){
109 // String artifactFileName = fileInArtifactsList.getArtifactName();
110 // ArtifactTypeEnum artifactFileType =
111 // fileInArtifactsList.getArtifactType();
112 // if (! artifactFileType.equals(ArtifactTypeEnum.ICON)){
113 // scriptNameArray.add(artifactFileName);
117 // return scriptNameArray;
123 // public String getYamlFileLocation(File testResourcesPath) {
124 // File[] files = testResourcesPath.listFiles();
125 // if (files.length == 0){
128 // for (int i = 0; i < files.length; i++){
129 // if (files[i].isFile()){
130 // return files[i].getAbsoluteFile().toString();
137 // public String readFileContentToString (String fileName) throws
140 // Path path = Paths.get(fileName);
141 // String stringFromFile = new String(Files.readAllBytes(path));
142 // return stringFromFile;
147 @SuppressWarnings("unchecked")
148 public ToscaNodeTypeInfo parseToscaNodeYaml(String fileContent) {
150 ToscaNodeTypeInfo result = new ToscaNodeTypeInfo();
151 Object templateVersion = null;
152 Object templateName = null;
154 if (fileContent != null) {
155 Yaml yaml = new Yaml();
157 Map<String, Object> yamlObject = (Map<String, Object>) yaml.load(fileContent);
159 templateVersion = yamlObject.get(YamlConstants.TEMPLATE_VERSION);
160 if (templateVersion != null) {
161 result.setTemplateVersion(templateVersion.toString());
163 templateName = yamlObject.get(YamlConstants.TEMPLATE_NAME);
164 if (templateName != null) {
165 result.setTemplateName(templateName.toString());
167 Object nodeTypes = yamlObject.get(YamlConstants.NODE_TYPES);
169 if (nodeTypes != null) {
170 Map<String, Object> nodeTypesMap = (Map<String, Object>) nodeTypes;
171 for (Entry<String, Object> entry : nodeTypesMap.entrySet()) {
173 String nodeName = entry.getKey();
174 if (nodeName != null) {
175 result.setNodeName(nodeName);
190 // public ArtifactsMetadata getArtifactsMetadata(String response){
191 // ArtifactsMetadata artifactsMetadata = new ArtifactsMetadata();
193 // artifactsMetadata.setId(getJsonObjectValueByKey(response, "id"));
194 // artifactsMetadata.setName(getJsonObjectValueByKey(response, "name"));
195 // artifactsMetadata.setType(getJsonObjectValueByKey(response, "type"));
197 // artifactsMetadata.setCreator(getJsonObjectValueByKey(response,
199 // artifactsMetadata.setCreationTime(getJsonObjectValueByKey(response,
201 // artifactsMetadata.setLastUpdateTime(getJsonObjectValueByKey(response,
202 // "lastUpdateTime"));
203 // artifactsMetadata.setChecksum(getJsonObjectValueByKey(response,
205 // artifactsMetadata.setDescription(getJsonObjectValueByKey(response,
207 // artifactsMetadata.setLastUpdater(getJsonObjectValueByKey(response,
210 // return artifactsMetadata;
213 public static String getJsonObjectValueByKey(String metadata, String key) {
214 JsonElement jelement = new JsonParser().parse(metadata);
216 JsonObject jobject = jelement.getAsJsonObject();
217 Object obj = jobject.get(key);
222 value = (String) jobject.get(key).getAsString();
227 public static Config getConfig() throws FileNotFoundException {
228 Config config = Config.instance();
232 public static Config getConfigHandleException() {
233 Config config = null;
235 config = Config.instance();
236 }catch (Exception e){
237 System.out.println("Configuration file not found. " + e);
241 // public void uploadNormativeTypes() throws IOException{
242 // Config config = getConfig();
243 // String[] normativeTypes = {"root", "compute", "blockStorage",
244 // "softwareComponent", "DBMS", "database", "network", "objectStorage",
245 // "webServer", "webApplication"};
246 // for( String normativeType : normativeTypes ){
247 // uploadComponent(config.getComponentsConfigDir()+File.separator+"normativeTypes"+File.separator+normativeType);
252 // public void uploadApacheComponent() throws IOException{
253 // Config config = getConfig();
254 // uploadComponent(config.getComponentsConfigDir()+File.separator+"apache");
257 // public void uploadComponent(String componentDir) throws IOException{
259 // //*********************************************upload*************************************************************
260 // Config config = getConfig();
261 // ZipDirectory zipDirectory = new ZipDirectory();
262 // System.out.println(config.getEsHost());
264 // List<UploadArtifactInfo> artifactsList = new
265 // ArrayList<UploadArtifactInfo>();
267 //// read test resources and zip it as byte array
268 // byte[] zippedAsByteArray = zipDirectory.zip(componentDir, artifactsList);
270 //// encode zipped directory using base64
271 // String payload = Decoder.encode(zippedAsByteArray);
273 //// zip name build as testName with ".zip" extension
274 // String payloadZipName = getPayloadZipName(componentDir);
277 // UploadResourceInfo resourceInfo = new UploadResourceInfo(payload,
278 // payloadZipName, "description", "category/mycategory", null,
280 // String json = new Gson().toJson(resourceInfo);
282 //// calculate md5 on the content of json
284 // org.apache.commons.codec.digest.DigestUtils.md5Hex(json);
286 //// encode the md5 to base64, sent as header in post http request
287 // String encodedMd5 = Decoder.encode(jsonMd5.getBytes());
289 //// upload component to Elastic Search DB
290 // String url = null;
291 // HttpRequest http = new HttpRequest();
293 // url = String.format(Urls.UPLOAD_ZIP_URL, config.getCatalogFeHost(),
294 // config.getCatalogFePort());
296 //// Prepare headers to post upload component request
297 // HeaderData headerData = new HeaderData(encodedMd5, "application/json",
298 // "att", "test", "testIvanovich", "RoyalSeal", "Far_Far_Away",
299 // "getResourceArtifactListTest");
301 // MustHeaders headers = new MustHeaders(headerData);
302 // System.out.println("headers:"+headers.getMap());
304 // RestResponse response = http.httpSendPost(url, json, headers.getMap());
306 // assertEquals("upload component failed with code " +
307 // response.getErrorCode().intValue(),response.getErrorCode().intValue(),
311 // private String getPayloadZipName(String componentDir) {
312 // String payloadName;
313 // if( componentDir.contains( File.separator) ){
314 // String delimiter = null;
315 // if( File.separator.equals("\\")){
316 // delimiter ="\\\\";
319 // delimiter = File.separator;
321 // String[] split = componentDir.split(delimiter);
322 // payloadName = split[split.length-1];
325 // payloadName = componentDir;
327 // return payloadName+".zip";
332 // public List<UploadArtifactInfo> createArtifactsList(String srcDir) {
334 // List<UploadArtifactInfo> artifactsList = new
335 // ArrayList<UploadArtifactInfo>();
336 // File srcFile = new File(srcDir);
337 // addFileToList(srcFile, artifactsList);
339 // return artifactsList;
342 // public void addFileToList(File srcFile, List<UploadArtifactInfo>
345 // File[] files = srcFile.listFiles();
347 // for (int i = 0; i < files.length; i++) {
348 // // if the file is directory, use recursion
349 // if (files[i].isDirectory()) {
350 // addFileToList(files[i], artifactsList);
354 // String fileName = files[i].getName();
355 // String artifactPath = fileName;
357 // if ( ! files[i].getName().matches("(.*)\\.y(?)ml($)")) {
358 // UploadArtifactInfo uploadArtifactInfo = new UploadArtifactInfo();
359 // uploadArtifactInfo.setArtifactName(files[i].getName());
360 // String parent = files[i].getParent();
362 // if (parent != null) {
363 // System.out.println(parent);
364 // int lastSepartor = parent.lastIndexOf(File.separator);
365 // if (lastSepartor > -1) {
366 // String actualParent = parent.substring(lastSepartor + 1);
367 // artifactPath = actualParent + "/" + artifactPath;
371 // uploadArtifactInfo.setArtifactPath(artifactPath);
372 // uploadArtifactInfo.setArtifactType(getFileTypeByExtension(fileName));
373 // uploadArtifactInfo.setArtifactDescription("description");
374 // artifactsList.add(uploadArtifactInfo);
376 // System.out.println("artifact list: " + artifactsList);
384 // public String buildArtifactListUrl (String nodesType, String
385 // templateVersion, String artifactName) throws FileNotFoundException{
386 // //"http://172.20.43.132/sdc2/v1/catalog/resources/tosca.nodes.Root/1.0.0.wd03-SNAPSHOT/artifacts/wxs_baseline_compare.sh"
387 // Config config = getConfig();
388 // return "\"http://" + config.getCatalogBeHost() + ":" +
389 // config.getCatalogBePort() + "/sdc2/v1/catalog/resources/" +nodesType +
390 // "/" + templateVersion + "/artifacts/" + artifactName +"\"";
394 // public void addTopologyToES(String testFolder, String
395 // serviceTopologyPattern) throws IOException{
396 // Config config = getConfig();
397 // String url = String.format(Urls.ES_URL, config.getEsHost(),
398 // config.getEsPort()) + serviceTopologyPattern;
399 // String sourceDir =
400 // config.getResourceConfigDir()+File.separator+testFolder;
401 // Path filePath = FileSystems.getDefault().getPath(sourceDir,
403 // postFileContentsToUrl(url, filePath);
406 // public void addTopologyTemplateToES(String testFolder, String
407 // serviceTopologyTemplatePattern) throws IOException{
408 // Config config = getConfig();
409 // String url = String.format(Urls.ES_URL, config.getEsHost(),
410 // config.getEsPort()) + serviceTopologyTemplatePattern;
411 // String sourceDir =
412 // config.getResourceConfigDir()+File.separator+testFolder;
413 // Path filePath = FileSystems.getDefault().getPath(sourceDir,
414 // "topologyTemplate.txt");
415 // postFileContentsToUrl(url, filePath);
419 // public void postFileContentsToUrl(String url, Path filePath) throws
421 // HttpClientContext localContext = HttpClientContext.create();
422 // CloseableHttpResponse response = null;
424 // byte[] fileContent = Files.readAllBytes(filePath);
426 // try(CloseableHttpClient httpClient = HttpClients.createDefault()){
427 // HttpPost httpPost = new HttpPost(url);
428 // StringEntity entity = new StringEntity(new String(fileContent) ,
429 // ContentType.APPLICATION_JSON);
430 // httpPost.setEntity(entity);
431 // response = httpClient.execute(httpPost, localContext);
442 //// public boolean isPatternInEsDb(String patternToSearch)throws
444 //// Config config = getConfig();
445 //// String url = String.format(Urls.GET_SEARCH_DATA_FROM_ES,
446 // config.getEsHost(), config.getEsPort(),patternToSearch);
447 //// HttpRequest httpRequest = new HttpRequest();
448 //// RestResponse restResponse = httpRequest.httpSendGet(url);
449 //// if (restResponse.getErrorCode() == 200){
452 //// if (restResponse.getErrorCode() == 404){
459 // public static RestResponse deleteAllDataFromEs() throws IOException{
460 // return deleteFromEsDbByPattern("_all");
465 // public List<String> buildIdArrayListByTypesIndex (String index, String
466 // types) throws IOException{
468 // Config config = getConfig();
469 // HttpRequest http = new HttpRequest();
470 // RestResponse getResponce =
471 // http.httpSendGet(String.format(Urls.GET_ID_LIST_BY_INDEX_FROM_ES,
472 // config.getEsHost(), config.getEsPort(), index, types), null);
474 // List <String> idArray = new ArrayList<String>();
476 // JsonElement jelement = new JsonParser().parse(getResponce.getResponse());
477 // JsonObject jobject = jelement.getAsJsonObject();
478 // JsonObject hitsObject = (JsonObject) jobject.get("hits");
479 // JsonArray hitsArray = (JsonArray) hitsObject.get("hits");
480 // for (int i = 0; i < hitsArray.size(); i ++){
481 // JsonObject idObject = (JsonObject) hitsArray.get(i);
482 // String id = idObject.get("_id").toString();
483 // id = id.replace("\"", "");
490 // public List<String> buildCategoriesTagsListFromJson(String
491 // categoriesTagsJson){
493 // ArrayList<String> categoriesTagsArray = new ArrayList<>();
494 // JsonElement jelement = new JsonParser().parse(categoriesTagsJson);
495 // JsonArray jArray = jelement.getAsJsonArray();
496 // for (int i = 0; i < jArray.size(); i ++){
497 // JsonObject categoriesTagsObject = (JsonObject) jArray.get(i);
498 // String categories = categoriesTagsObject.get("name").toString();
499 // categoriesTagsArray.add(categories);
502 // return categoriesTagsArray;
505 // public ArrayList <String> getCategoriesFromDb() throws Exception{
507 // ArrayList<String> categoriesFromDbArrayList = new ArrayList<>();
508 // RestResponse restResponse = new RestResponse();
509 // String contentTypeHeaderData = "application/json";
510 // String acceptHeaderDate = "application/json";
512 // Map<String, String> headersMap = new HashMap<String,String>();
513 // headersMap.put(HttpHeaderEnum.CONTENT_TYPE.getValue(),contentTypeHeaderData);
514 // headersMap.put(HttpHeaderEnum.ACCEPT.getValue(), acceptHeaderDate);
516 // HttpRequest httpRequest = new HttpRequest();
517 // String url = String.format(Urls.QUERY_NEO4J,
518 // Config.instance().getNeoHost(), Config.instance().getNeoPort());
519 // String body = "{\"statements\" : [ { \"statement\" : \"MATCH
520 // (category:category) return (category)\"} ]}";
521 // restResponse = httpRequest.httpSendPostWithAuth(url, body, headersMap,
522 // Config.instance().getNeoDBusername(),
523 // Config.instance().getNeoDBpassword());
525 // if (restResponse.getResponse()==null){
526 // return categoriesFromDbArrayList;
528 // JsonElement jelement = new
529 // JsonParser().parse(restResponse.getResponse());
530 // JsonObject jobject = jelement.getAsJsonObject();
531 // JsonArray resultsArray = (JsonArray) jobject.get("results");
532 // JsonObject resObject = (JsonObject) resultsArray.get(0);
533 // JsonArray dataArray = (JsonArray) resObject.get("data");
534 // for (int i = 0; i < dataArray.size(); i ++){
535 // JsonObject rowObject = (JsonObject) dataArray.get(i);
536 // JsonArray rowArray = (JsonArray) rowObject.get("row");
537 // JsonObject nameObject = (JsonObject) rowArray.get(0);
538 // String name = nameObject.get("name").toString();
539 //// name = name.replace("\"", "");
540 // categoriesFromDbArrayList.add(name);
546 // return categoriesFromDbArrayList;
549 public static void compareArrayLists(List<String> actualArraylList, List<String> expectedArrayList,
552 ArrayList<String> actual = new ArrayList<String>(actualArraylList);
553 ArrayList<String> expected = new ArrayList<String>(expectedArrayList);
554 // assertEquals(message + " count got by rest API not match to " +
555 // message + " expected count", expected.size(),actual.size());
556 expected.removeAll(actual);
557 assertEquals(message + " content got by rest API not match to " + message + " actual content", 0,
561 public static Object parseYamlConfig(String pattern) throws FileNotFoundException {
563 Yaml yaml = new Yaml();
564 Config config = getConfig();
565 String configurationFile = config.getConfigurationFile();
566 File file = new File(configurationFile);
568 // File("../catalog-be/src/main/resources/config/configuration.yaml");
569 InputStream inputStream = new FileInputStream(file);
570 Map<?, ?> map = (Map<?, ?>) yaml.load(inputStream);
571 Object patternMap = (Object) map.get(pattern);
576 public static String getDepArtLabelFromConfig(ArtifactTypeEnum artifactTypeEnum) throws FileNotFoundException {
578 @SuppressWarnings("unchecked")
579 Map<String, Object> mapOfDepResArtTypesObjects = (Map<String, Object>) parseYamlConfig(
580 "deploymentResourceArtifacts");
581 for (Map.Entry<String, Object> iter : mapOfDepResArtTypesObjects.entrySet()) {
582 if (iter.getValue().toString().contains(artifactTypeEnum.getType())) {
583 return iter.getKey().toLowerCase();
587 return "defaultLabelName";
591 public static String multipleChar(String ch, int repeat) {
592 return StringUtils.repeat(ch, repeat);
595 public static List<String> getListOfDepResArtLabels(Boolean isLowerCase) throws FileNotFoundException {
597 List<String> listOfResDepArtTypesFromConfig = new ArrayList<String>();
598 @SuppressWarnings("unchecked")
599 Map<String, Object> resourceDeploymentArtifacts = (Map<String, Object>) parseYamlConfig(
600 "deploymentResourceArtifacts");
601 if (resourceDeploymentArtifacts != null) {
604 for (Map.Entry<String, Object> iter : resourceDeploymentArtifacts.entrySet()) {
605 listOfResDepArtTypesFromConfig.add(iter.getKey().toLowerCase());
609 for (Map.Entry<String, Object> iter : resourceDeploymentArtifacts.entrySet()) {
610 listOfResDepArtTypesFromConfig.add(iter.getKey());
614 return listOfResDepArtTypesFromConfig;
617 public static List<String> getListOfToscaArtLabels(Boolean isLowerCase) throws FileNotFoundException {
619 List<String> listOfToscaArtTypesFromConfig = new ArrayList<String>();
620 @SuppressWarnings("unchecked")
621 Map<String, Object> toscaArtifacts = (Map<String, Object>) parseYamlConfig("toscaArtifacts");
622 if (toscaArtifacts != null) {
625 for (Map.Entry<String, Object> iter : toscaArtifacts.entrySet()) {
626 listOfToscaArtTypesFromConfig.add(iter.getKey().toLowerCase());
629 for (Map.Entry<String, Object> iter : toscaArtifacts.entrySet()) {
630 listOfToscaArtTypesFromConfig.add(iter.getKey());
634 return listOfToscaArtTypesFromConfig;
638 // public static List<String> getListOfResDepArtTypes() throws
639 // FileNotFoundException {
641 // List<String> listOfResDepArtTypesFromConfig = new ArrayList<String>();
642 // @SuppressWarnings("unchecked")
643 // Map<String, Object> resourceDeploymentArtifacts = (Map<String, Object>)
644 // parseYamlConfig("resourceDeploymentArtifacts");
645 // for (Map.Entry<String, Object> iter :
646 // resourceDeploymentArtifacts.entrySet()) {
647 // listOfResDepArtTypesFromConfig.add(iter.getKey());
650 // return listOfResDepArtTypesFromConfig;
653 // public static List<String> getListOfDepResInstArtTypes() throws
654 // FileNotFoundException {
656 // List<String> listOfResInstDepArtTypesFromConfig = new
657 // ArrayList<String>();
658 // @SuppressWarnings("unchecked")
659 // Map<String, Object> resourceDeploymentArtifacts = (Map<String, Object>)
660 // parseYamlConfig("deploymentResourceInstanceArtifacts");
661 // for (Map.Entry<String, Object> iter :
662 // resourceDeploymentArtifacts.entrySet()) {
663 // listOfResInstDepArtTypesFromConfig.add(iter.getKey().toLowerCase());
666 // return listOfResInstDepArtTypesFromConfig;
669 public static List<String> getListOfResPlaceHoldersDepArtTypes() throws FileNotFoundException {
670 List<String> listResDepArtTypesFromConfig = new ArrayList<String>();
671 List<String> listOfResDepArtLabelsFromConfig = getListOfDepResArtLabels(false);
672 assertNotNull("deployment artifact types list is null", listOfResDepArtLabelsFromConfig);
673 Object parseYamlConfig = Utils.parseYamlConfig("deploymentResourceArtifacts");
674 Map<String, Object> mapOfDepResArtTypesObjects = (Map<String, Object>) Utils
675 .parseYamlConfig("deploymentResourceArtifacts");
677 // assertNotNull("deployment artifact types list is null",
678 // mapOfDepResArtTypesObjects);
679 if (listOfResDepArtLabelsFromConfig != null) {
680 for (String resDepArtType : listOfResDepArtLabelsFromConfig) {
681 Object object = mapOfDepResArtTypesObjects.get(resDepArtType);
682 if (object instanceof Map<?, ?>) {
683 Map<String, Object> map = (Map<String, Object>) object;
684 listResDepArtTypesFromConfig.add((String) map.get("type"));
686 assertTrue("return object does not instance of map", false);
690 return listResDepArtTypesFromConfig;
693 public static Long getEpochTimeFromUTC(String time) throws ParseException {
694 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS zzz");
695 java.util.Date date = df.parse(time);
696 long epoch = date.getTime();
700 public static Long getActionDuration(Runnable func) throws Exception{
701 long startTime = System.nanoTime();
703 long estimateTime = System.nanoTime();
704 long duration = TimeUnit.NANOSECONDS.toSeconds(estimateTime - startTime);