@Override public JobMeta getJobMeta(final String jobType) { final Document document = collection .find(eq(ID, jobType)) .maxTime(mongoProperties.getDefaultReadTimeout(), TimeUnit.MILLISECONDS) .first(); if (document != null) { final Map<String, String> meta = document.keySet() .stream() .filter(key -> !key.startsWith("_e_") && !key.equals(ID)) .collect(toMap( key -> key, document::getString )); final boolean isRunning = document.containsKey(KEY_RUNNING); final boolean isDisabled = document.containsKey(KEY_DISABLED); final String comment = document.getString(KEY_DISABLED); return new JobMeta(jobType, isRunning, isDisabled, comment, meta); } else { return new JobMeta(jobType, false, false, "", emptyMap()); } }
@Test @SuppressWarnings("unchecked") public void shouldIndicateErrorIfJobCouldNotBeRetievedFromRepository() { // given when(jobRepository.findLatestBy(anyString(), eq(1+1))).thenThrow(RuntimeException.class); final JobMeta jobMeta = new JobMeta("test", false, false, null, emptyMap()); when(jobMetaRepository.getJobMeta(anyString())).thenReturn(jobMeta); // when final StatusDetail statusDetail = errorOnLastJobFailed.statusDetail(jobDefinition); // then assertThat(statusDetail.getStatus(), is(Status.ERROR)); }
@Test public void shouldNotHaveUriOrRunningIfNoJobPresent() { // given when(jobRepository.findLatestBy(anyString(), eq(1+1))).thenReturn(emptyList()); final JobMeta jobMeta = new JobMeta("test", false, false, null, emptyMap()); when(jobMetaRepository.getJobMeta(anyString())).thenReturn(jobMeta); // when final StatusDetail statusDetail = errorOnLastJobFailed.statusDetail(jobDefinition); // then assertThat(statusDetail.getLinks(), is(emptyList())); assertThat(statusDetail.getDetails(), not(hasKey("Running"))); }
@Test public void shouldIndicateWarningIfLastJobRunWasDead() { // given final List<JobInfo> jobInfos = singletonList(someStoppedJob(DEAD, 1)); when(jobRepository.findLatestBy(anyString(), eq(1+1))).thenReturn(jobInfos); final JobMeta jobMeta = new JobMeta("test", false, false, null, emptyMap()); when(jobMetaRepository.getJobMeta(anyString())).thenReturn(jobMeta); // when final StatusDetail statusDetail = errorOnLastJobFailed.statusDetail(jobDefinition); // then assertThat(statusDetail.getStatus(), is(Status.WARNING)); assertThat(statusDetail.getMessage(), is("Job died")); }
@Test public void shouldAquireRunLock() throws Exception { when(jobMetaRepository.findAllJobTypes()).thenReturn(emptySet()); when(jobMetaRepository.setRunningJob("myJobType", "jobId")).thenReturn(true); when(jobMetaRepository.getJobMeta("myJobType")).thenReturn(new JobMeta("myJobType", false, false, "", emptyMap())); jobMetaService.aquireRunLock("jobId", "myJobType"); verify(jobMetaRepository).setRunningJob("myJobType", "jobId"); }
@Test public void shouldIndicateWarningIfJobRunWasDead() { // given final List<JobInfo> jobInfos = singletonList(someRunningJob(DEAD, 1)); when(jobRepository.findLatestBy(anyString(), eq(1+1))).thenReturn(jobInfos); final JobMeta jobMeta = new JobMeta("test", false, false, null, emptyMap()); when(jobMetaRepository.getJobMeta(anyString())).thenReturn(jobMeta); // when final StatusDetail statusDetail = errorOnLastJobFailed.statusDetail(jobDefinition); // then assertThat(statusDetail.getStatus(), is(Status.WARNING)); assertThat(statusDetail.getMessage(), is("Job died")); }
@Test public void shouldNotAquireLockIfAlreadyRunning() throws Exception { // given when(jobMetaRepository.findAllJobTypes()).thenReturn(emptySet()); when(jobMetaRepository.getJobMeta("myJobType")).thenReturn(new JobMeta("myJobType", true, false, "", emptyMap())); when(jobMetaRepository.setRunningJob("myJobType", "someId")).thenReturn(false); // when assertThrows(JobBlockedException.class, () -> jobMetaService.aquireRunLock("jobId", "myJobType")); }
@Test public void shouldNotStartJobIfBlockedByAnotherJob() throws Exception { // given when(jobMetaRepository.findAllJobTypes()).thenReturn(new HashSet<>(asList("job1", "job2"))); when(jobMetaRepository.getRunningJob("job1")).thenReturn("42"); when(jobMetaRepository.getJobMeta("job2")).thenReturn(new JobMeta("job2", false, false, "", emptyMap())); when(jobMetaRepository.setRunningJob("job2", "first")).thenReturn(true); when(jobMutexGroups.mutexJobTypesFor("job2")).thenReturn(new HashSet<>(asList("job1", "job2"))); // when // then assertThrows(JobBlockedException.class, () -> jobMetaService.aquireRunLock("first", "job2")); verify(jobMetaRepository).clearRunningJob("job2"); }
@Test public void shouldNotStartADisabledJob() { // given when(jobMetaRepository.findAllJobTypes()).thenReturn(singleton("jobType")); when(jobMetaRepository.getJobMeta("jobType")).thenReturn( new JobMeta("jobType", false, true, "", emptyMap()) ); // when // then assertThrows(JobBlockedException.class, () -> jobMetaService.aquireRunLock("someId", "jobType"), "Job 'jobType' is currently disabled"); }
@Test public void shouldReturnStatusDetailWithJobLink() { // given final JobInfo jobInfo = someStoppedJob(OK, 1); final List<JobInfo> jobs = singletonList(jobInfo); when(jobRepository.findLatestBy(anyString(), eq(1+1))).thenReturn(jobs); final JobMeta jobMeta = new JobMeta("test", false, false, null, emptyMap()); when(jobMetaRepository.getJobMeta(anyString())).thenReturn(jobMeta); // when final StatusDetail first = errorOnLastJobFailed.statusDetail(jobDefinition); // then assertThat(first.getLinks().size(), is(1)); assertThat(first.getLinks().get(0).href, is("/someInternalPath/jobs/" + jobInfo.getJobId())); }
@Test public void shouldIndicateOkIfLastOfTwoJobsOk() { // given final List<JobInfo> jobInfos = asList( someStoppedJob(OK, 1), someStoppedJob(ERROR, 2)); when(jobRepository.findLatestBy(anyString(), eq(2+1))).thenReturn(jobInfos); final JobMeta jobMeta = new JobMeta("test", false, false, null, emptyMap()); when(jobMetaRepository.getJobMeta(anyString())).thenReturn(jobMeta); // when final StatusDetail detail = errorOnLastTwoJobsFailed.statusDetail(jobDefinition); // then assertThat(detail.getStatus(), is(Status.OK)); assertThat(detail.getMessage(), is("Last job was successful")); }
@Test @SuppressWarnings("unchecked") public void shouldIndicateThatJobIsNotRunning() { // given final List<JobInfo> jobInfos = singletonList(someStoppedJob(OK, 1)); when(jobRepository.findLatestBy(anyString(), eq(1+1))).thenReturn(jobInfos); final JobMeta jobMeta = new JobMeta("test", false, false, null, emptyMap()); when(jobMetaRepository.getJobMeta(anyString())).thenReturn(jobMeta); // when final StatusDetail statusDetail = errorOnLastJobFailed.statusDetail(jobDefinition); // then assertThat(statusDetail.getDetails(), not(hasKey("Running"))); assertThat(statusDetail.getDetails(), hasKey("Stopped")); }
@Test public void shouldIndicateOkIfLastJobOk() { // given final List<JobInfo> jobs = singletonList(someStoppedJob(OK, 1)); when(jobRepository.findLatestBy(anyString(), eq(1+1))).thenReturn(jobs); final JobMeta jobMeta = new JobMeta("test", false, false, null, emptyMap()); when(jobMetaRepository.getJobMeta(anyString())).thenReturn(jobMeta); // when final StatusDetail first = errorOnLastJobFailed.statusDetail(jobDefinition); final StatusDetail second = warningOnLastJobFailed.statusDetail(jobDefinition); // then assertThat(first.getStatus(), is(Status.OK)); assertThat(first.getMessage(), is("Last job was successful")); assertThat(second.getStatus(), is(Status.OK)); }
@Test public void shouldIndicateWarningIfOneOfTwoJobsOk() { // given final List<JobInfo> jobInfos = asList( someStoppedJob(ERROR, 1), someStoppedJob(OK, 2)); when(jobRepository.findLatestBy(anyString(), eq(2+1))).thenReturn(jobInfos); final JobMeta jobMeta = new JobMeta("test", false, false, null, emptyMap()); when(jobMetaRepository.getJobMeta(anyString())).thenReturn(jobMeta); // when final StatusDetail detail = errorOnLastTwoJobsFailed.statusDetail(jobDefinition); // then assertThat(detail.getStatus(), is(Status.WARNING)); assertThat(detail.getMessage(), is("1 out of 2 job executions failed")); }
@Test public void shouldIndicateErrorIfTwoJobsFailed() { // given final List<JobInfo> jobInfos = asList( someStoppedJob(ERROR, 1), someStoppedJob(ERROR, 2)); when(jobRepository.findLatestBy(anyString(), eq(2+1))).thenReturn(jobInfos); final JobMeta jobMeta = new JobMeta("test", false, false, null, emptyMap()); when(jobMetaRepository.getJobMeta(anyString())).thenReturn(jobMeta); // when final StatusDetail detail = errorOnLastTwoJobsFailed.statusDetail(jobDefinition); // then assertThat(detail.getStatus(), is(Status.ERROR)); assertThat(detail.getMessage(), is("2 out of 2 job executions failed")); }
@Test public void shouldIndicateOkIfLastJobSkipped() { // given final List<JobInfo> jobs = singletonList(someStoppedJob(SKIPPED, 1)); when(jobRepository.findLatestBy(anyString(), eq(1+1))).thenReturn(jobs); final JobMeta jobMeta = new JobMeta("test", false, false, null, emptyMap()); when(jobMetaRepository.getJobMeta(anyString())).thenReturn(jobMeta); // when final StatusDetail first = errorOnLastJobFailed.statusDetail(jobDefinition); final StatusDetail second = warningOnLastJobFailed.statusDetail(jobDefinition); // then assertThat(first.getStatus(), is(Status.OK)); assertThat(first.getMessage(), is("Last job was successful")); assertThat(second.getStatus(), is(Status.OK)); }
@Test public void shouldIndicateOkForDisabledJob() { // given final List<JobInfo> jobs = singletonList(someStoppedJob(ERROR, 1)); when(jobRepository.findLatestBy(anyString(), eq(1+1))).thenReturn(jobs); final JobMeta jobMeta = new JobMeta("test", false, true, "Test", emptyMap()); when(jobMetaRepository.getJobMeta(anyString())).thenReturn(jobMeta); // when final StatusDetail first = errorOnLastJobFailed.statusDetail(jobDefinition); final StatusDetail second = warningOnLastJobFailed.statusDetail(jobDefinition); // then assertThat(first.getStatus(), is(Status.OK)); assertThat(first.getMessage(), is("Job is deactivated: Test")); assertThat(second.getStatus(), is(Status.OK)); }
@Test public void shouldIndicateStateIfLastJobFailed() { // given final List<JobInfo> jobInfos = singletonList(someStoppedJob(ERROR, 1)); when(jobRepository.findLatestBy(anyString(), eq(1+1))).thenReturn(jobInfos); final JobMeta jobMeta = new JobMeta("test", false, false, null, emptyMap()); when(jobMetaRepository.getJobMeta(anyString())).thenReturn(jobMeta); // when final StatusDetail first = errorOnLastJobFailed.statusDetail(jobDefinition); final StatusDetail second = warningOnLastJobFailed.statusDetail(jobDefinition); // then assertThat(first.getStatus(), is(Status.ERROR)); assertThat(first.getMessage(), is("Job had an error")); assertThat(second.getStatus(), is(Status.WARNING)); assertThat(second.getMessage(), is("Job had an error")); }
@Test public void shouldIndicateWarningIfTwoOfThreeJobsFailed() { // given final List<JobInfo> jobInfos = asList( someStoppedJob(OK, 1), someStoppedJob(ERROR, 2), someStoppedJob(ERROR, 2)); when(jobRepository.findLatestBy(anyString(), eq(3+1))).thenReturn(jobInfos); final JobMeta jobMeta = new JobMeta("test", false, false, null, emptyMap()); when(jobMetaRepository.getJobMeta(anyString())).thenReturn(jobMeta); // when JobStatusCalculator maxOneOfThree = new JobStatusCalculator("test", 3, 1, jobRepository, jobMetaRepository,"/someInternalPath"); final StatusDetail detail = maxOneOfThree.statusDetail(jobDefinition); // then assertThat(detail.getStatus(), is(Status.WARNING)); assertThat(detail.getMessage(), is("2 out of 3 job executions failed")); }
@Test public void shouldIndicateErrorIfTwoOfThreeJobsFailed() { // given final List<JobInfo> jobInfos = asList( someStoppedJob(ERROR, 1), someStoppedJob(OK, 2), someStoppedJob(ERROR, 2)); when(jobRepository.findLatestBy(anyString(), eq(3+1))).thenReturn(jobInfos); final JobMeta jobMeta = new JobMeta("test", false, false, null, emptyMap()); when(jobMetaRepository.getJobMeta(anyString())).thenReturn(jobMeta); // when JobStatusCalculator maxOneOfThree = new JobStatusCalculator("test", 3, 1, jobRepository, jobMetaRepository, "/someInternalPath"); final StatusDetail detail = maxOneOfThree.statusDetail(jobDefinition); // then assertThat(detail.getStatus(), is(Status.ERROR)); assertThat(detail.getMessage(), is("2 out of 3 job executions failed")); }