HMSDebuggerComponent { public boolean isFileDeleteable( String logArchiveName ) throws HmsException { if ( isParameterNotNullNonEmpty( logArchiveName ) && logArchiveName.matches( HMS_ARCHIVE_REGEX ) ) { try { Date parsedDate = parseDateFromFileName( logArchiveName ); Date currentDate = new Date(); long duration = currentDate.getTime() - parsedDate.getTime(); long diffInMinutes = TimeUnit.MILLISECONDS.toMinutes( duration ); if ( ( diffInMinutes - ( logClearDurationInHours * 60 ) ) > 0 ) { return true; } } catch ( HmsException e ) { String err = "Exception occured when trying to get file duration since it was created for file [ " + logArchiveName + " ]"; logger.error( err, e ); throw new HmsException( err, e ); } return false; } else { String err = "Cannot get Date from log Archive name [ " + logArchiveName + " ], because either it is null or empty. or it doesnot matches the regular expression [ " + HMS_ARCHIVE_REGEX + " ]"; logger.error( err ); throw new HmsException( err ); } }  String archiveHmsDebugLogs( final String host_id, final String hmsIpAddr, final String hmsOobUsername,
                                       final String hmsLogArchiverScript, final String hmsLogArchiveLocation,
                                       final String hmsOobLogLocation, final String hmsIbLogLocation,
                                       final int noOfLines, final EventComponent component ); boolean writeAllEventsToFile( ServerNode node, String fileName, String logDirectoryPath ); boolean writeNBSwitchInfoToFile( NBSwitchInfo node, String fileName, String logDirectoryPath ); boolean writeServerInfoToFile( ServerInfo host, String fileName, String logDirectoryPath ); boolean writeFile( String eventsLogFilePath, String content ); String getAbsoluteFilePath( String directoryLocation, String filename ); String getEventsDataAsString( ServerNode node ); String getSwitchEventsDataAsString( SwitchNode node ); boolean writeHmsLogs( String node_id, String hmsLogArchiverScript, String hmsLogArchiveLocation,
                                 String timestamp, int noOfLines, String nodeInfoLogFileName, String hmsArchiveName,
                                 String hmsOobLogLocation, String hmsIbLogLocation,
                                 final EventComponent isHostOrSwitch ); boolean callLogArchiverShellScript( String node_id, String hmsLogArchiverScript,
                                               String hmsLogArchiveLocation, String eventsLogFileName,
                                               String hmsArchiveName, String host_id, String timestamp,
                                               String hmsOobLogFileName, String hmsIbLogLocation, int noOfLines ); boolean callServerLogArchiverShellScript( String node_id, String hmsLogArchiverScript,
                                                     String hmsLogArchiveLocation, String serverInfoFileName,
                                                     String hmsArchiveName, String host_id, String timestamp,
                                                     String hmsOobLogLocation, String hmsIbLogLocation, int noOfLines,
                                                     String eventLogFileName ); boolean isParameterNotNullNonEmpty( String param ); String getCurrentTimestamp(); String getServerInfoOrNBSwitchInfoLogFilename( String host_id, String timestamp,
                                                          EventComponent isHostOrSwitch ); String getHmsEventsLogFilename( String host_id, String timestamp ); String getHmsOobLogFilename( String host_id, String timestamp ); String getHmsLogArchiveName( String host_id, String timestamp ); boolean cleanHmsDebugLogs( String logArchiveDirectoryName ); boolean isFileDeleteable( String logArchiveName ); Date parseDateFromFileName( String logArchiveName ); void setHmsSwitchManager( HmsSwitchManager hmsSwitchManager ); final String TIMESTAMP_FORMAT; final String HMS_ARCHIVE_REGEX; final String HMS_TIMESTAMP_EXTRACTION_REGEX; final long logClearDurationInHours; }
@Test( expected = HmsException.class ) public void isFileDeleteable_invalidInput() throws HmsException { String invalidTimeString = "N1_hms_logs_2015-03-03_00"; boolean status = debuggerUtil.isFileDeleteable( invalidTimeString ); assertNotNull( status ); }
@Test public void isFileDeleteable_validInput() throws HmsException { Date currentDate = new Date(); long currMillisecs = currentDate.getTime(); long milliSecsOneDayAgo = currMillisecs - ( 24 * 60 * 60 * 1000 ); long milliSecsThreeDayAgo = currMillisecs - ( 3 * 24 * 60 * 60 * 1000 ); DateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd_hh-mm-ss" ); final String oneDayAgoDateString = formatter.format( new Date( milliSecsOneDayAgo ) ); final String threeDayAgoDateString = formatter.format( new Date( milliSecsThreeDayAgo ) ); String fileCreatedOneDayAgo = "N1_hms_logs_" + oneDayAgoDateString + ".zip"; String fileCreatedThreeDayAgo = "N1_hms_logs_" + threeDayAgoDateString + ".zip"; boolean status = debuggerUtil.isFileDeleteable( fileCreatedOneDayAgo ); assertFalse( status ); boolean status2 = debuggerUtil.isFileDeleteable( fileCreatedThreeDayAgo ); assertTrue( status2 ); }