ParserUtils { public static JobMetadata parseMetadata(FileSystem fs, YarnConfiguration yarnConf, Path jobFolderPath, String jobIdRegex) { if (!pathExists(fs, jobFolderPath)) { return null; } String jhistFilePath = getJhistFilePath(fs, jobFolderPath); if (Strings.isNullOrEmpty(jhistFilePath)) { return null; } String histFileName = HdfsUtils.getLastComponent(jhistFilePath); if (!isValidHistFileName(histFileName, jobIdRegex)) { LOG.warn("Invalid history file name " + histFileName); return null; } LOG.debug("Successfully parsed metadata"); return JobMetadata.newInstance(yarnConf, histFileName); } private  ParserUtils(); @VisibleForTesting static boolean isValidHistFileName(String fileName, String jobIdRegex); static String getJhistFilePath(FileSystem fs, Path jobFolderPath); static long getCompletedTimeFromJhistFileName(String jhistFileName); static JobMetadata parseMetadata(FileSystem fs, YarnConfiguration yarnConf, Path jobFolderPath, String jobIdRegex); static List<JobConfig> parseConfig(FileSystem fs, Path jobFolderPath); static List<Event> parseEvents(FileSystem fs, Path jobFolderPath); static List<JobEvent> mapEventToJobEvent(List<Event> events); static List<JobLog> mapEventToJobLog(List<Event> events, JobLogMetaData jobLogMetaData); static String getYearMonthDayDirectory(Date date, ZoneId zoneId);  }
@Test public void testParseMetadataSuccess() { Path jobFolder = new Path(Constants.TONY_CORE_SRC + "test/resources/typicalHistFolder/job1"); String jobRegex = "application\\d+"; JobMetadata expected = new JobMetadata.Builder() .setId("application123") .setConf(yarnConf) .setStarted(1) .setCompleted(1) .setStatus(Constants.SUCCEEDED) .setUser("user1") .build(); JobMetadata actual = ParserUtils.parseMetadata(fs, yarnConf, jobFolder, jobRegex); assertEquals(actual.getId(), expected.getId()); assertEquals(actual.getJobLink(), expected.getJobLink()); assertEquals(actual.getConfigLink(), expected.getConfigLink()); assertEquals(actual.getRMLink(), expected.getRMLink()); assertEquals(actual.getStartedDate(), expected.getStartedDate()); assertEquals(actual.getCompletedDate(), expected.getCompletedDate()); assertEquals(actual.getStatus(), expected.getStatus()); assertEquals(actual.getUser(), expected.getUser()); }
@Test public void testParseMetadataFailIOException() throws IOException { Path jobFolder = new Path(Constants.TONY_CORE_SRC + "test/resources/typicalHistFolder/job1"); String jobRegex = "application\\d+"; FileSystem mockFs = mock(FileSystem.class); when(mockFs.listStatus(jobFolder)).thenThrow(new IOException("IO Excpt")); JobMetadata result = ParserUtils.parseMetadata(mockFs, yarnConf, jobFolder, jobRegex); assertNull(result); }