diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/UtilsTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/UtilsTests.java index a062630c5..d64d2038b 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/UtilsTests.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/UtilsTests.java @@ -1,5 +1,8 @@ package ly.count.sdk.java.internal; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Collection; @@ -18,6 +21,8 @@ public class UtilsTests { Log logger; + static final String TEST_FILE_NAME = "testFile"; + @Before public void setupEveryTest() { logger = new Log(Config.LoggingLevel.VERBOSE, null); @@ -234,4 +239,67 @@ public void isValidDataType() { Assert.assertFalse(Utils.isValidDataType(new ArrayList<>())); Assert.assertFalse(Utils.isValidDataType(new HashMap<>())); } + + /** + * It checks if the "readFileContent" method is called. + * And if the created file is read correctly. + */ + @Test + public void readFileContent() throws IOException { + String fileName = "testFile"; + String fileContent = "testContent"; + + File file = new File(fileName); + file.createNewFile(); + FileWriter writer = new FileWriter(file); + writer.write(fileContent); + writer.close(); + + String result = Utils.readFileContent(file); + //delete file + file.delete(); + Assert.assertEquals(fileContent, result); + } + + /** + * If the file does not exist, + * the method should return an empty string. + */ + @Test + public void readFileContent_fileNotExist() throws IOException { + String fileName = "testFile"; + String fileContent = "testContent"; + + File file = new File(fileName); + + String result = Utils.readFileContent(file); + + Assert.assertNotEquals(fileContent, result); + Assert.assertEquals("", result); + } + + /** + * If the file is not readable for some reason, + * the method should throw exception. + */ + @Test(expected = IOException.class) + public void readFileContent_fileNotReadable() throws IOException { + try { + String fileContent = "testContent"; + + File file = new File(TEST_FILE_NAME); + file.createNewFile(); + FileWriter writer = new FileWriter(file); + writer.write(fileContent); + writer.close(); + file.setReadable(false); + + Utils.readFileContent(file); + } finally { + File file = new File(TEST_FILE_NAME); + if (file.exists()) { + file.delete(); + } + } + } }