test: centralize vehicle identity fixtures

This commit is contained in:
lingniu
2026-07-01 18:32:09 +08:00
parent 4eaaa88e9c
commit e06534d6a5
12 changed files with 95 additions and 150 deletions

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.lingniu.ingest</groupId>
<artifactId>lingniu-vehicle-ingest</artifactId>
<version>0.1.0-SNAPSHOT</version>
<relativePath>../../../pom.xml</relativePath>
</parent>
<artifactId>vehicle-identity-test-support</artifactId>
<name>vehicle-identity-test-support</name>
<description>车辆身份解析测试夹具,仅供协议和接入模块测试依赖。</description>
<dependencies>
<dependency>
<groupId>com.lingniu.ingest</groupId>
<artifactId>vehicle-identity</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,48 @@
package com.lingniu.ingest.identity;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
public final class InMemoryVehicleIdentityService implements VehicleIdentityResolver, VehicleIdentityRegistry {
private final List<VehicleIdentityBinding> bindings = new CopyOnWriteArrayList<>();
@Override
public VehicleIdentity resolve(VehicleIdentityLookup lookup) {
if (lookup == null) {
return new VehicleIdentity("unknown", false, VehicleIdentitySource.UNKNOWN);
}
if (!lookup.vin().isBlank() && !"unknown".equalsIgnoreCase(lookup.vin())) {
return new VehicleIdentity(lookup.vin(), true, VehicleIdentitySource.EXPLICIT_VIN);
}
for (VehicleIdentityBinding binding : bindings) {
if (binding.protocol() != lookup.protocol()) {
continue;
}
if (!lookup.phone().isBlank() && lookup.phone().equals(binding.phone())) {
return new VehicleIdentity(binding.vin(), true, VehicleIdentitySource.BOUND_PHONE);
}
if (!lookup.deviceId().isBlank() && lookup.deviceId().equals(binding.deviceId())) {
return new VehicleIdentity(binding.vin(), true, VehicleIdentitySource.BOUND_DEVICE_ID);
}
if (!lookup.plate().isBlank() && lookup.plate().equals(binding.plate())) {
return new VehicleIdentity(binding.vin(), true, VehicleIdentitySource.BOUND_PLATE);
}
}
if (!lookup.deviceId().isBlank()) {
return new VehicleIdentity(lookup.deviceId(), false, VehicleIdentitySource.FALLBACK_DEVICE_ID);
}
if (!lookup.phone().isBlank()) {
return new VehicleIdentity(lookup.phone(), false, VehicleIdentitySource.FALLBACK_PHONE);
}
if (!lookup.plate().isBlank()) {
return new VehicleIdentity(lookup.plate(), false, VehicleIdentitySource.FALLBACK_PLATE);
}
return new VehicleIdentity("unknown", false, VehicleIdentitySource.UNKNOWN);
}
@Override
public void bind(VehicleIdentityBinding binding) {
bindings.add(binding);
}
}