package cd.casic.ci.process.engine.worker;

import cd.casic.ci.process.common.WorkAtom;
import cd.casic.ci.process.engine.runContext.TaskRunContext;
import cd.casic.ci.process.engine.worker.base.BaseWorker;
import lombok.extern.slf4j.Slf4j;


/**
 * 数据库模糊测试
 * */
@Slf4j
@WorkAtom(taskType = "DB_FUZZ_TESTING")
public class DBFuzzTestingWorker extends BaseWorker {
    @Override
    public void execute(TaskRunContext context) {
//        PipBaseElement contextDef = context.getContextDef();
//        String id = contextDef.getId();
//        log.info("==============触发worker执行========");
//        log.info("==========运行context：{}===========", JSON.toJSONString(context));
//        int statusCode = -1;
//
//        String login = "http://192.168.100.100:10200/oauth/token";
//        String create = "http://192.168.100.100:10200/api/fuzz/add";
//        String control = "http://192.168.100.100:10200/api/fuzz/control";
//        String getTestDetail = "http://192.168.100.100:10200/api/fuzz/getTestDetail";
//        String genReport = "http://192.168.100.100:10200/api/fuzz/genReport";
////         static String = "http://192.168.100.100:10200/api/fuzz/add";
//        PipelineNodeConfigInfo nodeConfigInfo = workerParam.getPipelineNodeConfigInfo();
//        String taskId = workerParam.getPipelineLogger().getUuid();
//        NodeLogger nodeLogger = nodeLoggerThreadLocal.get();
//
//        try {
//            log.info("attr : {}", nodeConfigInfo.getInfo());
//            log.info("节点信息:" + nodeConfigInfo.getInfo());
//            //将节点的配置信息反编译成对象
//            DbTestConfigInfo dbTestConfigInfo = JSON.parseObject(nodeConfigInfo.getInfo(), DbTestConfigInfo.class);
//            HashMap<String, Object> hashMap = new HashMap<>();
//            hashMap.put("code", "f258d8cc7585bc71a10e9f9edba017f7");
//            hashMap.put("grant_type", "client_code");
//            hashMap.put("client_id", "client");
//            HttpResponse logRes = HttpRequest.post(login)
//                    .form(hashMap)/*.header(Header.CONTENT_TYPE, String.valueOf(ContentType.APPLICATION_JSON))*/.execute();
//            log.info(String.valueOf(logRes.isOk()));
//            if (!logRes.isOk()) {
//                nodeLogger.appendNow("获取token失败");
//            }
//            JSONObject loginJson = JSONObject.parseObject(logRes.body());
//            String accessToken = loginJson.get("access_token").toString();
//            nodeLogger.appendNow("authorize success\n");
//            nodeLogger.appendNow("access_token is " + accessToken);
//// 2、           create project
//            JSONObject jsonObject = new JSONObject();
//            jsonObject.put("node", dbTestConfigInfo.getNode());
//            jsonObject.put("engineType", "1");
//            jsonObject.put("taskId", taskId);
//            jsonObject.put("taskParams", "");
//            jsonObject.put("taskName", dbTestConfigInfo.getTaskName());
//            HttpResponse createRes = HttpRequest.post(create)
//                    .body(String.valueOf(jsonObject)).bearerAuth(accessToken).execute();
//            if (!createRes.isOk()) {
//                nodeLogger.appendNow("创建项目失败");
//                return String.valueOf(statusCode);
//            }
//            JSONObject taskjson = JSONObject.parseObject(createRes.body());
//            nodeLogger.appendNow("create project success\n");
//            nodeLogger.appendNow("project taskId is " + taskId);
////            3、 启动停止测试
//            JSONObject controlObject = new JSONObject();
//            controlObject.put("taskId", taskId);
//            // 1 start other stop
//            controlObject.put("code", 1);
//            HttpResponse controlRes = HttpRequest.post(control)
//                    .body(String.valueOf(controlObject)).bearerAuth(accessToken).execute();
//            log.info(String.valueOf(controlRes.isOk()));
//            if (!controlRes.isOk()) {
//                nodeLogger.appendNow("启动项目失败");
//                return String.valueOf(statusCode);
//
//            }
//            JSONObject controlJson = JSONObject.parseObject(createRes.body());
//            nodeLogger.appendNow("control start  success\n");
//            nodeLogger.appendNow("control start code is " + controlJson.get("data\n"));
////            4、 获取测试详情保存历史数据
//            HashMap<String, Object> haap = new HashMap<>();
//            haap.put("taskId", taskId);
//            HttpResponse testDetail = HttpRequest.get(getTestDetail)
//                    .form(haap).bearerAuth(accessToken).execute();
//            if (!testDetail.isOk()) {
//                nodeLogger.appendNow("获取测试详情失败");
//                return String.valueOf(statusCode);
//            }
//            JSONObject testDetailBody = JSONObject.parseObject(testDetail.body());
//            log.info(testDetailBody.toString());
//            FuzzDbTest fuzzDbTest = new FuzzDbTest();
//            fuzzDbTest.setNodeUuid(nodeConfigInfo.getNodeUuid());
//            fuzzDbTest.setTaskId(taskId);
//            fuzzDbTestService.save(fuzzDbTest);
//            statusCode = 0;
//        } catch (Exception e) {
//            String errorMessage = "该节点配置信息为空，请先配置该节点信息" + "\r\n";
//            errorHandle(e, errorMessage);
//        }
//
//        if (statusCode == 0) {
//            log.info("{}节点执行完成", getName());
//        } else {
//            log.error("{}节点执行失败", getName());
//        }
//        return String.valueOf(statusCode);
    }
}
