zhuml4
2026-03-02 fc7d601e8e04f4cb73a57bd1bd6eac86a1ec2187
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package com.xiaopeng.automaticPilot.controller;
 
import com.xiaopeng.api.feignClient.map.XpMapClient;
import com.xiaopeng.api.feignClient.operation.ReassignOrderClient;
import com.xiaopeng.api.feignClient.operation.XpCruisePlanClient;
import com.xiaopeng.api.feignClient.order.XpCruiseOrderClient;
import com.xiaopeng.api.feignClient.order.XpOrderClient;
import com.xiaopeng.api.feignClient.pushMessage.XpPushMessageClient;
import com.xiaopeng.api.feignClient.vehicleConvoy.UserVehicleClient;
import com.xiaopeng.api.feignClient.vehicleConvoy.XpVehicleClient;
import com.xiaopeng.api.model.order.XpOrder;
import com.xiaopeng.api.model.vehicleConvoy.XpVehicle;
import com.xiaopeng.api.vo.automaticPilot.VehiclePosition;
import com.xiaopeng.api.vo.operation.RouteVo;
import com.xiaopeng.automaticPilot.service.KafkaConsumerService;
import com.xiaopeng.automaticPilot.util.XPUUtil;
import com.xiaopeng.common.core.domain.R;
import com.xiaopeng.common.redis.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.GeoOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.util.ReflectionTestUtils;
 
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
 
import static org.mockito.Mockito.*;
 
/**
 * KafkaTestController 单元测试
 */
@Slf4j
@DisplayName("Kafka测试控制器单元测试")
@ExtendWith(MockitoExtension.class)
public class KafkaTestControllerTest {
 
    @Mock
    private XpVehicleClient xpVehicleClient;
 
    @Mock
    private XpMapClient xpMapClient;
 
    @Mock
    private XpOrderClient xpOrderClient;
 
    @Mock
    private MongoTemplate mongoTemplate;
 
    @Mock
    private XPUUtil xpuUtil;
 
    @Mock
    private RedisTemplate redisTemplate;
 
    @Mock
    private XpPushMessageClient xpPushMessageClient;
 
    @Mock
    private ReassignOrderClient reassignOrderClient;
 
    @Mock
    private UserVehicleClient userVehicleClient;
 
    @Mock
    private XpCruiseOrderClient xpCruiseOrderClient;
 
    @Mock
    private RedisService redisService;
 
    @Mock
    private XpCruisePlanClient xpCruisePlanClient;
 
    @Mock
    private ValueOperations valueOperations;
 
    @Mock
    private GeoOperations geoOperations;
 
    @InjectMocks
    private KafkaConsumerService kafkaConsumerService;
 
    @Spy
    @InjectMocks
    private KafkaTestController kafkaTestController;
 
    @Mock
    private R<XpVehicle> mockResponseResult;
 
    @Mock
    private R<XpOrder> mockOrderResponseResult;
 
    // 模拟的XpVehicle对象
    private XpVehicle mockXpVehicle;
 
    @BeforeEach
    public void setUp() {
        // 将包含所有 mock 依赖的 kafkaConsumerService 注入到 kafkaTestController
        ReflectionTestUtils.setField(kafkaTestController, "kafkaConsumerService", kafkaConsumerService);
    }
 
