mirror of
https://github.com/fabioformosa/quartz-manager.git
synced 2026-05-14 22:00:30 +09:00
#62 added missing tests
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
package it.fabioformosa.quartzmanager.api.dto;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.*;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class TriggerFiredBundleDTO {
|
||||
|
||||
private int timesTriggered;
|
||||
|
||||
@@ -8,11 +8,8 @@ public class ResourceConflictException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1791564636123821405L;
|
||||
|
||||
private final Long resourceId;
|
||||
|
||||
public ResourceConflictException(Long resourceId, String message) {
|
||||
super(message);
|
||||
this.resourceId = resourceId;
|
||||
super("Conflict on resourceID " + resourceId + " " + message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,16 +1,10 @@
|
||||
package it.fabioformosa.quartzmanager.api.exceptions;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
|
||||
@ToString
|
||||
@Getter
|
||||
public class TriggerNotFoundException extends Exception {
|
||||
|
||||
private final String name;
|
||||
|
||||
public TriggerNotFoundException(String name) {
|
||||
super("Trigger with name " + name + " not found!");
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package it.fabioformosa.quartzmanager.api.controllers;
|
||||
|
||||
import it.fabioformosa.quartzmanager.api.QuartManagerApplicationTests;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
|
||||
@ContextConfiguration(classes = {QuartManagerApplicationTests.class})
|
||||
@WebMvcTest(controllers = SimpleTriggerController.class, properties = {
|
||||
"quartz-manager.jobClassPackages=it.fabioformosa.quartzmanager.jobs"
|
||||
})
|
||||
class ResourceConflictControllerTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Test
|
||||
void whenAResourceConflictExceptionIsRaised_thenTheExceptionHandlerReturns409() throws Exception {
|
||||
mockMvc.perform(
|
||||
MockMvcRequestBuilders.post(TestController.TEST_CONTROLLER_BASE_URL + "/test-conflict")
|
||||
.contentType(MediaType.APPLICATION_JSON)).andExpect(MockMvcResultMatchers.status().isConflict());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package it.fabioformosa.quartzmanager.api.controllers;
|
||||
|
||||
import it.fabioformosa.quartzmanager.api.exceptions.ResourceConflictException;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RequestMapping(TestController.TEST_CONTROLLER_BASE_URL)
|
||||
@RestController
|
||||
public class TestController {
|
||||
|
||||
public static final String TEST_CONTROLLER_BASE_URL = "/test-controller";
|
||||
|
||||
@PostMapping("/test-conflict")
|
||||
public void raiseConflictException(){
|
||||
throw new ResourceConflictException(1000L, "another entity has found with the same ID");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package it.fabioformosa.quartzmanager.api.jobs;
|
||||
|
||||
import it.fabioformosa.quartzmanager.api.dto.TriggerFiredBundleDTO;
|
||||
import it.fabioformosa.quartzmanager.api.jobs.entities.LogRecord;
|
||||
import it.fabioformosa.quartzmanager.api.websockets.WebhookSender;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.quartz.*;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
|
||||
class SampleJobTest {
|
||||
|
||||
@InjectMocks
|
||||
private SampleJob sampleJob;
|
||||
|
||||
@Mock
|
||||
private WebhookSender<TriggerFiredBundleDTO> webSocketProgressNotifier;
|
||||
@Mock
|
||||
private WebhookSender<LogRecord> webSocketLogsNotifier;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenASampleJob_whenTheJobIsExecuted_thenTheWebhookSendersAreCalled() {
|
||||
JobExecutionContext jobExecutionContext = Mockito.mock(JobExecutionContext.class);
|
||||
|
||||
ScheduleBuilder schedulerBuilder = SimpleScheduleBuilder.simpleSchedule()
|
||||
.withRepeatCount(5)
|
||||
.withIntervalInMilliseconds(1000L);
|
||||
JobDetail jobDetail = JobBuilder
|
||||
.newJob(SampleJob.class).withIdentity(JobKey.jobKey("test-job"))
|
||||
.build();
|
||||
Trigger trigger = TriggerBuilder.newTrigger()
|
||||
.forJob(jobDetail)
|
||||
.withSchedule(schedulerBuilder)
|
||||
.build();
|
||||
Mockito.when(jobExecutionContext.getTrigger()).thenReturn(trigger);
|
||||
Mockito.when(jobExecutionContext.getJobDetail()).thenReturn(jobDetail);
|
||||
|
||||
sampleJob.execute(jobExecutionContext);
|
||||
Mockito.verify(webSocketLogsNotifier).send(any(LogRecord.class));
|
||||
Mockito.verify(webSocketProgressNotifier).send(any(TriggerFiredBundleDTO.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenASampleJob_whenTheDoItMethodIsCalled_thenALogRecordIsReturned() {
|
||||
JobExecutionContext jobExecutionContext = Mockito.mock(JobExecutionContext.class);
|
||||
LogRecord logRecord = sampleJob.doIt(jobExecutionContext);
|
||||
Assertions.assertThat(logRecord.getMessage()).isEqualTo("Hello!");
|
||||
Assertions.assertThat(logRecord.getType()).isEqualTo(LogRecord.LogType.INFO);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
mock-maker-inline
|
||||
Reference in New Issue
Block a user