use of org.pentaho.platform.web.http.api.resources.JobScheduleParam in project pentaho-platform by pentaho.
the class SchedulerServiceTest method testAddBlockout.
@Test
public void testAddBlockout() throws Exception {
JobScheduleRequest jobScheduleRequest = mock(JobScheduleRequest.class);
Job jobMock = mock(Job.class);
JobScheduleParam jobScheduleParamMock1 = mock(JobScheduleParam.class);
JobScheduleParam jobScheduleParamMock2 = mock(JobScheduleParam.class);
List<JobScheduleParam> jobScheduleParams = new ArrayList<>();
doReturn(true).when(schedulerService).canAdminister();
doNothing().when(jobScheduleRequest).setActionClass(anyString());
doReturn(jobScheduleParams).when(jobScheduleRequest).getJobParameters();
doReturn(jobScheduleParamMock1).when(schedulerService).getJobScheduleParam(anyString(), anyString());
doReturn(jobScheduleParamMock2).when(schedulerService).getJobScheduleParam(anyString(), anyLong());
doNothing().when(schedulerService).updateStartDateForTimeZone(jobScheduleRequest);
doReturn(jobMock).when(schedulerService).createJob(any(JobScheduleRequest.class));
Job job = schedulerService.addBlockout(jobScheduleRequest);
assertNotNull(job);
assertEquals(2, jobScheduleParams.size());
verify(schedulerService).canAdminister();
verify(jobScheduleRequest).setActionClass(anyString());
verify(jobScheduleRequest, times(2)).getJobParameters();
verify(schedulerService).createJob(any(JobScheduleRequest.class));
}
use of org.pentaho.platform.web.http.api.resources.JobScheduleParam in project pentaho-platform by pentaho.
the class SchedulerServiceTest method testCreateJob.
@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));
// Test 1
schedulerService.createJob(scheduleRequest);
// Test 2
doReturn("").when(scheduleRequest).getJobName();
schedulerService.createJob(scheduleRequest);
// Test 3
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( schedulerService, times( 2 ) ).resolveActionId( anyString() );
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));
}
use of org.pentaho.platform.web.http.api.resources.JobScheduleParam in project pentaho-platform by pentaho.
the class SolutionImportHandler method importSchedules.
protected void importSchedules(List<JobScheduleRequest> scheduleList) throws PlatformImportException {
if (CollectionUtils.isNotEmpty(scheduleList)) {
SchedulerResource schedulerResource = new SchedulerResource();
schedulerResource.pause();
for (JobScheduleRequest jobScheduleRequest : scheduleList) {
boolean jobExists = false;
List<Job> jobs = getAllJobs(schedulerResource);
if (jobs != null) {
// paramRequest to map<String, Serializable>
Map<String, Serializable> mapParamsRequest = new HashMap<>();
for (JobScheduleParam paramRequest : jobScheduleRequest.getJobParameters()) {
mapParamsRequest.put(paramRequest.getName(), paramRequest.getValue());
}
for (Job job : jobs) {
if ((mapParamsRequest.get(RESERVEDMAPKEY_LINEAGE_ID) != null) && (mapParamsRequest.get(RESERVEDMAPKEY_LINEAGE_ID).equals(job.getJobParams().get(RESERVEDMAPKEY_LINEAGE_ID)))) {
jobExists = true;
}
if (overwriteFile && jobExists) {
JobRequest jobRequest = new JobRequest();
jobRequest.setJobId(job.getJobId());
schedulerResource.removeJob(jobRequest);
jobExists = false;
break;
}
}
}
if (!jobExists) {
try {
Response response = createSchedulerJob(schedulerResource, jobScheduleRequest);
if (response.getStatus() == Response.Status.OK.getStatusCode()) {
if (response.getEntity() != null) {
// get the schedule job id from the response and add it to the import session
ImportSession.getSession().addImportedScheduleJobId(response.getEntity().toString());
}
}
} catch (Exception e) {
// the space(s)
if (jobScheduleRequest.getInputFile().contains(" ") || jobScheduleRequest.getOutputFile().contains(" ")) {
log.info("Could not import schedule, attempting to replace spaces with underscores and retrying: " + jobScheduleRequest.getInputFile());
File inFile = new File(jobScheduleRequest.getInputFile());
File outFile = new File(jobScheduleRequest.getOutputFile());
String inputFileName = inFile.getParent() + RepositoryFile.SEPARATOR + inFile.getName().replaceAll(" ", "_");
String outputFileName = outFile.getParent() + RepositoryFile.SEPARATOR + outFile.getName().replaceAll(" ", "_");
jobScheduleRequest.setInputFile(inputFileName);
jobScheduleRequest.setOutputFile(outputFileName);
try {
if (File.separator != RepositoryFile.SEPARATOR) {
// on windows systems, the backslashes will result in the file not being found in the repository
jobScheduleRequest.setInputFile(inputFileName.replace(File.separator, RepositoryFile.SEPARATOR));
jobScheduleRequest.setOutputFile(outputFileName.replace(File.separator, RepositoryFile.SEPARATOR));
}
Response response = createSchedulerJob(schedulerResource, jobScheduleRequest);
if (response.getStatus() == Response.Status.OK.getStatusCode()) {
if (response.getEntity() != null) {
// get the schedule job id from the response and add it to the import session
ImportSession.getSession().addImportedScheduleJobId(response.getEntity().toString());
}
}
} catch (Exception ex) {
// log it and keep going. we should stop processing all schedules just because one fails.
log.error(Messages.getInstance().getString("SolutionImportHandler.ERROR_0001_ERROR_CREATING_SCHEDULE", e.getMessage()), ex);
}
} else {
// log it and keep going. we should stop processing all schedules just because one fails.
log.error(Messages.getInstance().getString("SolutionImportHandler.ERROR_0001_ERROR_CREATING_SCHEDULE", e.getMessage()));
}
}
} else {
log.info(Messages.getInstance().getString("DefaultImportHandler.ERROR_0009_OVERWRITE_CONTENT", jobScheduleRequest.toString()));
}
}
schedulerResource.start();
}
}
use of org.pentaho.platform.web.http.api.resources.JobScheduleParam in project pentaho-platform by pentaho.
the class ScheduleExportUtil method createJobScheduleRequest.
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 {
// let's look to see if we can figure out the input and output file
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();
// force it to a cron trigger to get the auto-parsing of the complex trigger
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 {
// don't know what this is, can't export it
throw new IllegalArgumentException(Messages.getInstance().getString("PentahoPlatformExporter.UNSUPPORTED_JobTrigger", job.getJobTrigger().getClass().getName()));
}
return schedule;
}
use of org.pentaho.platform.web.http.api.resources.JobScheduleParam in project pentaho-platform by pentaho.
the class SchedulerServiceTest method testAddBlockoutException.
@Test
public void testAddBlockoutException() throws Exception {
// Test 1
JobScheduleRequest jobScheduleRequest = mock(JobScheduleRequest.class);
doReturn(false).when(schedulerService).canAdminister();
try {
schedulerService.addBlockout(jobScheduleRequest);
fail();
} catch (IllegalAccessException e) {
// Should catch exception
}
// Test 2
Job jobMock = mock(Job.class);
JobScheduleParam jobScheduleParamMock1 = mock(JobScheduleParam.class);
JobScheduleParam jobScheduleParamMock2 = mock(JobScheduleParam.class);
List<JobScheduleParam> jobScheduleParams = new ArrayList<>();
doReturn(true).when(schedulerService).canAdminister();
doNothing().when(jobScheduleRequest).setActionClass(anyString());
doReturn(jobScheduleParams).when(jobScheduleRequest).getJobParameters();
doReturn(jobScheduleParamMock1).when(schedulerService).getJobScheduleParam(anyString(), anyString());
doReturn(jobScheduleParamMock2).when(schedulerService).getJobScheduleParam(anyString(), anyLong());
doNothing().when(schedulerService).updateStartDateForTimeZone(jobScheduleRequest);
doReturn(jobMock).when(schedulerService).createJob(any(JobScheduleRequest.class));
doThrow(new IOException()).when(schedulerService).createJob(jobScheduleRequest);
try {
schedulerService.addBlockout(jobScheduleRequest);
fail();
} catch (IOException e) {
// Should catch exception
}
// Test 3
doThrow(new SchedulerException("")).when(schedulerService).createJob(jobScheduleRequest);
try {
schedulerService.addBlockout(jobScheduleRequest);
fail();
} catch (SchedulerException e) {
// Should catch exception
}
verify(schedulerService, times(3)).canAdminister();
verify(jobScheduleRequest, times(2)).setActionClass(anyString());
verify(jobScheduleRequest, times(4)).getJobParameters();
verify(schedulerService, times(2)).createJob(any(JobScheduleRequest.class));
}
Aggregations