|
|
@@ -0,0 +1,159 @@
|
|
|
+package com.etotem.cfc.controller.admin;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
|
|
+import com.etotem.cfc.entity.InviteMilestoneTemplate;
|
|
|
+import com.etotem.cfc.mapper.InviteMilestoneTemplateMapper;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import org.apache.ibatis.builder.MapperBuilderAssistant;
|
|
|
+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 java.util.Arrays;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.ArgumentMatchers.eq;
|
|
|
+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.jsonPath;
|
|
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
|
+
|
|
|
+/**
|
|
|
+ * AdminMilestoneController 单元测试(MockMvc,验证 T4-C 接口逻辑):
|
|
|
+ * <ul>
|
|
|
+ * <li>POST /api/admin/milestone/template 返回 3 行模板</li>
|
|
|
+ * <li>POST /api/admin/milestone/template/save 更新 reward_points/reward_energy 落库</li>
|
|
|
+ * <li>POST /api/admin/milestone/config(兼容旧接口)与 template 同值</li>
|
|
|
+ * <li>POST /api/admin/milestone/config/update(兼容旧接口)走同一保存逻辑</li>
|
|
|
+ * </ul>
|
|
|
+ */
|
|
|
+class AdminMilestoneControllerTest {
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ private InviteMilestoneTemplateMapper templateMapper;
|
|
|
+
|
|
|
+ @InjectMocks
|
|
|
+ private AdminMilestoneController controller;
|
|
|
+
|
|
|
+ private MockMvc mockMvc;
|
|
|
+ private ObjectMapper om = new ObjectMapper();
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() {
|
|
|
+ MockitoAnnotations.openMocks(this);
|
|
|
+ TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), InviteMilestoneTemplate.class);
|
|
|
+ mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
|
|
|
+ }
|
|
|
+
|
|
|
+ private InviteMilestoneTemplate tpl(String milestone, int target, int points, int energy, int enabled) {
|
|
|
+ InviteMilestoneTemplate t = new InviteMilestoneTemplate();
|
|
|
+ t.setMilestone(milestone);
|
|
|
+ t.setTargetCount(target);
|
|
|
+ t.setRewardPoints(points);
|
|
|
+ t.setRewardEnergy(energy);
|
|
|
+ t.setEnabled(enabled);
|
|
|
+ t.setSortOrder(target);
|
|
|
+ return t;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void template_列表返回3行() throws Exception {
|
|
|
+ when(templateMapper.selectList(any())).thenReturn(Arrays.asList(
|
|
|
+ tpl("INVITE_3", 3, 200, 20, 1),
|
|
|
+ tpl("INVITE_5", 5, 500, 50, 1),
|
|
|
+ tpl("INVITE_10", 10, 1000, 100, 1)
|
|
|
+ ));
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/admin/milestone/template")
|
|
|
+ .requestAttr("role", "admin")
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content("{}"))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(200))
|
|
|
+ .andExpect(jsonPath("$.data.length()").value(3))
|
|
|
+ .andExpect(jsonPath("$.data[0].milestone").value("INVITE_3"))
|
|
|
+ .andExpect(jsonPath("$.data[0].rewardPoints").value(200))
|
|
|
+ .andExpect(jsonPath("$.data[1].rewardPoints").value(500))
|
|
|
+ .andExpect(jsonPath("$.data[2].rewardPoints").value(1000));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void config_兼容旧接口同值() throws Exception {
|
|
|
+ when(templateMapper.selectList(any())).thenReturn(Arrays.asList(
|
|
|
+ tpl("INVITE_3", 3, 200, 20, 1),
|
|
|
+ tpl("INVITE_5", 5, 500, 50, 1)
|
|
|
+ ));
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/admin/milestone/config")
|
|
|
+ .requestAttr("role", "admin")
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content("{}"))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.data.length()").value(2));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void templateSave_更新rewardPoints落库() throws Exception {
|
|
|
+ // 第一次调用返回模板(模拟存在)
|
|
|
+ InviteMilestoneTemplate existing = tpl("INVITE_5", 5, 500, 50, 1);
|
|
|
+ when(templateMapper.selectList(any())).thenReturn(Arrays.asList(existing));
|
|
|
+ // update 返回 1 行受影响
|
|
|
+ when(templateMapper.update(any(), any())).thenReturn(1);
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/admin/milestone/template/save")
|
|
|
+ .requestAttr("role", "admin")
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content(om.writeValueAsString(
|
|
|
+ map("milestone", "INVITE_5", "rewardPoints", 600, "rewardEnergy", 60))))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(200))
|
|
|
+ .andExpect(jsonPath("$.data").value("更新成功"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void configUpdate_兼容旧接口同保存逻辑() throws Exception {
|
|
|
+ when(templateMapper.selectList(any())).thenReturn(Arrays.asList(tpl("INVITE_3", 3, 200, 20, 1)));
|
|
|
+ when(templateMapper.update(any(), any())).thenReturn(1);
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/admin/milestone/config/update")
|
|
|
+ .requestAttr("role", "admin")
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content(om.writeValueAsString(
|
|
|
+ map("milestone", "INVITE_3", "rewardPoints", 300, "rewardEnergy", 30))))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(200))
|
|
|
+ .andExpect(jsonPath("$.data").value("更新成功"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void templateSave_模板不存在返回错误() throws Exception {
|
|
|
+ when(templateMapper.selectList(any())).thenReturn(Arrays.asList());
|
|
|
+ when(templateMapper.update(any(), any())).thenReturn(0);
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/admin/milestone/template/save")
|
|
|
+ .requestAttr("role", "admin")
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content(om.writeValueAsString(
|
|
|
+ map("milestone", "INVITE_3", "rewardPoints", 100, "rewardEnergy", 10))))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(500))
|
|
|
+ .andExpect(jsonPath("$.message").value("模板不存在: INVITE_3"));
|
|
|
+ }
|
|
|
+
|
|
|
+ /** Java 8 兼容:构造单级 Map(值会自动装箱) */
|
|
|
+ private Map<String, Object> map(Object... kvs) {
|
|
|
+ Map<String, Object> m = new HashMap<>();
|
|
|
+ for (int i = 0; i + 1 < kvs.length; i += 2) {
|
|
|
+ m.put(String.valueOf(kvs[i]), kvs[i + 1]);
|
|
|
+ }
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+}
|