EntitlerJob implements AsyncJob { @Override public void execute(final JobExecutionContext context) throws JobExecutionException { try { final JobArguments arguments = context.getJobArguments(); final String uuid = arguments.getAsString(CONSUMER_UUID_KEY); final String poolId = arguments.getAsString(POOL_ID_KEY); final Integer poolQuantity = arguments.getAsInteger(POOL_QUANTITY_KEY); final Map<String, Integer> poolMap = new HashMap<>(); poolMap.put(poolId, poolQuantity); final List<Entitlement> ents = entitler.bindByPoolQuantities(uuid, poolMap); entitler.sendEvents(ents); final List<PoolIdAndQuantity> consumed = ents.stream() .map(ent -> new PoolIdAndQuantity(ent.getPool().getId(), ent.getQuantity())) .collect(Collectors.toList()); poolCurator.clear(); context.setJobResult(consumed); } catch (EntitlementRefusedException e) { log.error("EntitlerJob encountered a problem, translating errors", e); context.setJobResult(this.collectErrors(e.getResults())); } catch (Exception e) { log.error("EntitlerJob encountered a problem.", e); throw new JobExecutionException(e.getMessage(), e, false); } } @Inject  EntitlerJob(final Entitler e, final PoolCurator p, final I18n i18n); @Override void execute(final JobExecutionContext context); static EntitlerJobConfig createConfig(int limit); static final String JOB_KEY; static final String JOB_NAME; static final String CFG_JOB_THROTTLE; static final int DEFAULT_THROTTLE; }
@Test public void bindByPoolExec() throws JobExecutionException, EntitlementRefusedException { final JobConfig config = createJobConfig(); final JobExecutionContext context = createJobContext(config); final List<Entitlement> entitlements = createEntitlements(); final EntitlerJob job = createEntitlerJob(poolCurator, i18n); ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class); job.execute(context); verify(context, times(1)).setJobResult(captor.capture()); Object result = captor.getValue(); assertThat(result, instanceOf(List.class)); List<PoolIdAndQuantity> resultQuantities = (List<PoolIdAndQuantity>) result; verify(entitler).bindByPoolQuantities(eq(CONSUMER_UUID), anyMap()); verify(entitler).sendEvents(eq(entitlements)); assertEquals(1, resultQuantities.size()); assertEquals(POOL_ID, resultQuantities.get(0).getPoolId()); assertEquals(100, resultQuantities.get(0).getQuantity().intValue()); }
@Test public void handleException() throws EntitlementRefusedException { final JobConfig config = createJobConfig(); final JobExecutionContext ctx = createJobContext(config); when(entitler.bindByPoolQuantities(eq(CONSUMER_UUID), anyMap())) .thenThrow(new ForbiddenException("job should fail")); final EntitlerJob job = createEntitlerJob(poolCurator, i18n); assertThrows(JobExecutionException.class, () -> job.execute(ctx)); }
@Test @SuppressWarnings("unchecked") public void respondWithValidationErrors() throws JobExecutionException, EntitlementRefusedException { final JobConfig config = createJobConfig(); final JobExecutionContext ctx = createJobContext(config); final String poolId = "hello"; stubEntitlerErrorResult(poolId); stubPoolCuratorPoolsById(); EntitlerJob job = createEntitlerJob(poolCurator, i18n); ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class); job.execute(ctx); verify(ctx, times(1)).setJobResult(captor.capture()); Object result = captor.getValue(); assertThat(result, instanceOf(List.class)); List<PoolIdAndErrors> resultErrors = (List<PoolIdAndErrors>) result; assertEquals(1, resultErrors.size()); assertEquals(poolId, resultErrors.get(0).getPoolId()); assertEquals(1, resultErrors.get(0).getErrors().size()); assertEquals( "No subscriptions are available from the pool with ID \"" + poolId + "\".", resultErrors.get(0).getErrors().get(0)); }