Merge automation from ECOMP's repository
[vid.git] / vid-automation / src / main / java / org / onap / sdc / ci / tests / utilities / GeneralUtility.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.sdc.ci.tests.utilities;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.List;
26 import java.util.regex.Pattern;
27
28 import org.apache.commons.codec.binary.Base64;
29 import org.apache.commons.io.FileUtils;
30
31 public class GeneralUtility {
32
33         public static boolean generateTextFile(String fileName, String fileData) {
34                 boolean isSuccessfull = true;
35                 try {
36                         FileUtils.writeStringToFile(new File(fileName), fileData);
37                 } catch (IOException e) {
38                         isSuccessfull = false;
39                 }
40                 return isSuccessfull;
41         }
42         
43         /**
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.
47          */
48         public static boolean isBase64Encoded(byte[] data) {
49                 return Base64.isBase64(data);
50         }
51         
52         /**
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.
56          */
57         public static boolean isBase64Encoded(String str) {
58                 boolean isEncoded = false;
59                 try {
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)));
62                         if (isEncoded) {
63                                 // If no exception is caught, then it is possibly a base64
64                                 // encoded string
65                                 byte[] data = Base64.decodeBase64(str);
66                         }
67
68                 } catch (Exception e) {
69                         // If exception is caught, then it is not a base64 encoded string
70                         isEncoded = false;
71                 }
72                 return isEncoded;
73         }
74
75         /**
76          * Checks whether the passed string exceeds a limit of number of characters.
77          * 
78          * @param str
79          * @param limit
80          * @return the result of comparison, or false if str is null.
81          */
82         public static boolean isExceedingLimit(String str, int limit) {
83                 if (str == null) {
84                         return false;
85                 }
86                 return str.length() > limit;
87         }
88
89         /**
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
91          * 3+1+3+1+5=13
92          *
93          * @param strList
94          * @param limit
95          * @param delimiterLength
96          *            - 0 if there is no delimeter.
97          * @return the result of comparison, or false if strList is null.
98          */
99         public static boolean isExceedingLimit(List<String> strList, int limit, int delimiterLength) {
100                 if (strList == null || strList.isEmpty()) {
101                         return false;
102                 }
103                 int sum = 0;
104                 int size = strList.size();
105                 for (int i = 0; i < size - 1; i++) {
106                         String str = strList.get(i);
107                         if (str != null) {
108                                 sum += str.length();
109                         }
110                         sum += delimiterLength;
111                 }
112                 String str = strList.get(size - 1);
113                 if (str != null) {
114                         sum += str.length();
115                 }
116                 return sum > limit;
117         }
118
119         /**
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.
121          * 
122          * @param fileName
123          * @return extension
124          */
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);
131                         }
132                 }
133                 return res;
134         }
135
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);
140
141         }
142
143         /**
144          * 
145          * @param data
146          * @return
147          */
148         public static String calculateMD5ByString(String data) {
149                 String calculatedMd5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(data);
150
151                 // encode base-64 result
152                 byte[] encodeBase64 = Base64.encodeBase64(calculatedMd5.getBytes());
153                 return new String(encodeBase64);
154         }
155 }