| New file |
| | |
| | | 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); |
| | | } |
| | | } |
| | | |