puzhibing
22 小时以前 63b68c39653208a1756640034c028789946d0e12
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.ruoyi.order.service.impl;
 
import com.ruoyi.account.api.feignClient.AppUserIntegralChangeClient;
import com.ruoyi.chargingPile.api.feignClient.ChargingGunClient;
import com.ruoyi.chargingPile.api.feignClient.ChargingPileClient;
import com.ruoyi.chargingPile.api.feignClient.SiteClient;
import com.ruoyi.chargingPile.api.model.Site;
import com.ruoyi.chargingPile.api.model.TChargingGun;
import com.ruoyi.chargingPile.api.model.TChargingPile;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.integration.api.model.UploadRealTimeMonitoringData;
import com.ruoyi.order.api.model.TChargingOrder;
import com.ruoyi.order.api.model.TVinChargingOrder;
import com.ruoyi.order.dto.ChargingDetails;
import com.ruoyi.order.mapper.TChargingOrderMapper;
import com.ruoyi.order.mapper.TVinChargingOrderMapper;
import com.ruoyi.order.util.mongodb.service.UploadRealTimeMonitoringDataService;
import com.ruoyi.order.vo.EndOfChargePageInfo;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.ruoyi.system.api.model.LoginUserApplet;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.junit.jupiter.api.BeforeAll;
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 org.springframework.test.util.ReflectionTestUtils;
 
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
 
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.when;
 
/**
 * TChargingOrderServiceImpl 小程序接口对 VIN 码充电订单(t_vin_charging_order)的兜底逻辑测试:
 * getChargingDetails(正在充电详情)、getEndOfChargePageInfo(结束页详情)。
 */
@ExtendWith(MockitoExtension.class)
class TChargingOrderServiceImplVinFallbackTest {
 
    @InjectMocks
    private TChargingOrderServiceImpl service;
    @Mock
    private TChargingOrderMapper mapper;
    @Mock
    private TVinChargingOrderMapper vinChargingOrderMapper;
    @Mock
    private TokenService tokenService;
    @Mock
    private ChargingGunClient chargingGunClient;
    @Mock
    private ChargingPileClient chargingPileClient;
    @Mock
    private SiteClient siteClient;
    @Mock
    private UploadRealTimeMonitoringDataService uploadRealTimeMonitoringDataService;
    @Mock
    private AppUserIntegralChangeClient appUserIntegralChangeClient;
 
    /**
     * 纯单测无 Spring 容器,MyBatis-Plus 的 lambda 列缓存需手工初始化,
     * 否则 LambdaQueryWrapper 解析 getter 引用时抛 "can not find lambda cache"
     */
    @BeforeAll
    static void initMybatisPlusTableInfo() {
        MapperBuilderAssistant assistant = new MapperBuilderAssistant(new MybatisConfiguration(), "");
        TableInfoHelper.initTableInfo(assistant, TChargingOrder.class);
        TableInfoHelper.initTableInfo(assistant, TVinChargingOrder.class);
    }
 
    @BeforeEach
    void setUp() {
        ReflectionTestUtils.setField(service, "baseMapper", mapper);
    }
 
