refactoring tests in Common-App-Api util
[sdc.git] / common-app-api / src / test / java / org / openecomp / sdc / common / util / GeneralUtilityTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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.openecomp.sdc.common.util;
22
23
24 import com.google.common.collect.Lists;
25 import org.apache.commons.io.FileUtils;
26 import org.junit.Test;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.util.Base64;
31 import java.util.List;
32
33 import static org.junit.Assert.assertArrayEquals;
34 import static org.junit.Assert.assertEquals;
35 import static org.junit.Assert.assertFalse;
36 import static org.junit.Assert.assertTrue;
37
38 public class GeneralUtilityTest {
39
40         @Test
41         public void validateGenerateTextFileReturnsTrueIfGeneratesTextFile() throws IOException {
42
43                 final String fileName = "test.txt";
44                 final String fileData = "test data";
45                 final File expectedFile = new File(fileName);
46
47                 boolean result = GeneralUtility.generateTextFile(fileName, fileData);
48
49                 String createdFileData = FileUtils.readFileToString(expectedFile);
50
51                 assertTrue(result);
52                 assertEquals(createdFileData ,fileData);
53
54                 FileUtils.forceDelete(expectedFile);
55         }
56
57         @Test
58         public void validateIsBase64EncodedReturnsProperResponseFromByteArray() {
59
60                 final String testString = "testDataToEncode";
61                 final byte[] testBytes = testString.getBytes();
62                 final byte[] testEncodedBytes = Base64.getEncoder().encode(testBytes);
63
64                 boolean result = GeneralUtility.isBase64Encoded(testEncodedBytes);
65
66                 assertTrue(result);
67         }
68
69         @Test
70         public void validateIsBase64EncodedReturnsProperResponseFromString() {
71
72                 final String testString = "testDataToEncode";
73                 final byte[] testBytes = testString.getBytes();
74                 final byte[] testEncodedBytes = Base64.getEncoder().encode(testBytes);
75                 final String testEncodedString = new String(testEncodedBytes);
76
77                 boolean result = GeneralUtility.isBase64Encoded(testEncodedString);
78
79                 assertTrue(result);
80         }
81
82         @Test
83         public void validateIsExceedingLimitReturnsFalseIfStringIsShorterThenLimit() {
84
85                 final String testString = "test";
86                 final int limit = 5;
87
88                 boolean result = GeneralUtility.isExceedingLimit(testString, limit);
89
90                 assertFalse(result);
91         }
92
93         @Test
94         public void validateIsExceedingLimitReturnsFalseIfStringIsNull() {
95
96                 final String testString = null;
97                 final int limit = 5;
98
99                 boolean result = GeneralUtility.isExceedingLimit(testString, limit);
100
101                 assertFalse(result);
102         }
103
104         @Test
105         public void validateIsExceedingLimitReturnsFalseIfStringLengthIsEqualToLimit() {
106
107                 final String testString = "test";
108                 final int limit = 4;
109
110                 boolean result = GeneralUtility.isExceedingLimit(testString, limit);
111
112                 assertFalse(result);
113         }
114
115         @Test
116         public void validateIsExceedingLimitReturnsTrueIfStringExceedsLimit() {
117
118                 final String testString = "test";
119                 final int limit = 3;
120
121                 boolean result = GeneralUtility.isExceedingLimit(testString, limit);
122
123                 assertTrue(result);
124         }
125
126         @Test
127         public void validateIsExceedingLimitWithDelimiterReturnsFalseIfSumOfAllElementsLengthAndDelimiterLengthIsSmallerThenLimit() {
128
129                 final List<String> testString = Lists.newArrayList("testing","list");
130                 final int limit = 15;
131                 final int delimiterLength = 2;
132
133                 boolean result = GeneralUtility.isExceedingLimit(testString, limit, delimiterLength);
134
135                 assertFalse(result);
136         }
137
138         @Test
139         public void validateIsExceedingLimitWithDelimiterReturnsFalseIfListIsNull() {
140
141                 final List<String> testString = null;
142                 final int limit = 15;
143                 final int delimiterLength = 2;
144
145                 boolean result = GeneralUtility.isExceedingLimit(testString, limit, delimiterLength);
146
147                 assertFalse(result);
148         }
149
150         @Test
151         public void validateIsExceedingLimitWithDelimiterReturnsFalseIfSumOfAllElementsLengthAndDelimiterLengthIsEqualThenLimit() {
152
153                 final List<String> testString = Lists.newArrayList("testing","list","equal");
154                 final int limit = 18;
155                 final int delimiterLength = 1;
156
157                 boolean result = GeneralUtility.isExceedingLimit(testString, limit, delimiterLength);
158
159                 assertFalse(result);
160         }
161
162         @Test
163         public void validateIsExceedingLimitWithDelimiterReturnsTrueIfSumOfAllElementsLengthAndDelimiterLengthIsBiggerThenLimit() {
164
165                 final List<String> testString = Lists.newArrayList("long","testing","list","of","strings");
166                 final int limit = 20;
167                 final int delimiterLength = 2;
168
169                 boolean result = GeneralUtility.isExceedingLimit(testString, limit, delimiterLength);
170
171                 assertTrue(result);
172         }
173
174         @Test
175         public void validateGetFilenameExtensionReturnsProperExtension() {
176
177                 final String testFile = "test.yaml";
178
179                 String extension = GeneralUtility.getFilenameExtension(testFile);
180
181                 assertEquals(extension, "yaml");
182         }
183
184         @Test
185         public void validateCalculateMD5Base64EncodedByByteArrayReturnsCorrectString() {
186
187                 final String testStringToEncode = "testString";
188
189                 String result = GeneralUtility.calculateMD5Base64EncodedByByteArray(testStringToEncode.getBytes());
190
191                 final String encodedString =
192                                 org.apache.commons.codec.digest.DigestUtils.md5Hex(testStringToEncode.getBytes());
193
194                 assertArrayEquals(encodedString.getBytes(), Base64.getDecoder().decode(result));
195         }
196
197         @Test
198         public void validateCalculateMD5Base64EncodedByStringReturnsCorrectString() {
199
200                 final String testStringToEncode = "testString";
201
202                 String result = GeneralUtility.calculateMD5Base64EncodedByString(testStringToEncode);
203
204                 final String encodedString =
205                                 org.apache.commons.codec.digest.DigestUtils.md5Hex(testStringToEncode.getBytes());
206
207                 assertArrayEquals(encodedString.getBytes(), Base64.getDecoder().decode(result));
208         }
209
210         @Test
211         public void validateIsEmptyStringReturnTrueIfStringIsEmpty() {
212
213                 final String empty = "";
214
215                 boolean result = GeneralUtility.isEmptyString(empty);
216
217                 assertTrue(result);
218         }
219
220         @Test
221         public void validateIsEmptyStringReturnTrueIfStringIsContainingOnlyWightSpaces() {
222
223                 final String empty = "  \t ";
224
225                 boolean result = GeneralUtility.isEmptyString(empty);
226
227                 assertTrue(result);
228         }
229
230         @Test
231         public void validateIsEmptyStringReturnFalseIfStringIsNotEmpty() {
232
233                 final String empty = "test";
234
235                 boolean result = GeneralUtility.isEmptyString(empty);
236
237                 assertFalse(result);
238         }
239
240         @Test
241         public void validateIsEmptyStringReturnFalseIfStringIsNotEmptyAndSurroundedWithWightSpaces() {
242
243                 final String empty = " \ttest  ";
244
245                 boolean result = GeneralUtility.isEmptyString(empty);
246
247                 assertFalse(result);
248         }
249
250 }