From c58f445fcef56a6440f3b21d54b390700623c48c Mon Sep 17 00:00:00 2001 From: siddharth0905 Date: Tue, 21 Aug 2018 17:55:30 +0530 Subject: Test coverage Increase test coverage Change-Id: I43e3149d0d4b07dc8e260f3d5fac025197882f3a Issue-ID: SDC-1673 Signed-off-by: siddharth0905 --- .../utilities/file/FileContentHandlerTest.java | 75 ++++++++++++-- .../core/utilities/file/FileUtilsTest.java | 113 +++++++++++++++++++-- 2 files changed, 174 insertions(+), 14 deletions(-) (limited to 'openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file') diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileContentHandlerTest.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileContentHandlerTest.java index 57d69b1b68..1b06f37a16 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileContentHandlerTest.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileContentHandlerTest.java @@ -1,13 +1,20 @@ package org.openecomp.core.utilities.file; -import org.testng.Assert; -import org.testng.annotations.Test; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; +import java.io.ByteArrayInputStream; import java.io.IOException; +import java.util.AbstractMap; import java.util.Arrays; +import java.util.Map; import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; -import static org.testng.Assert.*; +import org.testng.Assert; +import org.testng.annotations.Test; /** * @author EVITALIY @@ -18,7 +25,7 @@ public class FileContentHandlerTest { private static final String FILE_NAME = "test-file.txt"; @Test - public void testProcessFileContent() throws Exception { + public void testProcessFileContent() { final int size = 13; FileContentHandler contentHandler = new FileContentHandler(); @@ -42,15 +49,71 @@ public class FileContentHandlerTest { } @Test - public void testProcessEmptyFileContent() throws Exception { + public void testProcessEmptyFileContent() { FileContentHandler contentHandler = new FileContentHandler(); contentHandler.addFile(FILE_NAME, new byte[0]); assertFalse(contentHandler.processFileContent(FILE_NAME, Optional::isPresent)); } @Test - public void testProcessNoFileContent() throws Exception { + public void testProcessNoFileContent() { FileContentHandler contentHandler = new FileContentHandler(); assertFalse(contentHandler.processFileContent("filename", Optional::isPresent)); } + + @Test + public void testAddFiles() { + FileContentHandler contentHandler = new FileContentHandler(); + contentHandler.addFile("org/openecomp/core/utilities/file/testFileUtils.txt", + new ByteArrayInputStream(new byte[100])); + + Assert.assertNotNull(contentHandler.getFiles()); + Assert.assertTrue(contentHandler.getFiles().containsKey("org/openecomp/core/utilities/file/testFileUtils.txt")); + } + + @Test + public void testSetFiles() { + FileContentHandler contentHandler = new FileContentHandler(); + Map fileMap = Stream.of(new AbstractMap.SimpleEntry<>("file1", new byte[0]), + new AbstractMap.SimpleEntry<>("file2", new byte[0])) + .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); + + contentHandler.setFiles(fileMap); + + Assert.assertEquals(contentHandler.getFiles().size(), 2); + Assert.assertEquals(contentHandler.getFileList().size(), 2); + Assert.assertFalse(contentHandler.isEmpty()); + contentHandler.remove("file1"); + Assert.assertFalse(contentHandler.containsFile("file1")); + } + + @Test + public void testAddAll() { + FileContentHandler contentHandler = new FileContentHandler(); + FileContentHandler contentHandler1 = createFileHandlerContent(); + + contentHandler.addAll(contentHandler1); + + Assert.assertTrue(contentHandler1.containsFile("file1")); + Assert.assertEquals(contentHandler.getFiles().size(), 2); + } + + @Test + public void testSetFilesUsingFIleContentHandlerObject() { + FileContentHandler contentHandler1 = createFileHandlerContent(); + + FileContentHandler contentHandler = new FileContentHandler(); + contentHandler.setFiles(contentHandler1); + + Assert.assertEquals(contentHandler.getFiles().size(), 2); + } + + private FileContentHandler createFileHandlerContent() { + FileContentHandler contentHandler1 = new FileContentHandler(); + Map fileMap = Stream.of(new AbstractMap.SimpleEntry<>("file1", new byte[0]), + new AbstractMap.SimpleEntry<>("file2", new byte[0])) + .collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue)); + contentHandler1.putAll(fileMap); + return contentHandler1; + } } \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileUtilsTest.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileUtilsTest.java index 15ba6293ef..74c1b1424d 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileUtilsTest.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileUtilsTest.java @@ -16,18 +16,25 @@ package org.openecomp.core.utilities.file; -import org.apache.commons.io.IOUtils; -import org.testng.annotations.Test; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; +import java.util.List; import java.util.Map; import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; -import static org.testng.Assert.*; +import org.apache.commons.io.IOUtils; +import org.testng.Assert; +import org.testng.annotations.Test; /** * @author EVITALIY @@ -48,22 +55,22 @@ public class FileUtilsTest { }; @Test - public void testReadViaInputStreamWithSlash() throws Exception { + public void testReadViaInputStreamWithSlash() { assertTrue(FileUtils.readViaInputStream(TEST_RESOURCE, TEST_FUNCTION) > 0); } @Test - public void testReadViaInputStreamWithoutSlash() throws Exception { + public void testReadViaInputStreamWithoutSlash() { assertTrue(FileUtils.readViaInputStream(TEST_RESOURCE, TEST_FUNCTION) > 0); } @Test(expectedExceptions = NullPointerException.class) - public void testReadViaInputStreamNull() throws Exception { + public void testReadViaInputStreamNull() { FileUtils.readViaInputStream((String) null, TEST_FUNCTION); } @Test(expectedExceptions = IllegalArgumentException.class) - public void testReadViaInputStreamNotFound() throws Exception { + public void testReadViaInputStreamNotFound() { FileUtils.readViaInputStream("notfound.txt", TEST_FUNCTION); } @@ -91,10 +98,100 @@ public class FileUtilsTest { } @Test - public void testIsValidYamlExtension() throws IOException { + public void testIsValidYamlExtension() { assertTrue(FileUtils.isValidYamlExtension("yaml")); assertTrue(FileUtils.isValidYamlExtension("yml")); assertFalse(FileUtils.isValidYamlExtension("yml1")); assertFalse(FileUtils.isValidYamlExtension("zip")); } + + @Test + public void testGetFileWithoutExtention() { + Assert.assertEquals(FileUtils.getFileWithoutExtention("test.txt"), "test"); + } + + @Test + public void testGetFileWithoutExtentionContainsNoExtension() { + Assert.assertEquals(FileUtils.getFileWithoutExtention("test"), "test"); + } + + @Test + public void testGetFileExtention() { + Assert.assertEquals(FileUtils.getFileExtension("test.txt"), "txt"); + } + + @Test + public void testGetNetworkPackageName() { + Assert.assertEquals(FileUtils.getNetworkPackageName("heat.zip"), "heat"); + } + + @Test + public void testGetNetworkPackageNameWithoutExtension() { + Assert.assertNull(FileUtils.getNetworkPackageName("heat")); + } + + @Test + public void testToByteArrayNullStream() { + Assert.assertNotNull(FileUtils.toByteArray(null)); + } + + @Test + public void testGetAllLocations() { + List urlList = FileUtils.getAllLocations("org/openecomp/core/utilities/file/testFileUtils.txt"); + Assert.assertNotNull(urlList); + Assert.assertEquals(urlList.size(), 1); + } + + @Test + public void testConvertToBytesNullObject() { + Assert.assertNotNull(FileUtils.convertToBytes(null, null)); + } + + @Test + public void testConvertToBytes() { + byte[] bytesArray = FileUtils.convertToBytes(Stream.of("Json", "Util", "Test").collect(Collectors.toList()), + FileUtils.FileExtension.YAML); + + Assert.assertNotNull(bytesArray); + } + + @Test + public void testConvertToBytesNotYaml() { + byte[] bytesArray = FileUtils.convertToBytes(Stream.of("Json", "Util", "Test").collect(Collectors.toList()), + FileUtils.FileExtension.JSON); + + Assert.assertNotNull(bytesArray); + } + + @Test + public void testConvertToInputStreamNullObject() { + Assert.assertNull(FileUtils.convertToInputStream(null, null)); + } + + @Test + public void testConvertToInputStream() { + InputStream inputStream = FileUtils.convertToInputStream(Stream.of("Json", "Util", "Test") + .collect(Collectors.toList()), FileUtils.FileExtension.YAML); + + Assert.assertNotNull(inputStream); + } + + @Test(expectedExceptions = RuntimeException.class) + public void testLoadFileToInputStreamIncorrectFilePath() { + FileUtils.loadFileToInputStream("invalidfilepath"); + } + + @Test + public void testLoadFileToInputStream() throws IOException{ + int i; + StringBuilder builder = new StringBuilder(20); + InputStream inputStream = FileUtils.loadFileToInputStream( + "org/openecomp/core/utilities/file/testFileUtils.txt"); + while((i = inputStream.read())!=-1) { + builder.append((char)i); + } + + Assert.assertNotNull(inputStream); + Assert.assertEquals(builder.toString(), "hello-test"); + } } \ No newline at end of file -- cgit 1.2.3-korg