-
Notifications
You must be signed in to change notification settings - Fork 1
/
JobStateRestControllerTest.java
121 lines (107 loc) · 5.32 KB
/
JobStateRestControllerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package org.appng.application.scheduler.service;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Properties;
import org.appng.api.ScheduledJobResult;
import org.appng.api.ScheduledJobResult.ExecutionResult;
import org.appng.application.scheduler.SchedulingProperties;
import org.appng.application.scheduler.business.SchedulingController;
import org.appng.application.scheduler.business.TestJobRecordings;
import org.appng.application.scheduler.quartz.RecordingJobListener;
import org.appng.core.domain.JobExecutionRecord;
import org.appng.core.repository.JobExecutionRecordRepository;
import org.appng.testsupport.TestBase;
import org.appng.testsupport.validation.WritingJsonValidator;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@ContextConfiguration(inheritLocations = false, locations = { TestBase.BEANS_PATH, TestBase.TESTCONTEXT_CORE,
TestBase.TESTCONTEXT_JPA, "classpath:beans-test-core.xml" }, initializers = JobStateRestControllerTest.class)
public class JobStateRestControllerTest extends TestBase {
private @Mock JobExecutionContext jobContext;
private @Autowired RecordingJobListener listener;
private @Autowired SchedulingController schedulerController;
private @Autowired MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter;
private MockMvc mvc;
public JobStateRestControllerTest() {
super("appng-scheduler", APPLICATION_HOME);
setEntityPackage(JobExecutionRecord.class.getPackage().getName());
setRepositoryBase(JobExecutionRecordRepository.class.getPackage().getName());
}
@Before
public void startController() {
mvc = MockMvcBuilders.standaloneSetup(context.getBean(JobStateRestController.class))
.setCustomArgumentResolvers(getHandlerMethodArgumentResolver())
.setMessageConverters(mappingJackson2HttpMessageConverter).build();
Mockito.when(site.getApplications()).thenReturn(new HashSet<>(Arrays.asList(application)));
Mockito.when(site.getApplication("appng-scheduler")).thenReturn(application);
schedulerController.start(site, application, environment);
ScheduledJobResult result = new ScheduledJobResult();
result.setResult(ExecutionResult.SUCCESS);
TestJobRecordings.createJobResult(listener, result, jobContext, site, application,
application.getName() + "_indexJob");
TestJobRecordings.createJobResult(listener, result, jobContext, site, application,
application.getName() + "_longRunningJob");
}
@Override
protected void mockSite(GenericApplicationContext applicationContext) {
super.mockSite(applicationContext);
Mockito.when(site.getDomain()).thenReturn("https://appng.org");
Mockito.when(site.getName()).thenReturn("appng");
}
@Override
protected Properties getProperties() {
return SchedulingProperties.getProperties();
}
@Test
public void testJobs() throws Exception {
Mockito.when(site.isActive()).thenReturn(true);
MockHttpServletRequestBuilder builder = get("/jobState/list")
.header(HttpHeaders.AUTHORIZATION, "Bearer TheBearer").contentType(MediaType.APPLICATION_JSON)
.characterEncoding("utf-8");
ResultActions response = mvc.perform(builder);
MvcResult result = response.andExpect(status().is(HttpStatus.OK.value())).andReturn();
String controlFile = "json/RecordsRestControllerTest-testJobs.json";
String responseJson = result.getResponse().getContentAsString();
System.err.println(responseJson);
String json = responseJson.replaceFirst("\\d{4}.*:\\d{2}", "2021-09-23T09:01:15.476+02:00");
WritingJsonValidator.validate(json, controlFile);
}
@Test
public void testIndexJob() throws Exception {
runTest("indexJob");
}
@Test
public void testLongRunningJob() throws Exception {
runTest("longRunningJob");
}
protected void runTest(String jobName) throws Exception {
ResultActions response = mvc.perform(
get("/jobState/appng-scheduler/" + jobName).header(HttpHeaders.AUTHORIZATION, "Bearer TheBearer")
.contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8"));
MvcResult result = response.andExpect(status().is(HttpStatus.OK.value())).andReturn();
String controlFile = "json/RecordsRestControllerTest-" + jobName + ".json";
String json = result.getResponse().getContentAsString().replaceFirst("\\d{4}.*:\\d{2}",
"2021-09-23T09:01:15.476+02:00");
WritingJsonValidator.validate(json, controlFile);
}
}