feat(appointments): discover external booking candidates through fleet boundary

This commit is contained in:
2026-10-10 14:03:42 +08:00
parent 369613a2e7
commit 71111bf21b
9 changed files with 265 additions and 0 deletions
@@ -0,0 +1,37 @@
package com.baiyue.lastleg.appointmentplanning;
import com.baiyue.common.security.AuthenticatedUser;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.*;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import java.util.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
class AppointmentExternalCandidateAuthorizationTest {
@Test void requiresBothPermissionsAndPlatformIdentity() {
try(var context=new AnnotationConfigApplicationContext(Config.class)) {
var controller=context.getBean(AppointmentExternalCandidateController.class);
var service=context.getBean(AppointmentExternalCandidateService.class);
var permissions=List.of("platform:appointment:list","platform:overseas-warehouse:last-mile-order:list");
var platform=new AuthenticatedUser(1L,0L,0L,1L,"staff","PLATFORM","WEB",Set.copyOf(permissions));
for(var permission:permissions) {
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(platform,"unused",List.of(new SimpleGrantedAuthority(permission))));
assertThatThrownBy(()->controller.page(platform,1,20,null,null,null)).isInstanceOf(AccessDeniedException.class);
}
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(platform,"unused",permissions.stream().map(SimpleGrantedAuthority::new).toList()));
var customer=new AuthenticatedUser(1L,9L,9L,1L,"customer","MERCHANT","WEB",Set.copyOf(permissions));
assertThatThrownBy(()->controller.page(customer,1,20,null,null,null)).isInstanceOf(org.springframework.web.server.ResponseStatusException.class);
verifyNoInteractions(service);
controller.page(platform,1,20,null,null,null);verify(service).page(1,20,null,null,null);
} finally {SecurityContextHolder.clearContext();}
}
@Configuration @EnableMethodSecurity static class Config {
@Bean AppointmentExternalCandidateService service(){return mock(AppointmentExternalCandidateService.class);}
@Bean AppointmentExternalCandidateController controller(AppointmentExternalCandidateService service){return new AppointmentExternalCandidateController(service);}
}
}
@@ -0,0 +1,33 @@
package com.baiyue.lastleg.appointmentplanning;
import com.baiyue.common.core.api.*;
import org.junit.jupiter.api.Test;
import java.util.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
class AppointmentExternalCandidateServiceTest {
@Test void batchesResolutionAndCoverageWhileKeepingOriginalIdentityAndServerTotal() {
var external=mock(AppointmentExternalSourceGateway.class);var warehouses=mock(AppointmentWarehouseClient.class);
var sources=mock(AppointmentSourceService.class);var coverage=mock(AppointmentCoverageService.class);
var service=new AppointmentExternalCandidateService(external,warehouses,sources,coverage,"token");
when(warehouses.catalog("token")).thenReturn(ApiResponse.success(List.of(
new AppointmentWarehouseClient.Warehouse(100L,"US","US","OVERSEAS","US"),
new AppointmentWarehouseClient.Warehouse(200L,"CA","CA","OVERSEAS","CA"),
new AppointmentWarehouseClient.Warehouse(300L,"ONT8","ONT8","FBA","US"))));
var query=new AppointmentExternalSourceGateway.CandidateQuery(2,20,List.of("100"),List.of("ONT8"),null,null,"FB");
when(external.candidates(query)).thenReturn(new PageResult<>(List.of(new AppointmentExternalSourceGateway.Candidate("C","Customer","FB-1","TR-1","FBA:ONT8")),2,20,21,2));
var refs=List.of(new AppointmentCoverageService.SourceRef("C","BOOKING:FB-1","FBA:ONT8"));
var source=new AppointmentSourceService.Source("BOOKING:FB-1","C",null,null,"FB-1",null,"100","US","FBA:ONT8","ONT8",null,"BOOKING:1",true,List.of());
var covered=new AppointmentCoverageService.Coverage("C","BOOKING:FB-1","FBA:ONT8","UNSCHEDULED",List.of(),List.of(),"BOOKING:1",false);
when(sources.resolveBatch(refs)).thenReturn(List.of(source));when(coverage.query(refs)).thenReturn(List.of(covered));
var result=service.page(2,20,null,null," FB ");
assertThat(result.total()).isEqualTo(21);assertThat(result.records().get(0).source()).isSameAs(source);
assertThat(result.records().get(0).source().packages()).isNull();
verify(external).candidates(query);verify(sources).resolveBatch(refs);verify(coverage).query(refs);
verifyNoMoreInteractions(external,sources,coverage);
assertThatThrownBy(()->service.page(1,20,"200",null,null)).hasMessageContaining("美国");
when(warehouses.catalog("token")).thenThrow(new IllegalStateException());
assertThatThrownBy(()->service.page(1,20,null,null,null)).hasMessageContaining("目录暂不可用");
}
}
@@ -0,0 +1,39 @@
package com.baiyue.lastleg.fleet.business;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.baiyue.lastleg.appointmentplanning.AppointmentExternalSourceGateway.CandidateQuery;
import org.junit.jupiter.api.Test;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import java.util.*;
import static org.assertj.core.api.Assertions.*;
class FleetAppointmentCandidateMapperTest {
@Test void pagesOriginalBookingsAcrossCustomersAndExcludesIneligibleScope() throws Exception {
var ds=new DriverManagerDataSource("jdbc:h2:mem:"+UUID.randomUUID()+";MODE=MySQL;DB_CLOSE_DELAY=-1","sa","");
var db=new JdbcTemplate(ds);
db.execute("CREATE TABLE ll_fleet_trip(id BIGINT PRIMARY KEY,warehouse_id BIGINT,status VARCHAR(32))");
db.execute("CREATE TABLE ll_fleet_external_booking(id BIGINT PRIMARY KEY,trip_id BIGINT,customer_id VARCHAR(64),customer_name VARCHAR(64),booking_no VARCHAR(64),trip_no VARCHAR(64),status VARCHAR(32),offer_id BIGINT,destination_key VARCHAR(160),incoming_no VARCHAR(64),created_at TIMESTAMP)");
db.update("INSERT INTO ll_fleet_trip VALUES(1,100,'PLANNED'),(2,200,'PLANNED'),(3,100,'COMPLETED')");
for(int i=1;i<=7;i++) db.update("INSERT INTO ll_fleet_external_booking VALUES(?,1,?,? ,?,'TR-1','CONFIRMED',1,'FBA:ONT8',NULL,CURRENT_TIMESTAMP)",i,"C"+i,"Customer "+i,"FB-"+i);
db.update("UPDATE ll_fleet_external_booking SET status='CANCELLED' WHERE id=3");
db.update("UPDATE ll_fleet_external_booking SET status='DRAFT' WHERE id=4");
db.update("UPDATE ll_fleet_external_booking SET trip_id=2 WHERE id=5");
db.update("UPDATE ll_fleet_external_booking SET trip_id=3 WHERE id=6");
db.update("UPDATE ll_fleet_external_booking SET destination_key='FBA:YYZ1' WHERE id=7");
db.update("UPDATE ll_fleet_external_booking SET status='BOUND',incoming_no='IN-2' WHERE id=2");
var config=new MybatisConfiguration();config.addMapper(FleetAppointmentCandidateMapper.class);
var factory=new MybatisSqlSessionFactoryBean();factory.setDataSource(ds);factory.setConfiguration(config);
var mapper=new SqlSessionTemplate(factory.getObject()).getMapper(FleetAppointmentCandidateMapper.class);
var query=new CandidateQuery(1,1,List.of("100"),List.of("ONT8"),null,null,null);
assertThat(mapper.count(query)).isEqualTo(2);
assertThat(mapper.page(query,0)).extracting(r->r.bookingNo()).containsExactly("FB-2");
assertThat(mapper.page(query,1)).extracting(r->r.bookingNo()).containsExactly("FB-1");
assertThat(mapper.page(new CandidateQuery(1,20,List.of("100"),List.of("ONT8"),"100","ONT8","IN-2"),0))
.extracting(r->r.customerId()).containsExactly("C2");
db.update("UPDATE ll_fleet_external_booking SET offer_id=NULL WHERE id=1");
assertThat(mapper.count(query)).isEqualTo(1);
}
}