|
|
@@ -1,381 +0,0 @@
|
|
|
-package com.etotem.cfc.controller;
|
|
|
-
|
|
|
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
-import com.etotem.cfc.common.Result;
|
|
|
-import com.etotem.cfc.controller.EnergyController;
|
|
|
-import com.etotem.cfc.dto.EnergySandboxDTO;
|
|
|
-import com.etotem.cfc.dto.MemberEnergyDTO;
|
|
|
-import com.etotem.cfc.entity.EnergyLog;
|
|
|
-import com.etotem.cfc.entity.User;
|
|
|
-import com.etotem.cfc.mapper.UserMapper;
|
|
|
-import com.etotem.cfc.service.EnergyService;
|
|
|
-import org.junit.jupiter.api.Test;
|
|
|
-import org.springframework.boot.test.context.SpringBootTest;
|
|
|
-import org.springframework.boot.test.mock.mockito.MockBean;
|
|
|
-
|
|
|
-import javax.annotation.Resource;
|
|
|
-import java.util.*;
|
|
|
-
|
|
|
-import static org.junit.jupiter.api.Assertions.*;
|
|
|
-import static org.mockito.ArgumentMatchers.*;
|
|
|
-import static org.mockito.Mockito.*;
|
|
|
-
|
|
|
-/**
|
|
|
- * P1-能量测试:五维能量沙盘/概览/流水/配置
|
|
|
- * 覆盖 EnergyController 全链路
|
|
|
- */
|
|
|
-@SpringBootTest
|
|
|
-public class EnergyControllerTest {
|
|
|
-
|
|
|
- @Resource
|
|
|
- private EnergyController controller;
|
|
|
-
|
|
|
- @MockBean
|
|
|
- private EnergyService energyService;
|
|
|
-
|
|
|
- @MockBean
|
|
|
- private UserMapper userMapper;
|
|
|
-
|
|
|
- // ===== 家庭能量沙盘 =====
|
|
|
-
|
|
|
- @Test
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- public void getFamilyEnergySandbox_Success() {
|
|
|
- Long userId = 100L;
|
|
|
- User user = new User();
|
|
|
- user.setId(userId);
|
|
|
- user.setFamilyId(100L);
|
|
|
- when(userMapper.selectById(userId)).thenReturn(user);
|
|
|
-
|
|
|
- Map<String, Object> mockData = new HashMap<>();
|
|
|
- mockData.put("overallScore", 850);
|
|
|
- mockData.put("bodyScore", 180);
|
|
|
- mockData.put("mindScore", 160);
|
|
|
- mockData.put("wisdomScore", 150);
|
|
|
- mockData.put("actionScore", 200);
|
|
|
- mockData.put("wealthScore", 160);
|
|
|
- mockData.put("members", java.util.Collections.emptyList());
|
|
|
-
|
|
|
- when(energyService.calculateFamilyEnergy(100L)).thenReturn(mockData);
|
|
|
-
|
|
|
- Result<?> result = controller.getFamilyEnergySandbox(userId, null);
|
|
|
-
|
|
|
- assertEquals(200, result.getCode());
|
|
|
- assertNotNull(result.getData());
|
|
|
- }
|
|
|
- @Test
|
|
|
- public void getFamilyEnergySandbox_UserNotFound() {
|
|
|
- when(userMapper.selectById(999L)).thenReturn(null);
|
|
|
-
|
|
|
- Result<?> result = controller.getFamilyEnergySandbox(999L, null);
|
|
|
-
|
|
|
- assertEquals(500, result.getCode());
|
|
|
- assertEquals("用户不存在", result.getMessage());
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void getFamilyEnergySandbox_NoFamily() {
|
|
|
- User user = new User();
|
|
|
- user.setId(1L);
|
|
|
- user.setFamilyId(null);
|
|
|
-
|
|
|
- when(userMapper.selectById(1L)).thenReturn(user);
|
|
|
-
|
|
|
- Result<?> result = controller.getFamilyEnergySandbox(1L, null);
|
|
|
-
|
|
|
- assertEquals(500, result.getCode());
|
|
|
- assertEquals("用户未加入家庭", result.getMessage());
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void getFamilyEnergySandbox_ZeroDimensions() {
|
|
|
- Long userId = 1L;
|
|
|
- User user = new User();
|
|
|
- user.setId(userId);
|
|
|
- user.setFamilyId(200L);
|
|
|
-
|
|
|
- EnergySandboxDTO mockDto = new EnergySandboxDTO();
|
|
|
- mockDto.setBodyScore(0);
|
|
|
- mockDto.setMindScore(0);
|
|
|
- mockDto.setWisdomScore(0);
|
|
|
- mockDto.setActionScore(0);
|
|
|
- mockDto.setWealthScore(0);
|
|
|
- mockDto.setOverallScore(0);
|
|
|
-
|
|
|
- when(userMapper.selectById(userId)).thenReturn(user);
|
|
|
- when(energyService.calculateFamilyEnergy(200L)).thenReturn(mockDto);
|
|
|
-
|
|
|
- Result<?> result = controller.getFamilyEnergySandbox(userId, null);
|
|
|
-
|
|
|
- assertEquals(200, result.getCode());
|
|
|
- }
|
|
|
-
|
|
|
- // ===== 五维能量概览 =====
|
|
|
-
|
|
|
- @Test
|
|
|
- public void getEnergyOverview_Success() {
|
|
|
- Map<String, Object> params = new HashMap<>();
|
|
|
- params.put("childId", 10L);
|
|
|
-
|
|
|
- Map<String, Object> mockOverview = new LinkedHashMap<>();
|
|
|
- mockOverview.put("childId", 10L);
|
|
|
- mockOverview.put("childName", "小明");
|
|
|
- mockOverview.put("totalEnergy", 850);
|
|
|
- mockOverview.put("bodyScore", 180);
|
|
|
- mockOverview.put("mindScore", 160);
|
|
|
- mockOverview.put("wisdomScore", 150);
|
|
|
- mockOverview.put("actionScore", 200);
|
|
|
- mockOverview.put("wealthScore", 160);
|
|
|
-
|
|
|
- when(energyService.getOverview(10L)).thenReturn(mockOverview);
|
|
|
-
|
|
|
- Result<Map<String, Object>> result = controller.getEnergyOverview(params, 10L, null);
|
|
|
-
|
|
|
- assertEquals(200, result.getCode());
|
|
|
- assertNotNull(result.getData());
|
|
|
- assertEquals(10L, result.getData().get("childId"));
|
|
|
- assertEquals("小明", result.getData().get("childName"));
|
|
|
- assertEquals(850, result.getData().get("totalEnergy"));
|
|
|
- assertEquals(180, result.getData().get("bodyScore"));
|
|
|
- assertEquals(160, result.getData().get("mindScore"));
|
|
|
- assertEquals(150, result.getData().get("wisdomScore"));
|
|
|
- assertEquals(200, result.getData().get("actionScore"));
|
|
|
- assertEquals(160, result.getData().get("wealthScore"));
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void getEnergyOverview_AllFiveDimensionsPresent() {
|
|
|
- Map<String, Object> params = new HashMap<>();
|
|
|
- params.put("childId", 20L);
|
|
|
-
|
|
|
- Map<String, Object> mockOverview = new HashMap<>();
|
|
|
- mockOverview.put("childId", 20L);
|
|
|
- mockOverview.put("bodyScore", 100);
|
|
|
- mockOverview.put("mindScore", 100);
|
|
|
- mockOverview.put("wisdomScore", 100);
|
|
|
- mockOverview.put("actionScore", 100);
|
|
|
- mockOverview.put("wealthScore", 100);
|
|
|
- mockOverview.put("totalEnergy", 500);
|
|
|
-
|
|
|
- when(energyService.getOverview(20L)).thenReturn(mockOverview);
|
|
|
-
|
|
|
- Result<Map<String, Object>> result = controller.getEnergyOverview(params, 20L, null);
|
|
|
-
|
|
|
- assertEquals(200, result.getCode());
|
|
|
- assertEquals(100, result.getData().get("bodyScore"));
|
|
|
- assertEquals(100, result.getData().get("mindScore"));
|
|
|
- assertEquals(100, result.getData().get("wisdomScore"));
|
|
|
- assertEquals(100, result.getData().get("actionScore"));
|
|
|
- assertEquals(100, result.getData().get("wealthScore"));
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void getEnergyOverview_NullChildId() {
|
|
|
- Map<String, Object> params = new HashMap<>();
|
|
|
-
|
|
|
- Result<Map<String, Object>> result = controller.getEnergyOverview(params, null, null);
|
|
|
-
|
|
|
- assertEquals(500, result.getCode());
|
|
|
- assertEquals("childId不能为空", result.getMessage());
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void getEnergyOverview_ZeroScore() {
|
|
|
- Map<String, Object> params = new HashMap<>();
|
|
|
- params.put("childId", 30L);
|
|
|
-
|
|
|
- Map<String, Object> mockOverview = new HashMap<>();
|
|
|
- mockOverview.put("childId", 30L);
|
|
|
- mockOverview.put("totalEnergy", 0);
|
|
|
-
|
|
|
- when(energyService.getOverview(30L)).thenReturn(mockOverview);
|
|
|
-
|
|
|
- Result<Map<String, Object>> result = controller.getEnergyOverview(params, 30L, null);
|
|
|
-
|
|
|
- assertEquals(200, result.getCode());
|
|
|
- assertEquals(0, result.getData().get("totalEnergy"));
|
|
|
- }
|
|
|
-
|
|
|
- // ===== 能量流水 =====
|
|
|
-
|
|
|
- @Test
|
|
|
- public void getEnergyLogs_Success() {
|
|
|
- Map<String, Object> params = new HashMap<>();
|
|
|
- params.put("childId", 10L);
|
|
|
- params.put("page", 1);
|
|
|
- params.put("size", 10);
|
|
|
-
|
|
|
- List<EnergyLog> logs = Arrays.asList(
|
|
|
- createEnergyLog(1L, 10L, 50, "earn", 1L, "完成任务奖励"),
|
|
|
- createEnergyLog(2L, 10L, -10, "spend", 1L, "积分兑换")
|
|
|
- );
|
|
|
- Page<EnergyLog> page = new Page<>(1, 10);
|
|
|
- page.setRecords(logs);
|
|
|
- page.setTotal(2);
|
|
|
-
|
|
|
- when(energyService.getLogs(10L, null, 1, 10)).thenReturn(page);
|
|
|
-
|
|
|
- Result<Page<EnergyLog>> result = controller.getEnergyLogs(params, 10L, null);
|
|
|
-
|
|
|
- assertEquals(200, result.getCode());
|
|
|
- assertNotNull(result.getData());
|
|
|
- assertEquals(2, result.getData().getRecords().size());
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void getEnergyLogs_FilterByDimension() {
|
|
|
- Map<String, Object> params = new HashMap<>();
|
|
|
- params.put("childId", 10L);
|
|
|
- params.put("dimensionCode", "body");
|
|
|
- params.put("page", 1);
|
|
|
- params.put("size", 10);
|
|
|
-
|
|
|
- List<EnergyLog> logs = Collections.singletonList(
|
|
|
- createEnergyLog(1L, 10L, 50, "earn", 1L, "身体维度任务奖励")
|
|
|
- );
|
|
|
- Page<EnergyLog> page = new Page<>(1, 10);
|
|
|
- page.setRecords(logs);
|
|
|
- page.setTotal(1);
|
|
|
-
|
|
|
- when(energyService.getLogs(10L, "body", 1, 10)).thenReturn(page);
|
|
|
-
|
|
|
- Result<Page<EnergyLog>> result = controller.getEnergyLogs(params, 10L, null);
|
|
|
-
|
|
|
- assertEquals(200, result.getCode());
|
|
|
- assertEquals(1, result.getData().getTotal());
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void getEnergyLogs_DefaultPagination() {
|
|
|
- Map<String, Object> params = new HashMap<>();
|
|
|
- params.put("childId", 10L);
|
|
|
- // page and size not provided
|
|
|
-
|
|
|
- Page<EnergyLog> emptyPage = new Page<>(1, 10);
|
|
|
- emptyPage.setRecords(Collections.emptyList());
|
|
|
- emptyPage.setTotal(0);
|
|
|
-
|
|
|
- when(energyService.getLogs(10L, null, 1, 10)).thenReturn(emptyPage);
|
|
|
-
|
|
|
- Result<Page<EnergyLog>> result = controller.getEnergyLogs(params, 10L, null);
|
|
|
-
|
|
|
- assertEquals(200, result.getCode());
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void getEnergyLogs_NullChildId() {
|
|
|
- Map<String, Object> params = new HashMap<>();
|
|
|
-
|
|
|
- Result<Page<EnergyLog>> result = controller.getEnergyLogs(params, null, null);
|
|
|
-
|
|
|
- assertEquals(500, result.getCode());
|
|
|
- assertEquals("childId不能为空", result.getMessage());
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void getEnergyLogs_EmptyResult() {
|
|
|
- Map<String, Object> params = new HashMap<>();
|
|
|
- params.put("childId", 99L);
|
|
|
- params.put("page", 1);
|
|
|
- params.put("size", 10);
|
|
|
-
|
|
|
- Page<EnergyLog> emptyPage = new Page<>(1, 10);
|
|
|
- emptyPage.setRecords(Collections.emptyList());
|
|
|
- emptyPage.setTotal(0);
|
|
|
-
|
|
|
- when(energyService.getLogs(99L, null, 1, 10)).thenReturn(emptyPage);
|
|
|
-
|
|
|
- Result<Page<EnergyLog>> result = controller.getEnergyLogs(params, 99L, null);
|
|
|
-
|
|
|
- assertEquals(200, result.getCode());
|
|
|
- assertEquals(0, result.getData().getTotal());
|
|
|
- }
|
|
|
-
|
|
|
- // ===== 配置来源比例 =====
|
|
|
-
|
|
|
- @Test
|
|
|
- public void configureSourceRatios_Success() {
|
|
|
- Map<String, Object> params = new HashMap<>();
|
|
|
- params.put("sourceType", "task");
|
|
|
- params.put("sourceId", 5L);
|
|
|
- params.put("sourceName", "每日任务");
|
|
|
- params.put("ratios", Arrays.asList(
|
|
|
- new HashMap<String, Object>() {{ put("dimension", "body"); put("ratio", 0.2); }},
|
|
|
- new HashMap<String, Object>() {{ put("dimension", "mind"); put("ratio", 0.3); }},
|
|
|
- new HashMap<String, Object>() {{ put("dimension", "wisdom"); put("ratio", 0.2); }},
|
|
|
- new HashMap<String, Object>() {{ put("dimension", "action"); put("ratio", 0.2); }},
|
|
|
- new HashMap<String, Object>() {{ put("dimension", "wealth"); put("ratio", 0.1); }}
|
|
|
- ));
|
|
|
-
|
|
|
- Result<String> result = controller.configureSourceRatios(params, 1L, null);
|
|
|
-
|
|
|
- assertEquals(200, result.getCode());
|
|
|
- assertEquals("配置成功", result.getData());
|
|
|
- verify(energyService).configureSourceRatios(eq("task"), eq(5L), eq("每日任务"), anyList());
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void configureSourceRatios_MissingSourceType() {
|
|
|
- Map<String, Object> params = new HashMap<>();
|
|
|
- params.put("sourceId", 5L);
|
|
|
- params.put("ratios", Collections.emptyList());
|
|
|
-
|
|
|
- Result<String> result = controller.configureSourceRatios(params, 1L, null);
|
|
|
-
|
|
|
- assertEquals(500, result.getCode());
|
|
|
- assertEquals("sourceType和sourceId不能为空", result.getMessage());
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void configureSourceRatios_EmptyRatios() {
|
|
|
- Map<String, Object> params = new HashMap<>();
|
|
|
- params.put("sourceType", "task");
|
|
|
- params.put("sourceId", 5L);
|
|
|
- params.put("ratios", Collections.emptyList());
|
|
|
-
|
|
|
- Result<String> result = controller.configureSourceRatios(params, 1L, null);
|
|
|
-
|
|
|
- assertEquals(500, result.getCode());
|
|
|
- assertEquals("ratios不能为空", result.getMessage());
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void configureSourceRatios_NullRatios() {
|
|
|
- Map<String, Object> params = new HashMap<>();
|
|
|
- params.put("sourceType", "game");
|
|
|
- params.put("sourceId", 3L);
|
|
|
- // ratios not provided
|
|
|
-
|
|
|
- Result<String> result = controller.configureSourceRatios(params, 1L, null);
|
|
|
-
|
|
|
- assertEquals(500, result.getCode());
|
|
|
- assertEquals("ratios不能为空", result.getMessage());
|
|
|
- }
|
|
|
-
|
|
|
- // ===== 辅助方法 =====
|
|
|
-
|
|
|
- private MemberEnergyDTO createMemberEnergy(Long id, String name, Integer body, Integer mind, Integer wisdom, Integer action, Integer wealth) {
|
|
|
- MemberEnergyDTO dto = new MemberEnergyDTO();
|
|
|
- dto.setMemberId(id);
|
|
|
- dto.setName(name);
|
|
|
- dto.setBodyScore(body);
|
|
|
- dto.setMindScore(mind);
|
|
|
- dto.setWisdomScore(wisdom);
|
|
|
- dto.setActionScore(action);
|
|
|
- dto.setWealthScore(wealth);
|
|
|
- dto.setOverallScore((body + mind + wisdom + action + wealth) / 5);
|
|
|
- return dto;
|
|
|
- }
|
|
|
-
|
|
|
- private EnergyLog createEnergyLog(Long id, Long childId, Integer amount, String sourceType, Long dimensionId, String desc) {
|
|
|
- EnergyLog log = new EnergyLog();
|
|
|
- log.setId(id);
|
|
|
- log.setChildId(childId);
|
|
|
- log.setAmount(amount);
|
|
|
- log.setSourceType(sourceType);
|
|
|
- log.setDimensionId(dimensionId);
|
|
|
- log.setDescription(desc);
|
|
|
- log.setCreatedAt(new Date());
|
|
|
- return log;
|
|
|
- }
|
|
|
-}
|