yeg
2026-09-17 75e20ed49063feac161beca13aba24147424d5a7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package com.talentcard.server.business.portal;
 
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
 
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
 
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 org.springframework.jdbc.core.JdbcTemplate;
 
@ExtendWith(MockitoExtension.class)
class PortalServiceTests {
 
    @Mock
    private JdbcTemplate jdbc;
 
    @InjectMocks
    private PortalService service;
 
    @Test
    void cinemaMerchantOnlyShowsCouponTagWhenAnActiveCouponExists() {
        when(jdbc.queryForList(anyString(), any(Object[].class))).thenReturn(List.of(
                Map.of("id", 1L, "merchant_name", "有券影院", "merchant_type", "cinema", "status", 1, "coupon_available", 1),
                Map.of("id", 2L, "merchant_name", "无券影院", "merchant_type", "cinema", "status", 1, "coupon_available", 0)));
        when(jdbc.queryForObject(anyString(), eq(Long.class), any(Object[].class))).thenReturn(2L);
 
        var result = service.entertainmentMerchants(1, 4);
 
        assertThat(result.records().get(0))
                .containsEntry("couponAvailable", true)
                .containsEntry("discountText", "券");
        assertThat(result.records().get(1))
                .containsEntry("couponAvailable", false)
                .containsEntry("discountText", "");
    }
 
    @Test
    void shoppingMerchantUsesConfiguredStoreDiscountInsteadOfRightsTag() throws Exception {
        Method method = PortalService.class.getDeclaredMethod("merchantRightDiscountText", Map.class);
        method.setAccessible(true);
 
        String discount = (String) method.invoke(service, Map.of(
                "enabled", true,
                "tag", "卓越人才到店消费可用",
                "storeConsume", true,
                "consumeEnabled", true,
                "discount", "8.50"));
 
        assertThat(discount).isEqualTo("8.5折");
    }
 
    @Test
    void carWashListHidesMiniProgramTagAndShowsNormalConfiguredDiscount() throws Exception {
        Method method = PortalService.class.getDeclaredMethod("carWashListDiscountText", String.class, Map.class);
        method.setAccessible(true);
        Map<String, Object> right = Map.of(
                "enabled", true,
                "storeConsume", true,
                "consumeEnabled", true,
                "discount", "8.50");
 
        assertThat(method.invoke(service, "miniProgram", right)).isEqualTo("");
        assertThat(method.invoke(service, "normal", right)).isEqualTo("8.5折");
    }
 
    @Test
    @SuppressWarnings("unchecked")
    void recommendedMerchantTagsFollowCategoryListRules() throws Exception {
        Method method = PortalService.class.getDeclaredMethod(
                "merchantListBenefitTags", Map.class, Map.class, Long.class);
        method.setAccessible(true);
 
        List<String> foodTags = (List<String>) method.invoke(service,
                Map.of("merchantType", "food", "couponAvailable", true, "discountText", "8.5折"),
                Map.of(), null);
        List<String> cinemaTags = (List<String>) method.invoke(service,
                Map.of("merchantType", "cinema", "couponAvailable", false, "discountText", "7折"),
                Map.of(), null);
 
        assertThat(foodTags).containsExactly("券", "8.5折");
        assertThat(cinemaTags).isEmpty();
        for (String type : List.of("employment", "finance", "approval", "apartment", "fuel",
                "housing", "car", "appliance", "digital", "telecom")) {
            List<String> tags = (List<String>) method.invoke(service,
                    Map.of("merchantType", type, "couponAvailable", true, "discountText", "7折"),
                    Map.of("serviceLabel", "人才专享"), null);
            assertThat(tags).as(type).isEmpty();
        }
 
        List<String> scenicTags = (List<String>) method.invoke(service,
                Map.of("merchantType", "scenic", "couponAvailable", true, "discountText", "8.8折"),
                Map.of(), null);
        List<String> cultureTags = (List<String>) method.invoke(service,
                Map.of("merchantType", "culture", "couponAvailable", true, "discountText", ""),
                Map.of(), null);
        assertThat(scenicTags).containsExactly("券", "8.8折");
        assertThat(cultureTags).containsExactly("券");
    }
 
    @Test
    @SuppressWarnings("unchecked")
    void merchantDetailHeaderTagsDriveListTags() throws Exception {
        Method method = PortalService.class.getDeclaredMethod(
                "merchantBenefitTags", Long.class, String.class, String.class, boolean.class, String.class, Map.class);
        method.setAccessible(true);
 
        List<String> foodTags = (List<String>) method.invoke(service, null, null, "food", true, "8.8折", null);
        List<String> hotelTags = (List<String>) method.invoke(service, null, null, "hotel", true, "7折", Map.of(
                "freeVoucherEnabled", true,
                "freeVoucherTimes", "2",
                "storeConsume", true,
                "consumeEnabled", true,
                "consumeType", "折扣",
                "consumeDiscount", "8.5"));
 
        assertThat(foodTags).containsExactly("券", "8.8折");
        // 酒店折扣同样取「团单折扣 7 折」与「商家权益到店消费折扣 8.5 折」中更低的一档。
        assertThat(hotelTags).containsExactly("券", "免费2次", "7.0折");
    }
 
