Skip to content

Commit af3ce99

Browse files
committed
add an api to download cases
1 parent 2146fdb commit af3ce99

File tree

2 files changed

+117
-28
lines changed

2 files changed

+117
-28
lines changed

center/src/main/java/com/microsoft/hydralab/center/controller/TestDetailController.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.alibaba.fastjson.JSONObject;
99
import com.microsoft.hydralab.center.service.StorageTokenManageService;
1010
import com.microsoft.hydralab.center.service.TestDataService;
11+
import com.microsoft.hydralab.center.service.generation.MaestroCaseGenerationService;
1112
import com.microsoft.hydralab.common.entity.agent.Result;
1213
import com.microsoft.hydralab.common.entity.center.SysUser;
1314
import com.microsoft.hydralab.common.entity.common.AndroidTestUnit;
@@ -26,9 +27,11 @@
2627
import com.microsoft.hydralab.common.util.FileUtil;
2728
import com.microsoft.hydralab.common.util.HydraLabRuntimeException;
2829
import com.microsoft.hydralab.common.util.LogUtils;
30+
import com.microsoft.hydralab.common.util.PageNode;
2931
import com.microsoft.hydralab.t2c.runner.T2CJsonGenerator;
3032
import org.apache.commons.io.IOUtils;
3133
import org.apache.commons.lang3.StringUtils;
34+
import org.junit.jupiter.api.Assertions;
3235
import org.slf4j.Logger;
3336
import org.slf4j.LoggerFactory;
3437
import org.springframework.http.HttpStatus;
@@ -47,11 +50,13 @@
4750
import javax.servlet.http.HttpServletResponse;
4851
import java.io.File;
4952
import java.io.FileInputStream;
53+
import java.io.FileNotFoundException;
5054
import java.io.IOException;
5155
import java.io.InputStream;
5256
import java.net.HttpURLConnection;
5357
import java.net.URL;
5458
import java.nio.charset.StandardCharsets;
59+
import java.util.ArrayList;
5560
import java.util.List;
5661

