88import com .alibaba .fastjson .JSONObject ;
99import com .microsoft .hydralab .center .service .StorageTokenManageService ;
1010import com .microsoft .hydralab .center .service .TestDataService ;
11+ import com .microsoft .hydralab .center .service .generation .MaestroCaseGenerationService ;
1112import com .microsoft .hydralab .common .entity .agent .Result ;
1213import com .microsoft .hydralab .common .entity .center .SysUser ;
1314import com .microsoft .hydralab .common .entity .common .AndroidTestUnit ;
2627import com .microsoft .hydralab .common .util .FileUtil ;
2728import com .microsoft .hydralab .common .util .HydraLabRuntimeException ;
2829import com .microsoft .hydralab .common .util .LogUtils ;
30+ import com .microsoft .hydralab .common .util .PageNode ;
2931import com .microsoft .hydralab .t2c .runner .T2CJsonGenerator ;
3032import org .apache .commons .io .IOUtils ;
3133import org .apache .commons .lang3 .StringUtils ;
34+ import org .junit .jupiter .api .Assertions ;
3235import org .slf4j .Logger ;
3336import org .slf4j .LoggerFactory ;
3437import org .springframework .http .HttpStatus ;
4750import javax .servlet .http .HttpServletResponse ;
4851import java .io .File ;
4952import java .io .FileInputStream ;
53+ import java .io .FileNotFoundException ;
5054import java .io .IOException ;
5155import java .io .InputStream ;
5256import java .net .HttpURLConnection ;
5357import java .net .URL ;
5458import java .nio .charset .StandardCharsets ;
59+ import java .util .ArrayList ;
5560import java .util .List ;
5661
5762import 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}
0 commit comments