Skip to content

Commit

Permalink
add an api to download cases
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou9584 committed Jul 26, 2023
1 parent 2146fdb commit af3ce99
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.alibaba.fastjson.JSONObject;
import com.microsoft.hydralab.center.service.StorageTokenManageService;
import com.microsoft.hydralab.center.service.TestDataService;
import com.microsoft.hydralab.center.service.generation.MaestroCaseGenerationService;
import com.microsoft.hydralab.common.entity.agent.Result;
import com.microsoft.hydralab.common.entity.center.SysUser;
import com.microsoft.hydralab.common.entity.common.AndroidTestUnit;
Expand All @@ -26,9 +27,11 @@
import com.microsoft.hydralab.common.util.FileUtil;
import com.microsoft.hydralab.common.util.HydraLabRuntimeException;
import com.microsoft.hydralab.common.util.LogUtils;
import com.microsoft.hydralab.common.util.PageNode;
import com.microsoft.hydralab.t2c.runner.T2CJsonGenerator;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Assertions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
Expand All @@ -47,11 +50,13 @@
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

import static com.microsoft.hydralab.center.util.CenterConstant.CENTER_TEMP_FILE_DIR;
Expand All @@ -72,6 +77,8 @@ public class TestDetailController {
AttachmentService attachmentService;
@Resource
StorageServiceClientProxy storageServiceClientProxy;
@Resource
MaestroCaseGenerationService maestroCaseGenerationService;

/**
* Authenticated USER:
Expand Down Expand Up @@ -412,4 +419,52 @@ public Result<String> generateT2CJsonFromSmartTest(@CurrentSecurityContext SysUs
return Result.ok(t2cJson);
}

@GetMapping(value = {"/api/test/generateMaestro/{fileId}"}, produces = MediaType.APPLICATION_JSON_VALUE)
public Result generateMaestroFromSmartTest(@CurrentSecurityContext SysUser requestor,
@PathVariable(value = "fileId") String fileId,
@RequestParam(value = "testRunId") String testRunId,
HttpServletResponse response) throws IOException {
if (requestor == null) {
return Result.error(HttpStatus.UNAUTHORIZED.value(), "unauthorized");
}

File graphZipFile = loadGraphFile(fileId);
File graphFile = new File(graphZipFile.getParentFile().getAbsolutePath(), Const.SmartTestConfig.GRAPH_FILE_NAME);
TestRun testRun = testDataService.findTestRunById(testRunId);
TestTask testTask = testDataService.getTestTaskDetail(testRun.getTestTaskId());

PageNode rootNode = maestroCaseGenerationService.parserXMLToPageNode(graphFile.getAbsolutePath());
Assertions.assertNotNull(rootNode, "parser xml to page node failed");
rootNode.setPageName(testTask.getPkgName());
System.out.println(rootNode);
List<PageNode.ExplorePath> explorePaths = new ArrayList<>();
maestroCaseGenerationService.explorePageNodePath(rootNode, "", "", explorePaths);
File caseZipFile = maestroCaseGenerationService.generateCaseFile(rootNode, explorePaths);

if (caseZipFile == null) {
return Result.error(HttpStatus.BAD_REQUEST.value(), "The file was not downloaded");
}
try {
FileInputStream in = new FileInputStream(caseZipFile);
ServletOutputStream out = response.getOutputStream();
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + caseZipFile.getName());
int len;
byte[] buffer = new byte[1024 * 10];
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.flush();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
response.flushBuffer();
caseZipFile.delete();
}

return Result.ok();
}

}
90 changes: 62 additions & 28 deletions react/src/component/TestReportView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -631,35 +631,69 @@ export default class TestReportView extends BaseView {

generateJSON() {
if (this.state.selectedPath.length === 0) {
this.setState({
snackbarIsShown: true,
snackbarSeverity: "error",
snackbarMessage: "Please select a path!"
})
return
axios({
url: `/api/test/generateMaestro/` + this.state.graphFileId + "?testRunId=" + this.state.task.deviceTestResults[0].id,
method: 'GET',
responseType: 'blob'
}).then((res) => {
if (res.data.type.includes('application/json')) {
let reader = new FileReader()
reader.onload = function () {
let result = JSON.parse(reader.result)
if (result.code !== 200) {
this.setState({
snackbarIsShown: true,
snackbarSeverity: "error",
snackbarMessage: "The file could not be downloaded"
})
}
}
reader.readAsText(res.data)
} else {
const href = URL.createObjectURL(res.data);
const link = document.createElement('a');
link.href = href;
const filename = res.headers["content-disposition"].replace('attachment;filename=', '');
link.setAttribute('download', 'maestro_case_'+filename);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(href);

if (res.data.code === 200) {
this.setState({
snackbarIsShown: true,
snackbarSeverity: "success",
snackbarMessage: "Maestro case file downloaded"
})
}
}
}).catch(this.snackBarError);
}else{
axios({
url: `/api/test/generateT2C/` + this.state.graphFileId + "?testRunId=" + this.state.task.deviceTestResults[0].id + "&path=" + this.state.selectedPath.join(','),
method: 'GET',
}).then((res) => {
var blob = new Blob([res.data.content]);
const href = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = href;
link.setAttribute('download', this.state.task.pkgName + '_t2c.json');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(href);

if (res.data.code === 200) {
this.setState({
snackbarIsShown: true,
snackbarSeverity: "success",
snackbarMessage: "T2C JSON file downloaded"
})
}
}).catch(this.snackBarError);
}
axios({
url: `/api/test/generateT2C/` + this.state.graphFileId + "?testRunId=" + this.state.task.deviceTestResults[0].id + "&path=" + this.state.selectedPath.join(','),
method: 'GET',
}).then((res) => {
var blob = new Blob([res.data.content]);
const href = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = href;
link.setAttribute('download', this.state.task.pkgName + '_t2c.json');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(href);

if (res.data.code === 200) {
this.setState({
snackbarIsShown: true,
snackbarSeverity: "success",
snackbarMessage: "T2C JSON file downloaded"
})
}
}).catch(this.snackBarError);

}

getDeviceLabel = (name) => {
Expand Down

0 comments on commit af3ce99

Please sign in to comment.