Skip to content
This repository has been archived by the owner on Oct 5, 2023. It is now read-only.

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangyitao committed Jul 16, 2020
1 parent 18e0bab commit 6304092
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 68 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.daxiang</groupId>
<artifactId>server</artifactId>
<version>0.7.0</version>
<version>0.7.1</version>
<packaging>jar</packaging>

<properties>
Expand Down
23 changes: 21 additions & 2 deletions src/main/java/com/daxiang/service/ActionProcessor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.daxiang.service;

import com.daxiang.mbg.po.Action;
import com.daxiang.model.action.LocalVar;
import com.daxiang.model.action.Step;
import org.springframework.util.CollectionUtils;

Expand All @@ -14,18 +15,25 @@
public class ActionProcessor {

private final ActionService actionService;
private final EnvironmentService environmentService;
private final Integer env;

/**
* actionId : action
*/
private final Map<Integer, Action> cachedActions = new HashMap<>();

public ActionProcessor(ActionService actionService) {
public ActionProcessor(ActionService actionService, EnvironmentService environmentService, Integer env) {
this.actionService = actionService;
this.environmentService = environmentService;
this.env = env;
}

public void process(List<Action> actions) {
actions.forEach(action -> cachedActions.put(action.getId(), action));
actions.forEach(action -> {
handleActionLocalVarsValue(action);
cachedActions.put(action.getId(), action);
});
recursivelyProcess(actions);
}

Expand Down Expand Up @@ -61,8 +69,19 @@ private Action getActionById(Integer actionId) {
Action action = cachedActions.get(actionId);
if (action == null) {
action = actionService.getActionById(actionId);
handleActionLocalVarsValue(action);
cachedActions.put(actionId, action);
}
return action;
}

private void handleActionLocalVarsValue(Action action) {
List<LocalVar> localVars = action.getLocalVars();
if (!CollectionUtils.isEmpty(localVars)) {
localVars.forEach(localVar -> {
String value = environmentService.getValueInEnvironmentValues(localVar.getEnvironmentValues(), env);
localVar.setValue(value);
});
}
}
}
36 changes: 10 additions & 26 deletions src/main/java/com/daxiang/service/ActionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,6 @@ private String getActionTypeName(Integer actionType) {

public Response debug(ActionDebugRequest actionDebugRequest) {
Action action = actionDebugRequest.getAction();
ActionDebugRequest.DebugInfo debugInfo = actionDebugRequest.getDebugInfo();
Integer env = debugInfo.getEnv();

action.setId(0);
action.setDepends(new ArrayList<>());

Expand All @@ -290,31 +287,17 @@ public Response debug(ActionDebugRequest actionDebugRequest) {
return Response.fail("至少选择一个启用的步骤");
}

// 根据环境处理action局部变量
List<LocalVar> localVars = action.getLocalVars();
if (!CollectionUtils.isEmpty(localVars)) {
localVars.forEach(localVar -> {
String value = environmentService.getValueInEnvironmentValues(localVar.getEnvironmentValues(), env);
localVar.setValue(value);
});
}
ActionDebugRequest.DebugInfo debugInfo = actionDebugRequest.getDebugInfo();

// 处理action
processActions(Arrays.asList(action));
Integer projectId = action.getProjectId();
Integer env = debugInfo.getEnv();

// 该项目下的全局变量
List<GlobalVar> globalVars = globalVarService.getGlobalVarsByProjectId(action.getProjectId());

// 根据环境处理全局变量
if (!CollectionUtils.isEmpty(globalVars)) {
globalVars.forEach(globalVar -> {
String value = environmentService.getValueInEnvironmentValues(globalVar.getEnvironmentValues(), env);
globalVar.setValue(value);
});
}
processActions(Arrays.asList(action), env);

// 该项目下的全局变量
List<GlobalVar> globalVars = globalVarService.getGlobalVarsByProjectIdAndEnv(projectId, env);
// 该项目下的Pages
List<com.daxiang.mbg.po.Page> pages = pageService.getPagesWithoutWindowHierarchyByProjectId(action.getProjectId());
List<com.daxiang.mbg.po.Page> pages = pageService.getPagesWithoutWindowHierarchyByProjectId(projectId);

JSONObject requestBody = new JSONObject();
requestBody.put("platform", debugInfo.getPlatform());
Expand Down Expand Up @@ -343,8 +326,8 @@ public List<Action> getActionsByIds(List<Integer> actionIds) {
return actionMapper.selectByExampleWithBLOBs(example);
}

public void processActions(List<Action> actions) {
new ActionProcessor(this).process(actions);
public void processActions(List<Action> actions, Integer env) {
new ActionProcessor(this, environmentService, env).process(actions);
}

/**
Expand Down Expand Up @@ -374,6 +357,7 @@ private void checkStepsNotContainsSelf(Action action) {

/**
* 导入Action不能包含自身
*
* @param action
*/
private void checkActionImportsNotContainsSelf(Action action) {
Expand Down
23 changes: 10 additions & 13 deletions src/main/java/com/daxiang/service/EnvironmentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,17 @@ public Environment getEnvironmentById(Integer id) {
* 在environmentValues中找到与envId匹配的value
*/
public String getValueInEnvironmentValues(List<EnvironmentValue> environmentValues, Integer envId) {
// 与envId匹配的environmentValue
EnvironmentValue environmentValue = environmentValues.stream()
.filter(env -> envId.equals(env.getEnvironmentId())).findFirst().orElse(null);
if (environmentValue != null) {
return environmentValue.getValue();
} else {
// 没有与env匹配的,用默认的
EnvironmentValue defaultEnvironmentValue = environmentValues.stream()
.filter(env -> EnvironmentValue.DEFAULT_ENVIRONMENT_ID == env.getEnvironmentId()).findFirst().orElse(null);
if (defaultEnvironmentValue != null) {
return defaultEnvironmentValue.getValue();
} else {
return null;
String defaultValue = null;

for (EnvironmentValue env : environmentValues) {
Integer environmentId = env.getEnvironmentId();
if (envId.equals(environmentId)) {
return env.getValue();
} else if (environmentId == EnvironmentValue.DEFAULT_ENVIRONMENT_ID) {
defaultValue = env.getValue();
}
}

return defaultValue;
}
}
15 changes: 12 additions & 3 deletions src/main/java/com/daxiang/service/GlobalVarService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class GlobalVarService {
private GlobalVarDao globalVarDao;
@Autowired
private UserService userService;
@Autowired
private EnvironmentService environmentService;

public Response add(GlobalVar globalVar) {
globalVar.setCreateTime(new Date());
Expand Down Expand Up @@ -166,14 +168,21 @@ public List<GlobalVar> getGlobalVarsByEnvironmentId(Integer envId) {
return globalVarDao.selectByEnvironmentId(envId);
}

public List<GlobalVar> getGlobalVarsByProjectId(Integer projectId) {
if (projectId == null) {
public List<GlobalVar> getGlobalVarsByProjectIdAndEnv(Integer projectId, Integer env) {
if (projectId == null || env == null) {
return new ArrayList<>();
}

GlobalVar query = new GlobalVar();
query.setProjectId(projectId);
return selectByGlobalVar(query);
List<GlobalVar> globalVars = selectByGlobalVar(query);

globalVars.forEach(globalVar -> {
String value = environmentService.getValueInEnvironmentValues(globalVar.getEnvironmentValues(), env);
globalVar.setValue(value);
});

return globalVars;
}

public List<GlobalVar> getGlobalVarsByCategoryIds(List<Integer> categoryIds) {
Expand Down
25 changes: 2 additions & 23 deletions src/main/java/com/daxiang/service/TestTaskService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.alibaba.fastjson.JSONObject;
import com.daxiang.mbg.mapper.TestTaskMapper;
import com.daxiang.mbg.po.*;
import com.daxiang.model.action.LocalVar;
import com.daxiang.model.environment.EnvironmentValue;
import com.daxiang.model.vo.TestTaskVo;
import com.daxiang.model.vo.TestTaskSummary;
Expand Down Expand Up @@ -102,33 +101,13 @@ public Response commit(Integer testPlanId, Integer commitorUid) {
List<Action> actions = new ArrayList<>(testcases);
actions.addAll(beforeAndAfterActionMap.values());

// 根据环境处理局部变量
actions.forEach(action -> {
List<LocalVar> localVars = action.getLocalVars();
if (!CollectionUtils.isEmpty(localVars)) {
localVars.forEach(localVar -> {
String value = environmentService.getValueInEnvironmentValues(localVar.getEnvironmentValues(), testPlan.getEnvironmentId());
localVar.setValue(value);
});
}
});

actionService.processActions(actions);
actionService.processActions(actions, testPlan.getEnvironmentId());

// 保存测试任务
TestTask testTask = saveTestTask(testPlan, commitorUid);

// 同一项目下的全局变量
List<GlobalVar> globalVars = globalVarService.getGlobalVarsByProjectId(testTask.getProjectId());

// 根据环境处理全局变量
if (!CollectionUtils.isEmpty(globalVars)) {
globalVars.forEach(globalVar -> {
String value = environmentService.getValueInEnvironmentValues(globalVar.getEnvironmentValues(), testPlan.getEnvironmentId());
globalVar.setValue(value);
});
}

List<GlobalVar> globalVars = globalVarService.getGlobalVarsByProjectIdAndEnv(testTask.getProjectId(), testPlan.getEnvironmentId());
// 该项目下的Pages
List<com.daxiang.mbg.po.Page> pages = pageService.getPagesWithoutWindowHierarchyByProjectId(testTask.getProjectId());

Expand Down

0 comments on commit 6304092

Please sign in to comment.