Search in sources :

Example 1 with JobStatus

use of org.candlepin.pinsetter.core.model.JobStatus in project candlepin by candlepin.

the class PinsetterAsyncFilter method postProcess.

/**
 * {@inheritDoc}
 *
 * @param response the server response (provided by Resteasy)
 */
@Override
public void postProcess(ServerResponse response) {
    Object entity = response.getEntity();
    if (entity instanceof JobDetail) {
        JobDetail jobDetail = (JobDetail) entity;
        setJobPrincipal(jobDetail);
        JobStatus status = this.scheduleJob(jobDetail);
        response.setEntity(this.translator.translate(status, JobStatusDTO.class));
        response.setStatus(HttpResponseCodes.SC_ACCEPTED);
    } else if (entity instanceof JobDetail[]) {
        JobDetail[] details = (JobDetail[]) entity;
        JobStatus[] statuses = new JobStatus[details.length];
        int i = 0;
        for (JobDetail jobDetail : details) {
            setJobPrincipal(jobDetail);
            JobStatus status = this.scheduleJob(jobDetail);
            statuses[i++] = status;
        }
        JobStatusDTO[] dtoStatuses = new JobStatusDTO[statuses.length];
        for (int j = 0; j < statuses.length; j++) {
            dtoStatuses[j] = this.translator.translate(statuses[j], JobStatusDTO.class);
        }
        response.setEntity(dtoStatuses);
        response.setStatus(HttpResponseCodes.SC_ACCEPTED);
    }
}
Also used : JobStatus(org.candlepin.pinsetter.core.model.JobStatus) JobDetail(org.quartz.JobDetail) JobStatusDTO(org.candlepin.dto.api.v1.JobStatusDTO)

Example 2 with JobStatus

use of org.candlepin.pinsetter.core.model.JobStatus in project candlepin by candlepin.

the class PinsetterAsyncFilterTest method jobStatusSet.

@Test
public void jobStatusSet() throws PinsetterException {
    JobDetail detail = newJob(RefreshPoolsJob.class).build();
    JobStatus status = new JobStatus();
    JobStatusDTO statusDTO = new JobStatusDTO();
    when(response.getEntity()).thenReturn(detail);
    when(this.pinsetterKernel.scheduleSingleJob(detail)).thenReturn(status);
    when(this.translator.translate(status, JobStatusDTO.class)).thenReturn(statusDTO);
    this.interceptor.postProcess(response);
    verify(response).setEntity(statusDTO);
}
Also used : RefreshPoolsJob(org.candlepin.pinsetter.tasks.RefreshPoolsJob) JobStatus(org.candlepin.pinsetter.core.model.JobStatus) JobDetail(org.quartz.JobDetail) JobStatusDTO(org.candlepin.dto.api.v1.JobStatusDTO) Test(org.junit.Test)

Example 3 with JobStatus

use of org.candlepin.pinsetter.core.model.JobStatus in project candlepin by candlepin.

the class JobStatusTranslatorTest method initSourceObject.

@Override
protected JobStatus initSourceObject() {
    // JobStatus uses a JobDetail argument to populate fields instead
    // of setters, which is why we need to mock here.
    JobDetail jobDetail = mock(JobDetail.class);
    JobDataMap jobDataMap = mock(JobDataMap.class);
    Principal principal = mock(Principal.class);
    JobKey jobKey = new JobKey("test-name", "test-group");
    when(jobDetail.getKey()).thenReturn(jobKey);
    when(jobDetail.getJobDataMap()).thenReturn(jobDataMap);
    when(jobDataMap.get(PinsetterJobListener.PRINCIPAL_KEY)).thenReturn(principal);
    when(principal.getPrincipalName()).thenReturn("test-principal-name");
    when(jobDataMap.get(TARGET_TYPE)).thenReturn(CONSUMER);
    when(jobDataMap.get(TARGET_ID)).thenReturn("test-target-id");
    when(jobDataMap.get(OWNER_ID)).thenReturn("test-owner-id");
    when(jobDataMap.get(CORRELATION_ID)).thenReturn("test-correlation-id");
    JobStatus source = new JobStatus(jobDetail);
    source.setState(JobStatus.JobState.CREATED);
    source.setResult("result of job");
    source.setResultData("result data of job");
    return source;
}
Also used : JobStatus(org.candlepin.pinsetter.core.model.JobStatus) JobDetail(org.quartz.JobDetail) JobDataMap(org.quartz.JobDataMap) JobKey(org.quartz.JobKey) Principal(org.candlepin.auth.Principal)

