[Improvement] Avoid using search in for and start using testSaveTaskDefine (#11383) (#12773)

Co-authored-by: zhangshunmin <zhangshunmin@kezaihui.com>
Co-authored-by: longtb <67264931+longtb976@users.noreply.github.com>
Co-authored-by: zhangshunmin <zhangshunmin@kezaihui.com>
This commit is contained in:
Eric Gao 2022-11-07 16:00:54 +08:00 committed by GitHub
parent 75eb911b91
commit 0aff74f5b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 11 deletions

View File

@ -2509,17 +2509,26 @@ public class ProcessServiceImpl implements ProcessService {
}
int insertResult = 0;
int updateResult = 0;
for (TaskDefinitionLog taskDefinitionToUpdate : updateTaskDefinitionLogs) {
TaskDefinition task = taskDefinitionMapper.queryByCode(taskDefinitionToUpdate.getCode());
if (task == null) {
newTaskDefinitionLogs.add(taskDefinitionToUpdate);
} else {
insertResult += taskDefinitionLogMapper.insert(taskDefinitionToUpdate);
if (Boolean.TRUE.equals(syncDefine)) {
taskDefinitionToUpdate.setId(task.getId());
updateResult += taskDefinitionMapper.updateById(taskDefinitionToUpdate);
if (!updateTaskDefinitionLogs.isEmpty()) {
List<TaskDefinition> taskDefinitions = taskDefinitionMapper.queryByCodeList(updateTaskDefinitionLogs.stream().map(TaskDefinition::getCode).distinct().collect(Collectors.toList()));
for (TaskDefinitionLog taskDefinitionToUpdate : updateTaskDefinitionLogs) {
TaskDefinition task = null;
for (TaskDefinition taskDefinition : taskDefinitions) {
if (taskDefinitionToUpdate.getCode() == taskDefinition.getCode()) {
task = taskDefinition;
break;
}
}
if (task == null) {
newTaskDefinitionLogs.add(taskDefinitionToUpdate);
} else {
updateResult++;
insertResult += taskDefinitionLogMapper.insert(taskDefinitionToUpdate);
if (Boolean.TRUE.equals(syncDefine)) {
taskDefinitionToUpdate.setId(task.getId());
updateResult += taskDefinitionMapper.updateById(taskDefinitionToUpdate);
} else {
updateResult++;
}
}
}
}

View File

@ -78,6 +78,7 @@ import org.apache.dolphinscheduler.plugin.task.api.enums.dp.ValueType;
import org.apache.dolphinscheduler.plugin.task.api.model.ResourceInfo;
import org.apache.dolphinscheduler.service.exceptions.ServiceException;
import org.apache.dolphinscheduler.service.quartz.cron.CronUtilsTest;
import org.apache.dolphinscheduler.service.task.TaskPluginManager;
import org.apache.dolphinscheduler.spi.params.base.FormType;
import java.util.ArrayList;
@ -162,6 +163,9 @@ public class ProcessServiceTest {
@Mock
private ScheduleMapper scheduleMapper;
@Mock
TaskPluginManager taskPluginManager;
@Test
public void testCreateSubCommand() {
ProcessInstance parentInstance = new ProcessInstance();
@ -671,6 +675,7 @@ public class ProcessServiceTest {
return list;
}
@Test
public void testSaveTaskDefine() {
User operator = new User();
operator.setId(-1);
@ -695,9 +700,10 @@ public class ProcessServiceTest {
taskDefinition.setVersion(1);
taskDefinition.setCreateTime(new Date());
taskDefinition.setUpdateTime(new Date());
Mockito.when(taskPluginManager.getParameters(any())).thenReturn(null);
Mockito.when(taskDefinitionLogMapper.queryByDefinitionCodeAndVersion(taskDefinition.getCode(), taskDefinition.getVersion())).thenReturn(taskDefinition);
Mockito.when(taskDefinitionLogMapper.queryMaxVersionForDefinition(taskDefinition.getCode())).thenReturn(1);
Mockito.when(taskDefinitionMapper.queryByCode(taskDefinition.getCode())).thenReturn(taskDefinition);
Mockito.when(taskDefinitionMapper.queryByCodeList(Collections.singletonList(taskDefinition.getCode()))).thenReturn(Collections.singletonList(taskDefinition));
int result = processService.saveTaskDefine(operator, projectCode, taskDefinitionLogs, Boolean.TRUE);
Assert.assertEquals(0, result);
}