Search in sources :

Example 1 with PoolIdAndQuantity

use of org.candlepin.model.dto.PoolIdAndQuantity in project candlepin by candlepin.

the class EntitlerJobTest method serializeJobDataMapForPool.

/**
 * At first glance this seems like a stupid test of Quartz functionality,
 * but its intent is to ensure that what we put into the JobDataMap can
 * be serialized to the database for Quartz clustering. If this test fails
 * 9/10 times one of the objects added does not implement the Serializable
 * interface.
 * @throws IOException
 */
@Test
public void serializeJobDataMapForPool() throws IOException {
    PoolIdAndQuantity[] pQs = new PoolIdAndQuantity[1];
    pQs[0] = new PoolIdAndQuantity("pool10", 1);
    JobDetail detail = EntitlerJob.bindByPoolAndQuantities(consumer, owner.getKey(), pQs);
    serialize(detail.getJobDataMap());
}
Also used : JobDetail(org.quartz.JobDetail) PoolIdAndQuantity(org.candlepin.model.dto.PoolIdAndQuantity) Test(org.junit.Test)

Example 2 with PoolIdAndQuantity

use of org.candlepin.model.dto.PoolIdAndQuantity in project candlepin by candlepin.

the class EntitlerJobTest method recoveryIsFalse.

@Test
public void recoveryIsFalse() {
    PoolIdAndQuantity[] pQs = new PoolIdAndQuantity[1];
    pQs[0] = new PoolIdAndQuantity("pool10", 1);
    JobDetail detail = EntitlerJob.bindByPoolAndQuantities(consumer, owner.getKey(), pQs);
    assertFalse(detail.requestsRecovery());
    assertTrue(detail.isDurable());
}
Also used : JobDetail(org.quartz.JobDetail) PoolIdAndQuantity(org.candlepin.model.dto.PoolIdAndQuantity) Test(org.junit.Test)

Example 3 with PoolIdAndQuantity

use of org.candlepin.model.dto.PoolIdAndQuantity in project candlepin by candlepin.

the class EntitlerJobTest method respondWithValidationErrors.

@Test
public void respondWithValidationErrors() throws JobExecutionException, EntitlementRefusedException {
    PoolIdAndQuantity[] pQs = new PoolIdAndQuantity[1];
    pQs[0] = new PoolIdAndQuantity("pool10", 1);
    JobDetail detail = EntitlerJob.bindByPoolAndQuantities(consumer, owner.getKey(), pQs);
    JobExecutionContext ctx = mock(JobExecutionContext.class);
    when(ctx.getMergedJobDataMap()).thenReturn(detail.getJobDataMap());
    HashMap<String, ValidationResult> mapResult = new HashMap<>();
    ValidationResult result = new ValidationResult();
    result.addError("rulefailed.no.entitlements.available");
    mapResult.put("hello", result);
    when(e.bindByPoolQuantities(eq(consumerUuid), anyMapOf(String.class, Integer.class))).thenThrow(new EntitlementRefusedException(mapResult));
    EntitlerJob job = new EntitlerJob(e, null, pC, i18n);
    injector.injectMembers(job);
    Pool p = new Pool();
    p.setId("hello");
    CandlepinQuery cqmock = mock(CandlepinQuery.class);
    when(cqmock.iterator()).thenReturn(Arrays.asList(p).iterator());
    when(pC.listAllByIds(anyListOf(String.class))).thenReturn(cqmock);
    job.execute(ctx);
    ArgumentCaptor<Object> argumentCaptor = ArgumentCaptor.forClass(Object.class);
    verify(ctx).setResult(argumentCaptor.capture());
    List<PoolIdAndErrors> resultErrors = (List<PoolIdAndErrors>) argumentCaptor.getValue();
    assertEquals(1, resultErrors.size());
    assertEquals("hello", resultErrors.get(0).getPoolId());
    assertEquals(1, resultErrors.get(0).getErrors().size());
    assertEquals("No subscriptions are available from the pool with ID \"hello\".", resultErrors.get(0).getErrors().get(0));
}
Also used : PoolIdAndQuantity(org.candlepin.model.dto.PoolIdAndQuantity) PoolIdAndErrors(org.candlepin.model.dto.PoolIdAndErrors) HashMap(java.util.HashMap) EntitlementRefusedException(org.candlepin.policy.EntitlementRefusedException) CandlepinQuery(org.candlepin.model.CandlepinQuery) ValidationResult(org.candlepin.policy.ValidationResult) JobDetail(org.quartz.JobDetail) JobExecutionContext(org.quartz.JobExecutionContext) Pool(org.candlepin.model.Pool) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 4 with PoolIdAndQuantity

use of org.candlepin.model.dto.PoolIdAndQuantity in project candlepin by candlepin.

the class EntitlerJobTest method handleException.

