|
|
|
@ -18,12 +18,14 @@
|
|
|
|
|
package org.apache.dolphinscheduler.api.service; |
|
|
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.api.enums.Status; |
|
|
|
|
import org.apache.dolphinscheduler.api.exceptions.ServiceException; |
|
|
|
|
import org.apache.dolphinscheduler.api.service.impl.ProjectServiceImpl; |
|
|
|
|
import org.apache.dolphinscheduler.api.service.impl.SchedulerServiceImpl; |
|
|
|
|
import org.apache.dolphinscheduler.common.constants.Constants; |
|
|
|
|
import org.apache.dolphinscheduler.common.enums.ReleaseState; |
|
|
|
|
import org.apache.dolphinscheduler.common.model.Server; |
|
|
|
|
import org.apache.dolphinscheduler.dao.entity.Environment; |
|
|
|
|
import org.apache.dolphinscheduler.dao.entity.ProcessDefinition; |
|
|
|
|
import org.apache.dolphinscheduler.dao.entity.ProcessTaskRelation; |
|
|
|
|
import org.apache.dolphinscheduler.dao.entity.Project; |
|
|
|
|
import org.apache.dolphinscheduler.dao.entity.Schedule; |
|
|
|
|
import org.apache.dolphinscheduler.dao.entity.User; |
|
|
|
@ -43,6 +45,8 @@ import java.util.Map;
|
|
|
|
|
import org.junit.Assert; |
|
|
|
|
import org.junit.Before; |
|
|
|
|
import org.junit.Test; |
|
|
|
|
import org.junit.jupiter.api.Assertions; |
|
|
|
|
import org.junit.jupiter.api.BeforeEach; |
|
|
|
|
import org.junit.runner.RunWith; |
|
|
|
|
import org.mockito.InjectMocks; |
|
|
|
|
import org.mockito.Mock; |
|
|
|
@ -83,19 +87,31 @@ public class SchedulerServiceTest {
|
|
|
|
|
@Mock |
|
|
|
|
private SchedulerApi schedulerApi; |
|
|
|
|
|
|
|
|
|
@Before |
|
|
|
|
protected static User user; |
|
|
|
|
protected Exception exception; |
|
|
|
|
private static final String userName = "userName"; |
|
|
|
|
private static final String projectName = "projectName"; |
|
|
|
|
private static final long projectCode = 1L; |
|
|
|
|
private static final int userId = 1; |
|
|
|
|
private static final String processDefinitionName = "processDefinitionName"; |
|
|
|
|
private static final long processDefinitionCode = 2L; |
|
|
|
|
private static final int processDefinitionVersion = 3; |
|
|
|
|
private static final int scheduleId = 3; |
|
|
|
|
private static final long environmentCode = 4L; |
|
|
|
|
private static final String startTime = "2020-01-01 12:13:14"; |
|
|
|
|
private static final String endTime = "2020-02-01 12:13:14"; |
|
|
|
|
private static final String crontab = "0 0 * * * ? *"; |
|
|
|
|
|
|
|
|
|
@BeforeEach |
|
|
|
|
public void setUp() { |
|
|
|
|
|
|
|
|
|
user = new User(); |
|
|
|
|
user.setUserName(userName); |
|
|
|
|
user.setId(userId); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testSetScheduleState() { |
|
|
|
|
String projectName = "test"; |
|
|
|
|
long projectCode = 1L; |
|
|
|
|
User loginUser = new User(); |
|
|
|
|
loginUser.setId(1); |
|
|
|
|
Map<String, Object> result = new HashMap<String, Object>(); |
|
|
|
|
Project project = getProject(projectName, projectCode); |
|
|
|
|
Project project = getProject(); |
|
|
|
|
|
|
|
|
|
ProcessDefinition processDefinition = new ProcessDefinition(); |
|
|
|
|
processDefinition.setProjectCode(projectCode); |
|
|
|
@ -109,53 +125,58 @@ public class SchedulerServiceTest {
|
|
|
|
|
masterServers.add(new Server()); |
|
|
|
|
|
|
|
|
|
Mockito.when(scheduleMapper.selectById(1)).thenReturn(schedule); |
|
|
|
|
|
|
|
|
|
Mockito.when(processDefinitionMapper.queryByCode(1)).thenReturn(processDefinition); |
|
|
|
|
Mockito.when(projectMapper.queryByCode(projectCode)).thenReturn(project); |
|
|
|
|
Mockito.when(projectMapper.queryByName(projectName)).thenReturn(project); |
|
|
|
|
|
|
|
|
|
Mockito.when(processDefinitionMapper.queryByCode(1)).thenReturn(processDefinition); |
|
|
|
|
|
|
|
|
|
//hash no auth
|
|
|
|
|
result = schedulerService.setScheduleState(loginUser, project.getCode(), 1, ReleaseState.ONLINE); |
|
|
|
|
|
|
|
|
|
Mockito.when(projectService.hasProjectAndPerm(loginUser, project, result,null)).thenReturn(true); |
|
|
|
|
//schedule not exists
|
|
|
|
|
result = schedulerService.setScheduleState(loginUser, project.getCode(), 2, ReleaseState.ONLINE); |
|
|
|
|
Assert.assertEquals(Status.SCHEDULE_CRON_NOT_EXISTS, result.get(Constants.STATUS)); |
|
|
|
|
// schedule not exists
|
|
|
|
|
exception = Assertions.assertThrows(ServiceException.class, () -> { |
|
|
|
|
schedulerService.setScheduleState(user, project.getCode(), 2, ReleaseState.ONLINE); |
|
|
|
|
}); |
|
|
|
|
Assertions.assertEquals(Status.SCHEDULE_CRON_NOT_EXISTS.getCode(), ((ServiceException) exception).getCode()); |
|
|
|
|
|
|
|
|
|
//SCHEDULE_CRON_REALEASE_NEED_NOT_CHANGE
|
|
|
|
|
result = schedulerService.setScheduleState(loginUser, project.getCode(), 1, ReleaseState.OFFLINE); |
|
|
|
|
Assert.assertEquals(Status.SCHEDULE_CRON_REALEASE_NEED_NOT_CHANGE, result.get(Constants.STATUS)); |
|
|
|
|
// SCHEDULE_CRON_RELEASE_NEED_NOT_CHANGE
|
|
|
|
|
exception = Assertions.assertThrows(ServiceException.class, () -> { |
|
|
|
|
schedulerService.setScheduleState(user, project.getCode(), 1, ReleaseState.OFFLINE); |
|
|
|
|
}); |
|
|
|
|
Assertions.assertEquals(Status.SCHEDULE_CRON_REALEASE_NEED_NOT_CHANGE.getCode(), |
|
|
|
|
((ServiceException) exception).getCode()); |
|
|
|
|
|
|
|
|
|
//PROCESS_DEFINE_NOT_EXIST
|
|
|
|
|
schedule.setProcessDefinitionCode(2); |
|
|
|
|
result = schedulerService.setScheduleState(loginUser, project.getCode(), 1, ReleaseState.ONLINE); |
|
|
|
|
Assert.assertEquals(Status.PROCESS_DEFINE_NOT_EXIST, result.get(Constants.STATUS)); |
|
|
|
|
exception = Assertions.assertThrows(ServiceException.class, () -> { |
|
|
|
|
schedulerService.setScheduleState(user, project.getCode(), 1, ReleaseState.ONLINE); |
|
|
|
|
}); |
|
|
|
|
Assertions.assertEquals(Status.PROCESS_DEFINE_NOT_EXIST.getCode(), ((ServiceException) exception).getCode()); |
|
|
|
|
schedule.setProcessDefinitionCode(1); |
|
|
|
|
|
|
|
|
|
result = schedulerService.setScheduleState(loginUser, project.getCode(), 1, ReleaseState.ONLINE); |
|
|
|
|
Assert.assertEquals(Status.PROCESS_DAG_IS_EMPTY, result.get(Constants.STATUS)); |
|
|
|
|
|
|
|
|
|
// online also success
|
|
|
|
|
ProcessTaskRelation processTaskRelation = new ProcessTaskRelation(); |
|
|
|
|
List<ProcessTaskRelation> processTaskRelationList = new ArrayList<>(); |
|
|
|
|
processTaskRelationList.add(processTaskRelation); |
|
|
|
|
Mockito.when(processTaskRelationMapper.queryByProcessCode(projectCode, 1)).thenReturn(processTaskRelationList); |
|
|
|
|
exception = Assertions.assertThrows(ServiceException.class, () -> { |
|
|
|
|
schedulerService.setScheduleState(user, project.getCode(), 1, ReleaseState.ONLINE); |
|
|
|
|
}); |
|
|
|
|
Assertions.assertEquals(Status.PROCESS_DEFINE_NOT_RELEASE.getCode(), ((ServiceException) exception).getCode()); |
|
|
|
|
|
|
|
|
|
// SUCCESS
|
|
|
|
|
Server server = new Server(); |
|
|
|
|
List<Server> serverList = new ArrayList<>(); |
|
|
|
|
serverList.add(server); |
|
|
|
|
Mockito.when(monitorService.getServerListFromRegistry(true)).thenReturn(serverList); |
|
|
|
|
processDefinition.setReleaseState(ReleaseState.ONLINE); |
|
|
|
|
Mockito.when(processService.findProcessDefineById(1)).thenReturn(processDefinition); |
|
|
|
|
|
|
|
|
|
result = schedulerService.setScheduleState(loginUser, project.getCode(), 1, ReleaseState.ONLINE); |
|
|
|
|
Assert.assertEquals(Status.PROCESS_DAG_IS_EMPTY, result.get(Constants.STATUS)); |
|
|
|
|
|
|
|
|
|
//set master
|
|
|
|
|
Mockito.when(monitorService.getServerListFromRegistry(true)).thenReturn(masterServers); |
|
|
|
|
|
|
|
|
|
//SUCCESS
|
|
|
|
|
result = schedulerService.setScheduleState(loginUser, project.getCode(), 1, ReleaseState.ONLINE); |
|
|
|
|
Assert.assertEquals(Status.PROCESS_DAG_IS_EMPTY, result.get(Constants.STATUS)); |
|
|
|
|
Assertions.assertDoesNotThrow(() -> { |
|
|
|
|
schedulerService.setScheduleState(user, project.getCode(), 1, ReleaseState.ONLINE); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Project getProject(String name, long code) { |
|
|
|
|
private Project getProject() { |
|
|
|
|
Project project = new Project(); |
|
|
|
|
project.setName(name); |
|
|
|
|
project.setCode(code); |
|
|
|
|
project.setUserId(1); |
|
|
|
|
|
|
|
|
|
project.setId(1); |
|
|
|
|
project.setName(projectName); |
|
|
|
|
project.setCode(projectCode); |
|
|
|
|
project.setUserId(userId); |
|
|
|
|
return project; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|