|
@@ -0,0 +1,268 @@
|
|
|
|
|
+package com.mangoo.rating.recommend.cart;
|
|
|
|
|
+
|
|
|
|
|
+import com.mangoo.rating.recommend.AjaxResult;
|
|
|
|
|
+import com.mangoo.rating.recommend.config.RecommendProperties;
|
|
|
|
|
+import com.mangoo.rating.recommend.enums.MatchLevelEnum;
|
|
|
|
|
+import com.mangoo.rating.recommend.manager.RatingRecommendRuleManager;
|
|
|
|
|
+import com.mangoo.rating.recommend.po.RatingRecommendRulePO;
|
|
|
|
|
+import com.mangoo.rating.recommend.request.recommend.RecommendCardDTO;
|
|
|
|
|
+import com.mangoo.rating.recommend.request.recommend.RecommendRequest;
|
|
|
|
|
+import com.mangoo.rating.recommend.response.recommend.OrderSuggestionVO;
|
|
|
|
|
+import com.mangoo.rating.recommend.response.recommend.RecommendResultVO;
|
|
|
|
|
+import com.mangoo.rating.recommend.service.impl.RecommendServiceImpl;
|
|
|
|
|
+import com.mangoo.rating.recommend.service.recommend.EfficiencyPriceProvider;
|
|
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
+import org.junit.jupiter.api.extension.ExtendWith;
|
|
|
|
|
+import org.mockito.InjectMocks;
|
|
|
|
|
+import org.mockito.Mock;
|
|
|
|
|
+import org.mockito.junit.jupiter.MockitoExtension;
|
|
|
|
|
+
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.Arrays;
|
|
|
|
|
+import java.util.Collections;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNull;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.anyString;
|
|
|
|
|
+import static org.mockito.Mockito.lenient;
|
|
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * RecommendServiceImpl Mockito 单测(11 项)
|
|
|
|
|
+ *
|
|
|
|
|
+ * 覆盖:入参校验、L1/L2/L3/兜底降级、规则时效非法跳过、价格缺失、
|
|
|
|
|
+ * 多档分组、同档合并、图片透传。
|
|
|
|
|
+ *
|
|
|
|
|
+ * 备注:AjaxResult 同时继承 HashMap 且提供 getCode() Bean 方法,
|
|
|
|
|
+ * 这里统一采用 Map 风格 .get("code") / .get("data") 取值。
|
|
|
|
|
+ *
|
|
|
|
|
+ * @author gengjintao
|
|
|
|
|
+ * @date 2026/06/24
|
|
|
|
|
+ */
|
|
|
|
|
+@ExtendWith(MockitoExtension.class)
|
|
|
|
|
+class RecommendServiceImplTest {
|
|
|
|
|
+
|
|
|
|
|
+ /** 规则查询 Manager Mock */
|
|
|
|
|
+ @Mock
|
|
|
|
|
+ private RatingRecommendRuleManager ruleManager;
|
|
|
|
|
+
|
|
|
|
|
+ /** 价格 Provider Mock */
|
|
|
|
|
+ @Mock
|
|
|
|
|
+ private EfficiencyPriceProvider priceProvider;
|
|
|
|
|
+
|
|
|
|
|
+ /** 配置属性 Mock */
|
|
|
|
|
+ @Mock
|
|
|
|
|
+ private RecommendProperties recommendProperties;
|
|
|
|
|
+
|
|
|
|
|
+ /** 被测对象:按类型注入上述 3 个 Mock */
|
|
|
|
|
+ @InjectMocks
|
|
|
|
|
+ private RecommendServiceImpl recommendService;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 公共桩:默认兜底档=1,上限=50,价格 Map={1:50.00, 2:100.00, 3:200.00}。
|
|
|
|
|
+ * 用 lenient() 避免严格模式下"未使用 stub"告警。
|
|
|
|
|
+ */
|
|
|
|
|
+ @BeforeEach
|
|
|
|
|
+ void setUp() {
|
|
|
|
|
+ lenient().when(recommendProperties.getDefaultEfficiency()).thenReturn(1);
|
|
|
|
|
+ lenient().when(recommendProperties.getBatchMax()).thenReturn(50);
|
|
|
|
|
+ Map<Integer, BigDecimal> defaultPrices = new HashMap<>();
|
|
|
|
|
+ defaultPrices.put(1, new BigDecimal("50.00"));
|
|
|
|
|
+ defaultPrices.put(2, new BigDecimal("100.00"));
|
|
|
|
|
+ defaultPrices.put(3, new BigDecimal("200.00"));
|
|
|
|
|
+ lenient().when(priceProvider.loadEfficiencyPrices()).thenReturn(defaultPrices);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 构造卡入参 DTO 的便捷方法 */
|
|
|
|
|
+ private RecommendCardDTO card(String id, String player, String year, String series, String cardSet) {
|
|
|
|
|
+ RecommendCardDTO d = new RecommendCardDTO();
|
|
|
|
|
+ d.setClientCardId(id);
|
|
|
|
|
+ d.setPlayer(player);
|
|
|
|
|
+ d.setYear(year);
|
|
|
|
|
+ d.setSeries(series);
|
|
|
|
|
+ d.setCardSet(cardSet);
|
|
|
|
|
+ return d;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 构造请求体 */
|
|
|
|
|
+ private RecommendRequest req(RecommendCardDTO... cards) {
|
|
|
|
|
+ RecommendRequest r = new RecommendRequest();
|
|
|
|
|
+ r.setCards(new ArrayList<>(Arrays.asList(cards)));
|
|
|
|
|
+ return r;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 构造规则 PO(只填推荐时效字段即可) */
|
|
|
|
|
+ private RatingRecommendRulePO rule(Integer eff) {
|
|
|
|
|
+ RatingRecommendRulePO po = new RatingRecommendRulePO();
|
|
|
|
|
+ po.setRecommendEfficiency(eff);
|
|
|
|
|
+ return po;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 从 AjaxResult(Map 风格)取出 code 数值 */
|
|
|
|
|
+ private int code(AjaxResult r) {
|
|
|
|
|
+ return ((Number) r.get("code")).intValue();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 从 AjaxResult(Map 风格)取出 data 数据体 */
|
|
|
|
|
+ private RecommendResultVO data(AjaxResult r) {
|
|
|
|
|
+ return (RecommendResultVO) r.get("data");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void 空入参_返回error() {
|
|
|
|
|
+ // null 请求
|
|
|
|
|
+ AjaxResult result = recommendService.recommendEfficiency(null);
|
|
|
|
|
+ assertNotEquals(0, code(result));
|
|
|
|
|
+
|
|
|
|
|
+ // 空卡列表
|
|
|
|
|
+ result = recommendService.recommendEfficiency(new RecommendRequest());
|
|
|
|
|
+ assertNotEquals(0, code(result));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void 超限_返回error() {
|
|
|
|
|
+ // 上限设置为 1,但传入 2 张卡 → 应返回 error
|
|
|
|
|
+ when(recommendProperties.getBatchMax()).thenReturn(1);
|
|
|
|
|
+ AjaxResult result = recommendService.recommendEfficiency(
|
|
|
|
|
+ req(card("c1", "p", "y", "s", "cs"), card("c2", "p", "y", "s", "cs")));
|
|
|
|
|
+ assertNotEquals(0, code(result));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void L1全等命中_单卡_单订单_含价格() {
|
|
|
|
|
+ // 四字段全等命中 → 推荐档 3(闪评),单价 200.00
|
|
|
|
|
+ when(ruleManager.selectByExact("Jordan", "1986", "Fleer", "Base")).thenReturn(rule(3));
|
|
|
|
|
+
|
|
|
|
|
+ AjaxResult result = recommendService.recommendEfficiency(req(card("c1", "Jordan", "1986", "Fleer", "Base")));
|
|
|
|
|
+ RecommendResultVO d = data(result);
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(1, d.getTotalCards());
|
|
|
|
|
+ assertEquals(1, d.getOrderCount());
|
|
|
|
|
+ OrderSuggestionVO order = d.getOrders().get(0);
|
|
|
|
|
+ assertEquals(3, order.getEfficiency());
|
|
|
|
|
+ assertEquals(MatchLevelEnum.EXACT.getCode(), order.getCards().get(0).getMatchLevel());
|
|
|
|
|
+ assertEquals(new BigDecimal("200.00"), order.getUnitPrice());
|
|
|
|
|
+ assertEquals(new BigDecimal("200.00"), order.getTotalAmount());
|
|
|
|
|
+ assertEquals(new BigDecimal("200.00"), d.getGrandTotal());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void L1未命中_L2命中_matchLevel为L2() {
|
|
|
|
|
+ // L1 返回 null → 走 L2,L2 命中 rule(2)
|
|
|
|
|
+ when(ruleManager.selectByExact(anyString(), anyString(), anyString(), anyString())).thenReturn(null);
|
|
|
|
|
+ when(ruleManager.selectBySeriesSetYear("Fleer", "Base", "1986")).thenReturn(rule(2));
|
|
|
|
|
+
|
|
|
|
|
+ AjaxResult result = recommendService.recommendEfficiency(req(card("c1", "Jordan", "1986", "Fleer", "Base")));
|
|
|
|
|
+ RecommendResultVO d = data(result);
|
|
|
|
|
+ assertEquals(MatchLevelEnum.L2.getCode(), d.getOrders().get(0).getCards().get(0).getMatchLevel());
|
|
|
|
|
+ assertEquals(2, d.getOrders().get(0).getEfficiency());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void L1L2未命中_L3命中_matchLevel为L3() {
|
|
|
|
|
+ // L1、L2 均返回 null → 走 L3,L3 命中 rule(2)
|
|
|
|
|
+ when(ruleManager.selectByExact(anyString(), anyString(), anyString(), anyString())).thenReturn(null);
|
|
|
|
|
+ when(ruleManager.selectBySeriesSetYear(anyString(), anyString(), anyString())).thenReturn(null);
|
|
|
|
|
+ when(ruleManager.selectBySeriesSet("Fleer", "Base")).thenReturn(rule(2));
|
|
|
|
|
+
|
|
|
|
|
+ AjaxResult result = recommendService.recommendEfficiency(req(card("c1", "Jordan", "1986", "Fleer", "Base")));
|
|
|
|
|
+ RecommendResultVO d = data(result);
|
|
|
|
|
+ assertEquals(MatchLevelEnum.L3.getCode(), d.getOrders().get(0).getCards().get(0).getMatchLevel());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void 三层均未命中_走兜底_matchLevel为DEFAULT() {
|
|
|
|
|
+ // 三层全 null → 走 DEFAULT,时效=defaultEfficiency=1
|
|
|
|
|
+ when(ruleManager.selectByExact(any(), any(), any(), any())).thenReturn(null);
|
|
|
|
|
+ when(ruleManager.selectBySeriesSetYear(any(), any(), any())).thenReturn(null);
|
|
|
|
|
+ when(ruleManager.selectBySeriesSet(any(), any())).thenReturn(null);
|
|
|
|
|
+
|
|
|
|
|
+ AjaxResult result = recommendService.recommendEfficiency(req(card("c1", "X", "Y", "Z", "W")));
|
|
|
|
|
+ RecommendResultVO d = data(result);
|
|
|
|
|
+ assertEquals(MatchLevelEnum.DEFAULT.getCode(), d.getOrders().get(0).getCards().get(0).getMatchLevel());
|
|
|
|
|
+ assertEquals(1, d.getOrders().get(0).getEfficiency());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void 规则时效非法_跳过该层继续降级() {
|
|
|
|
|
+ // L1 命中 rule(99) → recommendEfficiency=99 不在 EvaluateEfficiencyEnum 范围内 → isLegalRule=false
|
|
|
|
|
+ // → 继续 L2,L2 返回合法 rule(2) → matchLevel=L2
|
|
|
|
|
+ when(ruleManager.selectByExact(any(), any(), any(), any())).thenReturn(rule(99));
|
|
|
|
|
+ when(ruleManager.selectBySeriesSetYear(any(), any(), any())).thenReturn(rule(2));
|
|
|
|
|
+ AjaxResult result = recommendService.recommendEfficiency(req(card("c1", "p", "y", "s", "cs")));
|
|
|
|
|
+ RecommendResultVO d = data(result);
|
|
|
|
|
+ assertEquals(MatchLevelEnum.L2.getCode(), d.getOrders().get(0).getCards().get(0).getMatchLevel());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void 价格Map为空_订单与卡的单价均为null_grandTotal为null() {
|
|
|
|
|
+ // 全等命中 rule(3),但价格 Map 为空 → 该档无价
|
|
|
|
|
+ // → 订单 unitPrice/totalAmount=null,卡级 unitPrice=null,grandTotal=null
|
|
|
|
|
+ when(ruleManager.selectByExact(any(), any(), any(), any())).thenReturn(rule(3));
|
|
|
|
|
+ when(priceProvider.loadEfficiencyPrices()).thenReturn(Collections.emptyMap());
|
|
|
|
|
+
|
|
|
|
|
+ AjaxResult result = recommendService.recommendEfficiency(req(card("c1", "p", "y", "s", "cs")));
|
|
|
|
|
+ RecommendResultVO d = data(result);
|
|
|
|
|
+ assertNull(d.getOrders().get(0).getUnitPrice());
|
|
|
|
|
+ assertNull(d.getOrders().get(0).getTotalAmount());
|
|
|
|
|
+ assertNull(d.getOrders().get(0).getCards().get(0).getUnitPrice());
|
|
|
|
|
+ assertNull(d.getGrandTotal());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void 多卡多档_按档位升序分3单_总计正确() {
|
|
|
|
|
+ // 三张卡分别命中 1/2/3 档 → 分 3 单(按档位升序)
|
|
|
|
|
+ // grandTotal = 50 + 100 + 200 = 350.00
|
|
|
|
|
+ when(ruleManager.selectByExact("p1", "y", "s", "cs")).thenReturn(rule(1));
|
|
|
|
|
+ when(ruleManager.selectByExact("p2", "y", "s", "cs")).thenReturn(rule(2));
|
|
|
|
|
+ when(ruleManager.selectByExact("p3", "y", "s", "cs")).thenReturn(rule(3));
|
|
|
|
|
+
|
|
|
|
|
+ AjaxResult result = recommendService.recommendEfficiency(req(
|
|
|
|
|
+ card("c1", "p1", "y", "s", "cs"),
|
|
|
|
|
+ card("c2", "p2", "y", "s", "cs"),
|
|
|
|
|
+ card("c3", "p3", "y", "s", "cs")));
|
|
|
|
|
+ RecommendResultVO d = data(result);
|
|
|
|
|
+ assertEquals(3, d.getOrderCount());
|
|
|
|
|
+ assertEquals(1, d.getOrders().get(0).getEfficiency());
|
|
|
|
|
+ assertEquals(2, d.getOrders().get(1).getEfficiency());
|
|
|
|
|
+ assertEquals(3, d.getOrders().get(2).getEfficiency());
|
|
|
|
|
+ assertEquals(new BigDecimal("350.00"), d.getGrandTotal());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void 同档多卡_合并为一单_单价不变_总计为单价乘以数量() {
|
|
|
|
|
+ // 3 张同档 2 卡 → 合并为 1 单,cardCount=3,unitPrice=100.00,totalAmount=300.00
|
|
|
|
|
+ when(ruleManager.selectByExact(any(), any(), any(), any())).thenReturn(rule(2));
|
|
|
|
|
+ AjaxResult result = recommendService.recommendEfficiency(req(
|
|
|
|
|
+ card("c1", "p", "y", "s", "cs"),
|
|
|
|
|
+ card("c2", "p", "y", "s", "cs"),
|
|
|
|
|
+ card("c3", "p", "y", "s", "cs")));
|
|
|
|
|
+ RecommendResultVO d = data(result);
|
|
|
|
|
+ assertEquals(1, d.getOrderCount());
|
|
|
|
|
+ OrderSuggestionVO order = d.getOrders().get(0);
|
|
|
|
|
+ assertEquals(3, order.getCardCount());
|
|
|
|
|
+ assertEquals(new BigDecimal("100.00"), order.getUnitPrice());
|
|
|
|
|
+ assertEquals(new BigDecimal("300.00"), order.getTotalAmount());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void 图片透传_前端clientCardId与图片原样返回() {
|
|
|
|
|
+ // 验证 clientCardId、frontImageUrl、backImageUrl 原样透传
|
|
|
|
|
+ when(ruleManager.selectByExact(any(), any(), any(), any())).thenReturn(rule(1));
|
|
|
|
|
+ RecommendCardDTO c = card("MY-CARD-1", "p", "y", "s", "cs");
|
|
|
|
|
+ c.setFrontImageUrl("http://x/f.jpg");
|
|
|
|
|
+ c.setBackImageUrl("http://x/b.jpg");
|
|
|
|
|
+
|
|
|
|
|
+ AjaxResult result = recommendService.recommendEfficiency(req(c));
|
|
|
|
|
+ RecommendResultVO d = data(result);
|
|
|
|
|
+ assertEquals("MY-CARD-1", d.getOrders().get(0).getCards().get(0).getClientCardId());
|
|
|
|
|
+ assertEquals("http://x/f.jpg", d.getOrders().get(0).getCards().get(0).getFrontImageUrl());
|
|
|
|
|
+ assertEquals("http://x/b.jpg", d.getOrders().get(0).getCards().get(0).getBackImageUrl());
|
|
|
|
|
+ }
|
|
|
|
|
+}
|