feat(接口测试): 构建接口执行过程

This commit is contained in:
fit2-zhao 2023-10-30 10:44:47 +08:00 committed by Craftsman
parent 347d548fa9
commit 9f5b37b497
5 changed files with 119 additions and 57 deletions

View File

@ -2,8 +2,12 @@ package io.metersphere.sdk.constants;
public class KafkaTopicConstants {
public static final String PLUGIN = "PLUGIN";
// API TOPIC
public static final String API_REPORT_TOPIC = "API_REPORT_TOPIC";
public static final String API_REPORT_TASK_TOPIC = "API_REPORT_TASK_TOPIC";
public static final String API_REPORT_DEBUG_TOPIC = "API_REPORT_DEBUG_TOPIC";
public static class TYPE{
public static class TYPE {
public static final String ADD = "ADD";
public static final String DELETE = "DELETE";
}

View File

@ -0,0 +1,50 @@
package io.metersphere.sdk.dto.api.task;
import io.metersphere.sdk.constants.KafkaTopicConstants;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.RsaUtils;
import lombok.Data;
import org.apache.commons.collections4.MapUtils;
import java.io.Serial;
import java.io.Serializable;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
/**
* 任务请求参数数据
*/
@Data
public class TaskRequest implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
private String reportId;
private String msUrl;
private String kafkaConfig;
private String minioConfig;
// TODO 其它执行参数
// 默认声明对象时获取配置参数
public void setKafkaConfig(Map<String, String> kafkaConfig) {
if (MapUtils.isNotEmpty(kafkaConfig)) {
try {
this.kafkaConfig = RsaUtils.publicEncrypt(JSON.toJSONString(kafkaConfig), KafkaTopicConstants.API_REPORT_TOPIC);
} catch (NoSuchAlgorithmException e) {
throw new MSException(e);
}
}
}
public void setMinioConfig(Map<String, String> minioConfig) {
if (MapUtils.isNotEmpty(minioConfig)) {
try {
this.minioConfig = RsaUtils.publicEncrypt(JSON.toJSONString(minioConfig), KafkaTopicConstants.API_REPORT_TOPIC);
} catch (NoSuchAlgorithmException e) {
throw new MSException(e);
}
}
}
}

View File

@ -0,0 +1,15 @@
package io.metersphere.sdk.dto.api.task;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
@Data
public class TaskResult implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
// TODO: 补充任务执行结果数据结构
}

View File

