JobManager implements ModeChangeListener { public boolean isJobEnabled(String jobKey) { if (jobKey == null) { throw new IllegalArgumentException("jobKey is null"); } Configuration config = this.jobConfig.get(jobKey); if (config != null) { if (config.containsKey(ConfigProperties.ASYNC_JOBS_JOB_ENABLED)) { return config.getBoolean(ConfigProperties.ASYNC_JOBS_JOB_ENABLED); } } if (this.blacklist != null && this.blacklist.contains(jobKey)) { return false; } if (this.whitelist != null && !this.whitelist.contains(jobKey)) { return false; } return true; } @Inject  JobManager(
        Configuration configuration,
        SchedulerFactory schedulerFactory,
        CandlepinModeManager modeManager,
        AsyncJobStatusCurator jobCurator,
        OwnerCurator ownerCurator,
        JobMessageDispatcher dispatcher,
        JobMessageReceiver receiver,
        PrincipalProvider principalProvider,
        CandlepinRequestScope scope,
        Injector injector); static Class<? extends AsyncJob> registerJob(String jobKey, Class<? extends AsyncJob> jobClass); static Class<? extends AsyncJob> unregisterJob(String jobKey); static Class<? extends AsyncJob> getJobClass(String jobKey); synchronized void initialize(); void start(); synchronized void start(Object key); void resume(); void resume(Object key); void suspend(); synchronized void suspend(Object key); synchronized void shutdown(); synchronized ManagerState getManagerState(); boolean isJobEnabled(String jobKey); boolean isSchedulerEnabled(); AsyncJobStatus findJob(String jobId); List<AsyncJobStatus> findJobs(AsyncJobStatusQueryBuilder queryBuilder); @Transactional AsyncJobStatus queueJob(JobConfig config); AsyncJobStatus executeJob(JobMessage message); @Transactional AsyncJobStatus cancelJob(String jobId); @Transactional int cleanupJobs(AsyncJobStatusQueryBuilder queryBuilder); @Transactional int abortNonTerminalJobs(AsyncJobStatusQueryBuilder queryBuilder); @Override void handleModeChange(CandlepinModeManager modeManager, Mode previousMode, Mode currentMode);  }
@Test public void testJobIsEnabledDefaultsEnabledWithNoConfiguration() { JobManager manager = this.createJobManager(); boolean result = manager.isJobEnabled(TestJob.JOB_KEY); assertTrue(result); }
@Test public void testJobIsEnabledProperlyWhitelistsJobs() { List<String> jobs = Arrays.asList("a", "b", "c", "d", "e"); List<String> whitelist = jobs.subList(1, 3); this.config.setProperty(ConfigProperties.ASYNC_JOBS_WHITELIST, String.join(",", whitelist)); JobManager manager = this.createJobManager(); for (String jobKey : jobs) { boolean expected = whitelist.contains(jobKey); boolean result = manager.isJobEnabled(jobKey); assertEquals(expected, result); } }
@Test public void testJobIsEnabledProperlyBlacklistsJobs() { List<String> jobs = Arrays.asList("a", "b", "c", "d", "e"); List<String> blacklist = jobs.subList(1, 3); this.config.setProperty(ConfigProperties.ASYNC_JOBS_BLACKLIST, String.join(",", blacklist)); JobManager manager = this.createJobManager(); for (String jobKey : jobs) { boolean expected = !blacklist.contains(jobKey); boolean result = manager.isJobEnabled(jobKey); assertEquals(expected, result); } }
@Test public void testJobIsEnabledAllowsJobsToBeDisabled() { List<String> jobs = Arrays.asList("a", "b", "c", "d", "e"); List<String> disabled = jobs.subList(1, 3); for (String jobKey : disabled) { this.config.setProperty(ConfigProperties.ASYNC_JOBS_PREFIX + jobKey + '.' + ConfigProperties.ASYNC_JOBS_JOB_ENABLED, "false"); } JobManager manager = this.createJobManager(); for (String jobKey : jobs) { boolean expected = !disabled.contains(jobKey); boolean result = manager.isJobEnabled(jobKey); assertEquals(expected, result); } }
@Test public void testJobIsEnabledProperlyCombinesWhitelistAndBlacklist() { List<String> jobs = Arrays.asList("a", "b", "c", "d", "e"); List<String> whitelist = jobs.subList(1, 2); List<String> blacklist = jobs.subList(2, 4); this.config.setProperty(ConfigProperties.ASYNC_JOBS_WHITELIST, String.join(",", whitelist)); this.config.setProperty(ConfigProperties.ASYNC_JOBS_BLACKLIST, String.join(",", blacklist)); JobManager manager = this.createJobManager(); for (String jobKey : jobs) { boolean expected = whitelist.contains(jobKey) && !blacklist.contains(jobKey); boolean result = manager.isJobEnabled(jobKey); assertEquals(expected, result); } }
@Test public void testJobIsEnabledProperlyCombinesWhitelistAndBlacklistAndPerJobConfig() { List<String> jobs = Arrays.asList("a", "b", "c", "d", "e"); List<String> whitelist = jobs.subList(1, 3); List<String> blacklist = jobs.subList(2, 4); List<String> disabled = jobs.subList(3, 5); this.config.setProperty(ConfigProperties.ASYNC_JOBS_WHITELIST, String.join(",", whitelist)); this.config.setProperty(ConfigProperties.ASYNC_JOBS_BLACKLIST, String.join(",", blacklist)); for (String jobKey : disabled) { this.config.setProperty(ConfigProperties.ASYNC_JOBS_PREFIX + jobKey + '.' + ConfigProperties.ASYNC_JOBS_JOB_ENABLED, "false"); } JobManager manager = this.createJobManager(); for (String jobKey : jobs) { boolean expected = whitelist.contains(jobKey) && !blacklist.contains(jobKey) && !disabled.contains(jobKey); boolean result = manager.isJobEnabled(jobKey); assertEquals(expected, result); } }