ManagerListener implements IDmListener { @Override public void application( Application application, EventType eventType ) { if( eventType == EventType.DELETED ) { this.logger.fine( "Application " + application + " was deleted. Associated jobs are about to be deleted." ); try { Set<TriggerKey> tks = this.scheduler.scheduler.getTriggerKeys( GroupMatcher.triggerGroupEquals( application.getName())); for( TriggerKey tk : tks ) { try { this.scheduler.deleteJob( tk.getName()); } catch( Exception e ) { this.logger.warning( "An error occurred while unscheduling job " + tk.getName() + ". " + e.getMessage()); Utils.logException( this.logger, e ); } } } catch( Exception e ) { this.logger.warning( "An error occurred while listing jobs for application " + application + ". " + e.getMessage()); Utils.logException( this.logger, e ); } } } ManagerListener( RoboconfScheduler scheduler ); @Override String getId(); @Override void enableNotifications(); @Override void disableNotifications(); @Override void applicationTemplate( ApplicationTemplate tpl, EventType eventType ); @Override void instance( Instance instance, Application application, EventType eventType ); @Override void raw( String message, Object... data ); @Override void application( Application application, EventType eventType );  }
@Test public void testApplicationDeletion() throws Exception { Manager manager = Mockito.spy( new Manager()); manager.configurationMngr().setWorkingDirectory( this.folder.newFolder()); final String cmdName = "cmd"; final Application app1 = new TestApplication().name( "app1" ).directory( this.folder.newFolder()); final Application app2 = new TestApplication().name( "app2" ).directory( this.folder.newFolder()); TestManagerWrapper wrapper = new TestManagerWrapper( manager ); wrapper.addManagedApplication( new ManagedApplication( app1 )); wrapper.addManagedApplication( new ManagedApplication( app2 )); manager.commandsMngr().createOrUpdateCommand( app1, cmdName, "" ); manager.commandsMngr().createOrUpdateCommand( app2, cmdName, "" ); RoboconfScheduler scheduler = new RoboconfScheduler(); scheduler.manager = manager; Mockito.reset( manager ); try { scheduler.start(); Assert.assertNotNull( scheduler.saveJob( null, "job11", cmdName, "0 0 0 ? 1 *", app1.getName())); Assert.assertNotNull( scheduler.saveJob( null, "job12", cmdName, "0 0 0 ? 1 *", app1.getName())); Assert.assertNotNull( scheduler.saveJob( null, "job13", cmdName, "0 0 0 ? 1 *", app1.getName())); String jobId21 = scheduler.saveJob( null, "job21", cmdName, "0 0 0 ? 1 *", app2.getName()); Assert.assertNotNull( jobId21 ); String jobId22 = scheduler.saveJob( null, "job22", cmdName, "0 0 0 ? 1 *", app2.getName()); Assert.assertNotNull( jobId22 ); File schedulerDirectory = scheduler.getSchedulerDirectory(); Set<JobKey> jobKeys = scheduler.scheduler.getJobKeys( GroupMatcher.anyJobGroup()); Assert.assertEquals( 5, jobKeys.size()); Assert.assertEquals( 5, Utils.listAllFiles( schedulerDirectory ).size()); scheduler.dmListener.application( app1, EventType.CREATED ); jobKeys = scheduler.scheduler.getJobKeys( GroupMatcher.anyJobGroup()); Assert.assertEquals( 5, jobKeys.size()); Assert.assertEquals( 5, Utils.listAllFiles( schedulerDirectory ).size()); scheduler.dmListener.application( app1, EventType.CHANGED ); jobKeys = scheduler.scheduler.getJobKeys( GroupMatcher.anyJobGroup()); Assert.assertEquals( 5, jobKeys.size()); Assert.assertEquals( 5, Utils.listAllFiles( schedulerDirectory ).size()); scheduler.dmListener.application( app1, EventType.DELETED ); jobKeys = scheduler.scheduler.getJobKeys( GroupMatcher.anyJobGroup()); Assert.assertEquals( 2, jobKeys.size()); Assert.assertEquals( 2, Utils.listAllFiles( schedulerDirectory ).size()); Assert.assertTrue( jobKeys.contains( new JobKey( jobId21, "app2" ))); Assert.assertTrue( jobKeys.contains( new JobKey( jobId22, "app2" ))); } finally { scheduler.stop(); } }
@Test @SuppressWarnings( "unchecked" ) public void testApplicationDeleted_exceptionWhileListingJobs() throws Exception { RoboconfScheduler roboconfScheduler = Mockito.mock( RoboconfScheduler.class ); roboconfScheduler.scheduler = Mockito.mock( Scheduler.class ); Mockito .when( roboconfScheduler.scheduler.getTriggerKeys( Mockito.any( GroupMatcher.class ))) .thenThrow( new SchedulerException( "For test" )); ManagerListener listener = new ManagerListener( roboconfScheduler ); listener.application( new Application( "app", Mockito.mock( ApplicationTemplate.class )), EventType.DELETED ); Mockito .verify( roboconfScheduler.scheduler, Mockito.times( 1 )) .getTriggerKeys( Mockito.any( GroupMatcher.class )); }
@Test @SuppressWarnings( "unchecked" ) public void testApplicationDeleted_exceptionWhileDeletingJobs() throws Exception { RoboconfScheduler roboconfScheduler = Mockito.mock( RoboconfScheduler.class ); Mockito .doThrow( new IOException( "For test" )) .when( roboconfScheduler ).deleteJob( Mockito.anyString()); Set<TriggerKey> triggerKeys = new HashSet<> (); triggerKeys.add( new TriggerKey( "job", "app" )); roboconfScheduler.scheduler = Mockito.mock( Scheduler.class ); Mockito .when( roboconfScheduler.scheduler.getTriggerKeys( Mockito.any( GroupMatcher.class ))) .thenReturn( triggerKeys ); ManagerListener listener = new ManagerListener( roboconfScheduler ); listener.application( new Application( "app", Mockito.mock( ApplicationTemplate.class )), EventType.DELETED ); Mockito .verify( roboconfScheduler.scheduler, Mockito.times( 1 )) .getTriggerKeys( Mockito.any( GroupMatcher.class )); Mockito .verify( roboconfScheduler, Mockito.times( 1 )) .deleteJob( Mockito.anyString()); }