AsyncJobStatusCurator extends AbstractHibernateCurator<AsyncJobStatus> { public List<AsyncJobStatus> findJobs(AsyncJobStatusQueryBuilder queryBuilder) { CriteriaBuilder criteriaBuilder = this.getEntityManager().getCriteriaBuilder(); CriteriaQuery<AsyncJobStatus> criteriaQuery = criteriaBuilder.createQuery(AsyncJobStatus.class); Root<AsyncJobStatus> job = criteriaQuery.from(AsyncJobStatus.class); criteriaQuery.select(job); List<Predicate> predicates = this.buildJobQueryPredicates(criteriaBuilder, job, queryBuilder); if (predicates.size() > 0) { Predicate[] predicateArray = new Predicate[predicates.size()]; criteriaQuery.where(predicates.toArray(predicateArray)); } List<Order> order = this.buildJobQueryOrder(criteriaBuilder, job, queryBuilder); if (order.size() > 0) { criteriaQuery.orderBy(order); } TypedQuery<AsyncJobStatus> query = this.getEntityManager() .createQuery(criteriaQuery); if (queryBuilder != null) { Integer offset = queryBuilder.getOffset(); if (offset != null && offset > 0) { query.setFirstResult(offset); } Integer limit = queryBuilder.getLimit(); if (limit != null && limit > 0) { query.setMaxResults(limit); } } return query.getResultList(); } AsyncJobStatusCurator(); List<AsyncJobStatus> getJobsInState(Collection<JobState> states); List<AsyncJobStatus> getJobsInState(JobState... states); List<AsyncJobStatus> getNonTerminalJobs(); List<AsyncJobStatus> findJobs(AsyncJobStatusQueryBuilder queryBuilder); long getJobCount(AsyncJobStatusQueryBuilder queryBuilder); int deleteJobs(AsyncJobStatusQueryBuilder queryBuilder); int updateJobState(AsyncJobStatusQueryBuilder queryBuilder, JobState state); List<String> fetchJobIdsByArguments(String jobKey, Map<String, String> arguments); static final int MAX_JOB_ARGUMENTS_PER_QUERY; }
@Test public void testFindJobsByJobIdsWithExtraneous() { List<String> keys = Arrays.asList("job_key-1", "job_key-2", "job_key-3"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); List<String> expectedJobIds = new Random().ints(Math.min(3, created.size()), 0, created.size()) .mapToObj(i -> created.get(i).getId()) .collect(Collectors.toList()); long expected = expectedJobIds.size(); assertTrue(expected > 0); List<String> extraneousJobIds = Arrays.asList("extra_id-1", "extra_id-2", "extra_id-3"); List<String> jobIds = new LinkedList<>(); jobIds.addAll(expectedJobIds); jobIds.addAll(extraneousJobIds); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setJobIds(jobIds); List<AsyncJobStatus> fetched = this.asyncJobCurator.findJobs(queryBuilder); assertNotNull(fetched); assertEquals(expected, fetched.size()); for (AsyncJobStatus job : fetched) { assertTrue(expectedJobIds.contains(job.getId())); assertFalse(extraneousJobIds.contains(job.getId())); } }
@Test public void testFindJobsBySingleKey() { List<String> keys = Arrays.asList("job_key-1", "job_key-2", "job_key-3"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); List<String> jobKeys = Arrays.asList("job_key-1"); long expected = created.stream() .filter(job -> jobKeys.contains(job.getJobKey())) .count(); assertTrue(expected > 0); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setJobKeys(jobKeys); List<AsyncJobStatus> fetched = this.asyncJobCurator.findJobs(queryBuilder); assertNotNull(fetched); assertEquals(expected, fetched.size()); for (AsyncJobStatus job : fetched) { assertTrue(jobKeys.contains(job.getJobKey())); } }
@Test public void testFindJobsByMultipleKeys() { List<String> keys = Arrays.asList("job_key-1", "job_key-2", "job_key-3"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); List<String> jobKeys = Arrays.asList("job_key-1", "job_key-3"); long expected = created.stream() .filter(job -> jobKeys.contains(job.getJobKey())) .count(); assertTrue(expected > 0); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setJobKeys(jobKeys); List<AsyncJobStatus> fetched = this.asyncJobCurator.findJobs(queryBuilder); assertNotNull(fetched); assertEquals(expected, fetched.size()); for (AsyncJobStatus job : fetched) { assertTrue(jobKeys.contains(job.getJobKey())); } }
@Test public void testFindJobsByMultipleKeysWithExtraneousKeys() { List<String> keys = Arrays.asList("job_key-1", "job_key-2", "job_key-3"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); List<String> expectedKeys = Arrays.asList("job_key-1", "job_key-3"); List<String> extraneousKeys = Arrays.asList("job_key-4", "job_key-6"); long expected = created.stream() .filter(job -> expectedKeys.contains(job.getJobKey())) .count(); assertTrue(expected > 0); List<String> jobKeys = new LinkedList<>(); jobKeys.addAll(expectedKeys); jobKeys.addAll(extraneousKeys); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setJobKeys(jobKeys); List<AsyncJobStatus> fetched = this.asyncJobCurator.findJobs(queryBuilder); assertNotNull(fetched); assertEquals(expected, fetched.size()); for (AsyncJobStatus job : fetched) { assertTrue(jobKeys.contains(job.getJobKey())); assertFalse(extraneousKeys.contains(job.getJobKey())); } }
@Test public void testFindJobsBySingleJobState() { List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); List<JobState> jobStates = Arrays.asList(JobState.RUNNING); long expected = created.stream() .filter(job -> jobStates.contains(job.getState())) .count(); assertTrue(expected > 0); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setJobStates(jobStates); List<AsyncJobStatus> fetched = this.asyncJobCurator.findJobs(queryBuilder); assertNotNull(fetched); assertEquals(expected, fetched.size()); for (AsyncJobStatus job : fetched) { assertTrue(jobStates.contains(job.getState())); } }
@Test public void testFindJobsByMultipleJobStates() { List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); List<JobState> jobStates = Arrays.asList(JobState.QUEUED, JobState.FINISHED); long expected = created.stream() .filter(job -> jobStates.contains(job.getState())) .count(); assertTrue(expected > 0); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setJobStates(jobStates); List<AsyncJobStatus> fetched = this.asyncJobCurator.findJobs(queryBuilder); assertNotNull(fetched); assertEquals(expected, fetched.size()); for (AsyncJobStatus job : fetched) { assertTrue(jobStates.contains(job.getState())); } }
@Test public void testFindJobsByMultipleJobStatesWithExtraneousStates() { List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.QUEUED, JobState.RUNNING, JobState.FINISHED); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); List<JobState> expectedStates = Arrays.asList(JobState.QUEUED, JobState.FINISHED); List<JobState> extraneousStates = Arrays.asList(JobState.FAILED, JobState.ABORTED); long expected = created.stream() .filter(job -> expectedStates.contains(job.getState())) .count(); assertTrue(expected > 0); List<JobState> jobStates = new LinkedList<>(); jobStates.addAll(expectedStates); jobStates.addAll(extraneousStates); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setJobStates(jobStates); List<AsyncJobStatus> fetched = this.asyncJobCurator.findJobs(queryBuilder); assertNotNull(fetched); assertEquals(expected, fetched.size()); for (AsyncJobStatus job : fetched) { assertTrue(expectedStates.contains(job.getState())); assertFalse(extraneousStates.contains(job.getState())); } }
@Test public void testFindJobsBySingleOwnerId() { Owner owner1 = this.createOwner(); Owner owner2 = this.createOwner(); Owner owner3 = this.createOwner(); List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, owner1, owner2, owner3); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); List<String> ownerIds = Arrays.asList(owner1.getId()); long expected = created.stream() .filter(j -> j.getContextOwner() != null && ownerIds.contains(j.getContextOwner().getId())) .count(); assertTrue(expected > 0); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setOwnerIds(ownerIds); List<AsyncJobStatus> fetched = this.asyncJobCurator.findJobs(queryBuilder); assertNotNull(fetched); assertEquals(expected, fetched.size()); for (AsyncJobStatus job : fetched) { assertNotNull(job.getContextOwner()); assertTrue(ownerIds.contains(job.getContextOwner().getId())); } }
@Test public void testFindJobsByMultipleOwnerIds() { Owner owner1 = this.createOwner(); Owner owner2 = this.createOwner(); Owner owner3 = this.createOwner(); List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, owner1, owner2, owner3); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); List<String> ownerIds = Arrays.asList(owner1.getId(), owner3.getId()); long expected = created.stream() .filter(j -> j.getContextOwner() != null && ownerIds.contains(j.getContextOwner().getId())) .count(); assertTrue(expected > 0); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setOwnerIds(ownerIds); List<AsyncJobStatus> fetched = this.asyncJobCurator.findJobs(queryBuilder); assertNotNull(fetched); assertEquals(expected, fetched.size()); for (AsyncJobStatus job : fetched) { assertNotNull(job.getContextOwner()); assertTrue(ownerIds.contains(job.getContextOwner().getId())); } }
@Test public void testFindJobsByMultipleOwnerIdsWithExtraneousKeys() { Owner owner1 = this.createOwner(); Owner owner2 = this.createOwner(); Owner owner3 = this.createOwner(); List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, owner1, owner2, owner3); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); List<String> expectedKeys = Arrays.asList(owner1.getId(), owner3.getId()); List<String> extraneousKeys = Arrays.asList("extra_key-1", "extra_key-2"); long expected = created.stream() .filter(j -> j.getContextOwner() != null && expectedKeys.contains(j.getContextOwner().getId())) .count(); assertTrue(expected > 0); List<String> ownerIds = new LinkedList<>(); ownerIds.addAll(expectedKeys); ownerIds.addAll(extraneousKeys); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setOwnerIds(ownerIds); List<AsyncJobStatus> fetched = this.asyncJobCurator.findJobs(queryBuilder); assertNotNull(fetched); assertEquals(expected, fetched.size()); for (AsyncJobStatus job : fetched) { assertNotNull(job.getContextOwner()); assertTrue(expectedKeys.contains(job.getContextOwner().getId())); assertFalse(extraneousKeys.contains(job.getContextOwner().getId())); } }
@Test public void testFindJobsByNullOwnerId() { Owner owner1 = this.createOwner(); Owner owner2 = this.createOwner(); Owner owner3 = this.createOwner(); List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, owner1, owner2, owner3); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); List<String> ownerIds = new LinkedList<>(); ownerIds.add(null); long expected = created.stream() .filter(job -> job.getContextOwner() == null) .count(); assertTrue(expected > 0); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setOwnerIds(ownerIds); List<AsyncJobStatus> fetched = this.asyncJobCurator.findJobs(queryBuilder); assertNotNull(fetched); assertEquals(expected, fetched.size()); for (AsyncJobStatus job : fetched) { assertNull(job.getContextOwner()); } }
@Test public void testFindJobsByMixedOwnerIds() { Owner owner1 = this.createOwner(); Owner owner2 = this.createOwner(); Owner owner3 = this.createOwner(); List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, owner1, owner2, owner3); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); List<String> ownerIds = new LinkedList<>(); ownerIds.add(null); ownerIds.add(owner2.getId()); long expected = created.stream() .filter(j -> j.getContextOwner() == null || ownerIds.contains(j.getContextOwner().getId())) .count(); assertTrue(expected > 0); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setOwnerIds(ownerIds); List<AsyncJobStatus> fetched = this.asyncJobCurator.findJobs(queryBuilder); assertNotNull(fetched); assertEquals(expected, fetched.size()); for (AsyncJobStatus job : fetched) { assertTrue(job.getContextOwner() == null || ownerIds.contains(job.getContextOwner().getId())); } }
@Test public void testFindJobsByLastUpdateTimeBeforeDate() { List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); Date now = new Date(); long expected = created.stream() .filter(job -> now.compareTo(job.getUpdated()) >= 0) .count(); assertTrue(expected > 0); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setEndDate(now); List<AsyncJobStatus> fetched = this.asyncJobCurator.findJobs(queryBuilder); assertNotNull(fetched); assertEquals(expected, fetched.size()); for (AsyncJobStatus job : fetched) { assertTrue(now.compareTo(job.getUpdated()) >= 0); } }
@Test public void testFindJobsByLastUpdateTimeAfterDate() { List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); Date now = new Date(); long expected = created.stream() .filter(job -> now.compareTo(job.getUpdated()) <= 0) .count(); assertTrue(expected > 0); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setStartDate(now); List<AsyncJobStatus> fetched = this.asyncJobCurator.findJobs(queryBuilder); assertNotNull(fetched); assertEquals(expected, fetched.size()); for (AsyncJobStatus job : fetched) { assertTrue(now.compareTo(job.getUpdated()) <= 0); } }
@Test public void testFindJobsByLastUpdateTimeInRange() { List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); Date start = Util.addDaysToDt(-1); Date end = Util.addDaysToDt(1); long expected = created.stream() .filter(job -> start.compareTo(job.getUpdated()) <= 0 && end.compareTo(job.getUpdated()) >= 0) .count(); assertTrue(expected > 0); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setStartDate(start) .setEndDate(end); List<AsyncJobStatus> fetched = this.asyncJobCurator.findJobs(queryBuilder); assertNotNull(fetched); assertEquals(expected, fetched.size()); for (AsyncJobStatus job : fetched) { assertTrue(start.compareTo(job.getUpdated()) <= 0); assertTrue(end.compareTo(job.getUpdated()) >= 0); } }
@Test public void testFindJobsWithOffset() { List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); int offset = created.size() / 2; long expected = created.size() - offset; assertTrue(offset > 0); assertTrue(expected > 0); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setOffset(offset); List<AsyncJobStatus> fetched = this.asyncJobCurator.findJobs(queryBuilder); List<AsyncJobStatus> allJobs = this.asyncJobCurator.findJobs(null); assertNotNull(fetched); assertNotNull(allJobs); assertEquals(expected, fetched.size()); assertEquals(created.size(), allJobs.size()); }
@Test public void testFindJobsWithLimit() { List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); int limit = created.size() / 2; long expected = limit; assertTrue(limit > 0); assertTrue(expected > 0); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setLimit(limit); List<AsyncJobStatus> fetched = this.asyncJobCurator.findJobs(queryBuilder); List<AsyncJobStatus> allJobs = this.asyncJobCurator.findJobs(null); assertNotNull(fetched); assertNotNull(allJobs); assertEquals(expected, fetched.size()); assertEquals(created.size(), allJobs.size()); }
@Test public void testFindJobsWithSingleAscendingOrder() { List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setOrder(Arrays.asList(new AsyncJobStatusQueryBuilder.Order("id", false))); List<AsyncJobStatus> fetched = this.asyncJobCurator.findJobs(queryBuilder); assertNotNull(fetched); String lastId = null; for (AsyncJobStatus status : fetched) { if (lastId != null) { assertThat(status.getId(), greaterThanOrEqualTo(lastId)); } lastId = status.getId(); } }
@Test public void testFindJobsWithSingleDescendingOrder() { List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setOrder(Arrays.asList(new AsyncJobStatusQueryBuilder.Order("id", true))); List<AsyncJobStatus> fetched = this.asyncJobCurator.findJobs(queryBuilder); assertNotNull(fetched); String lastId = null; for (AsyncJobStatus status : fetched) { if (lastId != null) { assertThat(status.getId(), lessThanOrEqualTo(lastId)); } lastId = status.getId(); } }
@Test public void testFindJobsWithMultipleMixedOrders() { List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); List<AsyncJobStatusQueryBuilder.Order> order = Arrays.asList( new AsyncJobStatusQueryBuilder.Order("jobKey", false), new AsyncJobStatusQueryBuilder.Order("id", true)); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setOrder(order); List<AsyncJobStatus> fetched = this.asyncJobCurator.findJobs(queryBuilder); assertNotNull(fetched); String lastKey = null; String lastId = null; for (AsyncJobStatus status : fetched) { if (lastKey != null) { assertThat(status.getJobKey(), greaterThanOrEqualTo(lastKey)); if (lastKey.equals(status.getJobKey())) { assertThat(status.getId(), lessThanOrEqualTo(lastId)); } } lastId = status.getId(); lastKey = status.getJobKey(); } }
@Test public void testFindJobsWithInvalidOrderKey() { AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setOrder(Arrays.asList(new AsyncJobStatusQueryBuilder.Order("bad_key", true))); assertThrows(InvalidOrderKeyException.class, () -> this.asyncJobCurator.findJobs(queryBuilder)); }
@Test public void testFindJobsByMultipleParameters() { Owner owner1 = this.createOwner(); Owner owner2 = this.createOwner(); List<String> keys = Arrays.asList("job_key-1", "job_key-2", "job_key-3"); List<Owner> owners = Arrays.asList(null, owner1, owner2); List<JobState> states = Arrays.asList(JobState.QUEUED, JobState.RUNNING, JobState.FAILED, JobState.FINISHED); List<String> principals = Arrays.asList("principal-1", "principal-2", "principal-3"); List<String> origins = Arrays.asList("origin-1", "origin-2", "origin-3"); List<String> executors = Arrays.asList("executor-1", "executor-2", "executor-3"); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, principals, origins, executors); List<String> jobKeys = Arrays.asList("job_key-1"); List<JobState> jobStates = Arrays.asList(JobState.QUEUED, JobState.RUNNING); List<String> ownerIds = Arrays.asList(owner2.getId()); List<String> jobPrincipals = Arrays.asList("principal-2"); List<String> jobOrigins = Arrays.asList("origin-3"); List<String> jobExecutors = Arrays.asList("executor-1"); Date start = Util.addDaysToDt(-1); Date end = Util.addDaysToDt(1); long expected = created.stream() .filter(job -> jobKeys.contains(job.getJobKey())) .filter(job -> jobStates.contains(job.getState())) .filter(j -> j.getContextOwner() != null && ownerIds.contains(j.getContextOwner().getId())) .filter(job -> start.compareTo(job.getUpdated()) <= 0 && end.compareTo(job.getUpdated()) >= 0) .filter(job -> jobPrincipals.contains(job.getPrincipalName())) .filter(job -> jobOrigins.contains(job.getOrigin())) .filter(job -> jobExecutors.contains(job.getExecutor())) .count(); assertTrue(expected > 0); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder() .setJobKeys(jobKeys) .setJobStates(jobStates) .setOwnerIds(ownerIds) .setPrincipalNames(jobPrincipals) .setOrigins(jobOrigins) .setExecutors(jobExecutors) .setStartDate(start) .setEndDate(end); List<AsyncJobStatus> fetched = this.asyncJobCurator.findJobs(queryBuilder); assertNotNull(fetched); assertEquals(expected, fetched.size()); for (AsyncJobStatus job : fetched) { assertTrue(jobKeys.contains(job.getJobKey())); assertTrue(jobStates.contains(job.getState())); assertTrue(job.getContextOwner() != null && ownerIds.contains(job.getContextOwner().getId())); assertTrue(start.compareTo(job.getUpdated()) <= 0 && end.compareTo(job.getUpdated()) >= 0); assertTrue(jobPrincipals.contains(job.getPrincipalName())); assertTrue(jobOrigins.contains(job.getOrigin())); assertTrue(jobExecutors.contains(job.getExecutor())); } }
@Test public void testFindJobsWithoutParameters() { List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); AsyncJobStatusQueryBuilder queryBuilder = new AsyncJobStatusQueryBuilder(); List<AsyncJobStatus> fetched = this.asyncJobCurator.findJobs(queryBuilder); assertNotNull(fetched); assertEquals(created.size(), fetched.size()); for (AsyncJobStatus job : fetched) { assertTrue(created.contains(job)); } }
@Test public void testFindJobsWithNullBuilder() { List<String> keys = Arrays.asList("job_key-1", "job_key-2"); List<JobState> states = Arrays.asList(JobState.values()); List<Owner> owners = Arrays.asList(null, this.createOwner(), this.createOwner()); List<AsyncJobStatus> created = this.createJobsForQueryTests(keys, states, owners, null, null, null); List<AsyncJobStatus> fetched = this.asyncJobCurator.findJobs(null); assertNotNull(fetched); assertEquals(created.size(), fetched.size()); for (AsyncJobStatus job : fetched) { assertTrue(created.contains(job)); } }