package com.github.signed.sandboxes.spring.data.bg; import com.github.signed.sandboxes.spring.data.H2Configuration; import com.github.signed.sandboxes.spring.data.HibernateAutoGenerateDdlConfiguration; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import static com.github.signed.sandboxes.spring.data.bg.JobState.Cancelled; import static com.github.signed.sandboxes.spring.data.bg.JobState.Done; import static com.github.signed.sandboxes.spring.data.bg.JobState.InProgress; import static com.github.signed.sandboxes.spring.data.bg.JobState.Pending; import static com.github.signed.sandboxes.spring.data.bg.JobType.CancelAction; import static com.github.signed.sandboxes.spring.data.bg.JobType.ExecuteAction; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = {H2Configuration.class, HibernateAutoGenerateDdlConfiguration.class}) public class JobsRepositoryTest { @Autowired private JobsRepository repository; private final List<JobBuilder> existingJobs = new ArrayList<>(); @Test public void blub() throws Exception { thereIsAJobForFirst().withType(ExecuteAction).withState(Done); thereIsAJobForFirst().withType(CancelAction).withState(Cancelled); thereIsAJobForFirst().withType(CancelAction).withState(Pending); thereIsAJobForSecond().withType(ExecuteAction).withState(Pending); thereIsAJobForThird().withType(ExecuteAction).withState(Done); thereIsAJobForThird().withType(CancelAction).withState(InProgress); storeExistingJobs(); List<Job> jobs = repository.letsSeeAllJobsForAReferenceKeyWhereAtLeastOneJobIsIn(Pending); System.out.println(jobs); } private void storeExistingJobs() { repository.save(existingJobs.stream().map(JobBuilder::build).collect(Collectors.toList())); } private JobBuilder thereIsAJobForFirst() { return thereIsAJob().withReferenceKey(1L); } private JobBuilder thereIsAJobForSecond() { return thereIsAJob().withReferenceKey(2L); } private JobBuilder thereIsAJobForThird() { return thereIsAJob().withReferenceKey(3L); } private JobBuilder thereIsAJob() { JobBuilder jobBuilder = JobBuilder.aJob(); existingJobs.add(jobBuilder); return jobBuilder; } }