ScheduleExportUtil { public static JobScheduleRequest createJobScheduleRequest( Job job ) { if ( job == null ) { throw new IllegalArgumentException( Messages.getInstance().getString( "ScheduleExportUtil.JOB_MUST_NOT_BE_NULL" ) ); } JobScheduleRequest schedule = new JobScheduleRequest(); schedule.setJobName( job.getJobName() ); schedule.setDuration( job.getJobTrigger().getDuration() ); schedule.setJobState( job.getState() ); Map<String, Serializable> jobParams = job.getJobParams(); Object streamProviderObj = jobParams.get( QuartzScheduler.RESERVEDMAPKEY_STREAMPROVIDER ); RepositoryFileStreamProvider streamProvider = null; if ( streamProviderObj instanceof RepositoryFileStreamProvider ) { streamProvider = (RepositoryFileStreamProvider) streamProviderObj; } else if ( streamProviderObj instanceof String ) { String inputFilePath = null; String outputFilePath = null; String inputOutputString = (String) streamProviderObj; String[] tokens = inputOutputString.split( ":" ); if ( !ArrayUtils.isEmpty( tokens ) && tokens.length == 2 ) { inputFilePath = tokens[ 0 ].split( "=" )[ 1 ].trim(); outputFilePath = tokens[ 1 ].split( "=" )[ 1 ].trim(); streamProvider = new RepositoryFileStreamProvider( inputFilePath, outputFilePath, true ); } } if ( streamProvider != null ) { schedule.setInputFile( streamProvider.getInputFilePath() ); schedule.setOutputFile( streamProvider.getOutputFilePath() ); } else { String directory = (String) jobParams.get( "directory" ); String transName = (String) jobParams.get( "transformation" ); String jobName = (String) jobParams.get( "job" ); String artifact = transName == null ? jobName : transName; if ( directory != null && artifact != null ) { String outputFile = RepositoryFilenameUtils.concat( directory, artifact ); outputFile += "*"; if ( artifact.equals( jobName ) ) { artifact += ".kjb"; } else { artifact += ".ktr"; } String inputFile = RepositoryFilenameUtils.concat( directory, artifact ); schedule.setInputFile( inputFile ); schedule.setOutputFile( outputFile ); } } for ( String key : jobParams.keySet() ) { Serializable serializable = jobParams.get( key ); if ( RUN_PARAMETERS_KEY.equals( key ) ) { if ( schedule.getPdiParameters() == null ) { schedule.setPdiParameters( new HashMap<String, String>() ); } schedule.getPdiParameters().putAll( (Map<String, String>) serializable ); } else { JobScheduleParam param = null; if ( serializable instanceof String ) { String value = (String) serializable; if ( QuartzScheduler.RESERVEDMAPKEY_ACTIONCLASS.equals( key ) ) { schedule.setActionClass( value ); } else if ( IBlockoutManager.TIME_ZONE_PARAM.equals( key ) ) { schedule.setTimeZone( value ); } param = new JobScheduleParam( key, (String) serializable ); } else if ( serializable instanceof Number ) { param = new JobScheduleParam( key, (Number) serializable ); } else if ( serializable instanceof Date ) { param = new JobScheduleParam( key, (Date) serializable ); } else if ( serializable instanceof Boolean ) { param = new JobScheduleParam( key, (Boolean) serializable ); } if ( param != null ) { schedule.getJobParameters().add( param ); } } } if ( job.getJobTrigger() instanceof SimpleJobTrigger ) { SimpleJobTrigger jobTrigger = (SimpleJobTrigger) job.getJobTrigger(); schedule.setSimpleJobTrigger( jobTrigger ); } else if ( job.getJobTrigger() instanceof ComplexJobTrigger ) { ComplexJobTrigger jobTrigger = (ComplexJobTrigger) job.getJobTrigger(); CronJobTrigger cron = new CronJobTrigger(); cron.setCronString( jobTrigger.getCronString() ); cron.setStartTime( jobTrigger.getStartTime() ); cron.setEndTime( jobTrigger.getEndTime() ); cron.setDuration( jobTrigger.getDuration() ); cron.setUiPassParam( jobTrigger.getUiPassParam() ); schedule.setCronJobTrigger( cron ); } else if ( job.getJobTrigger() instanceof CronJobTrigger ) { CronJobTrigger jobTrigger = (CronJobTrigger) job.getJobTrigger(); schedule.setCronJobTrigger( jobTrigger ); } else { throw new IllegalArgumentException( Messages.getInstance().getString( "PentahoPlatformExporter.UNSUPPORTED_JobTrigger", job.getJobTrigger().getClass().getName() ) ); } return schedule; } ScheduleExportUtil(); static JobScheduleRequest createJobScheduleRequest( Job job ); static final String RUN_PARAMETERS_KEY; }
@Test( expected = IllegalArgumentException.class ) public void testCreateJobScheduleRequest_null() throws Exception { ScheduleExportUtil.createJobScheduleRequest( null ); }
@Test( expected = IllegalArgumentException.class ) public void testCreateJobScheduleRequest_unknownTrigger() throws Exception { String jobName = "JOB"; Job job = mock( Job.class ); JobTrigger trigger = mock( JobTrigger.class ); when( job.getJobTrigger() ).thenReturn( trigger ); JobScheduleRequest jobScheduleRequest = ScheduleExportUtil.createJobScheduleRequest( job ); }
@Test public void testCreateJobScheduleRequest_SimpleJobTrigger() throws Exception { String jobName = "JOB"; Job job = mock( Job.class ); SimpleJobTrigger trigger = mock( SimpleJobTrigger.class ); when( job.getJobTrigger() ).thenReturn( trigger ); when( job.getJobName() ).thenReturn( jobName ); JobScheduleRequest jobScheduleRequest = ScheduleExportUtil.createJobScheduleRequest( job ); assertNotNull( jobScheduleRequest ); assertEquals( jobName, jobScheduleRequest.getJobName() ); assertEquals( trigger, jobScheduleRequest.getSimpleJobTrigger() ); }
@Test public void testCreateJobScheduleRequest_NoStreamProvider() throws Exception { String jobName = "JOB"; Job job = mock( Job.class ); SimpleJobTrigger trigger = mock( SimpleJobTrigger.class ); when( job.getJobTrigger() ).thenReturn( trigger ); when( job.getJobName() ).thenReturn( jobName ); Map<String, Serializable> params = new HashMap<>(); params.put( "directory", "/home/admin" ); params.put( "transformation", "myTransform" ); HashMap<String, String> pdiParams = new HashMap<>(); pdiParams.put( "pdiParam", "pdiParamValue" ); params.put( ScheduleExportUtil.RUN_PARAMETERS_KEY, pdiParams ); when( job.getJobParams() ).thenReturn( params ); JobScheduleRequest jobScheduleRequest = ScheduleExportUtil.createJobScheduleRequest( job ); assertNotNull( jobScheduleRequest ); assertEquals( jobName, jobScheduleRequest.getJobName() ); assertEquals( trigger, jobScheduleRequest.getSimpleJobTrigger() ); assertEquals( "/home/admin/myTransform.ktr", jobScheduleRequest.getInputFile() ); assertEquals( "/home/admin/myTransform*", jobScheduleRequest.getOutputFile() ); assertEquals( "pdiParamValue", jobScheduleRequest.getPdiParameters().get( "pdiParam" ) ); }
@Test public void testCreateJobScheduleRequest_StringStreamProvider() throws Exception { String jobName = "JOB"; Job job = mock( Job.class ); SimpleJobTrigger trigger = mock( SimpleJobTrigger.class ); when( job.getJobTrigger() ).thenReturn( trigger ); when( job.getJobName() ).thenReturn( jobName ); Map<String, Serializable> params = new HashMap<>(); params.put( QuartzScheduler.RESERVEDMAPKEY_STREAMPROVIDER, "import file = /home/admin/myJob.kjb:output file=/home/admin/myJob*" ); when( job.getJobParams() ).thenReturn( params ); JobScheduleRequest jobScheduleRequest = ScheduleExportUtil.createJobScheduleRequest( job ); assertNotNull( jobScheduleRequest ); assertEquals( jobName, jobScheduleRequest.getJobName() ); assertEquals( trigger, jobScheduleRequest.getSimpleJobTrigger() ); assertEquals( "/home/admin/myJob.kjb", jobScheduleRequest.getInputFile() ); assertEquals( "/home/admin/myJob*", jobScheduleRequest.getOutputFile() ); }
@Test public void testCreateJobScheduleRequest_ComplexJobTrigger() throws Exception { String jobName = "JOB"; Date now = new Date(); Job job = mock( Job.class ); ComplexJobTrigger trigger = mock( ComplexJobTrigger.class ); when( job.getJobTrigger() ).thenReturn( trigger ); when( job.getJobName() ).thenReturn( jobName ); when( trigger.getCronString() ).thenReturn( "0 30 13 ? * 2,3,4,5,6 *" ); when( trigger.getDuration() ).thenReturn( -1L ); when( trigger.getStartTime() ).thenReturn( now ); when( trigger.getEndTime() ).thenReturn( now ); when( trigger.getUiPassParam() ).thenReturn( "uiPassParm" ); JobScheduleRequest jobScheduleRequest = ScheduleExportUtil.createJobScheduleRequest( job ); assertNotNull( jobScheduleRequest ); assertEquals( jobName, jobScheduleRequest.getJobName() ); assertNull( jobScheduleRequest.getSimpleJobTrigger() ); assertNull( jobScheduleRequest.getComplexJobTrigger() ); assertNotNull( jobScheduleRequest.getCronJobTrigger() ); assertEquals( trigger.getCronString(), jobScheduleRequest.getCronJobTrigger().getCronString() ); assertEquals( trigger.getDuration(), jobScheduleRequest.getCronJobTrigger().getDuration() ); assertEquals( trigger.getEndTime(), jobScheduleRequest.getCronJobTrigger().getEndTime() ); assertEquals( trigger.getStartTime(), jobScheduleRequest.getCronJobTrigger().getStartTime() ); assertEquals( trigger.getUiPassParam(), jobScheduleRequest.getCronJobTrigger().getUiPassParam() ); }
@Test public void testCreateJobScheduleRequest_CronJobTrigger() throws Exception { String jobName = "JOB"; Job job = mock( Job.class ); CronJobTrigger trigger = mock( CronJobTrigger.class ); when( job.getJobTrigger() ).thenReturn( trigger ); when( job.getJobName() ).thenReturn( jobName ); JobScheduleRequest jobScheduleRequest = ScheduleExportUtil.createJobScheduleRequest( job ); assertNotNull( jobScheduleRequest ); assertEquals( jobName, jobScheduleRequest.getJobName() ); assertEquals( trigger, jobScheduleRequest.getCronJobTrigger() ); }
@Test public void testCreateJobScheduleRequest_StreamProviderJobParam() throws Exception { String jobName = "JOB"; String inputPath = "/input/path/to/file.ext"; String outputPath = "/output/path/location.*"; Map<String, Serializable> params = new HashMap<>(); RepositoryFileStreamProvider streamProvider = mock( RepositoryFileStreamProvider.class ); params.put( QuartzScheduler.RESERVEDMAPKEY_STREAMPROVIDER, streamProvider ); Job job = mock( Job.class ); CronJobTrigger trigger = mock( CronJobTrigger.class ); when( job.getJobTrigger() ).thenReturn( trigger ); when( job.getJobName() ).thenReturn( jobName ); when( job.getJobParams() ).thenReturn( params ); when( streamProvider.getInputFilePath() ).thenReturn( inputPath ); when( streamProvider.getOutputFilePath() ).thenReturn( outputPath ); JobScheduleRequest jobScheduleRequest = ScheduleExportUtil.createJobScheduleRequest( job ); assertEquals( inputPath, jobScheduleRequest.getInputFile() ); assertEquals( outputPath, jobScheduleRequest.getOutputFile() ); assertEquals( 0, jobScheduleRequest.getJobParameters().size() ); }
@Test public void testCreateJobScheduleRequest_ActionClassJobParam() throws Exception { String jobName = "JOB"; String actionClass = "com.pentaho.Action"; Map<String, Serializable> params = new HashMap<>(); params.put( QuartzScheduler.RESERVEDMAPKEY_ACTIONCLASS, actionClass ); Job job = mock( Job.class ); CronJobTrigger trigger = mock( CronJobTrigger.class ); when( job.getJobTrigger() ).thenReturn( trigger ); when( job.getJobName() ).thenReturn( jobName ); when( job.getJobParams() ).thenReturn( params ); JobScheduleRequest jobScheduleRequest = ScheduleExportUtil.createJobScheduleRequest( job ); assertEquals( actionClass, jobScheduleRequest.getActionClass() ); assertEquals( actionClass, jobScheduleRequest.getJobParameters().get( 0 ).getValue() ); }
@Test public void testCreateJobScheduleRequest_TimeZoneJobParam() throws Exception { String jobName = "JOB"; String timeZone = "America/New_York"; Map<String, Serializable> params = new HashMap<>(); params.put( IBlockoutManager.TIME_ZONE_PARAM, timeZone ); Job job = mock( Job.class ); CronJobTrigger trigger = mock( CronJobTrigger.class ); when( job.getJobTrigger() ).thenReturn( trigger ); when( job.getJobName() ).thenReturn( jobName ); when( job.getJobParams() ).thenReturn( params ); JobScheduleRequest jobScheduleRequest = ScheduleExportUtil.createJobScheduleRequest( job ); assertEquals( timeZone, jobScheduleRequest.getTimeZone() ); assertEquals( timeZone, jobScheduleRequest.getJobParameters().get( 0 ).getValue() ); }
@Test public void testCreateJobScheduleRequest_MultipleTypesJobParam() throws Exception { String jobName = "JOB"; Long l = Long.MAX_VALUE; Date d = new Date(); Boolean b = true; Map<String, Serializable> params = new HashMap<>(); params.put( "NumberValue", l ); params.put( "DateValue", d ); params.put( "BooleanValue", b ); Job job = mock( Job.class ); CronJobTrigger trigger = mock( CronJobTrigger.class ); when( job.getJobTrigger() ).thenReturn( trigger ); when( job.getJobName() ).thenReturn( jobName ); when( job.getJobParams() ).thenReturn( params ); JobScheduleRequest jobScheduleRequest = ScheduleExportUtil.createJobScheduleRequest( job ); for ( JobScheduleParam jobScheduleParam : jobScheduleRequest.getJobParameters() ) { assertTrue( jobScheduleParam.getValue().equals( l ) || jobScheduleParam.getValue().equals( d ) || jobScheduleParam.getValue().equals( b ) ); } }