#62 added missing tests

This commit is contained in:
Fabio Formosa
2022-11-29 23:12:54 +01:00
parent b17d487c8b
commit 249cf49873
5 changed files with 90 additions and 16 deletions

View File

@@ -42,14 +42,6 @@ public class Try<R> {
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<T, R> {
R apply(T t) throws java.lang.Exception;

View File

@@ -20,7 +20,7 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<springdoc-openapi.version>1.5.12</springdoc-openapi.version>
<java.version>9</java.version>
<sonar.exclusions>**/QuartManagerApplicationTests.java</sonar.exclusions>
<sonar.exclusions>**/QuartManagerApplicationTests.java, **/OpenApiConfig.java</sonar.exclusions>
</properties>
<dependencies>

View File

@@ -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) {

View File

@@ -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<QuartzModuleProperties> quartzModulePropertiesList = new ArrayList<>();
quartzModulePropertiesList.add(quartzModuleProperties);
void givenASchedulerName_whenTheSchedulerIsInstantiated_thenTheSchedulerHasThatName() throws Exception {
List<QuartzModuleProperties> 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<QuartzModuleProperties> getQuartzModulePropertiesWithASchedulerName(String schedulerName) {
QuartzModuleProperties quartzModuleProperties = new QuartzModuleProperties();
quartzModuleProperties.getProperties().put("org.quartz.scheduler.instanceName", schedulerName);
List<QuartzModuleProperties> 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<QuartzModuleProperties> 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<QuartzModuleProperties> 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<QuartzModuleProperties> quartzModulePropertiesList = getQuartzModulePropertiesWithASchedulerName(TEST_SCHEDULER_NAME);
SchedulerConfig schedulerConfig = new SchedulerConfig(quartzModulePropertiesList);
GenericApplicationContext applicationContext = new GenericApplicationContext();
applicationContext.refresh();
Properties properties = schedulerConfig.quartzProperties();
Assertions.assertThat(properties).isEmpty();
}
}

View File

@@ -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<String> 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();
}
}