    /**
     * 从本地文件读取JSON内容
     *
     * @param filePath 文件路径
     * @return JSON字符串
     * @throws IOException 如果文件读取失败
     */
    private String readJsonFile(String filePath) throws IOException {
        return new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);
    }
 
    @Test
    @DisplayName("测试XPU发生系统运行状态")
    public void testXpuSendSystemRuntimeStatus() throws IOException {
        // 从本地文件读取测试数据
        String testMessage = readJsonFile("src/test/resources/jsondemo/common/system_runtime_status.json");
 
        mockXpVehicle = new XpVehicle();
        mockXpVehicle.setXpuId("xpu-001");
        mockXpVehicle.setVehicleVin("test-vin");
 
        when(mockResponseResult.getData()).thenReturn(mockXpVehicle);
        when(xpVehicleClient.findByVehicleXpuId(anyString())).thenReturn(mockResponseResult);
 
        // mock redisTemplate 的 geo 和 value 操作,避免空指针
        when(redisTemplate.opsForGeo()).thenReturn(geoOperations);
        when(redisTemplate.opsForValue()).thenReturn(valueOperations);
 
        // 执行测试
        kafkaTestController.topicProdRobotaxiXpuHigh(testMessage);
    }
 
    @Test
    @DisplayName("测试XPU发送导航请求")
    public void testXPUSendNavigationRequest() throws IOException {
        // 从本地文件读取测试数据
        String testMessage = readJsonFile("src/test/resources/jsondemo/cloud/navigation_requset.json");
 
        XpOrder mockOrder = new XpOrder();
        mockOrder.setId(1881347710714060802L);
        mockOrder.setVehicleId(1L);
        when(mockOrderResponseResult.getCode()).thenReturn(200);
        when(mockOrderResponseResult.getData()).thenReturn(mockOrder);
        when(xpOrderClient.queryOrderById(anyLong())).thenReturn(mockOrderResponseResult);
 
        mockXpVehicle = new XpVehicle();
        mockXpVehicle.setXpuId("xpu-001");
        mockXpVehicle.setVehicleVin("test-vin");
 
        when(mockResponseResult.getData()).thenReturn(mockXpVehicle);
        when(xpVehicleClient.queryById(anyLong())).thenReturn(mockResponseResult);
 
        // mock mongoTemplate.findOne 返回 VehiclePosition,避免 "车辆位置信息不存在" 异常
        VehiclePosition mockVehiclePosition = new VehiclePosition();
        mockVehiclePosition.setVin("test-vin");
        mockVehiclePosition.setPoint(new GeoJsonPoint(113.3245, 23.1235));
        when(mongoTemplate.findOne(any(Query.class), eq(VehiclePosition.class))).thenReturn(mockVehiclePosition);
 
        RouteVo mockRouteVo = new RouteVo();
        mockRouteVo.setRouteID("test-route-id");
        when(xpMapClient.getOrderDistanceAndDuration(any())).thenReturn(R.ok(mockRouteVo));
 
        when(xpMapClient.queryRouted(any())).thenReturn(R.ok("test-routed-data"));
 
        when(redisTemplate.opsForValue()).thenReturn(valueOperations);
        // 执行测试
        kafkaTestController.topicProdRobotaxiXpuHigh(testMessage);
    }
 
    @Test
    @DisplayName("测试XPU发送订单状态请求")
    public void testXPUSendOrderStatusRequest() throws IOException {
        // 从本地文件读取测试数据
        String testMessage = readJsonFile("src/test/resources/jsondemo/cloud/order_status_info_event.json");
        // 执行测试
        kafkaTestController.topicProdRobotaxiXpuHigh(testMessage);
    }
 
    //当前没有处理
    @Test
    @DisplayName("测试XPU发送导航信息更新")
    public void testXPUSendNavigationInfoUpdate() throws IOException {
        // 从本地文件读取测试数据
        String testMessage = readJsonFile("src/test/resources/jsondemo/cloud/navigation_info_update_event.json");
        // 执行测试
        kafkaTestController.topicProdRobotaxiXpuHigh(testMessage);
    }
 
    @Test
    @DisplayName("测试XPU发送偏航信息")
    public void testXPUSendNavigationDeviationEvent() throws IOException {
        // 从本地文件读取测试数据
        String testMessage = readJsonFile("src/test/resources/jsondemo/cloud/navi_deviation_event.json");
 
        XpOrder mockOrder = new XpOrder();
        mockOrder.setId(1881347710714060802L);
        mockOrder.setStatus(3);
        mockOrder.setVehicleId(1L);
        when(mockOrderResponseResult.getCode()).thenReturn(200);
        when(mockOrderResponseResult.getData()).thenReturn(mockOrder);
        when(xpOrderClient.queryOrderById(anyLong())).thenReturn(mockOrderResponseResult);
 
        mockXpVehicle = new XpVehicle();
        mockXpVehicle.setXpuId("xpu-001");
        mockXpVehicle.setVehicleVin("test-vin");
 
        when(mockResponseResult.getData()).thenReturn(mockXpVehicle);
        when(xpVehicleClient.queryById(anyLong())).thenReturn(mockResponseResult);
 
        RouteVo mockRouteVo = new RouteVo();
        mockRouteVo.setRouteID("test-route-id");
        when(xpMapClient.getOrderDistanceAndDuration(any())).thenReturn(R.ok(mockRouteVo));
 
        when(xpMapClient.queryRouted(any())).thenReturn(R.ok("test-routed-data"));
 
        when(redisTemplate.opsForValue()).thenReturn(valueOperations);
        // 执行测试
        kafkaTestController.topicProdRobotaxiXpuHigh(testMessage);
    }
}