PruneHistoryCommand implements Action { @Override public Object execute() throws Exception { if( this.daysToKeep < 0 ) { this.out.println( "[ WARNING ] The daysToKeep argument must be equal or greater than 0. Operation cancelled." ); return null; } Collection<ServiceReference<DataSource>> dataSources = this.bundleContext.getServiceReferences( DataSource.class, "(dataSourceName=roboconf-dm-db)" ); if( dataSources.isEmpty()) { this.out.println( "No data source was found to prune the commands history." ); } else { this.out.println( "Pruning the commands history." ); if( this.daysToKeep > 0 ) this.out.println( "Only the last " + this.daysToKeep + " day" + (this.daysToKeep == 1 ? "" : "s") +" will be kept." ); else this.out.println( "All the entries will be deleted." ); ServiceReference<DataSource> sr = dataSources.iterator().next(); DataSource dataSource = this.bundleContext.getService( sr ); Connection conn = null; PreparedStatement ps = null; try { long startLimitInMilliSeconds = System.currentTimeMillis() - 1000L * 24 * 3600 * this.daysToKeep; StringBuilder sb = new StringBuilder(); sb.append( "DELETE FROM commands_history WHERE start < ?" ); conn = dataSource.getConnection(); ps = conn.prepareStatement( sb.toString()); ps.setString( 1, String.valueOf( startLimitInMilliSeconds )); ps.executeUpdate(); this.out.println( "Pruning done." ); } catch( SQLException e ) { this.logger.severe( "An error occurred while pruning the commands history." ); Utils.logException( this.logger, e ); e.printStackTrace(); } finally { Utils.closeStatement( ps, this.logger ); Utils.closeConnection( conn, this.logger ); this.bundleContext.ungetService( sr ); } } return null; }  @Override Object execute();  }
@Test public void testExecute_withRealDataSource() throws Exception { JdbcDataSource ds = new JdbcDataSource(); File dataFile = this.folder.newFile(); ds.setURL( "jdbc:h2:" + dataFile.getAbsolutePath()); ds.setUser( "roboconf" ); ds.setPassword( "roboconf" ); ServiceReference<DataSource> sr = Mockito.mock( ServiceReference.class ); PruneHistoryCommand pruneHistoryCmd = new PruneHistoryCommand(); pruneHistoryCmd.bundleContext = Mockito.mock( BundleContext.class ); Mockito.when( pruneHistoryCmd.bundleContext.getServiceReferences( DataSource.class, "(dataSourceName=roboconf-dm-db)" )).thenReturn( Arrays.asList( sr )); Mockito.when( pruneHistoryCmd.bundleContext.getService( sr )).thenReturn( ds ); Manager manager = Mockito.mock( Manager.class ); Mockito.when( manager.getDataSource()).thenReturn( ds ); CommandsMngrImpl cmdMngr = new CommandsMngrImpl( manager ) ; TestApplication app = new TestApplication(); app.setDirectory( this.folder.newFolder()); String cmdName = "my-command"; String line = "rename /tomcat-vm as tomcat-vm-copy"; cmdMngr.createOrUpdateCommand( app, cmdName, line ); final int count = 5; for( int i=0; i<count; i++ ) { try { cmdMngr.execute( app, cmdName, CommandHistoryItem.ORIGIN_REST_API, "some source" ); } catch( Exception e ) { } } List<CommandHistoryItem> historyItems = cmdMngr.getHistory( 0, 10, null, null, app.getName()); Assert.assertEquals( count, historyItems.size()); ByteArrayOutputStream os = new ByteArrayOutputStream(); pruneHistoryCmd.out = new PrintStream( os, true, "UTF-8" ); pruneHistoryCmd.daysToKeep = -10; pruneHistoryCmd.execute(); historyItems = cmdMngr.getHistory( 0, 10, null, null, app.getName()); Assert.assertEquals( count, historyItems.size()); Assert.assertEquals( "[ WARNING ] The daysToKeep argument must be equal or greater than 0. Operation cancelled.", os.toString( "UTF-8" ).trim()); os = new ByteArrayOutputStream(); pruneHistoryCmd.out = new PrintStream( os, true, "UTF-8" ); pruneHistoryCmd.daysToKeep = 2; pruneHistoryCmd.execute(); historyItems = cmdMngr.getHistory( 0, 10, null, null, app.getName()); Assert.assertEquals( count, historyItems.size()); Assert.assertEquals( "Pruning the commands history.\nOnly the last 2 days will be kept.\nPruning done.", os.toString( "UTF-8" ).trim()); os = new ByteArrayOutputStream(); pruneHistoryCmd.out = new PrintStream( os, true, "UTF-8" ); pruneHistoryCmd.daysToKeep = 1; pruneHistoryCmd.execute(); historyItems = cmdMngr.getHistory( 0, 10, null, null, app.getName()); Assert.assertEquals( count, historyItems.size()); Assert.assertEquals( "Pruning the commands history.\nOnly the last 1 day will be kept.\nPruning done.", os.toString( "UTF-8" ).trim()); os = new ByteArrayOutputStream(); pruneHistoryCmd.out = new PrintStream( os, true, "UTF-8" ); pruneHistoryCmd.daysToKeep = 0; pruneHistoryCmd.execute(); historyItems = cmdMngr.getHistory( 0, 10, null, null, app.getName()); Assert.assertEquals( 0, historyItems.size()); Assert.assertEquals( "Pruning the commands history.\nAll the entries will be deleted.\nPruning done.", os.toString( "UTF-8" ).trim()); }
@Test public void testExecute_withoutDataSource() throws Exception { PruneHistoryCommand pruneHistoryCmd = new PruneHistoryCommand(); pruneHistoryCmd.bundleContext = Mockito.mock( BundleContext.class ); Mockito.when( pruneHistoryCmd.bundleContext.getServiceReferences( DataSource.class, "(dataSourceName=roboconf-dm-db)" )).thenReturn( new ArrayList<ServiceReference<DataSource>>( 0 )); ByteArrayOutputStream os = new ByteArrayOutputStream(); pruneHistoryCmd.out = new PrintStream( os, true, "UTF-8" ); pruneHistoryCmd.execute(); Assert.assertEquals( os.toString( "UTF-8" ).trim(), "No data source was found to prune the commands history." ); }