    @Test
    void getChargingDetailsFallsBackToVinOrderAndAssemblesRealTimeData() {
        LoginUserApplet loginUser = new LoginUserApplet();
        loginUser.setUserId(88L);
        when(tokenService.getLoginUserApplet()).thenReturn(loginUser);
        // 普通订单表查不到
        when(mapper.selectOne(any())).thenReturn(null);
        TVinChargingOrder vinOrder = new TVinChargingOrder();
        vinOrder.setId(1001L);
        vinOrder.setAppUserId(88L);
        vinOrder.setChargingGunId(5);
        vinOrder.setStatus(3);
        vinOrder.setCode("VD20261013001");
        vinOrder.setResidualAmount(new BigDecimal("66.60"));
        when(vinChargingOrderMapper.selectOne(any())).thenReturn(vinOrder);
        TChargingGun gun = new TChargingGun();
        gun.setChargingPileId(7);
        when(chargingGunClient.getChargingGunById(5)).thenReturn(R.ok(gun));
        TChargingPile pile = new TChargingPile();
        pile.setName("1号桩");
        pile.setSiteId(9);
        when(chargingPileClient.getChargingPileById(7)).thenReturn(R.ok(pile));
        Site site = new Site();
        site.setName("城东站");
        when(siteClient.getSiteByIds(Arrays.asList(9))).thenReturn(R.ok(Collections.singletonList(site)));
        UploadRealTimeMonitoringData data = new UploadRealTimeMonitoringData();
        data.setOutput_current(new BigDecimal("10"));
        data.setOutput_voltage(new BigDecimal("220"));
        data.setSoc(50);
        data.setTime_remaining(30);
        data.setCharging_degree(new BigDecimal("5.5"));
        data.setCumulative_charging_time(75);
        when(uploadRealTimeMonitoringDataService.getLastDataById("VD20261013001")).thenReturn(data);
 
        ChargingDetails details = service.getChargingDetails(5);
 
        assertEquals("1001", details.getId());
        assertEquals("城东站-1号桩", details.getName());
        assertEquals("VD20261013001", details.getCode());
        assertEquals(Integer.valueOf(3), details.getStatus());
        assertEquals(0, new BigDecimal("66.60").compareTo(details.getChargingCost()));
        assertEquals(0, new BigDecimal("10").compareTo(details.getChargeCurrent()));
        assertEquals(0, new BigDecimal("220").compareTo(details.getChargeVoltage()));
        assertEquals(0, new BigDecimal("2.200").compareTo(details.getChargePower()));
        assertEquals(Integer.valueOf(50), details.getCompletionRatio());
        assertEquals(Integer.valueOf(30), details.getRemainingChargeTime());
        assertEquals(0, new BigDecimal("5.5").compareTo(details.getChargedDegrees()));
        assertEquals("01:15", details.getChargedTime());
    }
 
    @Test
    void getChargingDetailsReturnsNullWhenNeitherNormalNorVinOrderExists() {
        LoginUserApplet loginUser = new LoginUserApplet();
        loginUser.setUserId(88L);
        when(tokenService.getLoginUserApplet()).thenReturn(loginUser);
        when(mapper.selectOne(any())).thenReturn(null);
        when(vinChargingOrderMapper.selectOne(any())).thenReturn(null);
 
        assertNull(service.getChargingDetails(5));
    }
 
    @Test
    void getEndOfChargePageInfoFallsBackToVinOrder() {
        LoginUserApplet loginUser = new LoginUserApplet();
        loginUser.setUserId(88L);
        when(tokenService.getLoginUserApplet()).thenReturn(loginUser);
        when(mapper.selectById("1001")).thenReturn(null);
        TVinChargingOrder vinOrder = new TVinChargingOrder();
        vinOrder.setId(1001L);
        vinOrder.setAppUserId(88L);
        vinOrder.setCode("VD20261013001");
        vinOrder.setPaymentAmount(new BigDecimal("18.50"));
        vinOrder.setRechargeAmount(new BigDecimal("50.00"));
        vinOrder.setRefundAmount(new BigDecimal("31.50"));
        vinOrder.setStartTime(LocalDateTime.of(2026, 10, 13, 10, 0));
        vinOrder.setEndTime(LocalDateTime.of(2026, 10, 13, 11, 30));
        when(vinChargingOrderMapper.selectById("1001")).thenReturn(vinOrder);
        when(appUserIntegralChangeClient.getTAppUserIntegralChangeByOrderCode("VD20261013001"))
                .thenReturn(R.ok(null));
 
        EndOfChargePageInfo info = service.getEndOfChargePageInfo("1001");
 
        assertEquals("1001", info.getId());
        assertEquals("VD20261013001", info.getCode());
        assertEquals(0, new BigDecimal("18.50").compareTo(info.getPaymentAmount()));
        assertEquals(0, new BigDecimal("50.00").compareTo(info.getRechargeAmount()));
        assertEquals(0, new BigDecimal("31.50").compareTo(info.getRefundAmount()));
        assertEquals(Integer.valueOf(0), info.getIntegral());
        assertEquals("10:00", info.getStartHour());
        assertEquals("11:30", info.getEndHour());
        assertEquals("1小时30分钟", info.getDuration());
    }
 
    @Test
    void getEndOfChargePageInfoThrowsWhenOrderMissingInBothTables() {
        // 两表均查不到订单时在获取登录用户之前即抛异常,tokenService 无需 stub
        when(mapper.selectById("1001")).thenReturn(null);
        when(vinChargingOrderMapper.selectById("1001")).thenReturn(null);
 
        RuntimeException exception = null;
        try {
            service.getEndOfChargePageInfo("1001");
        } catch (RuntimeException ex) {
            exception = ex;
        }
        assertNotNull(exception);
        assertEquals("充电订单不存在", exception.getMessage());
    }
}