|
|
@@ -0,0 +1,114 @@
|
|
|
+package com.etotem.cfc.controller;
|
|
|
+
|
|
|
+import com.etotem.cfc.entity.Badge;
|
|
|
+import com.etotem.cfc.entity.MemberBadge;
|
|
|
+import com.etotem.cfc.service.BadgeService;
|
|
|
+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.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.mockito.ArgumentMatchers.anyLong;
|
|
|
+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;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 契约缺口修复单测:POST /api/badges/child/list、POST /api/badges/statistics/child。
|
|
|
+ * 验证孩子勋章列表返回 [{badge, childBadge}]、统计返回 total/active 等字段,
|
|
|
+ * childId 从 body 读取(缺省回落 currentMemberId)。
|
|
|
+ */
|
|
|
+class BadgeControllerTest {
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ private BadgeService badgeService;
|
|
|
+
|
|
|
+ @InjectMocks
|
|
|
+ private BadgeController controller;
|
|
|
+
|
|
|
+ private MockMvc mockMvc;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() {
|
|
|
+ MockitoAnnotations.openMocks(this);
|
|
|
+ mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
|
|
|
+ }
|
|
|
+
|
|
|
+ private Badge badge(Long id, String name) {
|
|
|
+ Badge b = new Badge();
|
|
|
+ b.setId(id);
|
|
|
+ b.setName(name);
|
|
|
+ b.setCategory("学习");
|
|
|
+ b.setIsActive(1);
|
|
|
+ return b;
|
|
|
+ }
|
|
|
+
|
|
|
+ private MemberBadge childBadge(Long badgeId) {
|
|
|
+ MemberBadge cb = new MemberBadge();
|
|
|
+ cb.setBadgeId(badgeId);
|
|
|
+ cb.setStatus(1);
|
|
|
+ cb.setIsFavorite(false);
|
|
|
+ return cb;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void childList_返回badge加childBadge结构() throws Exception {
|
|
|
+ List<Map<String, Object>> data = new ArrayList<>();
|
|
|
+ Map<String, Object> item = new HashMap<>();
|
|
|
+ item.put("badge", badge(1L, "阅读之星"));
|
|
|
+ item.put("childBadge", childBadge(1L));
|
|
|
+ data.add(item);
|
|
|
+ when(badgeService.getChildBadgeList(anyLong())).thenReturn(data);
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/badges/child/list")
|
|
|
+ .requestAttr("currentMemberId", 9L)
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content("{\"childId\":9}"))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(200))
|
|
|
+ .andExpect(jsonPath("$.data[0].badge.name").value("阅读之星"))
|
|
|
+ .andExpect(jsonPath("$.data[0].childBadge.badgeId").value(1));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void childList_缺省回落currentMemberId() throws Exception {
|
|
|
+ when(badgeService.getChildBadgeList(9L)).thenReturn(new ArrayList<>());
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/badges/child/list")
|
|
|
+ .requestAttr("currentMemberId", 9L)
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content("{}"))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(200))
|
|
|
+ .andExpect(jsonPath("$.data").isArray());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void statisticsChild_返回统计字段() throws Exception {
|
|
|
+ Map<String, Object> stats = new HashMap<>();
|
|
|
+ stats.put("total", 3);
|
|
|
+ stats.put("active", 2);
|
|
|
+ stats.put("expired", 1);
|
|
|
+ stats.put("revoked", 0);
|
|
|
+ when(badgeService.getChildBadgeStats(9L)).thenReturn(stats);
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/badges/statistics/child")
|
|
|
+ .requestAttr("currentMemberId", 9L)
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content("{\"childId\":9}"))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(200))
|
|
|
+ .andExpect(jsonPath("$.data.total").value(3))
|
|
|
+ .andExpect(jsonPath("$.data.active").value(2));
|
|
|
+ }
|
|
|
+}
|