From 101a7c0e84a1e32bf6a100e35c08b4e689c683c4 Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Sun, 22 Dec 2024 21:32:59 +0000 Subject: [PATCH] adds integrationTests --- .../common/AbstractIntegrationTest.java | 4 +- .../web/controller/JobsControllerIntTest.java | 209 ++++++++++++++++++ 2 files changed, 211 insertions(+), 2 deletions(-) create mode 100644 scheduler/boot-scheduler-quartz/src/test/java/com/scheduler/quartz/web/controller/JobsControllerIntTest.java diff --git a/scheduler/boot-scheduler-quartz/src/test/java/com/scheduler/quartz/common/AbstractIntegrationTest.java b/scheduler/boot-scheduler-quartz/src/test/java/com/scheduler/quartz/common/AbstractIntegrationTest.java index f4d27c1e8..24d714fe7 100644 --- a/scheduler/boot-scheduler-quartz/src/test/java/com/scheduler/quartz/common/AbstractIntegrationTest.java +++ b/scheduler/boot-scheduler-quartz/src/test/java/com/scheduler/quartz/common/AbstractIntegrationTest.java @@ -8,7 +8,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.assertj.MockMvcTester; @ActiveProfiles({PROFILE_TEST}) @SpringBootTest( @@ -18,7 +18,7 @@ public abstract class AbstractIntegrationTest { @Autowired - protected MockMvc mockMvc; + protected MockMvcTester mockMvcTester; @Autowired protected ObjectMapper objectMapper; diff --git a/scheduler/boot-scheduler-quartz/src/test/java/com/scheduler/quartz/web/controller/JobsControllerIntTest.java b/scheduler/boot-scheduler-quartz/src/test/java/com/scheduler/quartz/web/controller/JobsControllerIntTest.java new file mode 100644 index 000000000..3b379b968 --- /dev/null +++ b/scheduler/boot-scheduler-quartz/src/test/java/com/scheduler/quartz/web/controller/JobsControllerIntTest.java @@ -0,0 +1,209 @@ +package com.scheduler.quartz.web.controller; + +import com.scheduler.quartz.common.AbstractIntegrationTest; +import org.junit.jupiter.api.Test; +import org.springframework.http.MediaType; + +class JobsControllerIntTest extends AbstractIntegrationTest { + + @Test + void testGetJobs() { + mockMvcTester.get().uri("/api").assertThat().hasStatusOk().hasContentType(MediaType.APPLICATION_JSON); + } + + @Test + void testGetJobsStatuses() { + mockMvcTester.get().uri("/api/statuses").assertThat().hasStatusOk().hasContentType(MediaType.APPLICATION_JSON); + } + + @Test + void testSaveOrUpdate() { + String requestBody = + """ + { + "jobName": "SampleJob", + "cronExpression": "0/5 * * * * ?", + "jobId": "12345", + "description": "Test job description" + } + """; + + mockMvcTester + .post() + .uri("/api/saveOrUpdate") + .contentType(MediaType.APPLICATION_JSON) + .content(requestBody) + .accept(MediaType.APPLICATION_JSON) + .assertThat() + .hasStatusOk() + .hasContentType(MediaType.APPLICATION_JSON); + } + + @Test + void testSaveOrUpdateWithInvalidCronExpression() { + String requestBody = + """ + { + "jobName": "SampleJob", + "cronExpression": "0/5 * * * *", + "jobId": "12345", + "description": "Test job description" + } + """; + + mockMvcTester + .post() + .uri("/api/saveOrUpdate") + .contentType(MediaType.APPLICATION_JSON) + .content(requestBody) + .accept(MediaType.APPLICATION_JSON) + .assertThat() + .hasStatusOk() + .hasContentType(MediaType.APPLICATION_JSON); + } + + @Test + void testPauseJob() { + String requestBody = + """ + { + "jobName": "SampleJob", + "jobId": "12345" + } + """; + + mockMvcTester + .post() + .uri("/api/pauseJob") + .contentType(MediaType.APPLICATION_JSON) + .content(requestBody) + .accept(MediaType.APPLICATION_JSON) + .assertThat() + .hasStatusOk() + .hasContentType(MediaType.APPLICATION_JSON); + } + + @Test + void testPauseJobWithInvalidJobName() { + String requestBody = + """ + { + "jobName": "InvalidJob", + "jobId": "12345" + } + """; + + mockMvcTester + .post() + .uri("/api/pauseJob") + .contentType(MediaType.APPLICATION_JSON) + .content(requestBody) + .accept(MediaType.APPLICATION_JSON) + .assertThat() + .hasStatusOk() + .hasContentType(MediaType.APPLICATION_JSON); + } + + @Test + void testResumeJob() { + String requestBody = + """ + { + "jobName": "SampleJob", + "jobId": "12345" + } + """; + mockMvcTester + .post() + .uri("/api/resumeJob") + .contentType(MediaType.APPLICATION_JSON) + .content(requestBody) + .accept(MediaType.APPLICATION_JSON) + .assertThat() + .hasStatusOk() + .hasContentType(MediaType.APPLICATION_JSON); + } + + @Test + void testResumeJobWithInvalidJobName() { + String requestBody = + """ + { + "jobName": "InvalidJob", + "jobId": "12345" + } + """; + mockMvcTester + .post() + .uri("/api/resumeJob") + .contentType(MediaType.APPLICATION_JSON) + .content(requestBody) + .accept(MediaType.APPLICATION_JSON) + .assertThat() + .hasStatusOk() + .hasContentType(MediaType.APPLICATION_JSON); + } + + @Test + void testRunJob() { + String requestBody = + """ + { + "jobName": "SampleJob", + "jobId": "12345" + } + """; + + mockMvcTester + .post() + .uri("/api/runJob") + .contentType(MediaType.APPLICATION_JSON) + .content(requestBody) + .accept(MediaType.APPLICATION_JSON) + .assertThat() + .hasStatusOk() + .hasContentType(MediaType.APPLICATION_JSON); + } + + @Test + void testRunJobWithInvalidJobName() { + String requestBody = + """ + { + "jobName": "InvalidJob", + "jobId": "12345" + } + """; + + mockMvcTester + .post() + .uri("/api/runJob") + .contentType(MediaType.APPLICATION_JSON) + .content(requestBody) + .accept(MediaType.APPLICATION_JSON) + .assertThat() + .hasStatusOk() + .hasContentType(MediaType.APPLICATION_JSON); + } + + @Test + void testDeleteJob() { + String requestBody = + """ + { + "jobName": "SampleJob", + "jobId": "12345" + } + """; + + mockMvcTester + .delete() + .uri("/api/deleteJob") + .contentType(MediaType.APPLICATION_JSON) + .content(requestBody) + .accept(MediaType.APPLICATION_JSON) + .assertThat() + .hasStatusOk() + .hasContentType(MediaType.APPLICATION_JSON); + } +}