View Javadoc

1   package com.atlassian.scheduler.tenancy;
2   
3   import com.atlassian.scheduler.SchedulerServiceException;
4   import com.atlassian.scheduler.config.JobConfig;
5   import com.atlassian.scheduler.config.JobId;
6   import com.atlassian.scheduler.config.JobRunnerKey;
7   import com.atlassian.scheduler.config.RunMode;
8   import com.atlassian.scheduler.core.LifecycleAwareSchedulerService;
9   import com.atlassian.tenancy.api.Tenant;
10  import com.atlassian.tenancy.api.TenantAccessor;
11  import com.google.common.collect.ImmutableList;
12  import org.junit.Before;
13  import org.junit.Rule;
14  import org.junit.Test;
15  import org.mockito.Mock;
16  import org.mockito.junit.MockitoJUnit;
17  import org.mockito.junit.MockitoRule;
18  
19  import static org.hamcrest.Matchers.containsString;
20  import static org.junit.Assert.assertThat;
21  import static org.junit.Assert.fail;
22  import static org.mockito.Mockito.verify;
23  import static org.mockito.Mockito.verifyZeroInteractions;
24  import static org.mockito.Mockito.when;
25  
26  public class TenantAwareSchedulerServiceTest {
27      @Rule
28      public MockitoRule mockitoRule = MockitoJUnit.rule();
29  
30      @Mock
31      TenantAccessor tenantAccessor;
32      @Mock
33      Tenant tenant;
34      @Mock
35      LifecycleAwareSchedulerService delegate;
36  
37      TenantAwareSchedulerService schedulerService;
38  
39      @Before
40      public void setUp() {
41          schedulerService = new TenantAwareSchedulerService(delegate, tenantAccessor);
42      }
43  
44      @Test
45      public void testScheduleJobOkWhenTenanted() throws SchedulerServiceException {
46          when(tenantAccessor.getAvailableTenants()).thenReturn(ImmutableList.of(tenant));
47          final JobConfig jobConfig = JobConfig.forJobRunnerKey(JobRunnerKey.of("test-key"))
48                  .withRunMode(RunMode.RUN_LOCALLY);
49          schedulerService.scheduleJob(JobId.of("jobId"), jobConfig);
50          verify(delegate).scheduleJob(JobId.of("jobId"), jobConfig);
51      }
52  
53      @Test
54      public void testScheduleJobWithGeneratedIdOkWhenTenanted() throws SchedulerServiceException {
55          when(tenantAccessor.getAvailableTenants()).thenReturn(ImmutableList.of(tenant));
56          final JobConfig jobConfig = JobConfig.forJobRunnerKey(JobRunnerKey.of("test-key"))
57                  .withRunMode(RunMode.RUN_LOCALLY);
58          schedulerService.scheduleJobWithGeneratedId(jobConfig);
59          verify(delegate).scheduleJobWithGeneratedId(jobConfig);
60      }
61  
62      @Test
63      public void testScheduleJobThrowsIllegalStateExceptionWhenTenantless() throws SchedulerServiceException {
64          when(tenantAccessor.getAvailableTenants()).thenReturn(ImmutableList.<Tenant>of());
65          final JobConfig jobConfig = JobConfig.forJobRunnerKey(JobRunnerKey.of("test-key"))
66                  .withRunMode(RunMode.RUN_LOCALLY);
67  
68          try {
69              schedulerService.scheduleJob(JobId.of("jobId"), jobConfig);
70              fail("Expected an IllegalStateException");
71          } catch (IllegalStateException ise) {
72              assertThat(ise.getMessage(), containsString("not allowed to schedule jobs before"));
73              verifyZeroInteractions(delegate);
74          }
75      }
76  
77      @Test
78      public void testScheduleJobWithGeneratedIdThrowsIllegalStateExceptionWhenTenantless() throws SchedulerServiceException {
79          when(tenantAccessor.getAvailableTenants()).thenReturn(ImmutableList.<Tenant>of());
80          final JobConfig jobConfig = JobConfig.forJobRunnerKey(JobRunnerKey.of("test-key"))
81                  .withRunMode(RunMode.RUN_LOCALLY);
82          try {
83              schedulerService.scheduleJobWithGeneratedId(jobConfig);
84              fail("Expected an IllegalStateException");
85          } catch (IllegalStateException ise) {
86              assertThat(ise.getMessage(), containsString("not allowed to schedule jobs before"));
87              verifyZeroInteractions(delegate);
88          }
89      }
90  
91  }