2 * ============LICENSE_START=======================================================
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
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.common.util;
24 import com.google.common.collect.Lists;
25 import org.apache.commons.io.FileUtils;
26 import org.junit.Test;
29 import java.io.IOException;
30 import java.util.Base64;
31 import java.util.List;
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;
38 public class GeneralUtilityTest {
41 public void validateGenerateTextFileReturnsTrueIfGeneratesTextFile() throws IOException {
43 final String fileName = "test.txt";
44 final String fileData = "test data";
45 final File expectedFile = new File(fileName);
47 boolean result = GeneralUtility.generateTextFile(fileName, fileData);
49 String createdFileData = FileUtils.readFileToString(expectedFile);
52 assertEquals(createdFileData ,fileData);
54 FileUtils.forceDelete(expectedFile);
58 public void validateIsBase64EncodedReturnsProperResponseFromByteArray() {
60 final String testString = "testDataToEncode";
61 final byte[] testBytes = testString.getBytes();
62 final byte[] testEncodedBytes = Base64.getEncoder().encode(testBytes);
64 boolean result = GeneralUtility.isBase64Encoded(testEncodedBytes);
70 public void validateIsBase64EncodedReturnsProperResponseFromString() {
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);
77 boolean result = GeneralUtility.isBase64Encoded(testEncodedString);
83 public void validateIsExceedingLimitReturnsFalseIfStringIsShorterThenLimit() {
85 final String testString = "test";
88 boolean result = GeneralUtility.isExceedingLimit(testString, limit);
94 public void validateIsExceedingLimitReturnsFalseIfStringIsNull() {
96 final String testString = null;
99 boolean result = GeneralUtility.isExceedingLimit(testString, limit);
105 public void validateIsExceedingLimitReturnsFalseIfStringLengthIsEqualToLimit() {
107 final String testString = "test";
110 boolean result = GeneralUtility.isExceedingLimit(testString, limit);
116 public void validateIsExceedingLimitReturnsTrueIfStringExceedsLimit() {
118 final String testString = "test";
121 boolean result = GeneralUtility.isExceedingLimit(testString, limit);
127 public void validateIsExceedingLimitWithDelimiterReturnsFalseIfSumOfAllElementsLengthAndDelimiterLengthIsSmallerThenLimit() {
129 final List<String> testString = Lists.newArrayList("testing","list");
130 final int limit = 15;
131 final int delimiterLength = 2;
133 boolean result = GeneralUtility.isExceedingLimit(testString, limit, delimiterLength);
139 public void validateIsExceedingLimitWithDelimiterReturnsFalseIfListIsNull() {
141 final List<String> testString = null;
142 final int limit = 15;
143 final int delimiterLength = 2;
145 boolean result = GeneralUtility.isExceedingLimit(testString, limit, delimiterLength);
151 public void validateIsExceedingLimitWithDelimiterReturnsFalseIfSumOfAllElementsLengthAndDelimiterLengthIsEqualThenLimit() {
153 final List<String> testString = Lists.newArrayList("testing","list","equal");
154 final int limit = 18;
155 final int delimiterLength = 1;
157 boolean result = GeneralUtility.isExceedingLimit(testString, limit, delimiterLength);
163 public void validateIsExceedingLimitWithDelimiterReturnsTrueIfSumOfAllElementsLengthAndDelimiterLengthIsBiggerThenLimit() {
165 final List<String> testString = Lists.newArrayList("long","testing","list","of","strings");
166 final int limit = 20;
167 final int delimiterLength = 2;
169 boolean result = GeneralUtility.isExceedingLimit(testString, limit, delimiterLength);
175 public void validateGetFilenameExtensionReturnsProperExtension() {
177 final String testFile = "test.yaml";
179 String extension = GeneralUtility.getFilenameExtension(testFile);
181 assertEquals(extension, "yaml");
185 public void validateCalculateMD5Base64EncodedByByteArrayReturnsCorrectString() {
187 final String testStringToEncode = "testString";
189 String result = GeneralUtility.calculateMD5Base64EncodedByByteArray(testStringToEncode.getBytes());
191 final String encodedString =
192 org.apache.commons.codec.digest.DigestUtils.md5Hex(testStringToEncode.getBytes());
194 assertArrayEquals(encodedString.getBytes(), Base64.getDecoder().decode(result));
198 public void validateCalculateMD5Base64EncodedByStringReturnsCorrectString() {
200 final String testStringToEncode = "testString";
202 String result = GeneralUtility.calculateMD5Base64EncodedByString(testStringToEncode);
204 final String encodedString =
205 org.apache.commons.codec.digest.DigestUtils.md5Hex(testStringToEncode.getBytes());
207 assertArrayEquals(encodedString.getBytes(), Base64.getDecoder().decode(result));
211 public void validateIsEmptyStringReturnTrueIfStringIsEmpty() {
213 final String empty = "";
215 boolean result = GeneralUtility.isEmptyString(empty);
221 public void validateIsEmptyStringReturnTrueIfStringIsContainingOnlyWightSpaces() {
223 final String empty = " \t ";
225 boolean result = GeneralUtility.isEmptyString(empty);
231 public void validateIsEmptyStringReturnFalseIfStringIsNotEmpty() {
233 final String empty = "test";
235 boolean result = GeneralUtility.isEmptyString(empty);
241 public void validateIsEmptyStringReturnFalseIfStringIsNotEmptyAndSurroundedWithWightSpaces() {
243 final String empty = " \ttest ";
245 boolean result = GeneralUtility.isEmptyString(empty);