HdfsUtils { public static String contentOfHdfsFile(FileSystem fs, Path filePath) { StringBuilder fileContent = new StringBuilder(); try (FSDataInputStream inStrm = fs.open(filePath); BufferedReader bufReader = new BufferedReader(new InputStreamReader(inStrm, StandardCharsets.UTF_8))) { String line; while ((line = bufReader.readLine()) != null) { fileContent.append(line); } return fileContent.toString(); } catch (IOException e) { LOG.error("Couldn't read content of file from HDFS", e); return ""; } } private  HdfsUtils(); static boolean pathExists(FileSystem fs, Path filePath); static String contentOfHdfsFile(FileSystem fs, Path filePath); static String getLastComponent(String path); @VisibleForTesting static boolean isJobFolder(Path p, String regex); static List<FileStatus> getDirectories(FileSystem fs, Path path); static Path getJobDirPath(FileSystem fs, Path curr, String applicationId); static List<Path> getJobDirs(FileSystem fs, Path curr, String regex); static void copySrcToDest(Path src, Path dst, Configuration conf);  }
@Test public void testContentOfHdfsFileWithContent() { Path filePath = new Path(Constants.TONY_CORE_SRC + "test/resources/file.txt"); assertEquals(HdfsUtils.contentOfHdfsFile(fs, filePath), "someContent"); }
@Test public void testContentOfHdfsFileNoContent() { Path filePath = new Path(Constants.TONY_CORE_SRC + "test/resources/empty.txt"); assertEquals(HdfsUtils.contentOfHdfsFile(fs, filePath), ""); }
@Test public void testContentOfHdfsFileThrowsException() throws IOException { FileSystem mockFs = mock(FileSystem.class); Path filePath = new Path(Constants.TONY_CORE_SRC + "test/resources/empty.txt"); when(mockFs.exists(filePath)).thenThrow(new IOException("IO Excpt")); assertEquals(HdfsUtils.contentOfHdfsFile(fs, filePath), ""); }