Example 4 with JobStatus

use of org.candlepin.pinsetter.core.model.JobStatus in project candlepin by candlepin.

the class PinsetterJobListenerDatabaseTest method verifyDatabaseConstraintIsNotViolated.

@Test
public void verifyDatabaseConstraintIsNotViolated() {
    JobExecutionException e = mock(JobExecutionException.class);
    String longstr = RandomStringUtils.randomAlphanumeric(300);
    when(e.getMessage()).thenReturn(longstr);
    JobDataMap map = new JobDataMap();
    Principal principal = mock(Principal.class);
    when(principal.getPrincipalName()).thenReturn("test-admin");
    map.put(PinsetterJobListener.PRINCIPAL_KEY, principal);
    map.put(JobStatus.TARGET_TYPE, JobStatus.TargetType.OWNER);
    map.put(JobStatus.TARGET_ID, "10");
    JobDetail detail = mock(JobDetail.class);
    when(detail.getKey()).thenReturn(jobKey("name", "group"));
    when(detail.getJobDataMap()).thenReturn(map);
    JobStatus status = new JobStatus(detail);
    // store a merge so we can find it in the test run
    curator.merge(status);
    JobExecutionContext ctx = mock(JobExecutionContext.class);
    when(ctx.getMergedJobDataMap()).thenReturn(map);
    when(ctx.getJobDetail()).thenReturn(detail);
    // thing to be tested
    listener.jobWasExecuted(ctx, e);
    // verify the message stored is a substring of the long message
    JobStatus verify = curator.find("name");
    assertEquals(longstr.substring(0, JobStatus.RESULT_COL_LENGTH), verify.getResult());
}
Also used : JobStatus(org.candlepin.pinsetter.core.model.JobStatus) JobDataMap(org.quartz.JobDataMap) JobDetail(org.quartz.JobDetail) JobExecutionException(org.quartz.JobExecutionException) JobExecutionContext(org.quartz.JobExecutionContext) Principal(org.candlepin.auth.Principal) Test(org.junit.Test)

Example 5 with JobStatus

use of org.candlepin.pinsetter.core.model.JobStatus in project candlepin by candlepin.

the class PinsetterKernelTest method handleExistingJobStatus.

@Test
public void handleExistingJobStatus() throws Exception {
    pk = new PinsetterKernel(config, jfactory, jlistener, jcurator, sfactory, triggerListener, modeManager);
    JobStatus status = mock(JobStatus.class);
    when(jcurator.find(startsWith(Util.getClassName(JobCleaner.class)))).thenReturn(status);
    when(jcurator.create(any(JobStatus.class))).thenThrow(new EntityExistsException());
    pk.startup();
    verify(sched).start();
    // this test will have 2 jobs each throwing an exception, we should
    // updated both statuses, then schedule both.
    verify(jcurator, atMost(2)).merge(any(JobStatus.class));
    verify(sched, atMost(2)).scheduleJob(any(JobDetail.class), any(Trigger.class));
}
Also used : JobStatus(org.candlepin.pinsetter.core.model.JobStatus) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) CronTrigger(org.quartz.CronTrigger) EntityExistsException(javax.persistence.EntityExistsException) Test(org.junit.Test)

Aggregations

JobStatus (org.candlepin.pinsetter.core.model.JobStatus)56 Test (org.junit.Test)43 JobDetail (org.quartz.JobDetail)22 JobKey (org.quartz.JobKey)10 ArrayList (java.util.ArrayList)7 CandlepinQuery (org.candlepin.model.CandlepinQuery)7 JobDataMap (org.quartz.JobDataMap)7 JobExecutionException (org.quartz.JobExecutionException)7 Trigger (org.quartz.Trigger)6 JobStatusDTO (org.candlepin.dto.api.v1.JobStatusDTO)5 TransformedCandlepinQuery (org.candlepin.model.TransformedCandlepinQuery)5 SchedulerException (org.quartz.SchedulerException)5 Principal (org.candlepin.auth.Principal)4 Date (java.util.Date)3 HashSet (java.util.HashSet)3 ConsumerPrincipal (org.candlepin.auth.ConsumerPrincipal)3 JobCurator (org.candlepin.model.JobCurator)3 MockResultIterator (org.candlepin.test.MockResultIterator)3 CronTrigger (org.quartz.CronTrigger)3 Transactional (com.google.inject.persist.Transactional)2