|
|
@@ -0,0 +1,252 @@
|
|
|
+package cn.seecoder.web.service.impl.InterfaceTest;
|
|
|
+
|
|
|
+import cn.seecoder.web.dao.InterfaceTest.InterfaceTestMapper;
|
|
|
+import cn.seecoder.web.dao.pipeline.PipelineMapper;
|
|
|
+import cn.seecoder.web.dao.pipeline.PipelineRecordMapper;
|
|
|
+import cn.seecoder.web.model.po.InterfaceTest.InterfaceTestInfo;
|
|
|
+import cn.seecoder.web.model.po.InterfaceTest.InterfaceTestResult;
|
|
|
+import cn.seecoder.web.model.po.pipeline.PipelinePO;
|
|
|
+import cn.seecoder.web.model.po.pipeline.PipelineRecordPO;
|
|
|
+import cn.seecoder.web.model.vo.InterfaceTest.InterfaceTestInfoVO;
|
|
|
+import cn.seecoder.web.model.vo.InterfaceTest.InterfaceTestResultVO;
|
|
|
+import cn.seecoder.web.service.InterfaceTest.InterfaceTestService;
|
|
|
+import com.google.gson.Gson;
|
|
|
+import net.sf.json.JSONArray;
|
|
|
+import net.sf.json.JSONObject;
|
|
|
+import org.apache.commons.lang.StringEscapeUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.net.*;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class InterfaceTestServiceImpl implements InterfaceTestService {
|
|
|
+
|
|
|
+ private final InterfaceTestMapper interfaceTestMapper;
|
|
|
+
|
|
|
+ private final PipelineMapper pipelineMapper;
|
|
|
+
|
|
|
+ private final PipelineRecordMapper pipelineRecordMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public InterfaceTestServiceImpl(InterfaceTestMapper interfaceTestMapper, PipelineMapper pipelineMapper, PipelineRecordMapper pipelineRecordMapper) {
|
|
|
+ this.interfaceTestMapper = interfaceTestMapper;
|
|
|
+ this.pipelineMapper = pipelineMapper;
|
|
|
+ this.pipelineRecordMapper = pipelineRecordMapper;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void createInterfaceTest(InterfaceTestInfoVO interfaceTestInfoVO) {
|
|
|
+ Date d = new Date();
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String createTime = sdf.format(d);
|
|
|
+ InterfaceTestInfo info = InterfaceTestInfo.builder()
|
|
|
+ .name(interfaceTestInfoVO.getName())
|
|
|
+ .projectId(interfaceTestInfoVO.getProjectId())
|
|
|
+ .createTime(createTime)
|
|
|
+ .username(interfaceTestInfoVO.getUsername())
|
|
|
+ .url(interfaceTestInfoVO.getUrl())
|
|
|
+ .method(interfaceTestInfoVO.getMethod())
|
|
|
+ .params(JSONObject.fromObject(interfaceTestInfoVO.getParams()).toString())
|
|
|
+ .build();
|
|
|
+ interfaceTestMapper.insert(info);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<InterfaceTestInfoVO> getInterfaceTestByProjectId(Integer projectId) {
|
|
|
+ return interfaceTestMapper.selectByProjectId(projectId).stream()
|
|
|
+ .map(InterfaceTestInfoVO::new).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteInterfaceTestById(Integer id) {
|
|
|
+ interfaceTestMapper.deleteInterfaceTestById(id);
|
|
|
+ interfaceTestMapper.deleteInterfaceResultsById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void executeInterfaceTestById(Integer id, String username, String type) throws IOException {
|
|
|
+ InterfaceTestInfo info = interfaceTestMapper.selectById(id);
|
|
|
+ String url = info.getUrl();
|
|
|
+ Map<String, Object> params = parseJson2Map(info.getParams());
|
|
|
+ String method = info.getMethod();
|
|
|
+ Integer projectId = info.getProjectId();
|
|
|
+ handleRequest(url, params, id, method, username, type, projectId);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<InterfaceTestResultVO> getInterfaceTestResultsById(Integer id) {
|
|
|
+ return interfaceTestMapper.selectByTestId(id).stream()
|
|
|
+ .map(InterfaceTestResultVO::new).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<InterfaceTestResultVO> getAutoInterfaceTestResultsById(Integer id) {
|
|
|
+ return interfaceTestMapper.selectAutoResultsByTestId(id).stream()
|
|
|
+ .map(InterfaceTestResultVO::new).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ public Map<String, Object> parseJson2Map(String str) {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ JSONObject json = JSONObject.fromObject(str);
|
|
|
+ for (Object k : json.keySet()) {
|
|
|
+ Object v = json.get(k);
|
|
|
+ if (v instanceof JSONArray) {
|
|
|
+ List<Map<String, Object>> list = new ArrayList<>();
|
|
|
+ for (JSONObject json2 : (Iterable<JSONObject>) v) {
|
|
|
+ list.add(parseJson2Map(json2.toString()));
|
|
|
+ }
|
|
|
+ map.put(k.toString(), list);
|
|
|
+ } else {
|
|
|
+ map.put(k.toString(), v);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void handleRequest(String url, Map<String, Object> params, int id, String method, String username, String type, Integer projectId) throws IOException {
|
|
|
+ String[] res = null;
|
|
|
+ if (method.equals("GET")) {
|
|
|
+ res = doGet(url, params);
|
|
|
+ } else if (method.equals("POST")) {
|
|
|
+ res = doPost(url, params);
|
|
|
+ }
|
|
|
+ for (String _res : res) {
|
|
|
+ System.out.println(_res);
|
|
|
+ }
|
|
|
+ Date d = new Date();
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String executeTime = sdf.format(d);
|
|
|
+ int deploymentId = -111;
|
|
|
+ if (type.equals("artificial")) {
|
|
|
+ deploymentId = -1;
|
|
|
+ } else if (type.equals("computer")) {
|
|
|
+ // 获取最新的部署构建信息id
|
|
|
+ List<Integer> pipelineIds = pipelineMapper.selectByProjectId(projectId).stream().map(PipelinePO::getId).collect(Collectors.toList());
|
|
|
+ PipelineRecordPO latestRecord = pipelineRecordMapper.selectPipelinesLatestRecord(pipelineIds);
|
|
|
+ deploymentId = latestRecord.getId();
|
|
|
+ }
|
|
|
+ InterfaceTestResult result = InterfaceTestResult.builder()
|
|
|
+ .testId(id)
|
|
|
+ .executeTime(executeTime)
|
|
|
+ .username(username)
|
|
|
+ .code(Integer.parseInt(res[0]))
|
|
|
+ .cost(Integer.parseInt(res[1]))
|
|
|
+ .result(res[2])
|
|
|
+ .deploymentId(deploymentId)
|
|
|
+ .build();
|
|
|
+ interfaceTestMapper.insertResult(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ public String[] doGet(String url, Map<String, Object> params) {
|
|
|
+ HttpURLConnection connection;
|
|
|
+ BufferedReader br = null;
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
+ String path = "";
|
|
|
+
|
|
|
+ String[] res = new String[3];
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ try {
|
|
|
+ if (params.size() == 1) {
|
|
|
+ for (String name : params.keySet()) {
|
|
|
+ sb.append(name).append("=").append(URLEncoder.encode(String.valueOf(params.get(name)), "utf-8"));
|
|
|
+ }
|
|
|
+ path = sb.toString();
|
|
|
+ } else if (params.size() >= 2) {
|
|
|
+ for (String name : params.keySet()) {
|
|
|
+ sb.append(name).append("=").append(URLEncoder.encode(String.valueOf(params.get(name)), "utf-8")).append("&");
|
|
|
+ }
|
|
|
+ path = sb.deleteCharAt(sb.length() - 1).toString();
|
|
|
+ }
|
|
|
+ String full_url = url;
|
|
|
+ if (!path.equals("")) {
|
|
|
+ full_url = url + "?" + path;
|
|
|
+ }
|
|
|
+
|
|
|
+ URL connURL = new URL(full_url);
|
|
|
+
|
|
|
+ connection = (HttpURLConnection) connURL.openConnection();
|
|
|
+ // 设置一些请求头
|
|
|
+ connection.setRequestProperty("Accept", "*/*");
|
|
|
+ connection.setRequestProperty("Connection", "Keep-Alive");
|
|
|
+ connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
|
|
|
+
|
|
|
+ // 建立连接
|
|
|
+ connection.connect();
|
|
|
+ res[0] = connection.getResponseCode() + "";
|
|
|
+ br = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
|
|
|
+ String line;
|
|
|
+ while ((line = br.readLine()) != null) {
|
|
|
+ result.append(line);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ res[2] = e + "";
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (br != null) {
|
|
|
+ br.close();
|
|
|
+ }
|
|
|
+ } catch (IOException ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+ res[1] = (end - start) + "";
|
|
|
+ if (result.length() != 0) {
|
|
|
+ res[2] = StringEscapeUtils.unescapeJava(result.toString());
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String[] doPost(String url, Map<String, Object> params) throws IOException {
|
|
|
+ Gson gson = new Gson();
|
|
|
+ String param = gson.toJson(params);
|
|
|
+ StringBuilder response = new StringBuilder();
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ String[] res = new String[3];
|
|
|
+ System.out.println(param);
|
|
|
+ try {
|
|
|
+ URL realUrl = new URL(url);
|
|
|
+ // 打开和URL之间的连接
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
|
|
|
+ // 设置通用的请求属性
|
|
|
+ connection.setDoOutput(true);
|
|
|
+ connection.setDoInput(true);
|
|
|
+ connection.setUseCaches(false);
|
|
|
+ connection.setInstanceFollowRedirects(true);
|
|
|
+ connection.setRequestMethod("POST"); // 设置请求方式
|
|
|
+ connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
|
|
|
+ connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
|
|
|
+ connection.connect();
|
|
|
+ // 获取URLConnection对象对应的输出流
|
|
|
+ OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码
|
|
|
+ out.append(param);
|
|
|
+ // flush输出流的缓冲
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+ // 定义BufferedReader输入流来读取URL的响应
|
|
|
+ BufferedReader reader = null;
|
|
|
+ reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
|
|
|
+ String line;
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ response.append(line);
|
|
|
+ }
|
|
|
+ reader.close();
|
|
|
+ res[0] = connection.getResponseCode() + "";
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println("发送POST请求出现异常!" + e);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+ res[1] = (end - start) + "";
|
|
|
+ res[2] = StringEscapeUtils.unescapeJava(response.toString());
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+}
|