yanderong
4 天以前 7617d46abee2950b7336e3379f8c7a564939cb54
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
package com.ruoyi.order.service.impl;
 
import com.ruoyi.account.api.feignClient.VinManagementClient;
import com.ruoyi.chargingPile.api.feignClient.ChargingGunClient;
import com.ruoyi.chargingPile.api.feignClient.SiteClient;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.system.api.model.LoginUserApplet;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.order.api.model.TVinChargingOrder;
import com.ruoyi.order.dto.MyVinChargingOrderList;
import com.ruoyi.order.mapper.TVinChargingOrderMapper;
import com.ruoyi.order.util.mongodb.service.UploadRealTimeMonitoringDataService;
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.util.Arrays;
import java.util.Collections;
import java.util.Map;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
@ExtendWith(MockitoExtension.class)
class TVinChargingOrderAppletServiceTest {
 
    @InjectMocks
    private TVinChargingOrderServiceImpl service;
    @Mock
    private TVinChargingOrderMapper mapper;
    @Mock
    private TokenService tokenService;
    @Mock
    private VinManagementClient vinManagementClient;
    @Mock
    private SiteClient siteClient;
    @Mock
    private ChargingGunClient chargingGunClient;
    @Mock
    private UploadRealTimeMonitoringDataService monitoringDataService;
 
    @BeforeEach
    void setUp() {
        ReflectionTestUtils.setField(service, "baseMapper", mapper);
    }
 
    @Test
    void queriesOnlyVinIdsBoundToCurrentAppletUser() {
        LoginUserApplet loginUser = new LoginUserApplet();
        loginUser.setUserId(88L);
        when(tokenService.getLoginUserApplet()).thenReturn(loginUser);
        when(vinManagementClient.getVinIdsByAppUserId(88L)).thenReturn(R.ok(Arrays.asList(11L, 22L)));
        when(mapper.countAppletOrders(88L, Arrays.asList(11L, 22L))).thenReturn(1L);
        MyVinChargingOrderList order = new MyVinChargingOrderList();
        order.setId(1001L);
        when(mapper.pageAppletOrders(88L, Arrays.asList(11L, 22L), 10L, 10L))
                .thenReturn(Collections.singletonList(order));
 
        PageInfo<MyVinChargingOrderList> result = service.pageAppletOrders(2, 10);
 
        assertEquals(1L, result.getTotal());
        assertEquals(1001L, result.getRecords().get(0).getId());
        verify(vinManagementClient).getVinIdsByAppUserId(88L);
        verify(mapper).countAppletOrders(88L, Arrays.asList(11L, 22L));
        verify(mapper).pageAppletOrders(88L, Arrays.asList(11L, 22L), 10L, 10L);
    }
 
    @Test
    void returnsEmptyPageWithoutQueryingOrderTableWhenUserHasNoBoundVin() {
        LoginUserApplet loginUser = new LoginUserApplet();
        loginUser.setUserId(88L);
        when(tokenService.getLoginUserApplet()).thenReturn(loginUser);
        when(vinManagementClient.getVinIdsByAppUserId(88L)).thenReturn(R.ok(Collections.emptyList()));
 
        PageInfo<MyVinChargingOrderList> result = service.pageAppletOrders(1, 10);
 
        assertEquals(0L, result.getTotal());
        assertEquals(0, result.getRecords().size());
        verify(mapper, never()).countAppletOrders(anyLong(), anyList());
        verify(mapper, never()).pageAppletOrders(anyLong(), anyList(), eq(0L), eq(10L));
    }
 
    @Test
    void returnsVinDetailWithRequiredFeeFieldsForOwner() {
        LoginUserApplet loginUser = new LoginUserApplet();
        loginUser.setUserId(88L);
        when(tokenService.getLoginUserApplet()).thenReturn(loginUser);
        when(vinManagementClient.getVinIdsByAppUserId(88L)).thenReturn(R.ok(Collections.singletonList(11L)));
        TVinChargingOrder order = new TVinChargingOrder();
        order.setId(1001L);
        order.setVinId(11L);
        order.setAppUserId(88L);
        order.setCode("VD_1001");
        order.setOrderAmount(new BigDecimal("18.50"));
        order.setElectrovalence(new BigDecimal("12.30"));
        order.setServiceCharge(new BigDecimal("6.20"));
        when(mapper.selectById(1001L)).thenReturn(order);
        when(mapper.accountingStrategyList(1001L)).thenReturn(Collections.emptyList());
        when(monitoringDataService.getDataByOrderCode("VD_1001")).thenReturn(Collections.emptyList());
 
        Map<String, Object> result = service.appletDetail(1001L);
 
        assertEquals("1001", result.get("id"));
        assertEquals(new BigDecimal("18.50"), result.get("orderAmount"));
        assertEquals(new BigDecimal("12.30"), result.get("electricMoney"));
        assertEquals(new BigDecimal("6.20"), result.get("serviceMoney"));
    }
 
    @Test
    void returnsEmptyDetailWhenVinIsNotBoundToCurrentUser() {
        LoginUserApplet loginUser = new LoginUserApplet();
        loginUser.setUserId(88L);
        when(tokenService.getLoginUserApplet()).thenReturn(loginUser);
        when(vinManagementClient.getVinIdsByAppUserId(88L)).thenReturn(R.ok(Collections.singletonList(22L)));
        TVinChargingOrder order = new TVinChargingOrder();
        order.setId(1001L);
        order.setVinId(11L);
        when(mapper.selectById(1001L)).thenReturn(order);
 
        assertTrue(service.appletDetail(1001L).isEmpty());
        verify(mapper, never()).accountingStrategyList(1001L);
    }
 
    @Test
    void returnsEmptyDetailWhenVinReboundAndOrderBelongsToPreviousUser() {
        LoginUserApplet loginUser = new LoginUserApplet();
        loginUser.setUserId(88L);
        when(tokenService.getLoginUserApplet()).thenReturn(loginUser);
        // VIN 11 当前绑定用户 88,但该订单是换绑前用户 99 下单的,不能查看
        when(vinManagementClient.getVinIdsByAppUserId(88L)).thenReturn(R.ok(Collections.singletonList(11L)));
        TVinChargingOrder order = new TVinChargingOrder();
        order.setId(1001L);
        order.setVinId(11L);
        order.setAppUserId(99L);
        when(mapper.selectById(1001L)).thenReturn(order);
 
        assertTrue(service.appletDetail(1001L).isEmpty());
        verify(mapper, never()).accountingStrategyList(1001L);
    }
}