|
|
@@ -5,8 +5,12 @@ import com.etotem.num.common.Result;
|
|
|
import com.etotem.num.config.JwtConfig;
|
|
|
import com.etotem.num.entity.ChartRecord;
|
|
|
import com.etotem.num.entity.ChatMessage;
|
|
|
+import com.etotem.num.entity.Commission;
|
|
|
import com.etotem.num.entity.Order;
|
|
|
+import com.etotem.num.entity.RatingAnnotation;
|
|
|
+import com.etotem.num.entity.Tag;
|
|
|
import com.etotem.num.entity.User;
|
|
|
+import com.etotem.num.entity.Withdraw;
|
|
|
import com.etotem.num.service.*;
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
@@ -56,6 +60,15 @@ class ControllerIntegrationTest {
|
|
|
@MockBean
|
|
|
private WithdrawService withdrawService;
|
|
|
|
|
|
+ @MockBean
|
|
|
+ private CommissionService commissionService;
|
|
|
+
|
|
|
+ @MockBean
|
|
|
+ private ConfigService configService;
|
|
|
+
|
|
|
+ @MockBean
|
|
|
+ private AnnotationService annotationService;
|
|
|
+
|
|
|
// Real JWT tokens for testing
|
|
|
private String userToken;
|
|
|
private String adminToken;
|
|
|
@@ -121,8 +134,9 @@ class ControllerIntegrationTest {
|
|
|
|
|
|
@Test
|
|
|
void testProtectedEndpoint_withoutToken() throws Exception {
|
|
|
- mockMvc.perform(post("/api/chart/list")
|
|
|
- .contentType(MediaType.APPLICATION_JSON))
|
|
|
+ mockMvc.perform(post("/api/chart/detail")
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content("{}"))
|
|
|
.andExpect(status().isOk())
|
|
|
.andExpect(jsonPath("$.code").value(1001))
|
|
|
.andExpect(jsonPath("$.message").value("Token is required"));
|
|
|
@@ -130,9 +144,10 @@ class ControllerIntegrationTest {
|
|
|
|
|
|
@Test
|
|
|
void testProtectedEndpoint_withInvalidToken() throws Exception {
|
|
|
- mockMvc.perform(post("/api/chart/list")
|
|
|
+ mockMvc.perform(post("/api/chart/detail")
|
|
|
.header("Authorization", "Bearer invalid_token_here")
|
|
|
- .contentType(MediaType.APPLICATION_JSON))
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content("{}"))
|
|
|
.andExpect(status().isOk())
|
|
|
.andExpect(jsonPath("$.code").value(1001))
|
|
|
.andExpect(jsonPath("$.message").value("Token invalid"));
|
|
|
@@ -140,11 +155,14 @@ class ControllerIntegrationTest {
|
|
|
|
|
|
@Test
|
|
|
void testProtectedEndpoint_withUserToken_success() throws Exception {
|
|
|
- when(chartService.getByUserId(100L)).thenReturn(new ArrayList<>());
|
|
|
+ ChartRecord mockRecord = new ChartRecord();
|
|
|
+ mockRecord.setId(100L);
|
|
|
+ when(chartService.getById(100L)).thenReturn(mockRecord);
|
|
|
|
|
|
- mockMvc.perform(post("/api/chart/list")
|
|
|
+ mockMvc.perform(post("/api/chart/detail")
|
|
|
.header("Authorization", "Bearer " + userToken)
|
|
|
- .contentType(MediaType.APPLICATION_JSON))
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content("{\"id\":100}"))
|
|
|
.andExpect(status().isOk())
|
|
|
.andExpect(jsonPath("$.code").value(0));
|
|
|
}
|
|
|
@@ -252,7 +270,7 @@ class ControllerIntegrationTest {
|
|
|
order.setTotalFee(39800);
|
|
|
order.setOutTradeNo("MOCK123456");
|
|
|
|
|
|
- when(orderService.createOrder(100L, 39800, "wxpay")).thenReturn(order);
|
|
|
+ when(orderService.createOrder(100L, 39800, "wxpay", null, false)).thenReturn(order);
|
|
|
Map<String, String> mockPayParams = new HashMap<>();
|
|
|
mockPayParams.put("package", "mock_prepay_123");
|
|
|
mockPayParams.put("timeStamp", "1234567890");
|
|
|
@@ -331,4 +349,335 @@ class ControllerIntegrationTest {
|
|
|
.andExpect(jsonPath("$.code").value(1004))
|
|
|
.andExpect(jsonPath("$.message").value("Chart record not found"));
|
|
|
}
|
|
|
+
|
|
|
+ // ==================== ProfileController ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testProfileInfo() throws Exception {
|
|
|
+ User mockUser = new User();
|
|
|
+ mockUser.setId(100L);
|
|
|
+ mockUser.setNickname("测试用户");
|
|
|
+ mockUser.setAvatarUrl("http://avatar.url");
|
|
|
+ when(userService.getById(100L)).thenReturn(mockUser);
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/profile/info")
|
|
|
+ .header("Authorization", "Bearer " + userToken))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(0))
|
|
|
+ .andExpect(jsonPath("$.data.nickname").value("测试用户"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testProfileVipStatus() throws Exception {
|
|
|
+ when(userService.isVip(100L)).thenReturn(true);
|
|
|
+ User mockUser = new User();
|
|
|
+ mockUser.setId(100L);
|
|
|
+ when(userService.getById(100L)).thenReturn(mockUser);
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/profile/vip-status")
|
|
|
+ .header("Authorization", "Bearer " + userToken))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(0))
|
|
|
+ .andExpect(jsonPath("$.data.isVip").value(true));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testProfileQuota_vip() throws Exception {
|
|
|
+ User mockUser = new User();
|
|
|
+ mockUser.setId(100L);
|
|
|
+ mockUser.setDailyChartCount(2);
|
|
|
+ mockUser.setDailyChatCount(1);
|
|
|
+ when(userService.isVip(100L)).thenReturn(true);
|
|
|
+ when(userService.getById(100L)).thenReturn(mockUser);
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/profile/quota")
|
|
|
+ .header("Authorization", "Bearer " + userToken))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(0))
|
|
|
+ .andExpect(jsonPath("$.data.isVip").value(true))
|
|
|
+ .andExpect(jsonPath("$.data.chartLimit").value(-1))
|
|
|
+ .andExpect(jsonPath("$.data.chatLimit").value(-1));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testProfileQuota_nonVip() throws Exception {
|
|
|
+ User mockUser = new User();
|
|
|
+ mockUser.setId(100L);
|
|
|
+ mockUser.setDailyChartCount(2);
|
|
|
+ mockUser.setDailyChatCount(1);
|
|
|
+ when(userService.isVip(100L)).thenReturn(false);
|
|
|
+ when(userService.getById(100L)).thenReturn(mockUser);
|
|
|
+ // ConfigService is mocked — set default quota return values
|
|
|
+ when(configService.getQuota("chart")).thenReturn(3);
|
|
|
+ when(configService.getQuota("chat")).thenReturn(3);
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/profile/quota")
|
|
|
+ .header("Authorization", "Bearer " + userToken))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(0))
|
|
|
+ .andExpect(jsonPath("$.data.isVip").value(false))
|
|
|
+ .andExpect(jsonPath("$.data.chartLimit").value(3))
|
|
|
+ .andExpect(jsonPath("$.data.chatLimit").value(3));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testProfileCommissionSummary() throws Exception {
|
|
|
+ when(commissionService.getTotalCommission(100L)).thenReturn(5000L);
|
|
|
+ when(commissionService.getAvailableBalance(100L)).thenReturn(3000L);
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/profile/commission-summary")
|
|
|
+ .header("Authorization", "Bearer " + userToken))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(0))
|
|
|
+ .andExpect(jsonPath("$.data.totalEarnings").value(5000))
|
|
|
+ .andExpect(jsonPath("$.data.availableBalance").value(3000));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testProfileChartCount() throws Exception {
|
|
|
+ when(chartService.getByUserId(100L))
|
|
|
+ .thenReturn(Collections.singletonList(new ChartRecord()));
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/profile/chart-count")
|
|
|
+ .header("Authorization", "Bearer " + userToken))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(0))
|
|
|
+ .andExpect(jsonPath("$.data.count").value(1));
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== CommissionController ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testCommissionList() throws Exception {
|
|
|
+ Commission comm = new Commission();
|
|
|
+ comm.setId(1L);
|
|
|
+ comm.setAmount(5000);
|
|
|
+ comm.setLevel(1);
|
|
|
+ comm.setStatus("settled");
|
|
|
+ when(commissionService.getMyCommissions(100L))
|
|
|
+ .thenReturn(Collections.singletonList(comm));
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/commission/list")
|
|
|
+ .header("Authorization", "Bearer " + userToken))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(0))
|
|
|
+ .andExpect(jsonPath("$.data[0].amount").value(5000));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testCommissionTotal() throws Exception {
|
|
|
+ when(commissionService.getTotalCommission(100L)).thenReturn(5000L);
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/commission/total")
|
|
|
+ .header("Authorization", "Bearer " + userToken))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(0))
|
|
|
+ .andExpect(jsonPath("$.data.total").value(5000));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testCommissionStats() throws Exception {
|
|
|
+ Map<String, Object> stats = new HashMap<>();
|
|
|
+ stats.put("totalEarnings", 5000L);
|
|
|
+ stats.put("totalCommissions", 1);
|
|
|
+ when(commissionService.getStats(100L)).thenReturn(stats);
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/commission/stats")
|
|
|
+ .header("Authorization", "Bearer " + userToken))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(0))
|
|
|
+ .andExpect(jsonPath("$.data.totalEarnings").value(5000));
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== WithdrawController ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testWithdrawApply() throws Exception {
|
|
|
+ Withdraw wd = new Withdraw();
|
|
|
+ wd.setId(1L);
|
|
|
+ wd.setUserId(100L);
|
|
|
+ wd.setAmount(10000);
|
|
|
+ wd.setStatus("pending");
|
|
|
+ when(withdrawService.apply(100L, 10000)).thenReturn(wd);
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/withdraw/apply")
|
|
|
+ .header("Authorization", "Bearer " + userToken)
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content("{\"amount\":10000}"))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(0))
|
|
|
+ .andExpect(jsonPath("$.data.id").value(1))
|
|
|
+ .andExpect(jsonPath("$.data.status").value("pending"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testWithdrawList() throws Exception {
|
|
|
+ Withdraw wd = new Withdraw();
|
|
|
+ wd.setId(1L);
|
|
|
+ wd.setAmount(5000);
|
|
|
+ wd.setStatus("approved");
|
|
|
+ when(withdrawService.getMyWithdraws(100L))
|
|
|
+ .thenReturn(Collections.singletonList(wd));
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/withdraw/list")
|
|
|
+ .header("Authorization", "Bearer " + userToken))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(0))
|
|
|
+ .andExpect(jsonPath("$.data[0].amount").value(5000));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testWithdrawBalance() throws Exception {
|
|
|
+ when(withdrawService.getAvailableBalance(100L)).thenReturn(10000L);
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/withdraw/balance")
|
|
|
+ .header("Authorization", "Bearer " + userToken))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(0))
|
|
|
+ .andExpect(jsonPath("$.data.balance").value(10000));
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== AnnotationController ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testAnnotationAdd() throws Exception {
|
|
|
+ RatingAnnotation annotation = new RatingAnnotation();
|
|
|
+ annotation.setId(1L);
|
|
|
+ annotation.setRecordId(10L);
|
|
|
+ annotation.setUserId(100L);
|
|
|
+ annotation.setPosition("O");
|
|
|
+ annotation.setContent("测试标注");
|
|
|
+ when(annotationService.addAnnotation(10L, 100L, "O", "测试标注")).thenReturn(annotation);
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/annotation/add")
|
|
|
+ .header("Authorization", "Bearer " + userToken)
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content("{\"recordId\":10,\"position\":\"O\",\"content\":\"测试标注\"}"))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(0))
|
|
|
+ .andExpect(jsonPath("$.data.id").value(1));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testAnnotationList() throws Exception {
|
|
|
+ RatingAnnotation annotation = new RatingAnnotation();
|
|
|
+ annotation.setPosition("O");
|
|
|
+ annotation.setContent("测试");
|
|
|
+ when(annotationService.getAnnotations(10L))
|
|
|
+ .thenReturn(Collections.singletonList(annotation));
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/annotation/list")
|
|
|
+ .header("Authorization", "Bearer " + userToken)
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content("{\"recordId\":10}"))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(0))
|
|
|
+ .andExpect(jsonPath("$.data[0].content").value("测试"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testAnnotationDelete() throws Exception {
|
|
|
+ doNothing().when(annotationService).deleteAnnotation(5L);
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/annotation/delete")
|
|
|
+ .header("Authorization", "Bearer " + userToken)
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content("{\"id\":5}"))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(0));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testAnnotationTagAdd() throws Exception {
|
|
|
+ Tag tag = new Tag();
|
|
|
+ tag.setId(1L);
|
|
|
+ tag.setRecordId(10L);
|
|
|
+ tag.setTag("重要");
|
|
|
+ when(annotationService.addTag(10L, 100L, "重要")).thenReturn(tag);
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/annotation/tag/add")
|
|
|
+ .header("Authorization", "Bearer " + userToken)
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content("{\"recordId\":10,\"tag\":\"重要\"}"))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(0))
|
|
|
+ .andExpect(jsonPath("$.data.id").value(1));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testAnnotationTagList() throws Exception {
|
|
|
+ Tag tag = new Tag();
|
|
|
+ tag.setTag("重要");
|
|
|
+ when(annotationService.getTags(10L))
|
|
|
+ .thenReturn(Collections.singletonList(tag));
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/annotation/tag/list")
|
|
|
+ .header("Authorization", "Bearer " + userToken)
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content("{\"recordId\":10}"))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(0))
|
|
|
+ .andExpect(jsonPath("$.data[0].tag").value("重要"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testAnnotationTagDelete() throws Exception {
|
|
|
+ doNothing().when(annotationService).deleteTag(5L);
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/annotation/tag/delete")
|
|
|
+ .header("Authorization", "Bearer " + userToken)
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content("{\"id\":5}"))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(0));
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== ChartController Consultation ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testConsultationStart_new() throws Exception {
|
|
|
+ Map<String, Object> mockResult = new HashMap<>();
|
|
|
+ Map<String, Object> recordData = new HashMap<>();
|
|
|
+ recordData.put("id", 1);
|
|
|
+ recordData.put("birthday", "1990-01-01");
|
|
|
+ mockResult.put("record", recordData);
|
|
|
+ mockResult.put("isNew", true);
|
|
|
+ when(chartService.startConsultation(100L, "测试", "1990-01-01", "事业运势"))
|
|
|
+ .thenReturn(mockResult);
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/consultation/start")
|
|
|
+ .header("Authorization", "Bearer " + userToken)
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content("{\"name\":\"测试\",\"birthday\":\"1990-01-01\",\"questions\":\"事业运势\"}"))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(0))
|
|
|
+ .andExpect(jsonPath("$.data.isNew").value(true))
|
|
|
+ .andExpect(jsonPath("$.data.record.id").value(1));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testConsultationCheck_exists() throws Exception {
|
|
|
+ when(chartService.checkExists(100L, "1990-01-01")).thenReturn(true);
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/consultation/check")
|
|
|
+ .header("Authorization", "Bearer " + userToken)
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content("{\"birthday\":\"1990-01-01\"}"))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(0))
|
|
|
+ .andExpect(jsonPath("$.data.exists").value(true));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testConsultationCheck_notExists() throws Exception {
|
|
|
+ when(chartService.checkExists(100L, "1995-05-05")).thenReturn(false);
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/consultation/check")
|
|
|
+ .header("Authorization", "Bearer " + userToken)
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content("{\"birthday\":\"1995-05-05\"}"))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(0))
|
|
|
+ .andExpect(jsonPath("$.data.exists").value(false));
|
|
|
+ }
|
|
|
}
|