ScheduledJobExecutor implements ApplicationContextAware { public void registerScheduledJob(@NonNull ScheduledJob scheduledJob) { if(!scheduledJobIdMap.containsKey(scheduledJob)) { var jobIdentifier = scheduledJob.getJobIdentifier(); if(Objects.isNull(jobIdentifier)) { throw new IllegalArgumentException("A job's identifier can't be null!"); } var trimmedJobIdentifier = jobIdentifier.trim(); if(trimmedJobIdentifier.isEmpty()) { throw new IllegalArgumentException("A job's identifier can't contain only whitespace!"); } if(scheduledJobIdMap.containsValue(trimmedJobIdentifier)) { throw new IllegalStateException(String.format("Multiple scheduled jobs with the same job identifier '%s' found!", trimmedJobIdentifier)); } scheduledJobIdMap.put(scheduledJob, trimmedJobIdentifier); log.debug(String.format("Added scheduled job (%s) with job identifier '%s'!", scheduledJob.getClass().getName(), jobIdentifier)); } } @Autowired  ScheduledJobExecutor(ScheduledJobLogRepository jobLogRepo, Clock clock); @Scheduled( initialDelay = 0L, fixedDelay = 120_000L ) void executeJobs(); void registerScheduledJob(@NonNull ScheduledJob scheduledJob); @Override void setApplicationContext(ApplicationContext applicationContext);  }
@Test public void registerScheduledJobThrowsExceptionWhenIdentifierIsNull() { assertThatThrownBy(() -> scheduledJobExecutor.registerScheduledJob(jobMock)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("A job's identifier can't be null!"); }
@Test public void registerScheduledJobThrowExceptionWhenIdentifierContainsOnlyWhitespace() { var jobIdOnlyWhitespace = " "; given(jobMock.getJobIdentifier()).willReturn(jobIdOnlyWhitespace); assertThatThrownBy(() -> scheduledJobExecutor.registerScheduledJob(jobMock)) .isInstanceOf(IllegalArgumentException.class) .hasMessage("A job's identifier can't contain only whitespace!"); }
@Test public void registerScheduledJobThrowsExceptionWhenDifferentJobWithSameIdRegistered() { var alreadyExistingJobId = "JOB"; scheduledJobExecutor.scheduledJobIdMap.put(jobMock, alreadyExistingJobId); given(otherJobMock.getJobIdentifier()).willReturn(alreadyExistingJobId); assertThatThrownBy(() -> scheduledJobExecutor.registerScheduledJob(otherJobMock)) .isInstanceOf(IllegalStateException.class) .hasMessage(String.format("Multiple scheduled jobs with the same job identifier '%s' found!", alreadyExistingJobId)); }
@Test public void registerScheduledJobDoesNotRegisterSameJobTwice() { var jobId = "JOB"; scheduledJobExecutor.scheduledJobIdMap.put(jobMock, jobId); scheduledJobExecutor.registerScheduledJob(jobMock); assertThat(scheduledJobExecutor.scheduledJobIdMap).containsExactly(Map.entry(jobMock, jobId)); }
@Test public void registerScheduledJobRegistersJobWhenJobHasUniqueId() { var existingJobId = "JOB"; var otherJobId = "OTHER-JOB"; scheduledJobExecutor.scheduledJobIdMap.put(jobMock, existingJobId); given(otherJobMock.getJobIdentifier()).willReturn(otherJobId); scheduledJobExecutor.registerScheduledJob(otherJobMock); assertThat(scheduledJobExecutor.scheduledJobIdMap) .containsOnly(Map.entry(jobMock, existingJobId), Map.entry(otherJobMock, otherJobId)); }