diff options
Diffstat (limited to 'common-app-api/src/test/java/org/openecomp/sdc/common/util/GeneralUtilityTest.java')
-rw-r--r-- | common-app-api/src/test/java/org/openecomp/sdc/common/util/GeneralUtilityTest.java | 258 |
1 files changed, 182 insertions, 76 deletions
diff --git a/common-app-api/src/test/java/org/openecomp/sdc/common/util/GeneralUtilityTest.java b/common-app-api/src/test/java/org/openecomp/sdc/common/util/GeneralUtilityTest.java index daeb0c94d2..b5036060eb 100644 --- a/common-app-api/src/test/java/org/openecomp/sdc/common/util/GeneralUtilityTest.java +++ b/common-app-api/src/test/java/org/openecomp/sdc/common/util/GeneralUtilityTest.java @@ -20,125 +20,231 @@ package org.openecomp.sdc.common.util; -import java.util.List; -import org.junit.Assert; +import com.google.common.collect.Lists; +import org.apache.commons.io.FileUtils; import org.junit.Test; +import java.io.File; +import java.io.IOException; +import java.util.Base64; +import java.util.List; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; public class GeneralUtilityTest { - private GeneralUtility createTestSubject() { - return new GeneralUtility(); + @Test + public void validateGenerateTextFileReturnsTrueIfGeneratesTextFile() throws IOException { + + final String fileName = "test.txt"; + final String fileData = "test data"; + final File expectedFile = new File(fileName); + + boolean result = GeneralUtility.generateTextFile(fileName, fileData); + + String createdFileData = FileUtils.readFileToString(expectedFile); + + assertTrue(result); + assertEquals(createdFileData ,fileData); + + FileUtils.forceDelete(expectedFile); + } + + @Test + public void validateIsBase64EncodedReturnsProperResponseFromByteArray() { + + final String testString = "testDataToEncode"; + final byte[] testBytes = testString.getBytes(); + final byte[] testEncodedBytes = Base64.getEncoder().encode(testBytes); + + boolean result = GeneralUtility.isBase64Encoded(testEncodedBytes); + + assertTrue(result); + } + + @Test + public void validateIsBase64EncodedReturnsProperResponseFromString() { + + final String testString = "testDataToEncode"; + final byte[] testBytes = testString.getBytes(); + final byte[] testEncodedBytes = Base64.getEncoder().encode(testBytes); + final String testEncodedString = new String(testEncodedBytes); + + boolean result = GeneralUtility.isBase64Encoded(testEncodedString); + + assertTrue(result); + } + + @Test + public void validateIsExceedingLimitReturnsFalseIfStringIsShorterThenLimit() { + + final String testString = "test"; + final int limit = 5; + + boolean result = GeneralUtility.isExceedingLimit(testString, limit); + + assertFalse(result); + } + + @Test + public void validateIsExceedingLimitReturnsFalseIfStringIsNull() { + + final String testString = null; + final int limit = 5; + + boolean result = GeneralUtility.isExceedingLimit(testString, limit); + + assertFalse(result); + } + + @Test + public void validateIsExceedingLimitReturnsFalseIfStringLengthIsEqualToLimit() { + + final String testString = "test"; + final int limit = 4; + + boolean result = GeneralUtility.isExceedingLimit(testString, limit); + + assertFalse(result); + } + + @Test + public void validateIsExceedingLimitReturnsTrueIfStringExceedsLimit() { + + final String testString = "test"; + final int limit = 3; + + boolean result = GeneralUtility.isExceedingLimit(testString, limit); + + assertTrue(result); + } + + @Test + public void validateIsExceedingLimitWithDelimiterReturnsFalseIfSumOfAllElementsLengthAndDelimiterLengthIsSmallerThenLimit() { + + final List<String> testString = Lists.newArrayList("testing","list"); + final int limit = 15; + final int delimiterLength = 2; + + boolean result = GeneralUtility.isExceedingLimit(testString, limit, delimiterLength); + + assertFalse(result); + } + + @Test + public void validateIsExceedingLimitWithDelimiterReturnsFalseIfListIsNull() { + + final List<String> testString = null; + final int limit = 15; + final int delimiterLength = 2; + + boolean result = GeneralUtility.isExceedingLimit(testString, limit, delimiterLength); + + assertFalse(result); } - @Test - public void testGenerateTextFile() throws Exception { - String fileName = ""; - String fileData = ""; - boolean result; + public void validateIsExceedingLimitWithDelimiterReturnsFalseIfSumOfAllElementsLengthAndDelimiterLengthIsEqualThenLimit() { - // default test - result = GeneralUtility.generateTextFile(fileName, fileData); + final List<String> testString = Lists.newArrayList("testing","list","equal"); + final int limit = 18; + final int delimiterLength = 1; + + boolean result = GeneralUtility.isExceedingLimit(testString, limit, delimiterLength); + + assertFalse(result); } - @Test - public void testIsBase64Encoded() throws Exception { - byte[] data = new byte[] { ' ' }; - boolean result; + public void validateIsExceedingLimitWithDelimiterReturnsTrueIfSumOfAllElementsLengthAndDelimiterLengthIsBiggerThenLimit() { + + final List<String> testString = Lists.newArrayList("long","testing","list","of","strings"); + final int limit = 20; + final int delimiterLength = 2; - // default test - result = GeneralUtility.isBase64Encoded(data); + boolean result = GeneralUtility.isExceedingLimit(testString, limit, delimiterLength); + + assertTrue(result); } - @Test - public void testIsBase64Encoded_1() throws Exception { - String str = ""; - boolean result; + public void validateGetFilenameExtensionReturnsProperExtension() { + + final String testFile = "test.yaml"; - // default test - result = GeneralUtility.isBase64Encoded(str); + String extension = GeneralUtility.getFilenameExtension(testFile); + + assertEquals(extension, "yaml"); } - @Test - public void testIsExceedingLimit() throws Exception { - String str = ""; - int limit = 0; - boolean result; + public void validateCalculateMD5Base64EncodedByByteArrayReturnsCorrectString() { + + final String testStringToEncode = "testString"; + + String result = GeneralUtility.calculateMD5Base64EncodedByByteArray(testStringToEncode.getBytes()); - // test 1 - str = null; - result = GeneralUtility.isExceedingLimit(str, limit); - Assert.assertEquals(false, result); + final String encodedString = + org.apache.commons.codec.digest.DigestUtils.md5Hex(testStringToEncode.getBytes()); - // test 2 - str = ""; - result = GeneralUtility.isExceedingLimit(str, limit); - Assert.assertEquals(false, result); + assertArrayEquals(encodedString.getBytes(), Base64.getDecoder().decode(result)); } - @Test - public void testIsExceedingLimit_1() throws Exception { - List<String> strList = null; - int limit = 0; - int delimiterLength = 0; - boolean result; + public void validateCalculateMD5Base64EncodedByStringReturnsCorrectString() { + + final String testStringToEncode = "testString"; + + String result = GeneralUtility.calculateMD5Base64EncodedByString(testStringToEncode); - // test 1 - strList = null; - result = GeneralUtility.isExceedingLimit(strList, limit, delimiterLength); - Assert.assertEquals(false, result); + final String encodedString = + org.apache.commons.codec.digest.DigestUtils.md5Hex(testStringToEncode.getBytes()); + + assertArrayEquals(encodedString.getBytes(), Base64.getDecoder().decode(result)); } - @Test - public void testGetFilenameExtension() throws Exception { - String fileName = ""; - String result; + public void validateIsEmptyStringReturnTrueIfStringIsEmpty() { + + final String empty = ""; - // test 1 - fileName = null; - result = GeneralUtility.getFilenameExtension(fileName); - Assert.assertEquals("", result); + boolean result = GeneralUtility.isEmptyString(empty); - // test 2 - fileName = ""; - result = GeneralUtility.getFilenameExtension(fileName); - Assert.assertEquals("", result); + assertTrue(result); } - @Test - public void testCalculateMD5Base64EncodedByByteArray() throws Exception { - byte[] payload = new byte[] { ' ' }; - String result; + public void validateIsEmptyStringReturnTrueIfStringIsContainingOnlyWightSpaces() { + + final String empty = " \t "; - // default test - result = GeneralUtility.calculateMD5Base64EncodedByByteArray(payload); + boolean result = GeneralUtility.isEmptyString(empty); + + assertTrue(result); } - @Test - public void testCalculateMD5Base64EncodedByString() throws Exception { - String data = ""; - String result; + public void validateIsEmptyStringReturnFalseIfStringIsNotEmpty() { + + final String empty = "test"; - // default test - result = GeneralUtility.calculateMD5Base64EncodedByString(data); + boolean result = GeneralUtility.isEmptyString(empty); + + assertFalse(result); } - @Test - public void testIsEmptyString() throws Exception { - String str = ""; - boolean result; + public void validateIsEmptyStringReturnFalseIfStringIsNotEmptyAndSurroundedWithWightSpaces() { + + final String empty = " \ttest "; - // default test - result = GeneralUtility.isEmptyString(str); + boolean result = GeneralUtility.isEmptyString(empty); + + assertFalse(result); } + } |