提交 1c5c8b75 编写于 作者: Y Yelli 提交者: qiaozhanwei

refactor import process (#1804)

* refactor import process

* add refactor import process UT

* add import process UT

* add null check UT for import process metadata

* add UT for import process

* modify dependentparam UT

* modify testAddImportDependentSpecialParam

* modify DependentParamTest

* modify processDefinitionService UT
上级 1a86e7ba
...@@ -59,7 +59,7 @@ public class ProcessMeta { ...@@ -59,7 +59,7 @@ public class ProcessMeta {
/** /**
* warning group id * warning group id
*/ */
private int scheduleWarningGroupId; private Integer scheduleWarningGroupId;
/** /**
* warning group name * warning group name
...@@ -99,7 +99,7 @@ public class ProcessMeta { ...@@ -99,7 +99,7 @@ public class ProcessMeta {
/** /**
* worker group id * worker group id
*/ */
private int scheduleWorkerGroupId; private Integer scheduleWorkerGroupId;
/** /**
* worker group name * worker group name
...@@ -165,7 +165,7 @@ public class ProcessMeta { ...@@ -165,7 +165,7 @@ public class ProcessMeta {
this.scheduleWarningType = scheduleWarningType; this.scheduleWarningType = scheduleWarningType;
} }
public int getScheduleWarningGroupId() { public Integer getScheduleWarningGroupId() {
return scheduleWarningGroupId; return scheduleWarningGroupId;
} }
...@@ -229,7 +229,7 @@ public class ProcessMeta { ...@@ -229,7 +229,7 @@ public class ProcessMeta {
this.scheduleProcessInstancePriority = scheduleProcessInstancePriority; this.scheduleProcessInstancePriority = scheduleProcessInstancePriority;
} }
public int getScheduleWorkerGroupId() { public Integer getScheduleWorkerGroupId() {
return scheduleWorkerGroupId; return scheduleWorkerGroupId;
} }
......
...@@ -22,9 +22,12 @@ import org.springframework.core.io.Resource; ...@@ -22,9 +22,12 @@ import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource; import org.springframework.core.io.UrlResource;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
...@@ -38,7 +41,7 @@ public class FileUtils { ...@@ -38,7 +41,7 @@ public class FileUtils {
/** /**
* copy source file to target file * copy source file to target file
* *
* @param file file * @param file file
* @param destFilename destination file name * @param destFilename destination file name
*/ */
...@@ -77,4 +80,27 @@ public class FileUtils { ...@@ -77,4 +80,27 @@ public class FileUtils {
} }
return null; return null;
} }
/**
* file convert String
* @param file MultipartFile file
* @return file content string
*/
public static String file2String(MultipartFile file) {
StringBuilder strBuilder = new StringBuilder();
try (InputStreamReader inputStreamReader = new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8)) {
BufferedReader streamReader = new BufferedReader(inputStreamReader);
String inputStr;
while ((inputStr = streamReader.readLine()) != null) {
strBuilder.append(inputStr);
}
} catch (IOException e) {
logger.error("file convert to string failed: {}", file.getName());
}
return strBuilder.toString();
}
} }
...@@ -25,11 +25,13 @@ import org.springframework.beans.factory.InitializingBean; ...@@ -25,11 +25,13 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
/** /**
* task node add datasource param strategy * task node add datasource param strategy
*/ */
@Service @Service
public class DataSourceParam implements exportProcessAddTaskParam, InitializingBean { public class DataSourceParam implements ProcessAddTaskParam, InitializingBean {
@Autowired @Autowired
private DataSourceMapper dataSourceMapper; private DataSourceMapper dataSourceMapper;
...@@ -40,7 +42,7 @@ public class DataSourceParam implements exportProcessAddTaskParam, InitializingB ...@@ -40,7 +42,7 @@ public class DataSourceParam implements exportProcessAddTaskParam, InitializingB
* @return task node json object * @return task node json object
*/ */
@Override @Override
public JSONObject addSpecialParam(JSONObject taskNode) { public JSONObject addExportSpecialParam(JSONObject taskNode) {
// add sqlParameters // add sqlParameters
JSONObject sqlParameters = JSONUtils.parseObject(taskNode.getString("params")); JSONObject sqlParameters = JSONUtils.parseObject(taskNode.getString("params"));
DataSource dataSource = dataSourceMapper.selectById((Integer) sqlParameters.get("datasource")); DataSource dataSource = dataSourceMapper.selectById((Integer) sqlParameters.get("datasource"));
...@@ -52,6 +54,23 @@ public class DataSourceParam implements exportProcessAddTaskParam, InitializingB ...@@ -52,6 +54,23 @@ public class DataSourceParam implements exportProcessAddTaskParam, InitializingB
return taskNode; return taskNode;
} }
/**
* import process add datasource params
* @param taskNode task node json object
* @return task node json object
*/
@Override
public JSONObject addImportSpecialParam(JSONObject taskNode) {
JSONObject sqlParameters = JSONUtils.parseObject(taskNode.getString("params"));
List<DataSource> dataSources = dataSourceMapper.queryDataSourceByName(sqlParameters.getString("datasourceName"));
if (!dataSources.isEmpty()) {
DataSource dataSource = dataSources.get(0);
sqlParameters.put("datasource", dataSource.getId());
}
taskNode.put("params", sqlParameters);
return taskNode;
}
/** /**
* put datasource strategy * put datasource strategy
......
...@@ -21,7 +21,9 @@ import com.alibaba.fastjson.JSONObject; ...@@ -21,7 +21,9 @@ import com.alibaba.fastjson.JSONObject;
import org.apache.dolphinscheduler.common.enums.TaskType; import org.apache.dolphinscheduler.common.enums.TaskType;
import org.apache.dolphinscheduler.common.utils.JSONUtils; import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.dolphinscheduler.dao.entity.ProcessDefinition; import org.apache.dolphinscheduler.dao.entity.ProcessDefinition;
import org.apache.dolphinscheduler.dao.entity.Project;
import org.apache.dolphinscheduler.dao.mapper.ProcessDefinitionMapper; import org.apache.dolphinscheduler.dao.mapper.ProcessDefinitionMapper;
import org.apache.dolphinscheduler.dao.mapper.ProjectMapper;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -30,19 +32,22 @@ import org.springframework.stereotype.Service; ...@@ -30,19 +32,22 @@ import org.springframework.stereotype.Service;
* task node add dependent param strategy * task node add dependent param strategy
*/ */
@Service @Service
public class DependentParam implements exportProcessAddTaskParam, InitializingBean { public class DependentParam implements ProcessAddTaskParam, InitializingBean {
@Autowired @Autowired
ProcessDefinitionMapper processDefineMapper; ProcessDefinitionMapper processDefineMapper;
@Autowired
ProjectMapper projectMapper;
/** /**
* add dependent param * add dependent param
* @param taskNode task node json object * @param taskNode task node json object
* @return task node json object * @return task node json object
*/ */
@Override @Override
public JSONObject addSpecialParam(JSONObject taskNode) { public JSONObject addExportSpecialParam(JSONObject taskNode) {
// add dependent param // add dependent param
JSONObject dependentParameters = JSONUtils.parseObject(taskNode.getString("dependence")); JSONObject dependentParameters = JSONUtils.parseObject(taskNode.getString("dependence"));
...@@ -67,6 +72,36 @@ public class DependentParam implements exportProcessAddTaskParam, InitializingBe ...@@ -67,6 +72,36 @@ public class DependentParam implements exportProcessAddTaskParam, InitializingBe
return taskNode; return taskNode;
} }
/**
* import process add dependent param
* @param taskNode task node json object
* @return
*/
@Override
public JSONObject addImportSpecialParam(JSONObject taskNode) {
JSONObject dependentParameters = JSONUtils.parseObject(taskNode.getString("dependence"));
if(dependentParameters != null){
JSONArray dependTaskList = (JSONArray) dependentParameters.get("dependTaskList");
for (int h = 0; h < dependTaskList.size(); h++) {
JSONObject dependentTaskModel = dependTaskList.getJSONObject(h);
JSONArray dependItemList = (JSONArray) dependentTaskModel.get("dependItemList");
for (int k = 0; k < dependItemList.size(); k++) {
JSONObject dependentItem = dependItemList.getJSONObject(k);
Project dependentItemProject = projectMapper.queryByName(dependentItem.getString("projectName"));
if(dependentItemProject != null){
ProcessDefinition definition = processDefineMapper.queryByDefineName(dependentItemProject.getId(),dependentItem.getString("definitionName"));
if(definition != null){
dependentItem.put("projectId",dependentItemProject.getId());
dependentItem.put("definitionId",definition.getId());
}
}
}
}
taskNode.put("dependence", dependentParameters);
}
return taskNode;
}
/** /**
* put dependent strategy * put dependent strategy
*/ */
......
...@@ -19,14 +19,21 @@ package org.apache.dolphinscheduler.api.utils.exportprocess; ...@@ -19,14 +19,21 @@ package org.apache.dolphinscheduler.api.utils.exportprocess;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
/** /**
* exportProcessAddTaskParam * ProcessAddTaskParam
*/ */
public interface exportProcessAddTaskParam { public interface ProcessAddTaskParam {
/**
* add export task special param: sql task dependent task
* @param taskNode task node json object
* @return task node json object
*/
JSONObject addExportSpecialParam(JSONObject taskNode);
/** /**
* add task special param: sql task dependent task * add task special param: sql task dependent task
* @param taskNode task node json object * @param taskNode task node json object
* @return task node json object * @return task node json object
*/ */
JSONObject addSpecialParam(JSONObject taskNode); JSONObject addImportSpecialParam(JSONObject taskNode);
} }
...@@ -24,13 +24,13 @@ import java.util.concurrent.ConcurrentHashMap; ...@@ -24,13 +24,13 @@ import java.util.concurrent.ConcurrentHashMap;
*/ */
public class TaskNodeParamFactory { public class TaskNodeParamFactory {
private static Map<String, exportProcessAddTaskParam> taskServices = new ConcurrentHashMap<>(); private static Map<String, ProcessAddTaskParam> taskServices = new ConcurrentHashMap<>();
public static exportProcessAddTaskParam getByTaskType(String taskType){ public static ProcessAddTaskParam getByTaskType(String taskType){
return taskServices.get(taskType); return taskServices.get(taskType);
} }
static void register(String taskType, exportProcessAddTaskParam addSpecialTaskParam){ static void register(String taskType, ProcessAddTaskParam addSpecialTaskParam){
if (null != taskType) { if (null != taskType) {
taskServices.put(taskType, addSpecialTaskParam); taskServices.put(taskType, addSpecialTaskParam);
} }
......
...@@ -20,9 +20,11 @@ import com.alibaba.fastjson.JSON; ...@@ -20,9 +20,11 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import org.apache.dolphinscheduler.api.ApiApplicationServer; import org.apache.dolphinscheduler.api.ApiApplicationServer;
import org.apache.dolphinscheduler.api.dto.ProcessMeta;
import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.enums.*; import org.apache.dolphinscheduler.common.enums.*;
import org.apache.dolphinscheduler.common.utils.DateUtils;
import org.apache.dolphinscheduler.common.utils.FileUtils; import org.apache.dolphinscheduler.common.utils.FileUtils;
import org.apache.dolphinscheduler.common.utils.JSONUtils; import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.dolphinscheduler.dao.entity.*; import org.apache.dolphinscheduler.dao.entity.*;
...@@ -164,7 +166,7 @@ public class ProcessDefinitionServiceTest { ...@@ -164,7 +166,7 @@ public class ProcessDefinitionServiceTest {
Mockito.when(dataSourceMapper.selectById(1)).thenReturn(getDataSource()); Mockito.when(dataSourceMapper.selectById(1)).thenReturn(getDataSource());
Mockito.when(processDefineMapper.queryByDefineId(2)).thenReturn(getProcessDefinition()); Mockito.when(processDefineMapper.queryByDefineId(2)).thenReturn(getProcessDefinition());
String corSqlDependentJson = processDefinitionService.addTaskNodeSpecialParam(sqlDependentJson); String corSqlDependentJson = processDefinitionService.addExportTaskNodeSpecialParam(sqlDependentJson);
JSONAssert.assertEquals(sqlDependentJson,corSqlDependentJson,false); JSONAssert.assertEquals(sqlDependentJson,corSqlDependentJson,false);
...@@ -182,6 +184,62 @@ public class ProcessDefinitionServiceTest { ...@@ -182,6 +184,62 @@ public class ProcessDefinitionServiceTest {
Assert.assertNotEquals(sqlDependentJson,exportProcessMetaDataStr); Assert.assertNotEquals(sqlDependentJson,exportProcessMetaDataStr);
} }
@Test
public void testAddExportTaskNodeSpecialParam() throws JSONException {
String shellJson = "{\"globalParams\":[],\"tasks\":[{\"id\":\"tasks-9527\",\"name\":\"shell-1\"," +
"\"params\":{\"resourceList\":[],\"localParams\":[],\"rawScript\":\"#!/bin/bash\\necho \\\"shell-1\\\"\"}," +
"\"description\":\"\",\"runFlag\":\"NORMAL\",\"dependence\":{},\"maxRetryTimes\":\"0\",\"retryInterval\":\"1\"," +
"\"timeout\":{\"strategy\":\"\",\"interval\":1,\"enable\":false},\"taskInstancePriority\":\"MEDIUM\"," +
"\"workerGroupId\":-1,\"preTasks\":[]}],\"tenantId\":1,\"timeout\":0}";
String resultStr = processDefinitionService.addExportTaskNodeSpecialParam(shellJson);
JSONAssert.assertEquals(shellJson, resultStr, false);
}
@Test
public void testImportProcessSchedule() {
User loginUser = new User();
loginUser.setId(1);
loginUser.setUserType(UserType.GENERAL_USER);
String currentProjectName = "test";
String processDefinitionName = "test_process";
Integer processDefinitionId = 1;
Schedule schedule = getSchedule();
ProcessMeta processMeta = getProcessMeta();
int insertFlag = processDefinitionService.importProcessSchedule(loginUser, currentProjectName, processMeta,
processDefinitionName, processDefinitionId);
Assert.assertEquals(0, insertFlag);
ProcessMeta processMetaCron = new ProcessMeta();
processMetaCron.setScheduleCrontab(schedule.getCrontab());
int insertFlagCron = processDefinitionService.importProcessSchedule(loginUser, currentProjectName, processMetaCron,
processDefinitionName, processDefinitionId);
Assert.assertEquals(0, insertFlagCron);
WorkerGroup workerGroup = new WorkerGroup();
workerGroup.setName("ds-test-workergroup");
workerGroup.setId(2);
List<WorkerGroup> workerGroups = new ArrayList<>();
workerGroups.add(workerGroup);
Mockito.when(workerGroupMapper.queryWorkerGroupByName("ds-test")).thenReturn(workerGroups);
processMetaCron.setScheduleWorkerGroupName("ds-test");
int insertFlagWorker = processDefinitionService.importProcessSchedule(loginUser, currentProjectName, processMetaCron,
processDefinitionName, processDefinitionId);
Assert.assertEquals(0, insertFlagWorker);
Mockito.when(workerGroupMapper.queryWorkerGroupByName("ds-test")).thenReturn(null);
int workerNullFlag = processDefinitionService.importProcessSchedule(loginUser, currentProjectName, processMetaCron,
processDefinitionName, processDefinitionId);
Assert.assertEquals(0, workerNullFlag);
}
/** /**
* import sub process test * import sub process test
*/ */
...@@ -321,9 +379,50 @@ public class ProcessDefinitionServiceTest { ...@@ -321,9 +379,50 @@ public class ProcessDefinitionServiceTest {
Assert.assertTrue(delete); Assert.assertTrue(delete);
String processMetaJson = "";
improssProcessCheckData(file, loginUser, currentProjectName, processMetaJson);
processMetaJson = "{\"scheduleWorkerGroupId\":-1}";
improssProcessCheckData(file, loginUser, currentProjectName, processMetaJson);
processMetaJson = "{\"scheduleWorkerGroupId\":-1,\"projectName\":\"test\"}";
improssProcessCheckData(file, loginUser, currentProjectName, processMetaJson);
processMetaJson = "{\"scheduleWorkerGroupId\":-1,\"projectName\":\"test\",\"processDefinitionName\":\"test_definition\"}";
improssProcessCheckData(file, loginUser, currentProjectName, processMetaJson);
}
/**
* check import process metadata
* @param file file
* @param loginUser login user
* @param currentProjectName current project name
* @param processMetaJson process meta json
* @throws IOException IO exception
*/
private void improssProcessCheckData(File file, User loginUser, String currentProjectName, String processMetaJson) throws IOException {
//check null
FileUtils.writeStringToFile(new File("/tmp/task.json"),processMetaJson);
File fileEmpty = new File("/tmp/task.json");
FileInputStream fileEmptyInputStream = new FileInputStream("/tmp/task.json");
MultipartFile multiFileEmpty = new MockMultipartFile(fileEmpty.getName(), fileEmpty.getName(),
ContentType.APPLICATION_OCTET_STREAM.toString(), fileEmptyInputStream);
Map<String, Object> resEmptyProcess = processDefinitionService.importProcessDefinition(loginUser, multiFileEmpty, currentProjectName);
Assert.assertEquals(Status.DATA_IS_NULL, resEmptyProcess.get(Constants.STATUS));
boolean deleteFlag = file.delete();
Assert.assertTrue(deleteFlag);
} }
/** /**
* get mock datasource * get mock datasource
* @return DataSource * @return DataSource
...@@ -382,6 +481,26 @@ public class ProcessDefinitionServiceTest { ...@@ -382,6 +481,26 @@ public class ProcessDefinitionServiceTest {
return schedule; return schedule;
} }
/**
* get mock processMeta
* @return processMeta
*/
private ProcessMeta getProcessMeta() {
ProcessMeta processMeta = new ProcessMeta();
Schedule schedule = getSchedule();
processMeta.setScheduleCrontab(schedule.getCrontab());
processMeta.setScheduleStartTime(DateUtils.dateToString(schedule.getStartTime()));
processMeta.setScheduleEndTime(DateUtils.dateToString(schedule.getEndTime()));
processMeta.setScheduleWarningType(String.valueOf(schedule.getWarningType()));
processMeta.setScheduleWarningGroupId(schedule.getWarningGroupId());
processMeta.setScheduleFailureStrategy(String.valueOf(schedule.getFailureStrategy()));
processMeta.setScheduleReleaseState(String.valueOf(schedule.getReleaseState()));
processMeta.setScheduleProcessInstancePriority(String.valueOf(schedule.getProcessInstancePriority()));
processMeta.setScheduleWorkerGroupId(schedule.getWorkerGroupId());
processMeta.setScheduleWorkerGroupName("workgroup1");
return processMeta;
}
private List<Schedule> getSchedulerList() { private List<Schedule> getSchedulerList() {
List<Schedule> scheduleList = new ArrayList<>(); List<Schedule> scheduleList = new ArrayList<>();
scheduleList.add(getSchedule()); scheduleList.add(getSchedule());
......
...@@ -17,21 +17,17 @@ ...@@ -17,21 +17,17 @@
package org.apache.dolphinscheduler.api.utils; package org.apache.dolphinscheduler.api.utils;
import org.junit.After; import org.apache.http.entity.ContentType;
import org.junit.Before; import org.junit.*;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream; import java.io.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import static org.junit.Assert.*; import static org.junit.Assert.*;
...@@ -106,4 +102,23 @@ public class FileUtilsTest { ...@@ -106,4 +102,23 @@ public class FileUtilsTest {
assertNull(resource1); assertNull(resource1);
} }
@Test
public void testFile2String() throws IOException {
String content = "123";
org.apache.dolphinscheduler.common.utils.FileUtils.writeStringToFile(new File("/tmp/task.json"),content);
File file = new File("/tmp/task.json");
FileInputStream fileInputStream = new FileInputStream("/tmp/task.json");
MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(),
ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
String resultStr = FileUtils.file2String(multipartFile);
Assert.assertEquals(content, resultStr);
boolean delete = file.delete();
Assert.assertTrue(delete);
}
} }
\ No newline at end of file
...@@ -35,25 +35,52 @@ import org.springframework.test.context.junit4.SpringRunner; ...@@ -35,25 +35,52 @@ import org.springframework.test.context.junit4.SpringRunner;
public class DataSourceParamTest { public class DataSourceParamTest {
@Test @Test
public void testAddDependentSpecialParam() throws JSONException { public void testAddExportDependentSpecialParam() throws JSONException {
String dependentJson = "{\"type\":\"DEPENDENT\",\"id\":\"tasks-33787\"," + String sqlJson = "{\"type\":\"SQL\",\"id\":\"tasks-27297\",\"name\":\"sql\"," +
"\"name\":\"dependent\",\"params\":{},\"description\":\"\",\"runFlag\":\"NORMAL\"," + "\"params\":{\"type\":\"MYSQL\",\"datasource\":1,\"sql\":\"select * from test\"," +
"\"dependence\":{\"relation\":\"AND\",\"dependTaskList\":[{\"relation\":\"AND\"," + "\"udfs\":\"\",\"sqlType\":\"1\",\"title\":\"\",\"receivers\":\"\",\"receiversCc\":\"\",\"showType\":\"TABLE\"" +
"\"dependItemList\":[{\"projectId\":2,\"definitionId\":46,\"depTasks\":\"ALL\"," + ",\"localParams\":[],\"connParams\":\"\"," +
"\"cycle\":\"day\",\"dateValue\":\"today\"}]}]}}"; "\"preStatements\":[],\"postStatements\":[]}," +
"\"description\":\"\",\"runFlag\":\"NORMAL\",\"dependence\":{},\"maxRetryTimes\":\"0\"," +
"\"retryInterval\":\"1\",\"timeout\":{\"strategy\":\"\"," +
"\"enable\":false},\"taskInstancePriority\":\"MEDIUM\",\"workerGroupId\":-1," +
"\"preTasks\":[\"dependent\"]}";
JSONObject taskNode = JSONUtils.parseObject(dependentJson); JSONObject taskNode = JSONUtils.parseObject(sqlJson);
if (StringUtils.isNotEmpty(taskNode.getString("type"))) { if (StringUtils.isNotEmpty(taskNode.getString("type"))) {
String taskType = taskNode.getString("type"); String taskType = taskNode.getString("type");
exportProcessAddTaskParam addTaskParam = TaskNodeParamFactory.getByTaskType(taskType); ProcessAddTaskParam addTaskParam = TaskNodeParamFactory.getByTaskType(taskType);
JSONObject dependent = addTaskParam.addSpecialParam(taskNode); JSONObject sql = addTaskParam.addExportSpecialParam(taskNode);
JSONAssert.assertEquals(taskNode.toString(),dependent.toString(),false); JSONAssert.assertEquals(taskNode.toString(), sql.toString(), false);
} }
}
@Test
public void testAddImportDependentSpecialParam() throws JSONException {
String sqlJson = "{\"workerGroupId\":-1,\"description\":\"\",\"runFlag\":\"NORMAL\"," +
"\"type\":\"SQL\",\"params\":{\"postStatements\":[]," +
"\"connParams\":\"\",\"receiversCc\":\"\",\"udfs\":\"\"," +
"\"type\":\"MYSQL\",\"title\":\"\",\"sql\":\"show tables\",\"" +
"preStatements\":[],\"sqlType\":\"1\",\"receivers\":\"\",\"datasource\":1," +
"\"showType\":\"TABLE\",\"localParams\":[],\"datasourceName\":\"dsmetadata\"},\"timeout\"" +
":{\"enable\":false,\"strategy\":\"\"},\"maxRetryTimes\":\"0\"," +
"\"taskInstancePriority\":\"MEDIUM\",\"name\":\"mysql\",\"dependence\":{}," +
"\"retryInterval\":\"1\",\"preTasks\":[\"dependent\"],\"id\":\"tasks-8745\"}";
JSONObject taskNode = JSONUtils.parseObject(sqlJson);
if (StringUtils.isNotEmpty(taskNode.getString("type"))) {
String taskType = taskNode.getString("type");
ProcessAddTaskParam addTaskParam = TaskNodeParamFactory.getByTaskType(taskType);
JSONObject sql = addTaskParam.addImportSpecialParam(taskNode);
JSONAssert.assertEquals(taskNode.toString(), sql.toString(), false);
}
} }
} }
...@@ -34,29 +34,78 @@ import org.springframework.test.context.junit4.SpringRunner; ...@@ -34,29 +34,78 @@ import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest(classes = ApiApplicationServer.class) @SpringBootTest(classes = ApiApplicationServer.class)
public class DependentParamTest { public class DependentParamTest {
@Test @Test
public void testAddDependentSpecialParam() throws JSONException { public void testAddExportDependentSpecialParam() throws JSONException {
String dependentJson = "{\"type\":\"DEPENDENT\",\"id\":\"tasks-33787\"," +
"\"name\":\"dependent\",\"params\":{},\"description\":\"\",\"runFlag\":\"NORMAL\"," +
"\"dependence\":{\"relation\":\"AND\",\"dependTaskList\":[{\"relation\":\"AND\"," +
"\"dependItemList\":[{\"projectId\":2,\"definitionId\":46,\"depTasks\":\"ALL\"," +
"\"cycle\":\"day\",\"dateValue\":\"today\"}]}]}}";
JSONObject taskNode = JSONUtils.parseObject(dependentJson);
if (StringUtils.isNotEmpty(taskNode.getString("type"))) {
String taskType = taskNode.getString("type");
ProcessAddTaskParam addTaskParam = TaskNodeParamFactory.getByTaskType(taskType);
JSONObject dependent = addTaskParam.addExportSpecialParam(taskNode);
JSONAssert.assertEquals(taskNode.toString(), dependent.toString(), false);
}
String dependentEmpty = "{\"type\":\"DEPENDENT\",\"id\":\"tasks-33787\"," +
"\"name\":\"dependent\",\"params\":{},\"description\":\"\",\"runFlag\":\"NORMAL\"}";
JSONObject taskEmpty = JSONUtils.parseObject(dependentEmpty);
if (StringUtils.isNotEmpty(taskEmpty.getString("type"))) {
String taskType = taskEmpty.getString("type");
ProcessAddTaskParam addTaskParam = TaskNodeParamFactory.getByTaskType(taskType);
String sqlJson = "{\"type\":\"SQL\",\"id\":\"tasks-27297\",\"name\":\"sql\"," + JSONObject dependent = addTaskParam.addImportSpecialParam(taskEmpty);
"\"params\":{\"type\":\"MYSQL\",\"datasource\":1,\"sql\":\"select * from test\"," +
"\"udfs\":\"\",\"sqlType\":\"1\",\"title\":\"\",\"receivers\":\"\",\"receiversCc\":\"\",\"showType\":\"TABLE\"" +
",\"localParams\":[],\"connParams\":\"\"," +
"\"preStatements\":[],\"postStatements\":[]}," +
"\"description\":\"\",\"runFlag\":\"NORMAL\",\"dependence\":{},\"maxRetryTimes\":\"0\"," +
"\"retryInterval\":\"1\",\"timeout\":{\"strategy\":\"\"," +
"\"enable\":false},\"taskInstancePriority\":\"MEDIUM\",\"workerGroupId\":-1," +
"\"preTasks\":[\"dependent\"]}";
JSONAssert.assertEquals(taskEmpty.toString(), dependent.toString(), false);
}
}
JSONObject taskNode = JSONUtils.parseObject(sqlJson); @Test
public void testAddImportDependentSpecialParam() throws JSONException {
String dependentJson = "{\"workerGroupId\":-1,\"description\":\"\",\"runFlag\":\"NORMAL\"" +
",\"type\":\"DEPENDENT\",\"params\":{},\"timeout\":{\"enable\":false," +
"\"strategy\":\"\"},\"maxRetryTimes\":\"0\",\"taskInstancePriority\":\"MEDIUM\"" +
",\"name\":\"dependent\"," +
"\"dependence\":{\"dependTaskList\":[{\"dependItemList\":[{\"dateValue\":\"today\"," +
"\"definitionName\":\"shell-1\",\"depTasks\":\"shell-1\",\"projectName\":\"test\"," +
"\"projectId\":1,\"cycle\":\"day\",\"definitionId\":7}],\"relation\":\"AND\"}]," +
"\"relation\":\"AND\"},\"retryInterval\":\"1\",\"preTasks\":[],\"id\":\"tasks-55485\"}";
JSONObject taskNode = JSONUtils.parseObject(dependentJson);
if (StringUtils.isNotEmpty(taskNode.getString("type"))) { if (StringUtils.isNotEmpty(taskNode.getString("type"))) {
String taskType = taskNode.getString("type"); String taskType = taskNode.getString("type");
exportProcessAddTaskParam addTaskParam = TaskNodeParamFactory.getByTaskType(taskType); ProcessAddTaskParam addTaskParam = TaskNodeParamFactory.getByTaskType(taskType);
JSONObject dependent = addTaskParam.addImportSpecialParam(taskNode);
JSONAssert.assertEquals(taskNode.toString(), dependent.toString(), false);
}
String dependentEmpty = "{\"workerGroupId\":-1,\"description\":\"\",\"runFlag\":\"NORMAL\"" +
",\"type\":\"DEPENDENT\",\"params\":{},\"timeout\":{\"enable\":false," +
"\"strategy\":\"\"},\"maxRetryTimes\":\"0\",\"taskInstancePriority\":\"MEDIUM\"" +
",\"name\":\"dependent\",\"retryInterval\":\"1\",\"preTasks\":[],\"id\":\"tasks-55485\"}";
JSONObject taskNodeEmpty = JSONUtils.parseObject(dependentEmpty);
if (StringUtils.isNotEmpty(taskNodeEmpty.getString("type"))) {
String taskType = taskNodeEmpty.getString("type");
ProcessAddTaskParam addTaskParam = TaskNodeParamFactory.getByTaskType(taskType);
JSONObject sql = addTaskParam.addSpecialParam(taskNode); JSONObject dependent = addTaskParam.addImportSpecialParam(taskNode);
JSONAssert.assertEquals(taskNode.toString(),sql.toString(),false); JSONAssert.assertEquals(taskNodeEmpty.toString(), dependent.toString(), false);
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册