Commit 4b651d01 by 吴博

feat: [none] ws_auto_test20241119

parent 20d381eb
package com.lemon.cases; package com.lemon.cases;
import cn.hutool.core.lang.UUID; import cn.hutool.core.lang.UUID;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.lemon.constants.Constants;
import com.lemon.pojo.Case; import com.lemon.pojo.Case;
import com.lemon.pojo.TestRecord; import com.lemon.pojo.TestRecord;
import com.lemon.testng.WebSocketManager; import com.lemon.testng.WebSocketManager;
...@@ -54,19 +50,23 @@ public class changjiangzuoyeserveCase extends BaseCase { ...@@ -54,19 +50,23 @@ public class changjiangzuoyeserveCase extends BaseCase {
ExcelUtils.parameterList.clear(); ExcelUtils.parameterList.clear();
ExcelUtils.apiList.clear(); ExcelUtils.apiList.clear();
} }
@Test(dataProvider = "data1", priority = 1, description = "接收event-3响应") @Test(dataProvider = "data1", priority = 1, description = "接收event-3响应")
public void testSendEvent2AndReceiveEvent3(Case cas) { public void testSendEvent2AndReceiveEvent3(Case cas) {
try { try {
// 重置参数
webSocketManager.event5Messages = new ArrayList<>();
caseTraceId = UUID.randomUUID().toString(); caseTraceId = UUID.randomUUID().toString();
log.info(caseTraceId + caseTraceId + "本次执行的case是:" + JSONUtil.toJsonStr(cas) + "caseTraceId是" + caseTraceId); log.info(caseTraceId + caseTraceId + "本次执行的case是:" + JSONUtil.toJsonStr(cas) + "caseTraceId是" + caseTraceId);
webSocketManager.messageCount = cas.getMessageCount(); webSocketManager.messageCount = cas.getMessageCount();
// 设置 latch,等待服务端 event-3 响应 // 设置 latch,等待服务端 event-3 响应
webSocketManager.latch4Event3 = new CountDownLatch(1); webSocketManager.latch4Event3 = new CountDownLatch(1);
// 定义 requestId,发送 event-2 消息时使用,并在接收 event-3 时验证 // 定义 requestId,发送 event-2 消息时使用,并在接收 event-3 时验证
String event2Message = "{\"event\":2,\"sendType\":0,\"userId\":\"143773987\",\"officialAccountsId\":997,\"messageBody\":\"{\\\"contentType\\\":\\\"Text\\\",\\\"content\\\":{\\\"textMsgDTO\\\":{\\\"content\\\":\\\"一起学中文\\\"}},\\\"digitalPersonId\\\":\\\"2938\\\",\\\"language\\\":\\\"zh-CN\\\",\\\"chatTypeEnum\\\":\\\"\\\"}\",\"origin\":\"WEB_H5\",\"bookId\":\"12597711\",\"requestId\":\"W5dI5aII\"}"; String event2Message = cas.getParams();
log.info(caseTraceId + "发送event2的消息是" + event2Message); log.info(caseTraceId + "发送event2的消息是" + event2Message);
JSONObject sendJson = JSONUtil.parseObj(event2Message); JSONObject sendJson = JSONUtil.parseObj(event2Message);
webSocketManager.sendMessage(event2Message); webSocketManager.sendMessage(event2Message);
...@@ -85,11 +85,7 @@ public class changjiangzuoyeserveCase extends BaseCase { ...@@ -85,11 +85,7 @@ public class changjiangzuoyeserveCase extends BaseCase {
log.info(caseTraceId + "testSendEvent2AndReceiveEvent3,WebSocket出错: " + e.getMessage()); log.info(caseTraceId + "testSendEvent2AndReceiveEvent3,WebSocket出错: " + e.getMessage());
} }
} // 接收event-5响应
@Test(dataProvider = "data1", priority = 2, dependsOnMethods = "testSendEvent2AndReceiveEvent3", description = "接收event-5响应")
public void testReceiveEvent5Responses(Case cas) {
try { try {
// 设置 latch,等待服务端 event-5 响应 // 设置 latch,等待服务端 event-5 响应
webSocketManager.latch4Event5 = new CountDownLatch(webSocketManager.messageCount); webSocketManager.latch4Event5 = new CountDownLatch(webSocketManager.messageCount);
...@@ -123,7 +119,16 @@ public class changjiangzuoyeserveCase extends BaseCase { ...@@ -123,7 +119,16 @@ public class changjiangzuoyeserveCase extends BaseCase {
if (cas.getPass() == null || cas.getPass() == 0) if (cas.getPass() == null || cas.getPass() == 0)
sendAlarm(cas.getParams()); sendAlarm(cas.getParams());
} }
}
/*
@Test(dataProvider = "data1", priority = 2, dependsOnMethods = "testSendEvent2AndReceiveEvent3", description = "接收event-5响应")
public void testReceiveEvent5Responses(Case cas) {
} }
*/
@AfterClass @AfterClass
......
...@@ -7,6 +7,7 @@ import cn.hutool.http.HttpUtil; ...@@ -7,6 +7,7 @@ import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.lemon.utils.AuthorizationUtils; import com.lemon.utils.AuthorizationUtils;
import com.pcloud.llm.cockpit.api.constants.Constants; import com.pcloud.llm.cockpit.api.constants.Constants;
import com.pcloud.llm.cockpit.api.domain.dto.request.WorkflowRequestBody; import com.pcloud.llm.cockpit.api.domain.dto.request.WorkflowRequestBody;
...@@ -15,16 +16,14 @@ import com.pcloud.llm.cockpit.api.domain.dto.response.CompletionResponseData; ...@@ -15,16 +16,14 @@ import com.pcloud.llm.cockpit.api.domain.dto.response.CompletionResponseData;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.java_websocket.client.WebSocketClient; import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake; import org.java_websocket.handshake.ServerHandshake;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Optional;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
...@@ -45,6 +44,8 @@ public class WebSocketManager { ...@@ -45,6 +44,8 @@ public class WebSocketManager {
// 创建定时任务发送心跳消息 // 创建定时任务发送心跳消息
public ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); public ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
public List<String> event5Messages = new ArrayList<>(); public List<String> event5Messages = new ArrayList<>();
public List<String> event5Messages4Stream = new ArrayList<>();
public String mergeStreamMessage;
public Integer messageCount; public Integer messageCount;
...@@ -67,44 +68,117 @@ public class WebSocketManager { ...@@ -67,44 +68,117 @@ public class WebSocketManager {
@Override @Override
public void onMessage(String message) { public void onMessage(String message) {
log.info("收到消息: " + message); log.info("收到消息: " + message);
// 检查消息是否为空或是数组
if (StrUtil.isBlank(message) || message.startsWith("[\"{")) {
log.info("接收的消息是一个数组,不是普通对话消息,不处理");
return;
}
// 解析消息为 JSON 对象
JSONObject receiveJson = JSONUtil.parseObj(message); JSONObject receiveJson = JSONUtil.parseObj(message);
Integer event = receiveJson.getInt("event"); Integer event = receiveJson.getInt("event");
if (null == event) {
log.info("event为null,消息异常不处理"); if (event == null || Lists.newArrayList(1).contains(event)) {
log.info("event为null或心跳信息不处理");
return; return;
} }
JSONObject messageBody = receiveJson.getJSONObject("messageBody");
Optional.ofNullable(receiveJson.getJSONObject("messageBody")).ifPresentOrElse(messageBody -> {
if (event == 3) { if (event == 3) {
// 提取并验证 requestId // 提取并验证 requestId
receivedRequestId = receiveJson.getStr("requestId"); receivedRequestId = receiveJson.getStr("requestId");
log.info("receivedRequestId是" + receivedRequestId + "、expectedRequestId是" + expectedRequestId); log.info("receivedRequestId是" + receivedRequestId + "、expectedRequestId是" + expectedRequestId);
if (receivedRequestId.equals(expectedRequestId)) { if (receivedRequestId.equals(expectedRequestId)) {
if (null == messageBody) {
log.info("messageBody为null,消息异常不处理");
return;
}
msgRemoteId = messageBody.getStr("msgRemoteId"); msgRemoteId = messageBody.getStr("msgRemoteId");
latch4Event3.countDown(); latch4Event3.countDown();
} }
} else if (event == 5) { } else if (event == 5) {
// 处理 event-5 消息 // 处理 event-5 消息
if (null == messageBody) { handleEvent5(messageBody);
log.info("messageBody为null,消息异常不处理"); }
return; }, () -> log.info("messageBody为null,消息异常不处理"));
} }
String msgRemoteId4Event5 = messageBody.getStr("msgRemoteId");
private void handleEvent5(JSONObject messageBody) {
Boolean stream = messageBody.getBool("stream"); Boolean stream = messageBody.getBool("stream");
if (msgRemoteId.equals(msgRemoteId4Event5)) { String streamState = messageBody.getStr("streamState");
latch4Event5.countDown(); // 结束接受消息 JSONObject messageBodyJSONObject = messageBody.getJSONObject("message");
if (null == messageBodyJSONObject) {
log.info("messageBodyJSONObject为null,不继续处理");
}
String msgRemoteId4Event5 = messageBodyJSONObject.getStr("msgRemoteId");
String contentType = messageBodyJSONObject.getStr("contentType");
JSONObject json4Option = messageBodyJSONObject.getJSONObject("optionMsgDTO");
String subType = json4Option != null ? json4Option.getStr("subType") : null;
if (!msgRemoteId.equals(msgRemoteId4Event5)) {
log.info("收到的msgRemoteId不匹配,忽略");
return;
} }
if(!stream) {
if (Lists.newArrayList("Option").contains(contentType) &&
Lists.newArrayList("CHAT_RECOMMEND").contains(subType)) {
log.info("推荐问题,不记录");
return;
}
if (!stream) {
if (msgRemoteId.equals(msgRemoteId4Event5)) {
event5Messages.add(messageBody.getStr("message")); event5Messages.add(messageBody.getStr("message"));
latch4Event5.countDown(); // 每条非流式消息执行一次 latch4Event5.countDown();
}
} else {
handleStreamMessages(messageBody, streamState);
}
} }
private void handleStreamMessages(JSONObject messageBody, String streamState) {
if ("START".equals(streamState)) {
mergeStreamMessage = messageBody.getStr("message");
} else if ("END".equals(streamState)) {
latch4Event5.countDown();
event5Messages.add(mergeStreamMessage);
} else {
JSONObject json4Message = JSONUtil.parseObj(mergeStreamMessage);
JSONObject json4Content = Optional.ofNullable(json4Message.getJSONObject("content"))
.orElseGet(() -> {
log.warn("mergeStreamMessage 中 content 为 null");
return new JSONObject();
});
JSONObject json4Text = Optional.ofNullable(json4Content.getJSONObject("textMsgDTO"))
.orElseGet(() -> {
log.warn("mergeStreamMessage 中 textMsgDTO 为 null");
return new JSONObject();
});
String content = json4Text.getStr("content", "");
JSONObject json4Message4Process = JSONUtil.parseObj(messageBody.getStr("message"));
JSONObject json4Content4Process = Optional.ofNullable(json4Message4Process.getJSONObject("content"))
.orElseGet(() -> {
log.warn("messageBody 中 content 为 null");
return new JSONObject();
});
JSONObject json4Text4Process = Optional.ofNullable(json4Content4Process.getJSONObject("textMsgDTO"))
.orElseGet(() -> {
log.warn("messageBody 中 textMsgDTO 为 null");
return new JSONObject();
});
String content4Process = json4Text4Process.getStr("content", "");
content = content + content4Process;
json4Text.set("content", content);
mergeStreamMessage = JSONUtil.toJsonStr(json4Message);
log.info("当前的流逝消息累积是:" + mergeStreamMessage);
} }
} }
@Override @Override
public void onClose(int code, String reason, boolean remote) { public void onClose(int code, String reason, boolean remote) {
log.info("WebSocket连接已关闭,原因: " + reason); log.info("WebSocket连接已关闭,原因: " + reason);
......
...@@ -13,6 +13,7 @@ import java.io.*; ...@@ -13,6 +13,7 @@ import java.io.*;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
/** /**
* @program: Lemon * @program: Lemon
...@@ -24,12 +25,12 @@ import java.util.List; ...@@ -24,12 +25,12 @@ import java.util.List;
public class ExcelUtils { public class ExcelUtils {
//读取Excel中第1个sheet,API数据 //读取Excel中第1个sheet,API数据
public static List<API> apiList = read(0, API.class); public static List<API> apiList = new ArrayList<>();
//读取Excel中第2个sheet,Case数据 //读取Excel中第2个sheet,Case数据
public static List<Case> caseList = read(1, Case.class); public static List<Case> caseList = new ArrayList<>();
// 读取excel中第3个sheet页,Parameter数据 // 读取excel中第3个sheet页,Parameter数据
public static List<Parameter> parameterList = read(2, Parameter.class); public static List<Parameter> parameterList = new ArrayList<>();
//回写Excel数据集合 //回写Excel数据集合
public static List<WriteBackData> wbdList = new ArrayList<>(); public static List<WriteBackData> wbdList = new ArrayList<>();
...@@ -225,6 +226,9 @@ public class ExcelUtils { ...@@ -225,6 +226,9 @@ public class ExcelUtils {
List<Case> caseList4Excel = read(filePath,0, Case.class); List<Case> caseList4Excel = read(filePath,0, Case.class);
List<Parameter> parameterList4Excel = read(filePath,1, Parameter.class); List<Parameter> parameterList4Excel = read(filePath,1, Parameter.class);
// 过滤掉idweinull的数据
caseList4Excel = caseList4Excel.stream().filter(aCase -> null != aCase.getId()).collect(Collectors.toList());
parameterList4Excel = parameterList4Excel.stream().filter(parameter -> null != parameter.getParams()).collect(Collectors.toList());
// 匹配Case对象 // 匹配Case对象
for (Case aCase : caseList4Excel) { for (Case aCase : caseList4Excel) {
//从parameter参数替换中将数据替换到case中 //从parameter参数替换中将数据替换到case中
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment