private JobInfo createJobInfo(final String jobType) { return newJobInfo(uuidProvider.getUuid(), jobType, clock, systemInfo.getHostname()); }
private JobInfo.Builder defaultJobInfo() { return newJobInfo(JOB_ID, JOB_TYPE, clock, HOSTNAME).copy(); }
private JobInfo defaultJobInfo(String jobId, int secondsAgo) { OffsetDateTime lastUpdated = OffsetDateTime.ofInstant(now(clock).minus(secondsAgo, SECONDS), systemDefault()); return newJobInfo(jobId, "someJobType", OffsetDateTime.MIN, lastUpdated, Optional.empty(), OK, emptyList(), clock, "someHostname"); } }
private JobInfo someRunningJobInfo(final String jobId, final String type, final OffsetDateTime started) { return JobInfo.newJobInfo( jobId, type, started, started.plus(1, SECONDS), Optional.empty(), OK, Collections.emptyList(), systemDefaultZone(), "localhost" ); } }
@Test public void shouldReturnAllJobs() throws IOException { // given JobInfo firstJob = newJobInfo("42", "TEST", fixed(ofEpochMilli(0), systemDefault()), "localhost"); JobInfo secondJob = newJobInfo("42", "TEST", fixed(ofEpochMilli(1), systemDefault()), "localhost"); when(jobService.findJobs(Optional.<String>empty(), 100)).thenReturn(asList(firstJob, secondJob)); // when Object job = jobsController.getJobsAsJson(null, 100, false, false, mock(HttpServletRequest.class)); // then assertThat(job, is(asList(representationOf(firstJob, null, false, "", ""), representationOf(secondJob, null, false, "", "")))); }
private JobInfo jobInfoWithRuntime(int finishAmount, ChronoUnit unit) { final Clock clock = fixed(Instant.now(), systemDefault()); final OffsetDateTime startTime = now(clock); final OffsetDateTime finishedTime = startTime.plus(finishAmount, unit); return newJobInfo("foo", "TEST", startTime, finishedTime, of(finishedTime), OK, emptyList(), clock, "localhost"); } }
@Test @SuppressWarnings("unchecked") public void shouldReturnAllJobsOfTypeAsHtml() { JobInfo firstJob = newJobInfo("42", "SOME_TYPE", systemDefaultZone(), "localhost"); when(jobService.findJobs(Optional.of("SOME_TYPE"), 100)).thenReturn(asList(firstJob)); ModelAndView modelAndView = jobsController.getJobsAsHtml("SOME_TYPE", 100, false, mock(HttpServletRequest.class)); List<JobRepresentation> jobs = (List<JobRepresentation>) modelAndView.getModel().get("jobs"); assertThat(jobs, is(asList(representationOf(firstJob, null, false, "", "")))); }
@Test public void shouldFindAll() { // given repository.createOrUpdate(newJobInfo("oldest", "FOO", fixed(Instant.now().minusSeconds(1), systemDefault()), "localhost")); repository.createOrUpdate(newJobInfo("youngest", "FOO", fixed(Instant.now(), systemDefault()), "localhost")); // when final List<JobInfo> jobInfos = repository.findAll(); // then assertThat(jobInfos.size(), is(2)); assertThat(jobInfos.get(0).getJobId(), is("youngest")); assertThat(jobInfos.get(1).getJobId(), is("oldest")); }
@Test public void shouldFindRunningJobsWithoutUpdatedSinceSpecificDate() throws Exception { // given repository.createOrUpdate(newJobInfo("deadJob", "FOO", fixed(Instant.now().minusSeconds(10), systemDefault()), "localhost")); repository.createOrUpdate(newJobInfo("running", "FOO", fixed(Instant.now(), systemDefault()), "localhost")); // when final List<JobInfo> jobInfos = repository.findRunningWithoutUpdateSince(now().minus(5, ChronoUnit.SECONDS)); // then assertThat(jobInfos, IsCollectionWithSize.hasSize(1)); assertThat(jobInfos.get(0).getJobId(), is("deadJob")); }
@Test public void shouldFindStatusOfJob() throws Exception { //Given final String type = "TEST"; JobInfo jobInfo = newJobInfo("1", type, systemDefaultZone(), "localhost"); repository.createOrUpdate(jobInfo); //When JobStatus status = repository.findStatus("1"); //Then assertThat(status, is(JobStatus.OK)); }
@Test public void shouldRunJob() { // given: String jobType = "bar"; when(jobRunnable.getJobDefinition()).thenReturn(someJobDefinition(jobType)); // when: Optional<String> optionalJobId = jobService.startAsyncJob(jobType); // then: final JobInfo expectedJobInfo = JobInfo.newJobInfo(optionalJobId.get(), jobType, clock, systemInfo.hostname); verify(executorService).execute(any(Runnable.class)); verify(jobRepository).createOrUpdate(expectedJobInfo); verify(jobRunnable).execute(); verify(jobMetaService).aquireRunLock(expectedJobInfo.getJobId(), expectedJobInfo.getJobType()); }
@Test public void shouldFindJobInfoByUri() { // given InMemJobRepository repository = new InMemJobRepository(); // when JobInfo job = newJobInfo(randomUUID().toString(), "MYJOB", clock, "localhost"); repository.createOrUpdate(job); // then assertThat(repository.findOne(job.getJobId()), isPresent()); }
@Test public void shouldNotRemoveRunningJobs() { // given final String testUri = "test"; repository.createOrUpdate(newJobInfo(testUri, "FOO", systemDefaultZone(), "localhost")); // when repository.removeIfStopped(testUri); // then assertThat(repository.size(), is(1L)); }
@Test public void shouldKillJob() { OffsetDateTime now = OffsetDateTime.now(clock); JobInfo jobInfo = JobInfo.newJobInfo("superId", "superType", clock, HOSTNAME); when(jobRepository.findOne("superId")).thenReturn(Optional.of(jobInfo)); jobService.killJob("superId"); JobInfo expected = jobInfo.copy().setStatus(JobInfo.JobStatus.DEAD).setStopped(now).setLastUpdated(now).build(); verify(jobMetaService).releaseRunLock("superType"); verify(jobRepository).createOrUpdate(expected); }
@Test public void shouldStopJob() { OffsetDateTime now = OffsetDateTime.now(clock); Clock earlierClock = offset(clock, Duration.of(-1, MINUTES)); JobInfo jobInfo = JobInfo.newJobInfo("superId", "superType", earlierClock, HOSTNAME); when(jobRepository.findOne("superId")).thenReturn(Optional.of(jobInfo)); jobService.stopJob("superId"); JobInfo expected = jobInfo.copy().setStatus(JobInfo.JobStatus.OK).setStopped(now).setLastUpdated(now).build(); verify(jobMetaService).releaseRunLock("superType"); verify(jobRepository).createOrUpdate(expected); }
private JobInfo someJobInfo(final String jobId) { return JobInfo.newJobInfo( jobId, "SOME_JOB", now(), now(), Optional.of(now()), OK, asList( jobMessage(Level.INFO, "foo", now()), jobMessage(Level.WARNING, "bar", now())), systemDefaultZone(), "localhost" ); }
private JobInfo jobInfo(final String jobId, final String type) { return JobInfo.newJobInfo( jobId, type, now(), now(), Optional.of(now()), OK, asList( jobMessage(Level.INFO, "foo", now()), jobMessage(Level.WARNING, "bar", now())), systemDefaultZone(), "localhost" ); }
private JobInfo jobInfo(final String jobId, final String type) { return JobInfo.newJobInfo( jobId, type, now(), now(), Optional.of(now()), OK, asList( jobMessage(Level.INFO, "foo", now()), jobMessage(Level.WARNING, "bar", now())), systemDefaultZone(), "localhost" ); } }
@Override protected final JobInfo decode(final Document document) { return newJobInfo( document.getString(JobStructure.ID.key()), document.getString(JobStructure.JOB_TYPE.key()), DateTimeConverters.toOffsetDateTime(document.getDate(JobStructure.STARTED.key())), DateTimeConverters.toOffsetDateTime(document.getDate(JobStructure.LAST_UPDATED.key())), ofNullable(DateTimeConverters.toOffsetDateTime(document.getDate(JobStructure.STOPPED.key()))), JobStatus.valueOf(document.getString(JobStructure.STATUS.key())), getMessagesFrom(document), clock, document.getString(JobStructure.HOSTNAME.key())); }
@Override protected final JobInfo decode(final Document document) { return newJobInfo( document.getString(JobStructure.ID.key()), document.getString(JobStructure.JOB_TYPE.key()), toOffsetDateTime(document.getDate(JobStructure.STARTED.key())), toOffsetDateTime(document.getDate(JobStructure.LAST_UPDATED.key())), ofNullable(toOffsetDateTime(document.getDate(JobStructure.STOPPED.key()))), JobStatus.valueOf(document.getString(JobStructure.STATUS.key())), getMessagesFrom(document), clock, document.getString(JobStructure.HOSTNAME.key())); }