5762
import static com.microsoft.hydralab.center.util.CenterConstant.CENTER_TEMP_FILE_DIR;
@@ -72,6 +77,8 @@ public class TestDetailController {
7277
AttachmentService attachmentService;
7378
@Resource
7479
StorageServiceClientProxy storageServiceClientProxy;
80+
@Resource
81+
MaestroCaseGenerationService maestroCaseGenerationService;
7582

7683
/**
7784
* Authenticated USER:
@@ -412,4 +419,52 @@ public Result<String> generateT2CJsonFromSmartTest(@CurrentSecurityContext SysUs
412419
return Result.ok(t2cJson);
413420
}
414421

422+
@GetMapping(value = {"/api/test/generateMaestro/{fileId}"}, produces = MediaType.APPLICATION_JSON_VALUE)
423+
public Result generateMaestroFromSmartTest(@CurrentSecurityContext SysUser requestor,
424+
@PathVariable(value = "fileId") String fileId,
425+
@RequestParam(value = "testRunId") String testRunId,
426+
HttpServletResponse response) throws IOException {
427+
if (requestor == null) {
428+
return Result.error(HttpStatus.UNAUTHORIZED.value(), "unauthorized");
429+
}
430+
431+
File graphZipFile = loadGraphFile(fileId);
432+
File graphFile = new File(graphZipFile.getParentFile().getAbsolutePath(), Const.SmartTestConfig.GRAPH_FILE_NAME);
433+
TestRun testRun = testDataService.findTestRunById(testRunId);
434+
TestTask testTask = testDataService.getTestTaskDetail(testRun.getTestTaskId());
435+
436+
PageNode rootNode = maestroCaseGenerationService.parserXMLToPageNode(graphFile.getAbsolutePath());
437+
Assertions.assertNotNull(rootNode, "parser xml to page node failed");
438+
rootNode.setPageName(testTask.getPkgName());
439+
System.out.println(rootNode);
440+
List<PageNode.ExplorePath> explorePaths = new ArrayList<>();
441+
maestroCaseGenerationService.explorePageNodePath(rootNode, "", "", explorePaths);
442+
File caseZipFile = maestroCaseGenerationService.generateCaseFile(rootNode, explorePaths);
443+
444+
if (caseZipFile == null) {
445+
return Result.error(HttpStatus.BAD_REQUEST.value(), "The file was not downloaded");
446+
}
447+
try {
448+
FileInputStream in = new FileInputStream(caseZipFile);
449+
ServletOutputStream out = response.getOutputStream();
450+
response.setContentType("application/octet-stream;charset=UTF-8");
451+
response.setHeader("Content-Disposition", "attachment;filename=" + caseZipFile.getName());
452+
int len;
453+
byte[] buffer = new byte[1024 * 10];
454+
while ((len = in.read(buffer)) != -1) {
455+
out.write(buffer, 0, len);
456+
}
457+
out.flush();
458+
} catch (FileNotFoundException e) {
459+
throw new RuntimeException(e);
460+
} catch (IOException e) {
461+
throw new RuntimeException(e);
462+
} finally {
463+
response.flushBuffer();
464+
caseZipFile.delete();
465+
}
466+
467+
return Result.ok();
468+
}
469+
415470
}

react/src/component/TestReportView.jsx

Lines changed: 62 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -631,35 +631,69 @@ export default class TestReportView extends BaseView {
631631

632632
generateJSON() {
633633
if (this.state.selectedPath.length === 0) {
634-
this.setState({
635-
snackbarIsShown: true,
636-
snackbarSeverity: "error",
637-
snackbarMessage: "Please select a path!"
638-
})
639-
return
634+
axios({
635+
url: `/api/test/generateMaestro/` + this.state.graphFileId + "?testRunId=" + this.state.task.deviceTestResults[0].id,
636+
method: 'GET',
637+
responseType: 'blob'
638+
}).then((res) => {
639+
if (res.data.type.includes('application/json')) {
640+
let reader = new FileReader()
641+
reader.onload = function () {
642+
let result = JSON.parse(reader.result)
643+
if (result.code !== 200) {
644+
this.setState({
645+
snackbarIsShown: true,
646+
snackbarSeverity: "error",
647+
snackbarMessage: "The file could not be downloaded"
648+
})
649+
}
650+
}
651+
reader.readAsText(res.data)
652+
} else {
653+
const href = URL.createObjectURL(res.data);
654+
const link = document.createElement('a');
655+
link.href = href;
656+
const filename = res.headers["content-disposition"].replace('attachment;filename=', '');
657+
link.setAttribute('download', 'maestro_case_'+filename);
658+
document.body.appendChild(link);
659+
link.click();
660+
document.body.removeChild(link);
661+
URL.revokeObjectURL(href);
662+
663+
if (res.data.code === 200) {
664+
this.setState({
665+
snackbarIsShown: true,
666+
snackbarSeverity: "success",
667+
snackbarMessage: "Maestro case file downloaded"
668+
})
669+
}
670+
}
671+
}).catch(this.snackBarError);
672+
}else{
673+
axios({
674+
url: `/api/test/generateT2C/` + this.state.graphFileId + "?testRunId=" + this.state.task.deviceTestResults[0].id + "&path=" + this.state.selectedPath.join(','),
675+
method: 'GET',
676+
}).then((res) => {
677+
var blob = new Blob([res.data.content]);
678+
const href = URL.createObjectURL(blob);
679+
const link = document.createElement('a');
680+
link.href = href;
681+
link.setAttribute('download', this.state.task.pkgName + '_t2c.json');
682+
document.body.appendChild(link);
683+
link.click();
684+
document.body.removeChild(link);
685+
URL.revokeObjectURL(href);
686+
687+
if (res.data.code === 200) {
688+
this.setState({
689+
snackbarIsShown: true,
690+
snackbarSeverity: "success",
691+
snackbarMessage: "T2C JSON file downloaded"
692+
})
693+
}
694+
}).catch(this.snackBarError);
640695
}
641-
axios({
642-
url: `/api/test/generateT2C/` + this.state.graphFileId + "?testRunId=" + this.state.task.deviceTestResults[0].id + "&path=" + this.state.selectedPath.join(','),
643-
method: 'GET',
644-
}).then((res) => {
645-
var blob = new Blob([res.data.content]);
646-
const href = URL.createObjectURL(blob);
647-
const link = document.createElement('a');
648-
link.href = href;
649-
link.setAttribute('download', this.state.task.pkgName + '_t2c.json');
650-
document.body.appendChild(link);
651-
link.click();
652-
document.body.removeChild(link);
653-
URL.revokeObjectURL(href);
654-
655-
if (res.data.code === 200) {
656-
this.setState({
657-
snackbarIsShown: true,
658-
snackbarSeverity: "success",
659-
snackbarMessage: "T2C JSON file downloaded"
660-
})
661-
}
662-
}).catch(this.snackBarError);
696+
663697
}
664698

665699
getDeviceLabel = (name) => {

0 commit comments

Comments
 (0)