diff --git a/quartz-manager-parent/quartz-manager-common/src/main/java/it/fabioformosa/quartzmanager/api/common/utils/Try.java b/quartz-manager-parent/quartz-manager-common/src/main/java/it/fabioformosa/quartzmanager/api/common/utils/Try.java index 27f7f19..4495440 100644 --- a/quartz-manager-parent/quartz-manager-common/src/main/java/it/fabioformosa/quartzmanager/api/common/utils/Try.java +++ b/quartz-manager-parent/quartz-manager-common/src/main/java/it/fabioformosa/quartzmanager/api/common/utils/Try.java @@ -42,14 +42,6 @@ public class Try { return t -> Try.with(checkedFunction).apply(t).getSuccess(); } - private boolean isSuccess(){ - return this.failure == null; - } - - private boolean isFailure(){ - return this.failure != null; - } - @FunctionalInterface public static interface CheckedFunction { R apply(T t) throws java.lang.Exception; diff --git a/quartz-manager-parent/quartz-manager-starter-api/pom.xml b/quartz-manager-parent/quartz-manager-starter-api/pom.xml index 8a52e41..58dc0b0 100644 --- a/quartz-manager-parent/quartz-manager-starter-api/pom.xml +++ b/quartz-manager-parent/quartz-manager-starter-api/pom.xml @@ -20,7 +20,7 @@ UTF-8 1.5.12 9 - **/QuartManagerApplicationTests.java + **/QuartManagerApplicationTests.java, **/OpenApiConfig.java diff --git a/quartz-manager-parent/quartz-manager-starter-api/src/main/java/it/fabioformosa/quartzmanager/api/controllers/JobController.java b/quartz-manager-parent/quartz-manager-starter-api/src/main/java/it/fabioformosa/quartzmanager/api/controllers/JobController.java index ff63601..ad47df2 100644 --- a/quartz-manager-parent/quartz-manager-starter-api/src/main/java/it/fabioformosa/quartzmanager/api/controllers/JobController.java +++ b/quartz-manager-parent/quartz-manager-starter-api/src/main/java/it/fabioformosa/quartzmanager/api/controllers/JobController.java @@ -16,10 +16,11 @@ import org.springframework.web.bind.annotation.RestController; import java.util.List; import java.util.stream.Collectors; -@RequestMapping(QuartzManagerPaths.QUARTZ_MANAGER_BASE_CONTEXT_PATH + "/jobs") +@RequestMapping(JobController.JOB_CONTROLLER_BASE_URL) @SecurityRequirement(name = OpenAPIConfigConsts.QUARTZ_MANAGER_SEC_OAS_SCHEMA) @RestController public class JobController { + public static final String JOB_CONTROLLER_BASE_URL = QuartzManagerPaths.QUARTZ_MANAGER_BASE_CONTEXT_PATH + "/jobs"; private final JobService jobService; public JobController(JobService jobService) { diff --git a/quartz-manager-parent/quartz-manager-starter-api/src/test/java/it/fabioformosa/quartzmanager/api/configuration/SchedulerConfigTest.java b/quartz-manager-parent/quartz-manager-starter-api/src/test/java/it/fabioformosa/quartzmanager/api/configuration/SchedulerConfigTest.java index e7b2d99..b94a5f3 100644 --- a/quartz-manager-parent/quartz-manager-starter-api/src/test/java/it/fabioformosa/quartzmanager/api/configuration/SchedulerConfigTest.java +++ b/quartz-manager-parent/quartz-manager-starter-api/src/test/java/it/fabioformosa/quartzmanager/api/configuration/SchedulerConfigTest.java @@ -7,8 +7,10 @@ import org.quartz.Scheduler; import org.springframework.context.support.GenericApplicationContext; import org.springframework.scheduling.quartz.SchedulerFactoryBean; +import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.Properties; class SchedulerConfigTest { @@ -16,11 +18,8 @@ class SchedulerConfigTest { public static final String QUARTZ_SCHEDULER_DEFAULT_NAME = "QuartzScheduler"; @Test - void givenASchedulerName_whenTheSchedulerIsInstatiated_thenTheSchedulerHasThatName() throws Exception { - QuartzModuleProperties quartzModuleProperties = new QuartzModuleProperties(); - quartzModuleProperties.getProperties().put("org.quartz.scheduler.instanceName", TEST_SCHEDULER_NAME); - List quartzModulePropertiesList = new ArrayList<>(); - quartzModulePropertiesList.add(quartzModuleProperties); + void givenASchedulerName_whenTheSchedulerIsInstantiated_thenTheSchedulerHasThatName() throws Exception { + List quartzModulePropertiesList = getQuartzModulePropertiesWithASchedulerName(TEST_SCHEDULER_NAME); SchedulerConfig schedulerConfig = new SchedulerConfig(quartzModulePropertiesList); GenericApplicationContext applicationContext = new GenericApplicationContext(); applicationContext.refresh(); @@ -31,8 +30,16 @@ class SchedulerConfigTest { Assertions.assertThat(scheduler.getSchedulerName()).isEqualTo(TEST_SCHEDULER_NAME); } + private static List getQuartzModulePropertiesWithASchedulerName(String schedulerName) { + QuartzModuleProperties quartzModuleProperties = new QuartzModuleProperties(); + quartzModuleProperties.getProperties().put("org.quartz.scheduler.instanceName", schedulerName); + List quartzModulePropertiesList = new ArrayList<>(); + quartzModulePropertiesList.add(quartzModuleProperties); + return quartzModulePropertiesList; + } + @Test - void givenNoSchedulerName_whenTheSchedulerIsInstatiated_thenTheSchedulerHasTheDefaultName() throws Exception { + void givenNoSchedulerName_whenTheSchedulerIsInstantiated_thenTheSchedulerHasTheDefaultName() throws Exception { QuartzModuleProperties quartzModuleProperties = new QuartzModuleProperties(); List quartzModulePropertiesList = new ArrayList<>(); quartzModulePropertiesList.add(quartzModuleProperties); @@ -46,4 +53,32 @@ class SchedulerConfigTest { Assertions.assertThat(scheduler.getSchedulerName()).isEqualTo(QUARTZ_SCHEDULER_DEFAULT_NAME); } + @Test + void givenAManagedProperties_whenTheSchedulerIsInstantiated_thenTheManagedPropsHavePriority() throws Exception { + List quartzModulePropertiesList = getQuartzModulePropertiesWithASchedulerName(TEST_SCHEDULER_NAME); + SchedulerConfig schedulerConfig = new SchedulerConfig(quartzModulePropertiesList); + GenericApplicationContext applicationContext = new GenericApplicationContext(); + applicationContext.refresh(); + + Properties managedProps = new Properties(); + String overridden_scheduler_name = "OVERRIDDEN_SCHEDULER_NAME"; + managedProps.put("org.quartz.scheduler.instanceName", overridden_scheduler_name); + SchedulerFactoryBean schedulerFactoryBean = schedulerConfig.schedulerFactoryBean(schedulerConfig.jobFactory(applicationContext), managedProps); + + schedulerFactoryBean.afterPropertiesSet(); + Scheduler scheduler = schedulerFactoryBean.getScheduler(); + Assertions.assertThat(scheduler.getSchedulerName()).isEqualTo(overridden_scheduler_name); + } + + @Test + void givenAnEmptyManagedPropFile_whenSchedulerConfigRuns_thenItReturnsAnEmptyPropList() throws IOException { + List quartzModulePropertiesList = getQuartzModulePropertiesWithASchedulerName(TEST_SCHEDULER_NAME); + SchedulerConfig schedulerConfig = new SchedulerConfig(quartzModulePropertiesList); + GenericApplicationContext applicationContext = new GenericApplicationContext(); + applicationContext.refresh(); + + Properties properties = schedulerConfig.quartzProperties(); + Assertions.assertThat(properties).isEmpty(); + } + } diff --git a/quartz-manager-parent/quartz-manager-starter-api/src/test/java/it/fabioformosa/quartzmanager/api/controllers/JobControllerTest.java b/quartz-manager-parent/quartz-manager-starter-api/src/test/java/it/fabioformosa/quartzmanager/api/controllers/JobControllerTest.java new file mode 100644 index 0000000..49838de --- /dev/null +++ b/quartz-manager-parent/quartz-manager-starter-api/src/test/java/it/fabioformosa/quartzmanager/api/controllers/JobControllerTest.java @@ -0,0 +1,46 @@ +package it.fabioformosa.quartzmanager.api.controllers; + +import it.fabioformosa.quartzmanager.api.QuartManagerApplicationTests; +import it.fabioformosa.quartzmanager.api.controllers.utils.TestUtils; +import it.fabioformosa.quartzmanager.api.jobs.SampleJob; +import it.fabioformosa.quartzmanager.api.services.JobService; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; + +@ContextConfiguration(classes = {QuartManagerApplicationTests.class}) +@WebMvcTest(controllers = SimpleTriggerController.class, properties = { + "quartz-manager.jobClassPackages=it.fabioformosa.quartzmanager.jobs" +}) +class JobControllerTest { + + @Autowired + private MockMvc mockMvc; + + @MockBean + private JobService jobService; + + @Test + void whenGetListIsCalled_thenTheSimpleJobIsReturned() throws Exception { + Mockito.when(jobService.getJobClasses()).thenReturn(List.of(SampleJob.class)); + + List expectedJobs = List.of(SampleJob.class.getName()); + mockMvc.perform(get(JobController.JOB_CONTROLLER_BASE_URL) + .contentType(MediaType.APPLICATION_JSON)).andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.content().json(TestUtils.toJson(expectedJobs))); + + Mockito.verify(jobService, Mockito.times(1)).getJobClasses(); + } + +}