|
|
@@ -0,0 +1,124 @@
|
|
|
+package com.etotem.cfc.service;
|
|
|
+
|
|
|
+import com.etotem.cfc.dto.FamilyWorkbenchCard;
|
|
|
+import com.etotem.cfc.dto.FamilyWorkbenchDTO;
|
|
|
+import com.etotem.cfc.entity.Family;
|
|
|
+import com.etotem.cfc.mapper.FamilyMapper;
|
|
|
+import com.etotem.cfc.mapper.FamilyWorkbenchMapper;
|
|
|
+import com.etotem.cfc.service.impl.FamilyWorkbenchServiceImpl;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.mockito.ArgumentCaptor;
|
|
|
+import org.mockito.InjectMocks;
|
|
|
+import org.mockito.Mock;
|
|
|
+import org.mockito.MockitoAnnotations;
|
|
|
+
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.ArgumentMatchers.anyString;
|
|
|
+import static org.mockito.ArgumentMatchers.eq;
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 单家庭工作台聚合服务契约:家庭不存在报错、6 卡齐全、totals 与序列正确、查询带正确 familyId/起始日。
|
|
|
+ */
|
|
|
+class FamilyWorkbenchServiceImplTest {
|
|
|
+
|
|
|
+ @Mock private FamilyMapper familyMapper;
|
|
|
+ @Mock private FamilyWorkbenchMapper workbenchMapper;
|
|
|
+ @InjectMocks private FamilyWorkbenchServiceImpl service;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() {
|
|
|
+ MockitoAnnotations.openMocks(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> row(String d, long c) {
|
|
|
+ Map<String, Object> m = new HashMap<>();
|
|
|
+ m.put("d", d);
|
|
|
+ m.put("c", c);
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Family family(Long id) {
|
|
|
+ Family f = new Family();
|
|
|
+ f.setId(id);
|
|
|
+ f.setName("测试家庭");
|
|
|
+ f.setInviteCode("TEST123");
|
|
|
+ return f;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 家庭不存在_抛业务异常() {
|
|
|
+ when(familyMapper.selectById(999L)).thenReturn(null);
|
|
|
+ RuntimeException e = assertThrows(RuntimeException.class, () -> service.aggregate(999L));
|
|
|
+ assertTrue(e.getMessage().contains("家庭不存在"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 聚合返回6卡_总额序列正确() {
|
|
|
+ when(familyMapper.selectById(5L)).thenReturn(family(5L));
|
|
|
+ when(workbenchMapper.countReports(5L)).thenReturn(7L);
|
|
|
+ when(workbenchMapper.countPlans(5L)).thenReturn(3L);
|
|
|
+ when(workbenchMapper.countTasks(5L)).thenReturn(9L);
|
|
|
+ when(workbenchMapper.countConversations(5L)).thenReturn(2L);
|
|
|
+ when(workbenchMapper.countCoupons(5L)).thenReturn(4L);
|
|
|
+ when(workbenchMapper.cfBalance(5L)).thenReturn(100L);
|
|
|
+ when(workbenchMapper.reportDailySeries(eq(5L), anyString())).thenReturn(List.of(row("2026-10-20", 2L), row("2026-10-20", 1L)));
|
|
|
+ when(workbenchMapper.planDailySeries(eq(5L), anyString())).thenReturn(new ArrayList<>());
|
|
|
+ when(workbenchMapper.taskDailySeries(eq(5L), anyString())).thenReturn(new ArrayList<>());
|
|
|
+ when(workbenchMapper.conversationDailySeries(eq(5L), anyString())).thenReturn(new ArrayList<>());
|
|
|
+ when(workbenchMapper.couponDailySeries(eq(5L), anyString())).thenReturn(new ArrayList<>());
|
|
|
+ when(workbenchMapper.cfDailySeries(eq(5L), anyString())).thenReturn(new ArrayList<>());
|
|
|
+
|
|
|
+ FamilyWorkbenchDTO dto = service.aggregate(5L);
|
|
|
+
|
|
|
+ assertEquals("测试家庭", dto.getFamily().get("name"));
|
|
|
+ assertEquals(6, dto.getCards().size());
|
|
|
+
|
|
|
+ FamilyWorkbenchCard reports = dto.getCards().get(0);
|
|
|
+ assertEquals("reports", reports.getKey());
|
|
|
+ assertEquals(7L, reports.getTotal());
|
|
|
+ assertEquals(30, reports.getSeries().size());
|
|
|
+ FamilyWorkbenchCard cf = dto.getCards().get(4);
|
|
|
+ assertEquals("cf", cf.getKey());
|
|
|
+ assertEquals(100L, cf.getTotal());
|
|
|
+
|
|
|
+ List<String> keys = dto.getCards().stream().map(FamilyWorkbenchCard::getKey).toList();
|
|
|
+ assertEquals(List.of("reports", "plans", "tasks", "messages", "cf", "coupons"), keys);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 聚合查询携带正确familyId与30天起始日() {
|
|
|
+ when(familyMapper.selectById(5L)).thenReturn(family(5L));
|
|
|
+ when(workbenchMapper.countReports(any())).thenReturn(0L);
|
|
|
+ when(workbenchMapper.countPlans(any())).thenReturn(0L);
|
|
|
+ when(workbenchMapper.countTasks(any())).thenReturn(0L);
|
|
|
+ when(workbenchMapper.countConversations(any())).thenReturn(0L);
|
|
|
+ when(workbenchMapper.countCoupons(any())).thenReturn(0L);
|
|
|
+ when(workbenchMapper.cfBalance(any())).thenReturn(0L);
|
|
|
+ when(workbenchMapper.reportDailySeries(any(), any())).thenReturn(new ArrayList<>());
|
|
|
+ when(workbenchMapper.planDailySeries(any(), any())).thenReturn(new ArrayList<>());
|
|
|
+ when(workbenchMapper.taskDailySeries(any(), any())).thenReturn(new ArrayList<>());
|
|
|
+ when(workbenchMapper.conversationDailySeries(any(), any())).thenReturn(new ArrayList<>());
|
|
|
+ when(workbenchMapper.couponDailySeries(any(), any())).thenReturn(new ArrayList<>());
|
|
|
+ when(workbenchMapper.cfDailySeries(any(), any())).thenReturn(new ArrayList<>());
|
|
|
+
|
|
|
+ service.aggregate(5L);
|
|
|
+
|
|
|
+ verify(workbenchMapper).countReports(5L);
|
|
|
+ verify(workbenchMapper).conversationDailySeries(eq(5L), anyString());
|
|
|
+ ArgumentCaptor<String> from = ArgumentCaptor.forClass(String.class);
|
|
|
+ verify(workbenchMapper).cfDailySeries(eq(5L), from.capture());
|
|
|
+ assertEquals(LocalDate.now().minusDays(29).toString(), from.getValue());
|
|
|
+ }
|
|
|
+}
|