    @Test
    void productFreeRightsProduceFreeTimesTag() throws Exception {
        when(jdbc.queryForList(anyString(), any(Object[].class))).thenReturn(List.of(
                Map.of("rightsConfig", "[{\"level\": \"卓越人才卡\", \"enabled\": true, \"free\": true,"
                        + " \"limitCount\": 10, \"exceedDiscount\": 7}]"),
                Map.of("rightsConfig", "[{\"level\": \"卓越人才卡\", \"enabled\": true, \"free\": true,"
                        + " \"limitCount\": 1, \"exceedDiscount\": 6}]")));
        Method method = PortalService.class.getDeclaredMethod("productFreeTimesTag", Long.class, String.class);
        method.setAccessible(true);
 
        assertThat((String) method.invoke(service, 83L, "卓越人才卡")).isEqualTo("免费10次");
        assertThat((String) method.invoke(service, 83L, "杰出人才卡")).isEmpty();
    }
 
    @Test
    @SuppressWarnings("unchecked")
    void parkingTagsUseFreeForFreeRightsAndDiscountForPaidRights() throws Exception {
        when(jdbc.queryForList(anyString(), any(Object[].class))).thenReturn(List.of());
        when(jdbc.queryForObject(anyString(), eq(Long.class), any(Object[].class))).thenReturn(0L);
        Method method = PortalService.class.getDeclaredMethod(
                "parkingBenefitTags", Long.class, List.class, String.class, String.class);
        method.setAccessible(true);
 
        List<String> freeTags = (List<String>) method.invoke(service, 1L, List.of(Map.of(
                "level", "卓越人才卡", "enabled", true, "parkingType", "free")), "卓越人才卡", "LOCAL");
        List<String> paidTags = (List<String>) method.invoke(service, 1L, List.of(Map.of(
                "level", "卓越人才卡", "enabled", true, "parkingType", "paid",
                "storeConsume", true, "consumeEnabled", false, "discount", "8.50")), "卓越人才卡", "LOCAL");
 
        assertThat(freeTags).containsExactly("免费");
        assertThat(paidTags).containsExactly("8.5折");
    }
 
    @Test
    @SuppressWarnings("unchecked")
    void merchantRightMatchingKeepsLocalAndOutsideRightsApart() throws Exception {
        Method method = PortalService.class.getDeclaredMethod(
                "matchingMerchantRight", List.class, String.class, String.class, boolean.class);
        method.setAccessible(true);
        List<Map<String, Object>> rights = List.of(
                Map.of("level", "卓越人才卡(外地)", "tag", "卓越人才卡(外地)", "detail", "外地权益", "enabled", true),
                Map.of("level", "卓越人才卡(本地)", "tag", "卓越人才卡(本地)", "detail", "本地权益", "enabled", true));
 
        Map<String, Object> local = (Map<String, Object>) method.invoke(service, rights, "卓越人才卡", "LOCAL", true);
        Map<String, Object> outside = (Map<String, Object>) method.invoke(service, rights, "卓越人才卡", "OUTSIDE", true);
 
        assertThat(local.get("detail")).isEqualTo("本地权益");
        assertThat(outside.get("detail")).isEqualTo("外地权益");
    }
 
    @Test
    void merchantRightMatchingNeverFallsBackToAnotherAreaScope() throws Exception {
        Method method = PortalService.class.getDeclaredMethod(
                "matchingMerchantRight", List.class, String.class, String.class, boolean.class);
        method.setAccessible(true);
        // 只配了“(外地)”时,本地人才拿不到这份权益;不带地区后缀的配置两边都能用。
        List<Map<String, Object>> outsideOnly = List.of(
                Map.of("level", "卓越人才卡(外地)", "detail", "外地权益", "enabled", true));
        List<Map<String, Object>> plain = List.of(Map.of("level", "卓越人才卡", "detail", "通用权益", "enabled", true));
 
        assertThat(method.invoke(service, outsideOnly, "卓越人才卡", "LOCAL", true)).isNull();
        assertThat(method.invoke(service, plain, "卓越人才卡", "LOCAL", true)).isNotNull();
        assertThat(method.invoke(service, plain, "卓越人才卡", "OUTSIDE", true)).isNotNull();
    }
}