feat(接口测试): 支持多数据源自动切换

--story=1007191 --user=赵勇 多数据源切换环境逻辑优化 https://www.tapd.cn/55049933/s/1141228
This commit is contained in:
fit2-zhao 2022-04-20 17:41:39 +08:00 committed by fit2-zhao
parent 87940f3527
commit fc7e396b7b
15 changed files with 212 additions and 65 deletions

View File

@ -61,7 +61,7 @@ public class MsScenarioParser extends MsAbstractParser<ScenarioImport> {
}
private ScenarioImport parseMsFormat(String testStr, ApiTestImportRequest importRequest) {
ScenarioImport scenarioImport = JSON.parseObject(testStr, ScenarioImport.class,Feature.DisableSpecialKeyDetect);
ScenarioImport scenarioImport = JSON.parseObject(testStr, ScenarioImport.class, Feature.DisableSpecialKeyDetect);
List<ApiScenarioWithBLOBs> data = scenarioImport.getData();
Set<String> moduleIdSet = scenarioImport.getData().stream()
@ -80,7 +80,7 @@ public class MsScenarioParser extends MsAbstractParser<ScenarioImport> {
data.forEach(item -> {
String scenarioDefinitionStr = item.getScenarioDefinition();
if (StringUtils.isNotBlank(scenarioDefinitionStr)) {
JSONObject scenarioDefinition = JSONObject.parseObject(scenarioDefinitionStr);
JSONObject scenarioDefinition = JSONObject.parseObject(scenarioDefinitionStr, Feature.DisableSpecialKeyDetect);
if (scenarioDefinition != null) {
JSONObject environmentMap = scenarioDefinition.getJSONObject("environmentMap");
if (environmentMap != null) {

View File

@ -121,7 +121,7 @@ public class MsDefinitionParser extends MsAbstractParser<ApiDefinitionImport> {
apiDefinition.setProjectId(this.projectId);
String request = apiDefinition.getRequest();
JSONObject requestObj = JSONObject.parseObject(request);
JSONObject requestObj = JSONObject.parseObject(request, Feature.DisableSpecialKeyDetect);
if(requestObj.get("projectId")!=null){
requestObj.put("projectId", apiDefinition.getProjectId());
}
@ -148,7 +148,7 @@ public class MsDefinitionParser extends MsAbstractParser<ApiDefinitionImport> {
}
cases.forEach(item -> {
String request = item.getRequest();
JSONObject requestObj = JSONObject.parseObject(request);
JSONObject requestObj = JSONObject.parseObject(request, Feature.DisableSpecialKeyDetect);
requestObj.put("useEnvironment", "");
item.setRequest(JSONObject.toJSONString(requestObj));
item.setApiDefinitionId(apiDefinition.getId());

View File

@ -233,7 +233,7 @@ public class ApiScenarioEnvService {
environmentType = EnvironmentType.JSON.toString();
}
String definition = apiScenarioWithBLOBs.getScenarioDefinition();
MsScenario scenario = JSONObject.parseObject(definition, MsScenario.class);
MsScenario scenario = JSONObject.parseObject(definition, MsScenario.class, Feature.DisableSpecialKeyDetect);
GenerateHashTreeUtil.parse(definition, scenario);
if (StringUtils.equals(environmentType, EnvironmentType.JSON.toString())) {
scenario.setEnvironmentMap(JSON.parseObject(environmentJson, Map.class));
@ -251,7 +251,7 @@ public class ApiScenarioEnvService {
boolean isEnv = true;
if (apiScenarioWithBLOBs != null) {
String definition = apiScenarioWithBLOBs.getScenarioDefinition();
MsScenario scenario = JSONObject.parseObject(definition, MsScenario.class);
MsScenario scenario = JSONObject.parseObject(definition, MsScenario.class, Feature.DisableSpecialKeyDetect);
Map<String, String> envMap = scenario.getEnvironmentMap();
if (testPlanApiScenarios != null) {
String envType = testPlanApiScenarios.getEnvironmentType();

View File

@ -126,7 +126,7 @@ public class GenerateHashTreeUtil {
MsThreadGroup group = new MsThreadGroup();
group.setLabel(item.getName());
group.setName(runRequest.getReportId());
MsScenario scenario = JSONObject.parseObject(item.getScenarioDefinition(), MsScenario.class);
MsScenario scenario = JSONObject.parseObject(item.getScenarioDefinition(), MsScenario.class, Feature.DisableSpecialKeyDetect);
group.setOnSampleError(scenario.getOnSampleError());
if (planEnvMap != null && planEnvMap.size() > 0) {
scenario.setEnvironmentMap(planEnvMap);

File diff suppressed because one or more lines are too long

View File

@ -62,6 +62,7 @@ public class MsHashTreeService {
public static final String AUTH_MANAGER = "authManager";
public static final String PROJECT_ID = "projectId";
public static final String ACTIVE = "active";
public static final String ENV_MAP = "environmentMap";
public void setHashTree(JSONArray hashTree) {
// 将引用转成复制
@ -86,14 +87,14 @@ public class MsHashTreeService {
} else {
ApiScenarioWithBLOBs bloBs = apiScenarioMapper.selectByPrimaryKey(object.getString(ID));
if (bloBs != null) {
object = JSON.parseObject(bloBs.getScenarioDefinition(),Feature.DisableSpecialKeyDetect);
object = JSON.parseObject(bloBs.getScenarioDefinition(), Feature.DisableSpecialKeyDetect);
hashTree.set(i, object);
}
}
} else if (SCENARIO.equals(object.getString(TYPE))) {
ApiScenarioWithBLOBs bloBs = apiScenarioMapper.selectByPrimaryKey(object.getString(ID));
if (bloBs != null) {
object = JSON.parseObject(bloBs.getScenarioDefinition(),Feature.DisableSpecialKeyDetect);
object = JSON.parseObject(bloBs.getScenarioDefinition(), Feature.DisableSpecialKeyDetect);
hashTree.set(i, object);
}
}
@ -183,7 +184,7 @@ public class MsHashTreeService {
ApiTestCaseInfo apiTestCase = apiTestCaseService.get(element.getString(ID));
if (apiTestCase != null) {
if (StringUtils.equalsIgnoreCase(element.getString(REFERENCED), REF)) {
JSONObject refElement = JSON.parseObject(apiTestCase.getRequest(),Feature.DisableSpecialKeyDetect);
JSONObject refElement = JSON.parseObject(apiTestCase.getRequest(), Feature.DisableSpecialKeyDetect);
ElementUtil.dataFormatting(refElement);
JSONArray array = refElement.getJSONArray(HASH_TREE);
ElementUtil.copyBean(element, refElement);
@ -255,8 +256,11 @@ public class MsHashTreeService {
boolean variableEnable = element.containsKey(VARIABLE_ENABLE)
? element.getBoolean(VARIABLE_ENABLE) : true;
if (environmentEnable && StringUtils.isNotEmpty(scenarioWithBLOBs.getEnvironmentJson())) {
element.put(ENV_MAP, JSON.parseObject(scenarioWithBLOBs.getEnvironmentJson(), Map.class));
}
if (StringUtils.equalsIgnoreCase(element.getString(REFERENCED), REF)) {
element = JSON.parseObject(scenarioWithBLOBs.getScenarioDefinition(),Feature.DisableSpecialKeyDetect);
element = JSON.parseObject(scenarioWithBLOBs.getScenarioDefinition(), Feature.DisableSpecialKeyDetect);
element.put(REFERENCED, REF);
element.put(NAME, scenarioWithBLOBs.getName());
}

View File

@ -1550,7 +1550,7 @@ export default {
if (!this.currentScenario.headers) {
this.currentScenario.headers = [];
}
if (this.currentScenario.id) {
if (this.currentScenario && this.currentScenario.id) {
this.result = this.$get("/api/automation/getApiScenario/" + this.currentScenario.id, response => {
if (response.data) {
this.path = "/api/automation/update";
@ -1818,19 +1818,15 @@ export default {
return [];
},
checkALevelChecked() {
let resourceIds = [];
if (this.$refs.stepTree) {
let resourceIds = [];
this.$refs.stepTree.root.childNodes.forEach(item => {
if (item.checked) {
resourceIds.push(item.data.resourceId);
}
})
if (resourceIds.length > 20) {
this.$warning(this.$t('api_test.automation.open_check_message'));
return false;
}
}
return true;
return resourceIds;
},
recursionExpansion(resourceIds, array) {
if (array) {
@ -1850,11 +1846,15 @@ export default {
},
openExpansion() {
this.expandedStatus = true;
if (this.checkALevelChecked()) {
let resourceIds = this.getAllResourceIds();
this.changeNodeStatus(resourceIds, this.scenarioDefinition);
this.recursionExpansion(resourceIds, this.$refs.stepTree.root.childNodes);
let resourceIds = [];
let openResourceIds = this.checkALevelChecked();
if (openResourceIds.length > 20) {
resourceIds = openResourceIds.slice(0, 20);
} else {
resourceIds = this.getAllResourceIds();
}
this.changeNodeStatus(resourceIds, this.scenarioDefinition);
this.recursionExpansion(resourceIds, this.$refs.stepTree.root.childNodes);
},
closeExpansion() {
this.expandedStatus = false;

View File

@ -52,6 +52,7 @@
:environmentType="environmentType"
:environmentGroupId="environmentGroupId"
:envMap="envMap"
:is-scenario="true"
@enable="enable"
@copy="copyRow"
@remove="remove"

View File

@ -82,7 +82,7 @@ import {createComponent} from "@/business/components/api/definition/components/j
import {Assertions, Extract} from "@/business/components/api/definition/model/ApiTestModel";
import {parseEnvironment} from "@/business/components/api/definition/model/EnvironmentModel";
import ApiEnvironmentConfig from "@/business/components/api/test/components/ApiEnvironmentConfig";
import {getCurrentProjectID} from "@/common/js/utils";
import {getCurrentProjectID, objToStrMap} from "@/common/js/utils";
import {getUUID} from "@/common/js/utils";
import MsJsr233Processor from "@/business/components/api/automation/scenario/component/Jsr233Processor";
@ -102,6 +102,10 @@ export default {
type: Boolean,
default: true,
},
isScenario: {
type: Boolean,
default: false,
},
isReadOnly: {
type: Boolean,
default: false
@ -133,7 +137,13 @@ export default {
this.getEnvironments();
},
deep: true
}
},
'$store.state.useEnvironment': function () {
if (!this.isScenario) {
this.request.environmentId = this.$store.state.useEnvironment;
this.getEnvironments();
}
},
},
methods: {
remove(row) {
@ -167,13 +177,42 @@ export default {
runTest() {
},
getEnvironments() {
this.environments = [];
itselfEnvironment() {
let id = this.request.projectId ? this.request.projectId : this.projectId;
this.result = this.$get('/api/environment/list/' + id, response => {
this.environments = response.data;
this.environments.forEach(environment => {
parseEnvironment(environment);
})
this.initDataSource();
});
},
getEnvironments() {
let envId = "";
let id = this.request.projectId ? this.request.projectId : this.projectId;
if (this.$store.state.scenarioEnvMap && this.$store.state.scenarioEnvMap instanceof Map
&& this.$store.state.scenarioEnvMap.has(this.projectId)) {
envId = this.$store.state.scenarioEnvMap.get(this.projectId);
&& this.$store.state.scenarioEnvMap.has(id)) {
envId = this.$store.state.scenarioEnvMap.get(id);
}
if (this.request.referenced === 'Created' && this.isScenario && !this.request.isRefEnvironment) {
this.itselfEnvironment();
return;
} else if (!this.isScenario && !this.request.customizeReq) {
this.itselfEnvironment();
return;
}
this.environments = [];
//
if (this.request.environmentEnable && this.request.refEevMap) {
let obj = Object.prototype.toString.call(this.request.refEevMap).match(/\[object (\w+)\]/)[1].toLowerCase();
if (obj !== 'object' && obj !== "map") {
this.request.refEevMap = objToStrMap(JSON.parse(this.request.refEevMap));
} else if (obj === 'object' && obj !== "map") {
this.request.refEevMap = objToStrMap(this.request.refEevMap);
}
if (this.request.refEevMap instanceof Map && this.request.refEevMap.has(id)) {
envId = this.request.refEevMap.get(id);
}
}
let targetDataSourceName = "";
let currentEnvironment = {};
@ -215,7 +254,7 @@ export default {
}
}
let flag = false;
if (currentEnvironment.config && currentEnvironment.config.databaseConfigs) {
if (currentEnvironment && currentEnvironment.config && currentEnvironment.config.databaseConfigs) {
currentEnvironment.config.databaseConfigs.forEach(item => {
if (item.id === this.request.dataSourceId) {
flag = true;

View File

@ -125,6 +125,7 @@ export default {
if (this.scenario.id && this.scenario.referenced === 'REF' && !this.scenario.loaded && this.scenario.hashTree) {
this.setDisabled(this.scenario.hashTree, this.scenario.projectId);
}
this.setOwnEnvironment(this.scenario.hashTree);
},
components: {ApiBaseComponent, MsSqlBasisParameters, MsTcpBasisParameters, MsDubboBasisParameters, MsApiRequestForm},
data() {
@ -138,7 +139,6 @@ export default {
computed: {
isDeletedOrRef() {
return this.scenario.referenced !== undefined && this.scenario.referenced === 'Deleted' || this.scenario.referenced === 'REF';
},
},
methods: {
@ -237,6 +237,21 @@ export default {
}
}
},
setOwnEnvironment(scenarioDefinition) {
for (let i in scenarioDefinition) {
let typeArray = ["JDBCPostProcessor", "JDBCSampler", "JDBCPreProcessor"]
if (typeArray.indexOf(scenarioDefinition[i].type) !== -1) {
scenarioDefinition[i].refEevMap = new Map();
scenarioDefinition[i].environmentEnable = this.scenario.environmentEnable;
if (this.scenario.environmentEnable && this.scenario.environmentMap) {
scenarioDefinition[i].refEevMap = this.scenario.environmentMap;
}
}
if (scenarioDefinition[i].hashTree !== undefined && scenarioDefinition[i].hashTree.length > 0) {
this.setOwnEnvironment(scenarioDefinition[i].hashTree);
}
}
},
calcProjectId(projectId, parentId) {
if (!projectId) {
return parentId ? parentId : getCurrentProjectID();
@ -258,36 +273,36 @@ export default {
clickResource(resource) {
let workspaceId = getCurrentWorkspaceId();
let isTurnSpace = true
if(resource.projectId!==getCurrentProjectID()){
if (resource.projectId !== getCurrentProjectID()) {
isTurnSpace = false;
this.$get("/project/get/" + resource.projectId, response => {
if (response.data) {
workspaceId = response.data.workspaceId;
workspaceId = response.data.workspaceId;
isTurnSpace = true;
this.checkPermission(resource,workspaceId,isTurnSpace);
this.checkPermission(resource, workspaceId, isTurnSpace);
}
});
}else {
this.checkPermission(resource,workspaceId,isTurnSpace);
} else {
this.checkPermission(resource, workspaceId, isTurnSpace);
}
},
gotoTurn(resource,workspaceId,isTurnSpace){
gotoTurn(resource, workspaceId, isTurnSpace) {
let automationData = this.$router.resolve({
name: 'ApiAutomation',
params: {redirectID: getUUID(), dataType: "scenario", dataSelectRange: 'edit:' + resource.id, projectId: resource.projectId, workspaceId: workspaceId}
});
if(isTurnSpace){
if (isTurnSpace) {
window.open(automationData.href, '_blank');
}
},
checkPermission(resource,workspaceId,isTurnSpace){
checkPermission(resource, workspaceId, isTurnSpace) {
this.$get('/project/getOwnerProjectIds', res => {
const project = res.data.find(p => p === resource.projectId);
if(!project){
if (!project) {
this.$warning(this.$t('commons.no_permission'));
}else{
this.gotoTurn(resource,workspaceId,isTurnSpace)
} else {
this.gotoTurn(resource, workspaceId, isTurnSpace)
}
})
}

View File

@ -9,7 +9,7 @@
<el-dropdown-item command="enable" v-if="data.command && data.enable">{{ this.$t('ui.disable') }}</el-dropdown-item>
<el-dropdown-item command="enable" v-if="data.command && !data.enable">{{ this.$t('ui.enable') }}</el-dropdown-item>
<el-dropdown-item command="remove">{{ this.$t('api_test.automation.delete_step') }}</el-dropdown-item>
<el-dropdown-item command="rename">{{ this.$t('test_track.module.rename') }}</el-dropdown-item>
<el-dropdown-item command="rename" v-if="!isScenario">{{ this.$t('test_track.module.rename') }}</el-dropdown-item>
<el-dropdown-item command="scenarioVar" v-if="data.type==='scenario'">
{{ this.$t("api_test.automation.view_scene_variables") }}
</el-dropdown-item>
@ -56,6 +56,12 @@ export default {
name: "StepExtendBtns",
components: {STEP, MsVariableList, MsAddBasisApi},
props: {
isScenario: {
type: Boolean,
default() {
return false;
}
},
data: Object,
environmentType: String,
environmentGroupId: String,
@ -147,9 +153,25 @@ export default {
if (res.data) {
let data = JSON.parse(res.data);
this.data.hashTree = data.hashTree;
this.setOwnEnvironment(this.data.hashTree);
}
})
},
setOwnEnvironment(scenarioDefinition) {
for (let i in scenarioDefinition) {
let typeArray = ["JDBCPostProcessor", "JDBCSampler", "JDBCPreProcessor"]
if (typeArray.indexOf(scenarioDefinition[i].type) !== -1) {
scenarioDefinition[i].environmentEnable = this.data.environmentEnable;
scenarioDefinition[i].refEevMap = new Map();
if (this.data.environmentEnable && this.data.environmentMap) {
scenarioDefinition[i].refEevMap = this.data.environmentMap;
}
}
if (scenarioDefinition[i].hashTree !== undefined && scenarioDefinition[i].hashTree.length > 0) {
this.setOwnEnvironment(scenarioDefinition[i].hashTree);
}
}
},
saveAsApi() {
this.currentProtocol = this.data.protocol;
this.data.customizeReq = false;

View File

@ -20,7 +20,7 @@
:project-id="projectId"
:is-read-only="isReadOnly"
:useEnvironment='useEnvironment'
@setEnvironment="setEnvironment" ref="environmentSelect"/>
@setEnvironment="setEnvironment" ref="environmentSelect" v-if="api.protocol==='HTTP'"/>
</el-col>
<el-col :span="2">
<!-- 保存操作 -->

View File

@ -144,7 +144,6 @@
v-if="isXpack&&api.method==='ESB'" ref="esbDefinition"/>
<ms-sql-basis-parameters
:showScript="true"
:is-scenario="true"
:request="apiCase.request"
:response="apiCase.responseData"
v-if="api.protocol==='SQL'"/>

View File

@ -98,7 +98,7 @@
<div class="el-step__icon-inner">{{ request.ruleSize }}</div>
</div>
</span>
<ms-jmx-step :request="request" :apiId="request.id" :response="response" @reload="reloadBody"
<ms-jmx-step :request="request" :apiId="request.id" :response="response" :isScenario="isScenario" @reload="reloadBody"
:tab-type="'assertionsRule'" ref="assertionsRule"/>
</el-tab-pane>
@ -123,7 +123,7 @@ import MsCodeEdit from "../../../../../common/components/MsCodeEdit";
import MsApiScenarioVariables from "../../ApiScenarioVariables";
import {parseEnvironment} from "../../../model/EnvironmentModel";
import ApiEnvironmentConfig from "@/business/components/api/test/components/ApiEnvironmentConfig";
import {getCurrentProjectID} from "@/common/js/utils";
import {getCurrentProjectID, objToStrMap} from "@/common/js/utils";
import {getUUID} from "@/common/js/utils";
import MsJsr233Processor from "../../../../automation/scenario/component/Jsr233Processor";
import MsJmxStep from "../../step/JmxStep";
@ -148,6 +148,10 @@ export default {
type: Boolean,
default: true,
},
isCase: {
type: Boolean,
default: false,
},
isScenario: {
type: Boolean,
default: false,
@ -177,12 +181,24 @@ export default {
},
deep: true
},
'$store.state.useEnvironment': function () {
if (!this.isScenario) {
this.request.environmentId = this.$store.state.useEnvironment;
this.getEnvironments();
}
},
'$store.state.scenarioEnvMap': {
handler(v) {
this.getEnvironments();
},
deep: true
}
},
'request.refEevMap': {
handler(v) {
this.getEnvironments();
},
deep: true
},
},
created() {
this.getEnvironments();
@ -265,9 +281,10 @@ export default {
},
getEnvironments() {
let envId = "";
let id = this.request.projectId ? this.request.projectId : this.projectId;
if (this.$store.state.scenarioEnvMap && this.$store.state.scenarioEnvMap instanceof Map
&& this.$store.state.scenarioEnvMap.has(this.projectId)) {
envId = this.$store.state.scenarioEnvMap.get(this.projectId);
&& this.$store.state.scenarioEnvMap.has(id)) {
envId = this.$store.state.scenarioEnvMap.get(id);
}
if (this.request.referenced === 'Created' && this.isScenario && !this.request.isRefEnvironment) {
this.itselfEnvironment();
@ -277,8 +294,18 @@ export default {
return;
}
this.environments = [];
let id = this.request.projectId ? this.request.projectId : this.projectId;
//
if (this.request.environmentEnable && this.request.refEevMap) {
let obj = Object.prototype.toString.call(this.request.refEevMap).match(/\[object (\w+)\]/)[1].toLowerCase();
if (obj !== 'object' && obj !== "map") {
this.request.refEevMap = objToStrMap(JSON.parse(this.request.refEevMap));
} else if (obj === 'object' && obj !== "map") {
this.request.refEevMap = objToStrMap(this.request.refEevMap);
}
if (this.request.refEevMap instanceof Map && this.request.refEevMap.has(id)) {
envId = this.request.refEevMap.get(id);
}
}
let targetDataSourceName = "";
let currentEnvironment = {};
this.result = this.$get('/api/environment/list/' + id, response => {
@ -297,7 +324,9 @@ export default {
}
if (envId && environment.id === envId) {
currentEnvironment = environment;
this.environments = [currentEnvironment];
if (!this.isCase) {
this.environments = [currentEnvironment];
}
}
});
this.initDataSource(envId, currentEnvironment, targetDataSourceName);
@ -319,7 +348,7 @@ export default {
}
}
let flag = false;
if (currentEnvironment.config && currentEnvironment.config.databaseConfigs) {
if (currentEnvironment && currentEnvironment.config && currentEnvironment.config.databaseConfigs) {
currentEnvironment.config.databaseConfigs.forEach(item => {
if (item.id === this.request.dataSourceId) {
flag = true;

View File

@ -137,6 +137,10 @@ export default {
tabType: String,
response: {},
apiId: String,
isScenario: {
type: Boolean,
default: false,
},
showScript: {
type: Boolean,
default: true,