调用dify,解决时间问题,源语料为空的问题
This commit is contained in:
113
src/test/java/com/rj/controller/DifyWorkflowControllerTest.java
Normal file
113
src/test/java/com/rj/controller/DifyWorkflowControllerTest.java
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
package com.rj.controller;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.rj.dto.DifyWorkflowRequestDto;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||||
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dify工作流控制器测试类
|
||||||
|
*
|
||||||
|
* @author 李中华
|
||||||
|
* @date 2025/1/3
|
||||||
|
*/
|
||||||
|
@SpringBootTest
|
||||||
|
@AutoConfigureWebMvc
|
||||||
|
@ActiveProfiles("test")
|
||||||
|
public class DifyWorkflowControllerTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private WebApplicationContext webApplicationContext;
|
||||||
|
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ObjectMapper objectMapper;
|
||||||
|
|
||||||
|
public void setup() {
|
||||||
|
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetWorkflowConfig() throws Exception {
|
||||||
|
setup();
|
||||||
|
|
||||||
|
mockMvc.perform(get("/api/dify/workflow/config")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$.message").value("工作流配置信息"))
|
||||||
|
.andExpect(jsonPath("$.workflowName").value("企微对话分析工作流"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTestWithPresetData() throws Exception {
|
||||||
|
setup();
|
||||||
|
|
||||||
|
mockMvc.perform(post("/api/dify/workflow/consulting-scenario/test")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$.success").exists());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConsultingScenarioWorkflowWithParameters() throws Exception {
|
||||||
|
setup();
|
||||||
|
|
||||||
|
// 读取真实的聊天内容
|
||||||
|
ClassLoader classLoader = getClass().getClassLoader();
|
||||||
|
java.io.InputStream inputStream = classLoader.getResourceAsStream("corpus/qiwei/qw1.txt");
|
||||||
|
java.util.Scanner scanner = new java.util.Scanner(inputStream, "UTF-8").useDelimiter("\\A");
|
||||||
|
String chatContent = scanner.hasNext() ? scanner.next() : "";
|
||||||
|
scanner.close();
|
||||||
|
|
||||||
|
// 生成随机值
|
||||||
|
String unionId = "test_union_" + java.util.UUID.randomUUID().toString().replace("-", "").substring(0, 8);
|
||||||
|
String consultantId = "consultant_" + java.util.UUID.randomUUID().toString().replace("-", "").substring(0, 8);
|
||||||
|
String communicateDate = generateRandomDateTime();
|
||||||
|
String analysisScene = "sales_consultation";
|
||||||
|
String aiAnalysisRequestId = "ai_req_" + java.util.UUID.randomUUID().toString().replace("-", "").substring(0, 12);
|
||||||
|
int version = new java.util.Random().nextInt(10) + 1; // 1-10之间的随机版本号
|
||||||
|
|
||||||
|
// 创建测试请求DTO
|
||||||
|
DifyWorkflowRequestDto requestDto = new DifyWorkflowRequestDto();
|
||||||
|
requestDto.setUnionId(unionId);
|
||||||
|
requestDto.setConsultantId(consultantId);
|
||||||
|
requestDto.setCommunicateDate(communicateDate);
|
||||||
|
requestDto.setAnalysisScene(analysisScene);
|
||||||
|
requestDto.setAiAnalysisRequestId(aiAnalysisRequestId);
|
||||||
|
requestDto.setVersion(version);
|
||||||
|
requestDto.setChat(chatContent);
|
||||||
|
|
||||||
|
mockMvc.perform(post("/api/dify/workflow/consulting-scenario")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content(objectMapper.writeValueAsString(requestDto)))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$.success").exists());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成随机日期时间字符串
|
||||||
|
* @return 随机日期时间字符串,格式为 "yyyy-MM-dd HH:mm:ss"
|
||||||
|
*/
|
||||||
|
private String generateRandomDateTime() {
|
||||||
|
java.util.Random random = new java.util.Random();
|
||||||
|
int year = 2020 + random.nextInt(6); // 2020-2025年
|
||||||
|
int month = 1 + random.nextInt(12); // 1-12月
|
||||||
|
int day = 1 + random.nextInt(28); // 1-28日(避免月份天数问题)
|
||||||
|
int hour = random.nextInt(24); // 0-23小时
|
||||||
|
int minute = random.nextInt(60); // 0-59分钟
|
||||||
|
int second = random.nextInt(60); // 0-59秒
|
||||||
|
|
||||||
|
return String.format("%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, second);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user