MongoJobRepository extends AbstractMongoRepository<String, JobInfo> implements JobRepository { @Override public List<JobInfo> findLatestJobsDistinct() { final List<String> allJobIds = findAllJobIdsDistinct(); return collection() .find(in(ID, allJobIds)) .maxTime(mongoProperties.getDefaultReadTimeout(), TimeUnit.MILLISECONDS) .map(this::decode) .into(new ArrayList<>()); } MongoJobRepository(final MongoDatabase mongoDatabase,
                              final String jobInfoCollectionName,
                              final MongoProperties mongoProperties); @Override JobStatus findStatus(final String jobId); @Override void removeIfStopped(final String id); @Override void appendMessage(final String jobId, final JobMessage jobMessage); @Override void setJobStatus(final String jobId, final JobStatus jobStatus); @Override void setLastUpdate(final String jobId, final OffsetDateTime lastUpdate); @Override List<JobInfo> findLatest(final int maxCount); @Override List<JobInfo> findLatestJobsDistinct(); List<String> findAllJobIdsDistinct(); @Override List<JobInfo> findLatestBy(final String type, final int maxCount); @Override List<JobInfo> findByType(final String type); @Override List<JobInfo> findRunningWithoutUpdateSince(final OffsetDateTime timeOffset); @Override List<JobInfo> findAllJobInfoWithoutMessages(); @Override void keepJobMessagesWithinMaximumSize(String jobId); static final String ID; }
@Test public void shouldFindLatestDistinct() throws Exception { final OffsetDateTime now = OffsetDateTime.now(); final JobInfo eins = someRunningJobInfo("http: final JobInfo zwei = someRunningJobInfo("http: final JobInfo drei = someRunningJobInfo("http: final JobInfo vier = someRunningJobInfo("http: final JobInfo fuenf = someRunningJobInfo("http: repo.createOrUpdate(eins); repo.createOrUpdate(zwei); repo.createOrUpdate(drei); repo.createOrUpdate(vier); repo.createOrUpdate(fuenf); final List<JobInfo> latestDistinct = repo.findLatestJobsDistinct(); assertThat(latestDistinct, hasSize(3)); assertThat(latestDistinct, containsInAnyOrder(fuenf, zwei, drei)); }