test(server): ShortNovelController 集成测试
This commit is contained in:
@@ -0,0 +1,79 @@
|
|||||||
|
package com.emotion.controller;
|
||||||
|
|
||||||
|
import com.emotion.service.ShortNovelService;
|
||||||
|
import com.emotion.util.JwtUtil;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ShortNovelController 测试
|
||||||
|
*
|
||||||
|
* @author huazhongmin
|
||||||
|
* @date 2026-07-19
|
||||||
|
*/
|
||||||
|
@SpringBootTest
|
||||||
|
@AutoConfigureMockMvc(addFilters = false)
|
||||||
|
public class ShortNovelControllerTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
private ShortNovelService shortNovelService;
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
private JwtUtil jwtUtil;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setUp() {
|
||||||
|
when(jwtUtil.validateToken("test-token")).thenReturn(true);
|
||||||
|
when(jwtUtil.getUserIdFromToken("test-token")).thenReturn("test-user");
|
||||||
|
when(jwtUtil.getUsernameFromToken("test-token")).thenReturn("tester");
|
||||||
|
when(jwtUtil.getUserTypeFromToken("test-token")).thenReturn("user");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStreamEndpoint() throws Exception {
|
||||||
|
SseEmitter emitter = new SseEmitter();
|
||||||
|
when(shortNovelService.stream(any())).thenReturn(emitter);
|
||||||
|
|
||||||
|
mockMvc.perform(post("/shortNovel/stream")
|
||||||
|
.header("Authorization", "Bearer test-token")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content("{\"query\":\"今天我想当 CEO\"}"))
|
||||||
|
.andExpect(status().isOk());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFollowupEndpoint() throws Exception {
|
||||||
|
SseEmitter emitter = new SseEmitter();
|
||||||
|
when(shortNovelService.followup(any())).thenReturn(emitter);
|
||||||
|
|
||||||
|
mockMvc.perform(post("/shortNovel/followup")
|
||||||
|
.header("Authorization", "Bearer test-token")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content("{\"sessionId\":\"sess-123\",\"action\":\"confirm_outline\",\"payload\":{}}"))
|
||||||
|
.andExpect(status().isOk());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStreamWithBlankQuery() throws Exception {
|
||||||
|
mockMvc.perform(post("/shortNovel/stream")
|
||||||
|
.header("Authorization", "Bearer test-token")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content("{\"query\":\"\"}"))
|
||||||
|
.andExpect(status().isBadRequest());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user