fix(场景自动化): 数据迁移后附件丢失问题修复

This commit is contained in:
fit2-zhao 2021-01-26 17:19:57 +08:00
parent bc2c2beefa
commit e967cb0539
5 changed files with 50 additions and 59 deletions

View File

@ -59,6 +59,20 @@ public class MsScenario extends MsTestElement {
if (!this.isEnable()) { if (!this.isEnable()) {
return; return;
} }
if (this.getReferenced() != null && this.getReferenced().equals("Deleted")) {
return;
} else if (this.getReferenced() != null && this.getReferenced().equals("REF")) {
try {
ApiAutomationService apiAutomationService = CommonBeanFactory.getBean(ApiAutomationService.class);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
ApiScenarioWithBLOBs scenario = apiAutomationService.getApiScenario(this.getId());
JSONObject element = JSON.parseObject(scenario.getScenarioDefinition());
hashTree = mapper.readValue(element.getString("hashTree"), new TypeReference<LinkedList<MsTestElement>>() {});
} catch (Exception ex) {
ex.printStackTrace();
}
}
config.setStep(this.name); config.setStep(this.name);
config.setStepType("SCENARIO"); config.setStepType("SCENARIO");
config.setEnableCookieShare(enableCookieShare); config.setEnableCookieShare(enableCookieShare);
@ -72,26 +86,6 @@ public class MsScenario extends MsTestElement {
if (CollectionUtils.isNotEmpty(this.getVariables())) { if (CollectionUtils.isNotEmpty(this.getVariables())) {
config.setVariables(this.variables); config.setVariables(this.variables);
} }
if (this.getReferenced() != null && this.getReferenced().equals("Deleted")) {
return;
} else if (this.getReferenced() != null && this.getReferenced().equals("REF")) {
try {
ApiAutomationService apiAutomationService = CommonBeanFactory.getBean(ApiAutomationService.class);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
ApiScenarioWithBLOBs scenario = apiAutomationService.getApiScenario(this.getId());
JSONObject element = JSON.parseObject(scenario.getScenarioDefinition());
LinkedList<MsTestElement> elements = mapper.readValue(element.getString("hashTree"), new TypeReference<LinkedList<MsTestElement>>() {
});
if (hashTree == null) {
hashTree = elements;
} else {
hashTree.addAll(elements);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 场景变量和环境变量 // 场景变量和环境变量
tree.add(arguments(config)); tree.add(arguments(config));
addCsvDataSet(tree); addCsvDataSet(tree);

View File

@ -39,8 +39,11 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.*; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.URL; import java.net.URL;
import java.nio.channels.FileChannel;
import java.util.*; import java.util.*;
@Service @Service
@ -276,45 +279,12 @@ public class HistoricalDataUpgradeService {
private static final String BODY_FILE_DIR = "/opt/metersphere/data/body"; private static final String BODY_FILE_DIR = "/opt/metersphere/data/body";
//文件的拷贝 //文件的拷贝
private static void copyFile(String sourcePath, String newPath) { public static void copyFile(String sourcePath, String newPath) {
File readfile = new File(sourcePath); try (FileChannel inChannel = new FileInputStream(new File(sourcePath)).getChannel();
File newFile = new File(newPath); FileChannel outChannel = new FileOutputStream(new File(newPath)).getChannel();) {
BufferedWriter bufferedWriter = null; inChannel.transferTo(0, inChannel.size(), outChannel);
Writer writer = null; } catch (Exception e) {
FileOutputStream fileOutputStream = null;
BufferedReader bufferedReader = null;
try {
fileOutputStream = new FileOutputStream(newFile, true);
writer = new OutputStreamWriter(fileOutputStream, "UTF-8");
bufferedWriter = new BufferedWriter(writer);
bufferedReader = new BufferedReader(new FileReader(readfile));
String line = null;
while ((line = bufferedReader.readLine()) != null) {
bufferedWriter.write(line);
bufferedWriter.newLine();
bufferedWriter.flush();
}
} catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
try {
if (bufferedWriter != null) {
bufferedWriter.close();
}
if (bufferedReader != null) {
bufferedReader.close();
}
if (writer != null) {
writer.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
} }
} }

View File

@ -234,6 +234,9 @@
} else { } else {
request = JSON.parse(this.api.request); request = JSON.parse(this.api.request);
} }
if (!request.hashTree) {
request.hashTree = [];
}
let obj = {apiDefinitionId: this.api.id, name: '', priority: 'P0', active: true, tags: []}; let obj = {apiDefinitionId: this.api.id, name: '', priority: 'P0', active: true, tags: []};
obj.request = request; obj.request = request;
this.apiCaseList.unshift(obj); this.apiCaseList.unshift(obj);

View File

@ -27,18 +27,30 @@
methods: { methods: {
addPre() { addPre() {
let jsr223PreProcessor = createComponent("JSR223PreProcessor"); let jsr223PreProcessor = createComponent("JSR223PreProcessor");
if (!this.request.hashTree) {
this.request.hashTree = [];
}
this.request.hashTree.push(jsr223PreProcessor); this.request.hashTree.push(jsr223PreProcessor);
}, },
addPost() { addPost() {
let jsr223PostProcessor = createComponent("JSR223PostProcessor"); let jsr223PostProcessor = createComponent("JSR223PostProcessor");
if (!this.request.hashTree) {
this.request.hashTree = [];
}
this.request.hashTree.push(jsr223PostProcessor); this.request.hashTree.push(jsr223PostProcessor);
}, },
addAssertions() { addAssertions() {
let assertions = new Assertions(); let assertions = new Assertions();
if (!this.request.hashTree) {
this.request.hashTree = [];
}
this.request.hashTree.push(assertions); this.request.hashTree.push(assertions);
}, },
addExtract() { addExtract() {
let jsonPostProcessor = new Extract(); let jsonPostProcessor = new Extract();
if (!this.request.hashTree) {
this.request.hashTree = [];
}
this.request.hashTree.push(jsonPostProcessor); this.request.hashTree.push(jsonPostProcessor);
}, },
} }

View File

@ -105,21 +105,33 @@
methods: { methods: {
addPre() { addPre() {
let jsr223PreProcessor = createComponent("JSR223PreProcessor"); let jsr223PreProcessor = createComponent("JSR223PreProcessor");
if (!this.request.hashTree) {
this.request.hashTree = [];
}
this.request.hashTree.push(jsr223PreProcessor); this.request.hashTree.push(jsr223PreProcessor);
this.reload(); this.reload();
}, },
addPost() { addPost() {
let jsr223PostProcessor = createComponent("JSR223PostProcessor"); let jsr223PostProcessor = createComponent("JSR223PostProcessor");
if (!this.request.hashTree) {
this.request.hashTree = [];
}
this.request.hashTree.push(jsr223PostProcessor); this.request.hashTree.push(jsr223PostProcessor);
this.reload(); this.reload();
}, },
addAssertions() { addAssertions() {
let assertions = new Assertions(); let assertions = new Assertions();
if (!this.request.hashTree) {
this.request.hashTree = [];
}
this.request.hashTree.push(assertions); this.request.hashTree.push(assertions);
this.reload(); this.reload();
}, },
addExtract() { addExtract() {
let jsonPostProcessor = new Extract(); let jsonPostProcessor = new Extract();
if (!this.request.hashTree) {
this.request.hashTree = [];
}
this.request.hashTree.push(jsonPostProcessor); this.request.hashTree.push(jsonPostProcessor);
this.reload(); this.reload();
}, },