|
|
@@ -0,0 +1,139 @@
|
|
|
+package com.etotem.cfc.controller.admin;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
+import com.etotem.cfc.entity.ButlerCommissionSettlement;
|
|
|
+import com.etotem.cfc.entity.ButlerProfile;
|
|
|
+import com.etotem.cfc.entity.ButlerServiceRecord;
|
|
|
+import com.etotem.cfc.entity.User;
|
|
|
+import com.etotem.cfc.mapper.ButlerCommissionSettlementMapper;
|
|
|
+import com.etotem.cfc.mapper.ButlerProfileMapper;
|
|
|
+import com.etotem.cfc.mapper.ButlerServiceRecordMapper;
|
|
|
+import com.etotem.cfc.mapper.UserMapper;
|
|
|
+import com.etotem.cfc.service.ButlerService;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.springframework.test.util.ReflectionTestUtils;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNull;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * B-4/F1 契约级单测:管理端管家列表 envelope 必须为 data.records(与其他管理端列表统一),
|
|
|
+ * 不再返回 data.list。覆盖 user-list / list / service-records / settlements 四个列表方法。
|
|
|
+ *
|
|
|
+ * 背景:自动化测试断言 j.data.records,butler 接口此前返回 data.list 导致取空崩溃(F1)。
|
|
|
+ */
|
|
|
+public class AdminButlerControllerEnvelopeTest {
|
|
|
+
|
|
|
+ private AdminButlerController controller;
|
|
|
+ private ButlerService butlerService;
|
|
|
+ private ButlerProfileMapper butlerProfileMapper;
|
|
|
+ private ButlerServiceRecordMapper butlerServiceRecordMapper;
|
|
|
+ private ButlerCommissionSettlementMapper butlerCommissionSettlementMapper;
|
|
|
+ private UserMapper userMapper;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ public void setUp() {
|
|
|
+ controller = new AdminButlerController();
|
|
|
+ butlerService = mock(ButlerService.class);
|
|
|
+ butlerProfileMapper = mock(ButlerProfileMapper.class);
|
|
|
+ butlerServiceRecordMapper = mock(ButlerServiceRecordMapper.class);
|
|
|
+ butlerCommissionSettlementMapper = mock(ButlerCommissionSettlementMapper.class);
|
|
|
+ userMapper = mock(UserMapper.class);
|
|
|
+ ReflectionTestUtils.setField(controller, "butlerService", butlerService);
|
|
|
+ ReflectionTestUtils.setField(controller, "butlerProfileMapper", butlerProfileMapper);
|
|
|
+ ReflectionTestUtils.setField(controller, "butlerServiceRecordMapper", butlerServiceRecordMapper);
|
|
|
+ ReflectionTestUtils.setField(controller, "butlerCommissionSettlementMapper", butlerCommissionSettlementMapper);
|
|
|
+ ReflectionTestUtils.setField(controller, "userMapper", userMapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> baseParams() {
|
|
|
+ Map<String, Object> p = new HashMap<>();
|
|
|
+ p.put("page", 1);
|
|
|
+ p.put("size", 10);
|
|
|
+ return p;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void assertRecordsEnvelope(Result<Map<String, Object>> r, int expectTotal) {
|
|
|
+ assertEquals(200, r.getCode());
|
|
|
+ assertNotNull(r.getData(), "data 不能为 null");
|
|
|
+ assertNotNull(r.getData().get("records"), "列表 envelope 必须包含 data.records");
|
|
|
+ assertNull(r.getData().get("list"), "列表 envelope 不得再返回 data.list(契约统一为 records)");
|
|
|
+ assertEquals(expectTotal, ((Number) r.getData().get("total")).longValue());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void userList_returnsRecordsEnvelope() {
|
|
|
+ Page<User> page = new Page<>(1, 10);
|
|
|
+ User u = new User();
|
|
|
+ u.setId(1001L);
|
|
|
+ u.setNickname("管家A");
|
|
|
+ u.setButlerStatus("pending");
|
|
|
+ page.setRecords(java.util.Collections.singletonList(u));
|
|
|
+ page.setTotal(1);
|
|
|
+ when(userMapper.selectPage(any(Page.class), any())).thenReturn(page);
|
|
|
+
|
|
|
+ Map<String, Object> params = baseParams();
|
|
|
+ params.put("status", "pending");
|
|
|
+ Result<Map<String, Object>> r = controller.userList(params, "admin");
|
|
|
+ assertRecordsEnvelope(r, 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void list_returnsRecordsEnvelope() {
|
|
|
+ Page<ButlerProfile> page = new Page<>(1, 10);
|
|
|
+ ButlerProfile bp = new ButlerProfile();
|
|
|
+ bp.setId(2001L);
|
|
|
+ bp.setUserId(1001L);
|
|
|
+ bp.setTier("L1");
|
|
|
+ bp.setStatus("pending");
|
|
|
+ page.setRecords(java.util.Collections.singletonList(bp));
|
|
|
+ page.setTotal(1);
|
|
|
+ when(butlerProfileMapper.selectPage(any(Page.class), any())).thenReturn(page);
|
|
|
+
|
|
|
+ Map<String, Object> params = baseParams();
|
|
|
+ params.put("status", "pending");
|
|
|
+ Result<Map<String, Object>> r = controller.list(params, "admin");
|
|
|
+ assertRecordsEnvelope(r, 1);
|
|
|
+ assertEquals(1, ((Number) r.getData().get("page")).intValue());
|
|
|
+ assertEquals(10, ((Number) r.getData().get("size")).intValue());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void serviceRecords_returnsRecordsEnvelope() {
|
|
|
+ Page<ButlerServiceRecord> page = new Page<>(1, 10);
|
|
|
+ ButlerServiceRecord rec = new ButlerServiceRecord();
|
|
|
+ rec.setId(3001L);
|
|
|
+ rec.setServiceType("annual");
|
|
|
+ rec.setCreatedAt(new Date());
|
|
|
+ page.setRecords(java.util.Collections.singletonList(rec));
|
|
|
+ page.setTotal(1);
|
|
|
+ when(butlerServiceRecordMapper.selectPage(any(Page.class), any())).thenReturn(page);
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = controller.serviceRecords(baseParams(), "admin");
|
|
|
+ assertRecordsEnvelope(r, 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void settlements_returnsRecordsEnvelope() {
|
|
|
+ Page<ButlerCommissionSettlement> page = new Page<>(1, 10);
|
|
|
+ ButlerCommissionSettlement s = new ButlerCommissionSettlement();
|
|
|
+ s.setId(4001L);
|
|
|
+ s.setStatus("settled");
|
|
|
+ page.setRecords(java.util.Collections.singletonList(s));
|
|
|
+ page.setTotal(1);
|
|
|
+ when(butlerCommissionSettlementMapper.selectPage(any(Page.class), any())).thenReturn(page);
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = controller.settlements(baseParams(), "admin");
|
|
|
+ assertRecordsEnvelope(r, 1);
|
|
|
+ }
|
|
|
+}
|