use of org.ligoj.app.plugin.vm.dao.VmScheduleRepository in project plugin-vm by ligoj.
the class VmScheduleResourceTest method createAndUpdateSchedule.
@Test
public void createAndUpdateSchedule() throws Exception {
final ApplicationContext mockContext = Mockito.mock(ApplicationContext.class);
final VmScheduleRepository repository = Mockito.mock(VmScheduleRepository.class);
final VmResource mockResource = Mockito.mock(VmResource.class);
final Subscription entity = this.subscriptionRepository.findOneExpected(subscription);
Mockito.when(mockContext.getBean(VmScheduleRepository.class)).thenReturn(repository);
Mockito.when(mockContext.getBean(SecurityHelper.class)).thenReturn(Mockito.mock(SecurityHelper.class));
Mockito.when(mockContext.getBean(VmResource.class)).thenReturn(mockResource);
final StdScheduler scheduler = (StdScheduler) vmSchedulerFactoryBean.getScheduler();
final QuartzScheduler qscheduler = (QuartzScheduler) FieldUtils.getField(StdScheduler.class, "sched", true).get(scheduler);
final QuartzSchedulerResources resources = (QuartzSchedulerResources) FieldUtils.getField(QuartzScheduler.class, "resources", true).get(qscheduler);
final JobDetail jobDetail = scheduler.getJobDetail(scheduler.getJobKeys(GroupMatcher.anyJobGroup()).iterator().next());
// "ON" call would fail
Mockito.doThrow(new RuntimeException()).when(mockResource).execute(entity, VmOperation.ON);
try {
// Mock the factory
jobDetail.getJobDataMap().put("context", mockContext);
((RAMJobStore) resources.getJobStore()).storeJob(jobDetail, true);
Assertions.assertEquals(1, this.repository.findAll().size());
// Schedule all operations within the next 2 seconds
final String cron = "" + ((DateUtils.newCalendar().get(Calendar.SECOND) + 2) % 60) + " * * * * ?";
final int id = mockSchedule(repository, resource.create(subscription, newSchedule(cron, VmOperation.OFF)));
mockSchedule(repository, resource.create(subscription, newSchedule(cron + " *", VmOperation.ON)));
Assertions.assertEquals(3, this.repository.findAll().size());
// Yield for the schedules
Thread.sleep(2500);
// Check the executions
Mockito.verify(mockResource).execute(entity, VmOperation.OFF);
// Failed
Mockito.verify(mockResource).execute(entity, VmOperation.ON);
Mockito.verify(mockResource, Mockito.never()).execute(entity, VmOperation.REBOOT);
Mockito.verify(mockResource, Mockito.never()).execute(entity, VmOperation.RESET);
Mockito.verify(mockResource, Mockito.never()).execute(entity, VmOperation.SHUTDOWN);
Mockito.verify(mockResource, Mockito.never()).execute(entity, VmOperation.SUSPEND);
// Update the CRON and the operation
final VmScheduleVo vo = newSchedule("" + ((DateUtils.newCalendar().get(Calendar.SECOND) + 2) % 60) + " * * * * ?", VmOperation.SHUTDOWN);
vo.setId(id);
resource.update(subscription, vo);
Assertions.assertEquals(3, this.repository.findAll().size());
// Yield for the schedules
Thread.sleep(2500);
Mockito.verify(mockResource).execute(entity, VmOperation.SHUTDOWN);
} finally {
// Restore the factory's context
jobDetail.getJobDataMap().put("context", applicationContext);
((RAMJobStore) resources.getJobStore()).storeJob(jobDetail, true);
}
}
use of org.ligoj.app.plugin.vm.dao.VmScheduleRepository in project plugin-vm by ligoj.
the class VmScheduleResourceTest method unscheduleAll.
@Test
public void unscheduleAll() throws Exception {
Assertions.assertEquals(1, repository.findAll().size());
repository.deleteAll();
Assertions.assertEquals(0, repository.findAll().size());
final Subscription entity = this.subscriptionRepository.findOneExpected(subscription);
final ApplicationContext mockContext = Mockito.mock(ApplicationContext.class);
final VmScheduleRepository repository = Mockito.mock(VmScheduleRepository.class);
final VmResource mockResource = Mockito.mock(VmResource.class);
Mockito.when(mockContext.getBean(VmScheduleRepository.class)).thenReturn(repository);
Mockito.when(mockContext.getBean(SecurityHelper.class)).thenReturn(Mockito.mock(SecurityHelper.class));
Mockito.when(mockContext.getBean(VmResource.class)).thenReturn(mockResource);
final StdScheduler scheduler = (StdScheduler) vmSchedulerFactoryBean.getScheduler();
final QuartzScheduler qscheduler = (QuartzScheduler) FieldUtils.getField(StdScheduler.class, "sched", true).get(scheduler);
final QuartzSchedulerResources resources = (QuartzSchedulerResources) FieldUtils.getField(QuartzScheduler.class, "resources", true).get(qscheduler);
final JobDetail jobDetail = scheduler.getJobDetail(scheduler.getJobKeys(GroupMatcher.anyJobGroup()).iterator().next());
// One call would fail
Mockito.doThrow(new RuntimeException()).when(mockResource).execute(entity, VmOperation.ON);
final Subscription otherEntity = new Subscription();
try {
// Mock the factory
jobDetail.getJobDataMap().put("context", mockContext);
((RAMJobStore) resources.getJobStore()).storeJob(jobDetail, true);
// Schedule all operations within the next 2 seconds
final String cron = "" + ((DateUtils.newCalendar().get(Calendar.SECOND) + 2) % 60) + " * * * * ? *";
mockSchedule(repository, resource.create(subscription, newSchedule(cron, VmOperation.ON)));
mockSchedule(repository, resource.create(subscription, newSchedule(cron, VmOperation.ON)));
mockSchedule(repository, resource.create(subscription, newSchedule(cron, VmOperation.ON)));
mockSchedule(repository, resource.create(subscription, newSchedule(cron, VmOperation.ON)));
mockSchedule(repository, resource.create(subscription, newSchedule(cron, VmOperation.ON)));
Assertions.assertEquals(5, this.repository.findAll().size());
// Persist another VM schedule for another subscription within the
// next 2 seconds
otherEntity.setProject(entity.getProject());
otherEntity.setNode(entity.getNode());
this.subscriptionRepository.saveAndFlush(otherEntity);
final VmScheduleVo schedule2 = newSchedule("0 0 0 1 1 ? 2050", VmOperation.ON);
resource.create(otherEntity.getId(), schedule2);
Assertions.assertEquals(6, this.repository.findAll().size());
// Yield for the schedules
Thread.sleep(2500);
} finally {
// Restore the factory's context
jobDetail.getJobDataMap().put("context", applicationContext);
((RAMJobStore) resources.getJobStore()).storeJob(jobDetail, true);
}
Mockito.inOrder(mockResource).verify(mockResource, Mockito.calls(5)).execute(entity, VmOperation.ON);
Mockito.verify(mockResource, Mockito.never()).execute(entity, VmOperation.OFF);
Mockito.verify(mockResource, Mockito.never()).execute(entity, VmOperation.REBOOT);
Mockito.verify(mockResource, Mockito.never()).execute(entity, VmOperation.RESET);
Mockito.verify(mockResource, Mockito.never()).execute(entity, VmOperation.SHUTDOWN);
Mockito.verify(mockResource, Mockito.never()).execute(entity, VmOperation.SUSPEND);
// Remove all triggers of the subscription
resource.unscheduleAll(subscription);
Assertions.assertEquals(1, this.repository.findAll().size());
resource.unscheduleAll(otherEntity.getId());
Assertions.assertEquals(0, this.repository.findAll().size());
}
Aggregations