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.onap.sdc.ci.tests.utilities;
24 import java.io.IOException;
25 import java.util.List;
26 import java.util.regex.Pattern;
28 import org.apache.commons.codec.binary.Base64;
29 import org.apache.commons.io.FileUtils;
31 public class GeneralUtility {
33 public static boolean generateTextFile(String fileName, String fileData) {
34 boolean isSuccessfull = true;
36 FileUtils.writeStringToFile(new File(fileName), fileData);
37 } catch (IOException e) {
38 isSuccessfull = false;
44 * Use with care, usage is not advised!!!
45 * The method only checks if String does not contain special characters + length divided by 4 with no remainder.
46 * The methods contained in other common libraries do the same.
48 public static boolean isBase64Encoded(byte[] data) {
49 return Base64.isBase64(data);
53 *Use with care, usage is not advised!!!
54 * The method only checks if String does not contain special characters + length divided by 4 with no remainder.
55 * The methods contained in other common libraries do the same.
57 public static boolean isBase64Encoded(String str) {
58 boolean isEncoded = false;
60 // checks if the string was properly padded to the
61 isEncoded = ((str.length() % 4 == 0) && (Pattern.matches("\\A[a-zA-Z0-9/+]+={0,2}\\z", str)));
63 // If no exception is caught, then it is possibly a base64
65 byte[] data = Base64.decodeBase64(str);
68 } catch (Exception e) {
69 // If exception is caught, then it is not a base64 encoded string
76 * Checks whether the passed string exceeds a limit of number of characters.
80 * @return the result of comparison, or false if str is null.
82 public static boolean isExceedingLimit(String str, int limit) {
86 return str.length() > limit;
90 * Checks the passed string list whether the cumulative length of strings and delimiters between them exceeds a limit of number of characters. For example for list ("one","two","three") with delimiter "," the length of list is calculated
95 * @param delimiterLength
96 * - 0 if there is no delimeter.
97 * @return the result of comparison, or false if strList is null.
99 public static boolean isExceedingLimit(List<String> strList, int limit, int delimiterLength) {
100 if (strList == null || strList.isEmpty()) {
104 int size = strList.size();
105 for (int i = 0; i < size - 1; i++) {
106 String str = strList.get(i);
110 sum += delimiterLength;
112 String str = strList.get(size - 1);
120 * Return the extension as the substring from the last dot. For input "kuku.txt", "txt" will be returned. If no dot is found or input is null, empty string is returned.
125 public static String getFilenameExtension(String fileName) {
126 String res = Constants.EMPTY_STRING;
127 if (fileName != null) {
128 int indexOf = fileName.lastIndexOf('.');
129 if (indexOf != -1 && indexOf < (fileName.length() - 1)) {
130 res = fileName.substring(indexOf + 1);
136 public static String calculateMD5ByByteArray(byte[] payload) {
137 String decodedMd5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(payload);
138 byte[] encodeMd5 = Base64.encodeBase64(decodedMd5.getBytes());
139 return new String(encodeMd5);
148 public static String calculateMD5ByString(String data) {
149 String calculatedMd5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(data);
151 // encode base-64 result
152 byte[] encodeBase64 = Base64.encodeBase64(calculatedMd5.getBytes());
153 return new String(encodeBase64);