SchedulerService { public Job createJob( JobScheduleRequest scheduleRequest ) throws IOException, SchedulerException, IllegalAccessException { boolean runInBackground = scheduleRequest.getSimpleJobTrigger() == null && scheduleRequest.getComplexJobTrigger() == null && scheduleRequest.getCronJobTrigger() == null; if ( !runInBackground && !getPolicy().isAllowed( SchedulerAction.NAME ) ) { throw new SecurityException(); } boolean hasInputFile = !StringUtils.isEmpty( scheduleRequest.getInputFile() ); RepositoryFile file = null; if ( hasInputFile ) { try { file = getRepository().getFile( scheduleRequest.getInputFile() ); } catch ( UnifiedRepositoryException ure ) { hasInputFile = false; logger.warn( ure.getMessage(), ure ); } } if ( hasInputFile && StringUtils.isEmpty( scheduleRequest.getJobName() ) ) { scheduleRequest.setJobName( file.getName().substring( 0, file.getName().lastIndexOf( "." ) ) ); } else if ( !StringUtils.isEmpty( scheduleRequest.getActionClass() ) ) { String actionClass = scheduleRequest.getActionClass().substring( scheduleRequest.getActionClass().lastIndexOf( "." ) + 1 ); scheduleRequest.setJobName( actionClass ); } else if ( !hasInputFile && StringUtils.isEmpty( scheduleRequest.getJobName() ) ) { scheduleRequest.setJobName( "" + System.currentTimeMillis() ); } if ( hasInputFile ) { if ( file == null ) { logger.error( "Cannot find input source file " + scheduleRequest.getInputFile() + " Aborting schedule..." ); throw new SchedulerException( new ServiceException( "Cannot find input source file " + scheduleRequest.getInputFile() ) ); } Map<String, Serializable> metadata = getRepository().getFileMetadata( file.getId() ); if ( metadata.containsKey( RepositoryFile.SCHEDULABLE_KEY ) ) { boolean schedulable = BooleanUtils.toBoolean( (String) metadata.get( RepositoryFile.SCHEDULABLE_KEY ) ); if ( !schedulable ) { throw new IllegalAccessException(); } } } if ( scheduleRequest.getTimeZone() != null ) { updateStartDateForTimeZone( scheduleRequest ); } Job job = null; IJobTrigger jobTrigger = SchedulerResourceUtil.convertScheduleRequestToJobTrigger( scheduleRequest, scheduler ); HashMap<String, Serializable> parameterMap = new HashMap<>(); for ( JobScheduleParam param : scheduleRequest.getJobParameters() ) { parameterMap.put( param.getName(), param.getValue() ); } if ( isPdiFile( file ) ) { parameterMap = handlePDIScheduling( file, parameterMap, scheduleRequest.getPdiParameters() ); } parameterMap.put( LocaleHelper.USER_LOCALE_PARAM, LocaleHelper.getLocale() ); if ( hasInputFile ) { SchedulerOutputPathResolver outputPathResolver = getSchedulerOutputPathResolver( scheduleRequest ); String outputFile = outputPathResolver.resolveOutputFilePath(); String actionId = SchedulerResourceUtil.resolveActionId( scheduleRequest.getInputFile() ); final String inputFile = scheduleRequest.getInputFile(); parameterMap.put( ActionUtil.QUARTZ_STREAMPROVIDER_INPUT_FILE, inputFile ); job = getScheduler().createJob( scheduleRequest.getJobName(), actionId, parameterMap, jobTrigger, new RepositoryFileStreamProvider( inputFile, outputFile, getAutoCreateUniqueFilename( scheduleRequest ), getAppendDateFormat( scheduleRequest ) ) ); } else { String actionClass = scheduleRequest.getActionClass(); try { @SuppressWarnings( "unchecked" ) Class<IAction> iaction = getAction( actionClass ); job = getScheduler().createJob( scheduleRequest.getJobName(), iaction, parameterMap, jobTrigger ); } catch ( ClassNotFoundException e ) { throw new RuntimeException( e ); } } return job; }  Job createJob( JobScheduleRequest scheduleRequest ); Job updateJob( JobScheduleRequest scheduleRequest ); Job triggerNow( String jobId ); Job getContentCleanerJob(); List<RepositoryFileDto> doGetGeneratedContentForSchedule( String lineageId ); Job getJob( String jobId ); boolean isScheduleAllowed(); boolean isScheduleAllowed( String id ); IJobFilter getJobFilter( boolean canAdminister, String principalName ); String doGetCanSchedule(); String getState(); String start(); String pause(); String shutdown(); JobState pauseJob( String jobId ); JobState resumeJob( String jobId ); boolean removeJob( String jobId ); Job getJobInfo( String jobId ); List<Job> getBlockOutJobs(); boolean hasBlockouts(); boolean willFire( IJobTrigger trigger ); boolean shouldFireNow(); Job addBlockout( JobScheduleRequest jobScheduleRequest ); Job updateBlockout( String jobId, JobScheduleRequest jobScheduleRequest ); BlockStatusProxy getBlockStatus( JobScheduleRequest jobScheduleRequest ); JobScheduleRequest getJobInfo(); JobState getJobState( JobRequest jobRequest ); Class<IAction> getAction( String actionClass ); IUnifiedRepository getRepository(); IScheduler getScheduler(); IAuthorizationPolicy getPolicy(); boolean getAutoCreateUniqueFilename( final JobScheduleRequest scheduleRequest ); String getAppendDateFormat( final JobScheduleRequest scheduleRequest ); List<Job> getJobs();  }
@Test public void testCreateJob() throws Exception { List<JobScheduleParam> jobParameters = new ArrayList<>(); JobScheduleParam jobScheduleParam1 = mock( JobScheduleParam.class ); doReturn( "name1" ).when( jobScheduleParam1 ).getName(); doReturn( "value1" ).when( jobScheduleParam1 ).getValue(); jobParameters.add( jobScheduleParam1 ); Job job = mock( Job.class ); JobScheduleRequest scheduleRequest = mock( JobScheduleRequest.class ); doReturn( "className" ).when( scheduleRequest ).getActionClass(); doReturn( "jobName" ).when( scheduleRequest ).getJobName(); doReturn( jobParameters ).when( scheduleRequest ).getJobParameters(); doNothing().when( scheduleRequest ).setJobName( anyString() ); doReturn( "timezone" ).when( scheduleRequest ).getTimeZone(); doNothing().when( schedulerService ).updateStartDateForTimeZone( scheduleRequest ); doReturn( true ).when( schedulerService ).isPdiFile( any( RepositoryFile.class ) ); SchedulerOutputPathResolver schedulerOutputPathResolver = mock( SchedulerOutputPathResolver.class ); doReturn( "outputFile" ).when( schedulerOutputPathResolver ).resolveOutputFilePath(); SimpleJobTrigger simpleJobTrigger = mock( SimpleJobTrigger.class ); ComplexJobTriggerProxy complexJobTriggerProxy = mock( ComplexJobTriggerProxy.class ); CronJobTrigger cronJobTrigger = mock( CronJobTrigger.class ); RepositoryFile repositoryFile = mock( RepositoryFile.class ); doReturn( "file.ext" ).when( repositoryFile ).getName(); Map<String, Serializable> metadata = mock( Map.class ); doReturn( metadata ).when( schedulerService.repository ).getFileMetadata( anyString() ); doReturn( true ).when( metadata ).containsKey( RepositoryFile.SCHEDULABLE_KEY ); doReturn( "true" ).when( metadata ).get( RepositoryFile.SCHEDULABLE_KEY ); doReturn( simpleJobTrigger ).when( scheduleRequest ).getSimpleJobTrigger(); doReturn( complexJobTriggerProxy ).when( scheduleRequest ).getComplexJobTrigger(); doReturn( cronJobTrigger ).when( scheduleRequest ).getCronJobTrigger(); doReturn( true ).when( schedulerService.policy ).isAllowed( SchedulerAction.NAME ); doReturn( "file.ext" ).when( scheduleRequest ).getInputFile(); doReturn( repositoryFile ).when( schedulerService.repository ).getFile( anyString() ); doReturn( "ext.backgroundExecution" ).when( schedulerService ).resolveActionId( anyString() ); doReturn( true ).when( schedulerService ).getAutoCreateUniqueFilename( any( JobScheduleRequest.class ) ); doReturn( job ).when( schedulerService.scheduler ) .createJob( anyString(), anyString(), any( Map.class ), any( IJobTrigger.class ), any( IBackgroundExecutionStreamProvider.class ) ); doReturn( Class.class ).when( schedulerService ).getAction( anyString() ); doReturn( job ).when( schedulerService.scheduler ) .createJob( anyString(), any( Class.class ), any( Map.class ), any( IJobTrigger.class ) ); schedulerService.createJob( scheduleRequest ); doReturn( "" ).when( scheduleRequest ).getJobName(); schedulerService.createJob( scheduleRequest ); doReturn( "" ).when( scheduleRequest ).getInputFile(); doReturn( "" ).when( scheduleRequest ).getActionClass(); schedulerService.createJob( scheduleRequest ); verify( scheduleRequest, times( 15 ) ).getSimpleJobTrigger(); verify( scheduleRequest, times( 11 ) ).getInputFile(); verify( schedulerService.policy, times( 3 ) ).isAllowed( SchedulerAction.NAME ); verify( schedulerService.repository, times( 2 ) ).getFile( anyString() ); verify( scheduleRequest, times( 9 ) ).getJobName(); verify( scheduleRequest, times( 3 ) ).setJobName( anyString() ); verify( scheduleRequest, times( 5 ) ).getActionClass(); verify( schedulerService.repository, times( 2 ) ).getFileMetadata( anyString() ); verify( schedulerService, times( 3 ) ).isPdiFile( any( RepositoryFile.class ) ); verify( schedulerService, times( 3 ) ).handlePDIScheduling( any( RepositoryFile.class ), any( HashMap.class ), any( HashMap.class ) ); verify( schedulerService, times( 2 ) ).getSchedulerOutputPathResolver( any( JobScheduleRequest.class ) ); verify( scheduleRequest, times( 5 ) ).getActionClass(); verify( schedulerService ).getAction( anyString() ); verify( schedulerService, times( 3 ) ).updateStartDateForTimeZone( scheduleRequest ); verify( schedulerService.scheduler ) .createJob( anyString(), any( Class.class ), any( Map.class ), any( IJobTrigger.class ) ); }