feat: 完善后端架构 - 标准化Controller层和Service层

 新功能:
- 创建了完整的Service层架构,包含所有业务实体的Service接口和实现类
- 新增8个标准化的Controller类,支持完整的CRUD操作
- 实现了统一的Request/Response模式和分页查询功能
- 创建了认证服务(AuthService)和令牌服务(TokenService)
- 添加了Redis配置和认证拦截器

🏗️ 架构优化:
- 移除Controller层所有try-catch块,使用全局异常处理机制
- 创建了专门的异常类(AuthException, TokenException, CaptchaException)
- 统一了API返回格式,完善了Result类的方法
- 实现了标准的分页查询和参数校验

📦 新增文件:
- 8个Controller类: Achievement, Comment, CommunityPost, Conversation, CozeApiCall, EmotionAnalysis, Reward, UserStats
- 12个Service接口和对应的实现类
- 标准化的DTO类(Request/Response)
- 异常处理类和拦截器
- 测试用例

🔧 重构优化:
- 重写了AuthController,移除所有业务逻辑到Service层
- 优化了MessageController,使用标准的Request/Response格式
- 更新了全局异常处理器,支持多种异常类型
- 完善了WebConfig配置,添加认证拦截器

📊 代码统计:
- 新增文件: 60+个
- 新增代码行数: 8000+行
- 重构代码行数: 1000+行
- 移除过时接口: 4个
This commit is contained in:
2025-07-24 07:38:40 +08:00
parent 880e0e3c88
commit 873b8e55da
67 changed files with 8619 additions and 850 deletions
@@ -0,0 +1,190 @@
package com.emotion.controller;
import com.emotion.dto.request.LoginRequest;
import com.emotion.dto.request.RegisterRequest;
import com.emotion.dto.response.AuthResponse;
import com.emotion.dto.response.CaptchaResponse;
import com.emotion.exception.AuthException;
import com.emotion.exception.CaptchaException;
import com.emotion.service.AuthService;
import com.emotion.service.TokenService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
/**
* AuthController测试类
*
* @author emotion-museum
* @date 2025-07-23
*/
public class AuthControllerTest {
@Mock
private AuthService authService;
@InjectMocks
private AuthController authController;
private MockMvc mockMvc;
private ObjectMapper objectMapper;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(authController).build();
objectMapper = new ObjectMapper();
}
@Test
void testLogin() throws Exception {
// 准备测试数据
LoginRequest request = new LoginRequest();
request.setAccount("testuser");
request.setPassword("password123");
request.setCaptcha("1234");
request.setCaptchaKey("test-key");
AuthResponse response = new AuthResponse();
response.setAccessToken("test-access-token");
response.setRefreshToken("test-refresh-token");
response.setExpiresIn(86400L);
// Mock服务方法
when(authService.login(any(LoginRequest.class))).thenReturn(response);
// 执行测试
mockMvc.perform(post("/auth/login")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(200))
.andExpect(jsonPath("$.message").value("登录成功"))
.andExpect(jsonPath("$.data.accessToken").value("test-access-token"));
}
@Test
void testRegister() throws Exception {
// 准备测试数据
RegisterRequest request = new RegisterRequest();
request.setAccount("newuser");
request.setPassword("password123");
request.setUsername("New User");
request.setEmail("newuser@example.com");
request.setCaptcha("1234");
request.setCaptchaKey("test-key");
AuthResponse response = new AuthResponse();
response.setAccessToken("test-access-token");
response.setRefreshToken("test-refresh-token");
response.setExpiresIn(86400L);
// Mock服务方法
when(authService.register(any(RegisterRequest.class))).thenReturn(response);
// 执行测试
mockMvc.perform(post("/auth/register")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(200))
.andExpect(jsonPath("$.message").value("注册成功"));
}
@Test
void testGenerateCaptcha() throws Exception {
// 准备测试数据
CaptchaResponse response = new CaptchaResponse();
response.setCaptchaKey("test-captcha-key");
response.setCaptchaImage("data:image/png;base64,test-image");
response.setExpiresIn(300L);
// Mock服务方法
when(authService.generateCaptcha()).thenReturn(response);
// 执行测试
mockMvc.perform(get("/auth/captcha"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(200))
.andExpect(jsonPath("$.data.captchaKey").value("test-captcha-key"))
.andExpect(jsonPath("$.data.expiresIn").value(300));
}
@Test
void testValidateToken() throws Exception {
// Mock服务方法
when(authService.validateToken("valid-token")).thenReturn(true);
// 执行测试
mockMvc.perform(get("/auth/validate")
.header("Authorization", "Bearer valid-token"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(200))
.andExpect(jsonPath("$.data").value(true));
}
@Test
void testLogout() throws Exception {
// Mock服务方法
when(authService.logoutByToken("valid-token")).thenReturn(true);
// 执行测试
mockMvc.perform(post("/auth/logout")
.header("Authorization", "Bearer valid-token"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(200));
}
@Test
void testLoginWithInvalidCaptcha() throws Exception {
// 准备测试数据
LoginRequest request = new LoginRequest();
request.setAccount("testuser");
request.setPassword("password123");
request.setCaptcha("wrong");
request.setCaptchaKey("test-key");
// Mock服务方法抛出异常
when(authService.login(any(LoginRequest.class))).thenThrow(new CaptchaException("验证码错误"));
// 执行测试
mockMvc.perform(post("/auth/login")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value(400))
.andExpect(jsonPath("$.message").value("验证码错误"));
}
@Test
void testLoginWithInvalidAccount() throws Exception {
// 准备测试数据
LoginRequest request = new LoginRequest();
request.setAccount("nonexistent");
request.setPassword("password123");
request.setCaptcha("1234");
request.setCaptchaKey("test-key");
// Mock服务方法抛出异常
when(authService.login(any(LoginRequest.class))).thenThrow(new AuthException("账号不存在"));
// 执行测试
mockMvc.perform(post("/auth/login")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isUnauthorized())
.andExpect(jsonPath("$.code").value(401))
.andExpect(jsonPath("$.message").value("账号不存在"));
}
}