@ -1,6 +1,8 @@
package io.metersphere.sdk.util;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
@ -12,7 +14,7 @@ import java.util.function.Function;
public class CommonBeanFactory implements ApplicationContextAware {
private static ApplicationContext context;
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
public void setApplicationContext(@NotNull ApplicationContext ctx) throws BeansException {
context = ctx;
}
@ -36,13 +38,15 @@ public class CommonBeanFactory implements ApplicationContextAware {
return context.getBeansOfType(className);
}
public static Object invoke(String beanName, Function<Class, Method> methodFunction, Object... args) {
Object bean = getBean(beanName);
public static Object invoke(String beanName, Function<Class<?>, Method> methodFunction, Object... args) {
try {
Class<?> clazz = bean.getClass();
return methodFunction.apply(clazz).invoke(bean, args);
Object bean = getBean(beanName);
if (ObjectUtils.isNotEmpty(bean)) {
Class<?> clazz = bean.getClass();
return methodFunction.apply(clazz).invoke(bean, args);
}
} catch (Exception e) {
// LogUtil.error(e);
LogUtils.error(e);
}
return null;
}

View File

@ -1,5 +1,6 @@
package io.metersphere.sdk.util;
import io.metersphere.sdk.exception.MSException;
import org.apache.commons.codec.binary.Base64;
import java.io.*;
@ -17,12 +18,10 @@ public class CompressUtils {
* @return 压缩后数据
*/
public static Object zip(Object data) {
if (!(data instanceof byte[])) {
if (!(data instanceof byte[] temp)) {
return data;
}
byte[] temp = (byte[]) data;
byte[] b = (byte[]) data;
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(bos);
@ -32,25 +31,21 @@ public class CompressUtils {
zip.write(temp);
zip.closeEntry();
zip.close();
b = bos.toByteArray();
temp = bos.toByteArray();
bos.close();
} catch (Exception ignore) {
}
return b;
return temp;
}
private static File getFile(String fileName) throws IOException {
// 创建文件对象
File file;
if (ZIP_PATH != null && !ZIP_PATH.equals("")) {
file = new File(ZIP_PATH, fileName);
} else {
file = new File(fileName);
}
if (!file.exists()) {
file.createNewFile();
file = new File(ZIP_PATH, fileName);
if (!file.exists() && !file.createNewFile()) {
throw new MSException("创建文件失败");
}
// 返回文件
return file;
@ -63,38 +58,35 @@ public class CompressUtils {
private static void zipFile(File file, ZipOutputStream zipOutputStream) throws IOException {
if (file.exists()) {
if (file.isFile()) {
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
ZipEntry entry = new ZipEntry(file.getName());
zipOutputStream.putNextEntry(entry);
try (FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis)) {
ZipEntry entry = new ZipEntry(file.getName());
zipOutputStream.putNextEntry(entry);
final int MAX_BYTE = 10 * 1024 * 1024; // 最大流为10MB
long streamTotal = 0; // 接收流的容量
int streamNum = 0; // 需要分开的流数目
int leaveByte = 0; // 文件剩下的字符数
byte[] buffer; // byte数据接受文件的数据
final int MAX_BYTE = 10 * 1024 * 1024; // 最大流为10MB
int streamTotal = 0; // 接收流的容量
int streamNum = 0; // 需要分开的流数目
int leaveByte = 0; // 文件剩下的字符数
byte[] buffer; // byte数据接受文件的数据
streamTotal = bis.available(); // 获取流的最大字符数
streamNum = (int) Math.floor(streamTotal / MAX_BYTE);
leaveByte = (int) (streamTotal % MAX_BYTE);
streamTotal = bis.available(); // 获取流的最大字符数
streamNum = (int) Math.floor((double) streamTotal / MAX_BYTE);
leaveByte = (streamTotal % MAX_BYTE);
if (streamNum > 0) {
for (int i = 0; i < streamNum; i++) {
buffer = new byte[MAX_BYTE];
bis.read(buffer, 0, MAX_BYTE);
zipOutputStream.write(buffer, 0, MAX_BYTE);
if (streamNum > 0) {
for (int i = 0; i < streamNum; i++) {
buffer = new byte[MAX_BYTE];
bis.read(buffer, 0, MAX_BYTE);
zipOutputStream.write(buffer, 0, MAX_BYTE);
}
}
// 写入剩下的流数据
buffer = new byte[leaveByte];
bis.read(buffer, 0, leaveByte); // 读入流
zipOutputStream.write(buffer, 0, leaveByte); // 写入流
zipOutputStream.closeEntry(); // 关闭当前的zip entry
}
// 写入剩下的流数据
buffer = new byte[leaveByte];
bis.read(buffer, 0, leaveByte); // 读入流
zipOutputStream.write(buffer, 0, leaveByte); // 写入流
zipOutputStream.closeEntry(); // 关闭当前的zip entry
// 关闭输入流
bis.close();
fis.close();
}
}
}
@ -116,8 +108,7 @@ public class CompressUtils {
int size = fileList.size();
// 压缩列表中的文件
for (int i = 0; i < size; i++) {
File file = fileList.get(i);
for (File file : fileList) {
zipFile(file, zipOutputStream);
}
// 关闭压缩流文件流
@ -133,30 +124,28 @@ public class CompressUtils {
* @return 解压后数据
*/
public static Object unzip(Object data) {
if (!(data instanceof byte[])) {
if (!(data instanceof byte[] temp)) {
return data;
}
byte[] temp = (byte[]) data;
byte[] b = (byte[]) data;
try {
ByteArrayInputStream bis = new ByteArrayInputStream(temp);
ZipInputStream zip = new ZipInputStream(bis);
while (zip.getNextEntry() != null) {
byte[] buf = new byte[1024];
int num;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ByteArrayOutputStream bas = new ByteArrayOutputStream();
while ((num = zip.read(buf, 0, buf.length)) != -1) {
baos.write(buf, 0, num);
bas.write(buf, 0, num);
}
b = baos.toByteArray();
baos.flush();
baos.close();
temp = bas.toByteArray();
bas.flush();
bas.close();
}
zip.close();
bis.close();
} catch (Exception ignore) {
}
return b;
return temp;
}
public static Object zipString(Object data) {