Skip to content

Commit

Permalink
Fix tests by not using new application context
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwilshire committed Oct 25, 2024
1 parent 94208dd commit fff8a9f
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.box.l10n.mojito.entity.ScheduledJob;
import com.box.l10n.mojito.retry.DeadLockLoserExceptionRetryTemplate;
import java.util.Date;
import java.util.Optional;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.listeners.JobListenerSupport;
Expand Down Expand Up @@ -45,8 +46,13 @@ public String getName() {
*/
@Override
public void jobToBeExecuted(JobExecutionContext context) {
ScheduledJob scheduledJob =
scheduledJobRepository.findByJobKey(context.getJobDetail().getKey());
/* If multi quartz scheduler is not being used, the listener will
be attached to default jobs, ignore if it's not a scheduled job */
Optional<ScheduledJob> optScheduledJob =
scheduledJobRepository.findById(context.getJobDetail().getKey().getName());

if (optScheduledJob.isEmpty()) return;
ScheduledJob scheduledJob = optScheduledJob.get();

logger.debug(
"Preparing to execute job {} for repository {}",
Expand Down Expand Up @@ -76,8 +82,13 @@ public void jobToBeExecuted(JobExecutionContext context) {
/** The job finished execution, if an error occurred jobException will not be null */
@Override
public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
ScheduledJob scheduledJob =
scheduledJobRepository.findByJobKey(context.getJobDetail().getKey());

Optional<ScheduledJob> optScheduledJob =
scheduledJobRepository.findById(context.getJobDetail().getKey().getName());

if (optScheduledJob.isEmpty()) return;
ScheduledJob scheduledJob = optScheduledJob.get();

logger.debug(
"Handling post execution for job {} for repository {}",
scheduledJob.getJobType().getEnum(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,39 @@
public class ScheduledJobManager {
static Logger logger = LoggerFactory.getLogger(ScheduledJobManager.class);

@Autowired ThirdPartySyncJobsConfig thirdPartySyncJobsConfig;
@Autowired QuartzSchedulerManager schedulerManager;
@Autowired ScheduledJobRepository scheduledJobRepository;
@Autowired ScheduledJobStatusRepository scheduledJobStatusRepository;
@Autowired ScheduledJobTypeRepository scheduledJobTypeRepository;
@Autowired RepositoryRepository repositoryRepository;
@Autowired DeadLockLoserExceptionRetryTemplate deadlockRetryTemplate;

/* Quartz scheduler dedicated to scheduled jobs using in memory data source */
private final ThirdPartySyncJobsConfig thirdPartySyncJobsConfig;
private final QuartzSchedulerManager schedulerManager;
private final ScheduledJobRepository scheduledJobRepository;
private final ScheduledJobStatusRepository scheduledJobStatusRepository;
private final ScheduledJobTypeRepository scheduledJobTypeRepository;
private final RepositoryRepository repositoryRepository;
private final DeadLockLoserExceptionRetryTemplate deadlockRetryTemplate;

/* Quartz scheduler dedicated to scheduled jobs using in memory data source.
* The value is also set using equals as this is not managed by Spring Boot in the tests. */
@Value("${l10n.scheduledJobs.quartz.schedulerName:" + DEFAULT_SCHEDULER_NAME + "}")
private String schedulerName;
private String schedulerName = DEFAULT_SCHEDULER_NAME;

public List<String> uuidPool = new ArrayList<>();

@Autowired
public ScheduledJobManager(
ThirdPartySyncJobsConfig thirdPartySyncJobConfig,
QuartzSchedulerManager schedulerManager,
ScheduledJobRepository scheduledJobRepository,
ScheduledJobStatusRepository scheduledJobStatusRepository,
ScheduledJobTypeRepository scheduledJobTypeRepository,
RepositoryRepository repositoryRepository,
DeadLockLoserExceptionRetryTemplate deadlockRetryTemplate) {
this.thirdPartySyncJobsConfig = thirdPartySyncJobConfig;
this.schedulerManager = schedulerManager;
this.scheduledJobRepository = scheduledJobRepository;
this.scheduledJobStatusRepository = scheduledJobStatusRepository;
this.scheduledJobTypeRepository = scheduledJobTypeRepository;
this.repositoryRepository = repositoryRepository;
this.deadlockRetryTemplate = deadlockRetryTemplate;
}

@PostConstruct
public void init() throws ClassNotFoundException, SchedulerException {
logger.info("Scheduled Job Manager started.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.box.l10n.mojito.service.scheduledjob;

import com.box.l10n.mojito.entity.ScheduledJob;
import java.util.Optional;
import org.quartz.JobExecutionContext;
import org.quartz.Trigger;
import org.quartz.listeners.TriggerListenerSupport;
Expand Down Expand Up @@ -33,14 +34,16 @@ public String getName() {
@Override
public void triggerMisfired(Trigger trigger) {
super.triggerMisfired(trigger);
ScheduledJob job = scheduledJobRepository.findByJobKey(trigger.getJobKey());
logger.warn("TRIGGER MISFIRE FOR {} | {}", job.getRepository().getName(), job.getJobType());
Optional<ScheduledJob> job = scheduledJobRepository.findById(trigger.getJobKey().getName());
if (job.isEmpty()) return;
logger.warn(
"TRIGGER MISFIRE FOR {} | {}", job.get().getRepository().getName(), job.get().getJobType());
}

@Override
public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) {
// If the job is disabled, don't execute
ScheduledJob job = scheduledJobRepository.findByJobKey(trigger.getJobKey());
return !job.getEnabled();
Optional<ScheduledJob> job = scheduledJobRepository.findById(trigger.getJobKey().getName());
return job.filter(scheduledJob -> !scheduledJob.getEnabled()).isPresent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.box.l10n.mojito.service.scheduledjob;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class NoOpScheduledJobTest implements IScheduledJob {
@Override
public void onSuccess(JobExecutionContext context) {}

@Override
public void onFailure(JobExecutionContext context, JobExecutionException jobException) {}

@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {}
}
Original file line number Diff line number Diff line change
@@ -1,88 +1,121 @@
// package com.box.l10n.mojito.service.scheduledjob;
//
// import static org.junit.Assert.assertEquals;
// import static org.mockito.ArgumentMatchers.any;
// import static org.mockito.Mockito.doNothing;
//
// import com.box.l10n.mojito.service.assetExtraction.ServiceTestBase;
// import org.junit.Before;
// import org.junit.Test;
// import org.mockito.Mock;
// import org.mockito.MockitoAnnotations;
// import org.quartz.JobExecutionContext;
// import org.quartz.JobExecutionException;
// import org.quartz.impl.matchers.GroupMatcher;
// import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.test.context.ContextConfiguration;
// import org.springframework.test.context.TestPropertySource;
//
// @TestPropertySource(
// properties = {
// "l10n.scheduledJobs.enabled=true",
// "l10n.thirdPartySyncJobs.test1.uuid=e4c72563-d8f0-4465-9eec-9ed96087665e",
// "l10n.thirdPartySyncJobs.test1.cron=0/2 * * * * ?",
// "l10n.thirdPartySyncJobs.test1.repository=scheduled-job-1",
// "l10n.thirdPartySyncJobs.test1.thirdPartyProjectId=123456",
// "l10n.thirdPartySyncJobs.test1.actions=PUSH,PULL,MAP_TEXTUNIT,PUSH_SCREENSHOT",
// "",
// "l10n.thirdPartySyncJobs.test2.uuid=e4c72563-d8f0-4465-9eec-aaa96087665e",
// "l10n.thirdPartySyncJobs.test2.cron=0 0/5 * * * ?",
// "l10n.thirdPartySyncJobs.test2.repository=scheduled-job-2",
// "l10n.thirdPartySyncJobs.test2.thirdPartyProjectId=123456",
// "l10n.thirdPartySyncJobs.test2.actions=PUSH,PULL,MAP_TEXTUNIT,PUSH_SCREENSHOT",
// "",
// "l10n.thirdPartySyncJobs.test3.cron=0 0/5 * * * ?",
// "l10n.thirdPartySyncJobs.test3.repository=scheduled-job-3",
// "l10n.thirdPartySyncJobs.test3.thirdPartyProjectId=123456",
// "l10n.thirdPartySyncJobs.test3.actions=PUSH,PULL,MAP_TEXTUNIT,PUSH_SCREENSHOT",
// "",
// "l10n.thirdPartySyncJobs.test4.uuid=e4c72563-d8f0-4465-9eec-aaa96087665e",
// "l10n.thirdPartySyncJobs.test4.repository=scheduled-job-4",
// "l10n.thirdPartySyncJobs.test4.thirdPartyProjectId=123456",
// "l10n.thirdPartySyncJobs.test4.actions=PUSH,PULL,MAP_TEXTUNIT,PUSH_SCREENSHOT"
// })
// @ContextConfiguration(classes = {ScheduledJobTestConfiguration.class})
// public class ScheduledJobManagerTest extends ServiceTestBase {
// @Mock IScheduledJob scheduledThirdPartySync;
// @Autowired ScheduledJobManager scheduledJobManager;
//
// @Before
// public void setUp() throws JobExecutionException {
// MockitoAnnotations.openMocks(this);
// doNothing().when(scheduledThirdPartySync).execute(any(JobExecutionContext.class));
// }
//
// @Test
// public void testListenersExists() throws Exception {
// assertEquals(
// 1, scheduledJobManager.getScheduler().getListenerManager().getJobListeners().size());
//
// assertEquals(
// 1, scheduledJobManager.getScheduler().getListenerManager().getTriggerListeners().size());
// }
//
// @Test
// public void testQuartzScheduledJob() throws Exception {
// assertEquals(
// 2,
// scheduledJobManager
// .getScheduler()
// .getJobKeys(GroupMatcher.groupEquals(ScheduledJobType.THIRD_PARTY_SYNC.toString()))
// .size());
//
// Thread.sleep(10000);
// }
//
// // @Test
// // public void testQuartzCleanup() throws Exception {
// // scheduledJobManager.uuidPool = List.of("e4c72563-d8f0-4465-9eec-9ed96087665e");
// // scheduledJobManager.cleanQuartzJobs();
// // assertEquals(
// // 1,
// // scheduledJobManager
// // .getScheduler()
// //
// .getJobKeys(GroupMatcher.groupEquals(ScheduledJobType.THIRD_PARTY_SYNC.toString()))
// // .size());
// // }
// }
package com.box.l10n.mojito.service.scheduledjob;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import com.box.l10n.mojito.entity.ScheduledJob;
import com.box.l10n.mojito.quartz.QuartzSchedulerManager;
import com.box.l10n.mojito.retry.DeadLockLoserExceptionRetryTemplate;
import com.box.l10n.mojito.service.assetExtraction.ServiceTestBase;
import com.box.l10n.mojito.service.repository.RepositoryLocaleCreationException;
import com.box.l10n.mojito.service.repository.RepositoryNameAlreadyUsedException;
import com.box.l10n.mojito.service.repository.RepositoryRepository;
import com.box.l10n.mojito.service.repository.RepositoryService;
import com.box.l10n.mojito.service.thirdparty.ThirdPartySyncJobConfig;
import com.box.l10n.mojito.service.thirdparty.ThirdPartySyncJobsConfig;
import com.google.common.collect.ImmutableMap;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.quartz.SchedulerException;
import org.quartz.impl.matchers.GroupMatcher;
import org.springframework.beans.factory.annotation.Autowired;

public class ScheduledJobManagerTest extends ServiceTestBase {
private ScheduledJobManager scheduledJobManager;
@Autowired RepositoryService repositoryService;
@Autowired QuartzSchedulerManager schedulerManager;
@Autowired ScheduledJobRepository scheduledJobRepository;
@Autowired ScheduledJobStatusRepository scheduledJobStatusRepository;
@Autowired ScheduledJobTypeRepository scheduledJobTypeRepository;
@Autowired RepositoryRepository repositoryRepository;
@Autowired DeadLockLoserExceptionRetryTemplate deadlockRetryTemplate;

@Before
public void before()
throws RepositoryNameAlreadyUsedException,
RepositoryLocaleCreationException,
ClassNotFoundException,
SchedulerException {
for (int i = 1; i <= 4; i++) {
if (repositoryService
.findRepositoriesIsNotDeletedOrderByName("scheduled-job-" + i)
.isEmpty()) {
repositoryService.addRepositoryLocale(
repositoryService.createRepository("scheduled-job-" + i), "fr-FR");
}
}

ThirdPartySyncJobConfig scheduledJobOne = new ThirdPartySyncJobConfig();
scheduledJobOne.setUuid("e4c72563-d8f0-4465-9eec-9ed96087665e");
scheduledJobOne.setCron("0/5 * * * * ?");
scheduledJobOne.setRepository("scheduled-job-1");
scheduledJobOne.setThirdPartyProjectId("123456");

ThirdPartySyncJobConfig scheduledJobTwo = new ThirdPartySyncJobConfig();
scheduledJobTwo.setUuid("e4c72563-d8f0-4465-9eec-aaa96087665e");
scheduledJobTwo.setCron("0/5 * * * * ?");
scheduledJobTwo.setRepository("scheduled-job-2");
scheduledJobTwo.setThirdPartyProjectId("123456");

ThirdPartySyncJobsConfig thirdPartySyncJobsConfig = new ThirdPartySyncJobsConfig();
thirdPartySyncJobsConfig.setThirdPartySyncJobs(
ImmutableMap.of("scheduled-job-1", scheduledJobOne, "scheduled-job-2", scheduledJobTwo));

// Return a Spy with the Job always being a no-op job
scheduledJobManager =
Mockito.spy(
new ScheduledJobManager(
thirdPartySyncJobsConfig,
schedulerManager,
scheduledJobRepository,
scheduledJobStatusRepository,
scheduledJobTypeRepository,
repositoryRepository,
deadlockRetryTemplate));

Mockito.doReturn(NoOpScheduledJobTest.class)
.when(scheduledJobManager)
.loadJobClass(ScheduledJobType.THIRD_PARTY_SYNC.getJobClassName());

scheduledJobManager.init();
}

@Test
public void testListenersExists() throws Exception {
assertEquals(
1, scheduledJobManager.getScheduler().getListenerManager().getJobListeners().size());

assertEquals(
1, scheduledJobManager.getScheduler().getListenerManager().getTriggerListeners().size());
}

@Test
public void testQuartzScheduledJob() throws Exception {
assertEquals(
2,
scheduledJobManager
.getScheduler()
.getJobKeys(GroupMatcher.groupEquals(ScheduledJobType.THIRD_PARTY_SYNC.toString()))
.size());

Thread.sleep(10000);

ScheduledJob sj = scheduledJobRepository.findById("e4c72563-d8f0-4465-9eec-9ed96087665e").get();
assertEquals(sj.getJobStatus().getEnum(), ScheduledJobStatus.SUCCEEDED);
assertNotNull(sj.getEndDate());
}

// @Test
// public void testQuartzCleanup() throws Exception {
// scheduledJobManager.uuidPool = List.of("e4c72563-d8f0-4465-9eec-9ed96087665e");
// scheduledJobManager.cleanQuartzJobs();
// assertEquals(
// 1,
// scheduledJobManager
// .getScheduler()
//
// .getJobKeys(GroupMatcher.groupEquals(ScheduledJobType.THIRD_PARTY_SYNC.toString()))
// .size());
// }
}

This file was deleted.

0 comments on commit fff8a9f

Please sign in to comment.