[Fix-5509]: createSchedule interface, process definition id -> process definition code (#5756)

* fix: createSchedule interface, process definition id -> process definition code

* fix: add junit

* fix junit

* fix: projectName -> projectCode

* fix UT

* Optimize variable type

Co-authored-by: wen-hemin <wenhemin@apache.com>
This commit is contained in:
wen-hemin 2021-07-07 14:01:02 +08:00 committed by GitHub
parent 654fa39789
commit d382a7ba8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 71 additions and 37 deletions

View File

@ -65,7 +65,7 @@ import springfox.documentation.annotations.ApiIgnore;
*/ */
@Api(tags = "SCHEDULER_TAG") @Api(tags = "SCHEDULER_TAG")
@RestController @RestController
@RequestMapping("/projects/{projectName}/schedule") @RequestMapping("/projects/{projectCode}/schedule")
public class SchedulerController extends BaseController { public class SchedulerController extends BaseController {
public static final String DEFAULT_WARNING_TYPE = "NONE"; public static final String DEFAULT_WARNING_TYPE = "NONE";
@ -81,8 +81,8 @@ public class SchedulerController extends BaseController {
* create schedule * create schedule
* *
* @param loginUser login user * @param loginUser login user
* @param projectName project name * @param projectCode project code
* @param processDefinitionId process definition id * @param processDefinitionCode process definition code
* @param schedule scheduler * @param schedule scheduler
* @param warningType warning type * @param warningType warning type
* @param warningGroupId warning group id * @param warningGroupId warning group id
@ -93,7 +93,7 @@ public class SchedulerController extends BaseController {
*/ */
@ApiOperation(value = "createSchedule", notes = "CREATE_SCHEDULE_NOTES") @ApiOperation(value = "createSchedule", notes = "CREATE_SCHEDULE_NOTES")
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(name = "processDefinitionId", value = "PROCESS_DEFINITION_ID", required = true, dataType = "Int", example = "100"), @ApiImplicitParam(name = "processDefinitionCode", value = "PROCESS_DEFINITION_CODE", required = true, dataType = "Long", example = "100"),
@ApiImplicitParam(name = "schedule", value = "SCHEDULE", dataType = "String", @ApiImplicitParam(name = "schedule", value = "SCHEDULE", dataType = "String",
example = "{'startTime':'2019-06-10 00:00:00','endTime':'2019-06-13 00:00:00','timezoneId':'America/Phoenix','crontab':'0 0 3/6 * * ? *'}"), example = "{'startTime':'2019-06-10 00:00:00','endTime':'2019-06-13 00:00:00','timezoneId':'America/Phoenix','crontab':'0 0 3/6 * * ? *'}"),
@ApiImplicitParam(name = "warningType", value = "WARNING_TYPE", type = "WarningType"), @ApiImplicitParam(name = "warningType", value = "WARNING_TYPE", type = "WarningType"),
@ -107,15 +107,15 @@ public class SchedulerController extends BaseController {
@ApiException(CREATE_SCHEDULE_ERROR) @ApiException(CREATE_SCHEDULE_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser") @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result createSchedule(@ApiIgnore @RequestAttribute(value = SESSION_USER) User loginUser, public Result createSchedule(@ApiIgnore @RequestAttribute(value = SESSION_USER) User loginUser,
@ApiParam(name = "projectName", value = "PROJECT_NAME", required = true) @PathVariable String projectName, @ApiParam(name = "projectCode", value = "PROJECT_CODE", required = true) @PathVariable long projectCode,
@RequestParam(value = "processDefinitionId") Integer processDefinitionId, @RequestParam(value = "processDefinitionCode") long processDefinitionCode,
@RequestParam(value = "schedule") String schedule, @RequestParam(value = "schedule") String schedule,
@RequestParam(value = "warningType", required = false, defaultValue = DEFAULT_WARNING_TYPE) WarningType warningType, @RequestParam(value = "warningType", required = false, defaultValue = DEFAULT_WARNING_TYPE) WarningType warningType,
@RequestParam(value = "warningGroupId", required = false, defaultValue = DEFAULT_NOTIFY_GROUP_ID) int warningGroupId, @RequestParam(value = "warningGroupId", required = false, defaultValue = DEFAULT_NOTIFY_GROUP_ID) int warningGroupId,
@RequestParam(value = "failureStrategy", required = false, defaultValue = DEFAULT_FAILURE_POLICY) FailureStrategy failureStrategy, @RequestParam(value = "failureStrategy", required = false, defaultValue = DEFAULT_FAILURE_POLICY) FailureStrategy failureStrategy,
@RequestParam(value = "workerGroup", required = false, defaultValue = "default") String workerGroup, @RequestParam(value = "workerGroup", required = false, defaultValue = "default") String workerGroup,
@RequestParam(value = "processInstancePriority", required = false, defaultValue = DEFAULT_PROCESS_INSTANCE_PRIORITY) Priority processInstancePriority) { @RequestParam(value = "processInstancePriority", required = false, defaultValue = DEFAULT_PROCESS_INSTANCE_PRIORITY) Priority processInstancePriority) {
Map<String, Object> result = schedulerService.insertSchedule(loginUser, projectName, processDefinitionId, schedule, Map<String, Object> result = schedulerService.insertSchedule(loginUser, projectCode, processDefinitionCode, schedule,
warningType, warningGroupId, failureStrategy, processInstancePriority, workerGroup); warningType, warningGroupId, failureStrategy, processInstancePriority, workerGroup);
return returnDataList(result); return returnDataList(result);

View File

@ -34,8 +34,8 @@ public interface SchedulerService {
* save schedule * save schedule
* *
* @param loginUser login user * @param loginUser login user
* @param projectName project name * @param projectCode project code
* @param processDefineId process definition id * @param processDefineCode process definition code
* @param schedule scheduler * @param schedule scheduler
* @param warningType warning type * @param warningType warning type
* @param warningGroupId warning group id * @param warningGroupId warning group id
@ -44,8 +44,9 @@ public interface SchedulerService {
* @param workerGroup worker group * @param workerGroup worker group
* @return create result code * @return create result code
*/ */
Map<String, Object> insertSchedule(User loginUser, String projectName, Map<String, Object> insertSchedule(User loginUser,
Integer processDefineId, long projectCode,
long processDefineCode,
String schedule, String schedule,
WarningType warningType, WarningType warningType,
int warningGroupId, int warningGroupId,

View File

@ -97,8 +97,8 @@ public class SchedulerServiceImpl extends BaseServiceImpl implements SchedulerSe
* save schedule * save schedule
* *
* @param loginUser login user * @param loginUser login user
* @param projectName project name * @param projectCode project name
* @param processDefineId process definition id * @param processDefineCode process definition code
* @param schedule scheduler * @param schedule scheduler
* @param warningType warning type * @param warningType warning type
* @param warningGroupId warning group id * @param warningGroupId warning group id
@ -109,8 +109,9 @@ public class SchedulerServiceImpl extends BaseServiceImpl implements SchedulerSe
*/ */
@Override @Override
@Transactional(rollbackFor = RuntimeException.class) @Transactional(rollbackFor = RuntimeException.class)
public Map<String, Object> insertSchedule(User loginUser, String projectName, public Map<String, Object> insertSchedule(User loginUser,
Integer processDefineId, long projectCode,
long processDefineCode,
String schedule, String schedule,
WarningType warningType, WarningType warningType,
int warningGroupId, int warningGroupId,
@ -120,7 +121,7 @@ public class SchedulerServiceImpl extends BaseServiceImpl implements SchedulerSe
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
Project project = projectMapper.queryByName(projectName); Project project = projectMapper.queryByCode(projectCode);
// check project auth // check project auth
boolean hasProjectAndPerm = projectService.hasProjectAndPerm(loginUser, project, result); boolean hasProjectAndPerm = projectService.hasProjectAndPerm(loginUser, project, result);
@ -129,8 +130,8 @@ public class SchedulerServiceImpl extends BaseServiceImpl implements SchedulerSe
} }
// check work flow define release state // check work flow define release state
ProcessDefinition processDefinition = processService.findProcessDefineById(processDefineId); ProcessDefinition processDefinition = processDefinitionMapper.queryByCode(processDefineCode);
result = executorService.checkProcessDefinitionValid(processDefinition, processDefineId); result = executorService.checkProcessDefinitionValid(processDefinition, processDefineCode);
if (result.get(Constants.STATUS) != Status.SUCCESS) { if (result.get(Constants.STATUS) != Status.SUCCESS) {
return result; return result;
} }
@ -138,7 +139,7 @@ public class SchedulerServiceImpl extends BaseServiceImpl implements SchedulerSe
Schedule scheduleObj = new Schedule(); Schedule scheduleObj = new Schedule();
Date now = new Date(); Date now = new Date();
scheduleObj.setProjectName(projectName); scheduleObj.setProjectName(project.getName());
scheduleObj.setProcessDefinitionId(processDefinition.getId()); scheduleObj.setProcessDefinitionId(processDefinition.getId());
scheduleObj.setProcessDefinitionName(processDefinition.getName()); scheduleObj.setProcessDefinitionName(processDefinition.getName());

View File

@ -19,13 +19,17 @@ package org.apache.dolphinscheduler.api.controller;
import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doNothing;
import java.text.MessageFormat;
import java.util.Map;
import org.apache.dolphinscheduler.api.ApiApplicationServer; import org.apache.dolphinscheduler.api.ApiApplicationServer;
import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.service.SessionService; import org.apache.dolphinscheduler.api.service.SessionService;
import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.enums.UserType; import org.apache.dolphinscheduler.common.enums.UserType;
import org.apache.dolphinscheduler.common.utils.StringUtils; import org.apache.dolphinscheduler.common.utils.StringUtils;
import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.service.registry.RegistryClient; import org.apache.dolphinscheduler.service.registry.RegistryClient;
import org.junit.After; import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
@ -87,6 +91,14 @@ public class AbstractControllerTest {
sessionId = session; sessionId = session;
Assert.assertTrue(StringUtils.isNotEmpty(session)); Assert.assertTrue(StringUtils.isNotEmpty(session));
}
public void putMsg(Map<String, Object> result, Status status, Object... statusParams) {
result.put(Constants.STATUS, status);
if (statusParams != null && statusParams.length > 0) {
result.put(Constants.MSG, MessageFormat.format(status.getMsg(), statusParams));
} else {
result.put(Constants.MSG, status.getMsg());
}
} }
} }

View File

@ -17,22 +17,30 @@
package org.apache.dolphinscheduler.api.controller; package org.apache.dolphinscheduler.api.controller;
import static org.mockito.ArgumentMatchers.isA;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.HashMap;
import java.util.Map;
import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.service.SchedulerService;
import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.enums.FailureStrategy; import org.apache.dolphinscheduler.common.enums.FailureStrategy;
import org.apache.dolphinscheduler.common.enums.Priority; import org.apache.dolphinscheduler.common.enums.Priority;
import org.apache.dolphinscheduler.common.enums.WarningType; import org.apache.dolphinscheduler.common.enums.WarningType;
import org.apache.dolphinscheduler.common.utils.JSONUtils; import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.dolphinscheduler.dao.entity.User;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mockito;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.MvcResult;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
@ -41,14 +49,17 @@ import org.springframework.util.MultiValueMap;
/** /**
* scheduler controller test * scheduler controller test
*/ */
public class SchedulerControllerTest extends AbstractControllerTest{ public class SchedulerControllerTest extends AbstractControllerTest {
private static Logger logger = LoggerFactory.getLogger(SchedulerControllerTest.class); private static Logger logger = LoggerFactory.getLogger(SchedulerControllerTest.class);
@MockBean
private SchedulerService schedulerService;
@Test @Test
public void testCreateSchedule() throws Exception { public void testCreateSchedule() throws Exception {
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>(); MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
paramsMap.add("processDefinitionId","40"); paramsMap.add("processDefinitionCode","40");
paramsMap.add("schedule","{'startTime':'2019-12-16 00:00:00','endTime':'2019-12-17 00:00:00','crontab':'0 0 6 * * ? *'}"); paramsMap.add("schedule","{'startTime':'2019-12-16 00:00:00','endTime':'2019-12-17 00:00:00','crontab':'0 0 6 * * ? *'}");
paramsMap.add("warningType",String.valueOf(WarningType.NONE)); paramsMap.add("warningType",String.valueOf(WarningType.NONE));
paramsMap.add("warningGroupId","1"); paramsMap.add("warningGroupId","1");
@ -58,7 +69,15 @@ public class SchedulerControllerTest extends AbstractControllerTest{
paramsMap.add("workerGroupId","1"); paramsMap.add("workerGroupId","1");
paramsMap.add("processInstancePriority",String.valueOf(Priority.HIGH)); paramsMap.add("processInstancePriority",String.valueOf(Priority.HIGH));
MvcResult mvcResult = mockMvc.perform(post("/projects/{projectName}/schedule/create","cxc_1113") Map<String, Object> serviceResult = new HashMap<>();
putMsg(serviceResult, Status.SUCCESS);
serviceResult.put(Constants.DATA_LIST, 1);
Mockito.when(schedulerService.insertSchedule(isA(User.class), isA(Long.class), isA(Long.class),
isA(String.class), isA(WarningType.class), isA(int.class), isA(FailureStrategy.class),
isA(Priority.class), isA(String.class))).thenReturn(serviceResult);
MvcResult mvcResult = mockMvc.perform(post("/projects/{projectCode}/schedule/create",123)
.header(SESSION_ID, sessionId) .header(SESSION_ID, sessionId)
.params(paramsMap)) .params(paramsMap))
.andExpect(status().isCreated()) .andExpect(status().isCreated())

View File

@ -76,15 +76,6 @@ public class WorkFlowLineageControllerTest extends AbstractControllerTest {
Assert.assertEquals(Status.SUCCESS.getCode(), response.getCode().intValue()); Assert.assertEquals(Status.SUCCESS.getCode(), response.getCode().intValue());
} }
private void putMsg(Map<String, Object> result, Status status, Object... statusParams) {
result.put(Constants.STATUS, status);
if (statusParams != null && statusParams.length > 0) {
result.put(Constants.MSG, MessageFormat.format(status.getMsg(), statusParams));
} else {
result.put(Constants.MSG, status.getMsg());
}
}
@Test @Test
public void testQueryWorkFlowLineageByIds() { public void testQueryWorkFlowLineageByIds() {
int projectId = 1; int projectId = 1;

View File

@ -240,7 +240,7 @@
msg = `${i18n.$t('Edit')}${i18n.$t('Success')},${i18n.$t('Please go online')}` msg = `${i18n.$t('Edit')}${i18n.$t('Success')},${i18n.$t('Please go online')}`
} else { } else {
api = 'dag/createSchedule' api = 'dag/createSchedule'
searchParams.processDefinitionId = this.timingData.item.id searchParams.processDefinitionCode = this.timingData.item.code
msg = `${i18n.$t('Create')}${i18n.$t('Success')}` msg = `${i18n.$t('Create')}${i18n.$t('Success')}`
} }

View File

@ -89,11 +89,13 @@
}, },
methods: { methods: {
...mapActions('projects', ['deleteProjects']), ...mapActions('projects', ['deleteProjects']),
...mapMutations('dag', ['setProjectId', 'setProjectName']), ...mapMutations('dag', ['setProjectId', 'setProjectCode', 'setProjectName']),
_switchProjects (item) { _switchProjects (item) {
this.setProjectId(item.id) this.setProjectId(item.id)
this.setProjectCode(item.code)
this.setProjectName(item.name) this.setProjectName(item.name)
localStore.setItem('projectId', item.id) localStore.setItem('projectId', item.id)
localStore.setItem('projectCode', item.code)
localStore.setItem('projectName', item.name) localStore.setItem('projectName', item.name)
this.$router.push({ path: `/projects/${item.id}/index` }) this.$router.push({ path: `/projects/${item.id}/index` })
}, },

View File

@ -59,8 +59,10 @@ const router = new Router({
projectId: to.params.projectId projectId: to.params.projectId
}).then(res => { }).then(res => {
store.commit('dag/setProjectId', res.id) store.commit('dag/setProjectId', res.id)
store.commit('dag/setProjectCode', res.code)
store.commit('dag/setProjectName', res.name) store.commit('dag/setProjectName', res.name)
localStore.setItem('projectId', res.id) localStore.setItem('projectId', res.id)
localStore.setItem('projectCode', res.code)
localStore.setItem('projectName', res.name) localStore.setItem('projectName', res.name)
next() next()
}).catch(e => { }).catch(e => {

View File

@ -555,7 +555,7 @@ export default {
*/ */
createSchedule ({ state }, payload) { createSchedule ({ state }, payload) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
io.post(`projects/${state.projectName}/schedule/create`, payload, res => { io.post(`projects/${state.projectCode}/schedule/create`, payload, res => {
resolve(res) resolve(res)
}).catch(e => { }).catch(e => {
reject(e) reject(e)

View File

@ -21,6 +21,9 @@ export default {
setProjectId (state, payload) { setProjectId (state, payload) {
state.projectId = payload state.projectId = payload
}, },
setProjectCode (state, payload) {
state.projectCode = payload
},
setProjectName (state, payload) { setProjectName (state, payload) {
state.projectName = payload state.projectName = payload
}, },

View File

@ -19,6 +19,7 @@ import localStore from '@/module/util/localStorage'
// Get the project currently clicked // Get the project currently clicked
const projectId = localStore.getItem('projectId') const projectId = localStore.getItem('projectId')
const projectCode = localStore.getItem('projectCode')
const projectName = localStore.getItem('projectName') const projectName = localStore.getItem('projectName')
export default { export default {
@ -50,6 +51,8 @@ export default {
isEditDag: false, isEditDag: false,
// Current project id // Current project id
projectId: projectId, projectId: projectId,
// Current project code
projectCode: projectCode,
// Current project name // Current project name
projectName: projectName || '', projectName: projectName || '',
// Whether to go online the process definition // Whether to go online the process definition