@Test(expected = JobExecutionException.class)
public void handleException() throws JobExecutionException, EntitlementRefusedException {
    PoolIdAndQuantity[] pQs = new PoolIdAndQuantity[1];
    pQs[0] = new PoolIdAndQuantity("pool10", 1);
    JobDetail detail = EntitlerJob.bindByPoolAndQuantities(consumer, owner.getKey(), pQs);
    JobExecutionContext ctx = mock(JobExecutionContext.class);
    when(ctx.getMergedJobDataMap()).thenReturn(detail.getJobDataMap());
    Class<HashMap<String, Integer>> className = (Class<HashMap<String, Integer>>) (Class) Map.class;
    ArgumentCaptor<HashMap<String, Integer>> pqMapCaptor = ArgumentCaptor.forClass(className);
    when(e.bindByPoolQuantities(eq(consumerUuid), pqMapCaptor.capture())).thenThrow(new ForbiddenException("job should fail"));
    EntitlerJob job = new EntitlerJob(e, null, null, null);
    injector.injectMembers(job);
    job.execute(ctx);
}
Also used : ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) PoolIdAndQuantity(org.candlepin.model.dto.PoolIdAndQuantity) HashMap(java.util.HashMap) JobDetail(org.quartz.JobDetail) JobExecutionContext(org.quartz.JobExecutionContext) HashMap(java.util.HashMap) Map(org.hibernate.mapping.Map) Test(org.junit.Test)

Example 5 with PoolIdAndQuantity

use of org.candlepin.model.dto.PoolIdAndQuantity in project candlepin by candlepin.

the class EntitlerJob method toExecute.

@Override
public void toExecute(JobExecutionContext ctx) throws JobExecutionException {
    try {
        JobDataMap map = ctx.getMergedJobDataMap();
        String uuid = (String) map.get(JobStatus.TARGET_ID);
        PoolIdAndQuantity[] poolQuantities = (PoolIdAndQuantity[]) map.get("pool_and_quantities");
        Map<String, Integer> poolMap = new HashMap<>();
        for (PoolIdAndQuantity poolIdAndQuantity : poolQuantities) {
            poolMap.put(poolIdAndQuantity.getPoolId(), poolIdAndQuantity.getQuantity());
        }
        List<Entitlement> ents = entitler.bindByPoolQuantities(uuid, poolMap);
        entitler.sendEvents(ents);
        PoolIdAndQuantity[] consumed = new PoolIdAndQuantity[ents.size()];
        for (int i = 0; i < ents.size(); i++) {
            consumed[i] = new PoolIdAndQuantity(ents.get(i).getPool().getId(), ents.get(i).getQuantity());
        }
        ctx.setResult(Arrays.asList(consumed));
        poolCurator.clear();
    } catch (EntitlementRefusedException e) {
        log.error("EntitlerJob encountered a problem, translating errors", e);
        Map<String, ValidationResult> validationResults = e.getResults();
        EntitlementRulesTranslator translator = new EntitlementRulesTranslator(i18n);
        List<PoolIdAndErrors> poolErrors = new ArrayList<>();
        for (Pool pool : poolCurator.listAllByIds(validationResults.keySet())) {
            List<String> errorMessages = new ArrayList<>();
            for (ValidationError error : validationResults.get(pool.getId()).getErrors()) {
                errorMessages.add(translator.poolErrorToMessage(pool, error));
            }
            poolErrors.add(new PoolIdAndErrors(pool.getId(), errorMessages));
        }
        ctx.setResult(poolErrors);
    }// so that the job will be properly cleaned up on failure.
     catch (Exception e) {
        log.error("EntitlerJob encountered a problem.", e);
        throw new JobExecutionException(e.getMessage(), e, false);
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) PoolIdAndQuantity(org.candlepin.model.dto.PoolIdAndQuantity) PoolIdAndErrors(org.candlepin.model.dto.PoolIdAndErrors) HashMap(java.util.HashMap) EntitlementRefusedException(org.candlepin.policy.EntitlementRefusedException) SchedulerException(org.quartz.SchedulerException) EntitlementRefusedException(org.candlepin.policy.EntitlementRefusedException) JobExecutionException(org.quartz.JobExecutionException) JobExecutionException(org.quartz.JobExecutionException) ArrayList(java.util.ArrayList) List(java.util.List) Pool(org.candlepin.model.Pool) ValidationError(org.candlepin.policy.ValidationError) Entitlement(org.candlepin.model.Entitlement) EntitlementRulesTranslator(org.candlepin.policy.js.entitlement.EntitlementRulesTranslator) HashMap(java.util.HashMap) Map(java.util.Map) JobDataMap(org.quartz.JobDataMap)

Aggregations

PoolIdAndQuantity (org.candlepin.model.dto.PoolIdAndQuantity)8 Test (org.junit.Test)6 JobDetail (org.quartz.JobDetail)6 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Pool (org.candlepin.model.Pool)3 JobExecutionContext (org.quartz.JobExecutionContext)3 Entitlement (org.candlepin.model.Entitlement)2 PoolIdAndErrors (org.candlepin.model.dto.PoolIdAndErrors)2 EntitlementRefusedException (org.candlepin.policy.EntitlementRefusedException)2 Map (java.util.Map)1 ForbiddenException (org.candlepin.common.exceptions.ForbiddenException)1 CandlepinQuery (org.candlepin.model.CandlepinQuery)1 ValidationError (org.candlepin.policy.ValidationError)1 ValidationResult (org.candlepin.policy.ValidationResult)1 EntitlementRulesTranslator (org.candlepin.policy.js.entitlement.EntitlementRulesTranslator)1 Map (org.hibernate.mapping.Map)1 JobDataMap (org.quartz.JobDataMap)1 JobExecutionException (org.quartz.JobExecutionException)1