perf(lastleg): reuse warehouse directory within sync generation

This commit is contained in:
2026-10-17 09:57:54 +08:00
parent 81e4123b10
commit 2971d6f7f0
11 changed files with 212 additions and 42 deletions
@@ -12,6 +12,38 @@ import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
class AppointmentSourceServiceTest {
/** 目录失败不缓存,成功只在当前作用域复用;来源、客户和目的地仍逐次校验。 */
@Test void explicitScopeRetriesFailedCatalogAndNeverCachesSourceOrCrossScopeDirectory() {
var orders=mock(LastlegOrderMapper.class);var shipping=mock(ContainerShippingGateMapper.class);
var external=mock(AppointmentExternalSourceGateway.class);var warehouse=mock(AppointmentWarehouseClient.class);
var service=new AppointmentSourceService(orders,shipping,external,warehouse,"token");
var order=new LastlegOrder();order.setId(1L);order.setCustomerId("A");order.setOrderType("FIRST_LEG");
order.setSourceContainerBookingId("C1");order.setOverseasWarehouseId("100");order.setDestinationType("FBA");order.setFbaWarehouseCode("ONT8");order.setPackageCount(10);
when(orders.selectBatchIds(anyCollection())).thenReturn(List.of(order));
var gate=new com.baiyue.lastleg.shipping.entity.ContainerShippingGate();gate.setContainerBookingId("C1");gate.setNodeSequence(40);
when(shipping.selectBatchIds(anyCollection())).thenReturn(List.of(gate));
var catalog=ApiResponse.success(List.of(new AppointmentWarehouseClient.Warehouse(100L,"US-WH","美国仓","OVERSEAS","US"),new AppointmentWarehouseClient.Warehouse(200L,"ONT8","ONT8","FBA","US")));
var outage=new IllegalStateException("offline");
when(warehouse.catalog("token")).thenThrow(outage).thenReturn(catalog);
var refs=List.of(new AppointmentCoverageService.SourceRef("A","ORDER:1","FBA:ONT8"));
var scope=service.openReadScope();verifyNoInteractions(warehouse);
var error=assertThrows(ResponseStatusException.class,()->scope.resolveBatch(refs));assertEquals(503,error.getStatusCode().value());assertSame(outage,error.getCause());
assertEquals(10,scope.resolveBatch(refs).get(0).packages());verify(warehouse,times(2)).catalog("token");
order.setPackageCount(12);assertEquals(12,scope.resolveBatch(refs).get(0).packages());verify(warehouse,times(2)).catalog("token");
assertEquals(404,assertThrows(ResponseStatusException.class,()->scope.resolveBatch(List.of(new AppointmentCoverageService.SourceRef("OTHER","ORDER:1","FBA:ONT8")))).getStatusCode().value());
assertThrows(ResponseStatusException.class,()->scope.resolveBatch(List.of(new AppointmentCoverageService.SourceRef("A","ORDER:1","FBA:LAX9"))));
assertThrows(ResponseStatusException.class,()->scope.resolveBatch(List.of(refs.get(0),refs.get(0))));
gate.setNodeSequence(30);assertThrows(ResponseStatusException.class,()->scope.resolveBatch(refs));gate.setNodeSequence(40);
order.setOverseasWarehouseId("missing");assertThrows(ResponseStatusException.class,()->scope.resolveBatch(refs));order.setOverseasWarehouseId("100");
when(external.resolveOrders(anyList())).thenReturn(List.of(new AppointmentExternalSourceGateway.Source("A","B-1","TR-1","RECEIVED","100","FBA:ONT8",1L,12,1L)));
assertThrows(ResponseStatusException.class,()->scope.resolveBatch(refs));when(external.resolveOrders(anyList())).thenReturn(List.of());
assertEquals(12,service.openReadScope().resolveBatch(refs).get(0).packages());verify(warehouse,times(3)).catalog("token");
service.resolveBatch(refs);service.resolveBatch(refs);service.inspectBatch(refs);service.inspectBatch(refs);verify(warehouse,times(7)).catalog("token");
when(warehouse.catalog("token")).thenReturn(null).thenReturn(catalog);
var next=service.openReadScope();assertEquals(503,assertThrows(ResponseStatusException.class,()->next.resolveBatch(refs)).getStatusCode().value());
assertEquals(12,next.resolveBatch(refs).get(0).packages());verify(warehouse,times(9)).catalog("token");
}
@Test void batchResolvesTwoCustomersWithOneCatalogAndOneOrderAndShippingQuery() {
var orders=mock(LastlegOrderMapper.class);var shipping=mock(ContainerShippingGateMapper.class);
var gateway=mock(AppointmentExternalSourceGateway.class);var warehouse=mock(AppointmentWarehouseClient.class);
@@ -9,6 +9,33 @@ import static com.baiyue.lastleg.appointmentwork.AppointmentWorkFactGateway.*;
import static com.baiyue.lastleg.appointmentwork.WorkTestFixture.*;
class AppointmentWorkFactAdapterTest {
/** 未读取需求的未知路线不依赖提供端,首次读取才开作用域并逐份校验结果。 */
@SuppressWarnings("unchecked")
@Test void requirementReaderIsLazyAndKeepsProviderCompletenessChecks() {
var providers=(org.springframework.beans.factory.ObjectProvider<RequirementProvider>)org.mockito.Mockito.mock(org.springframework.beans.factory.ObjectProvider.class);
var business=(org.springframework.beans.factory.ObjectProvider<BusinessProvider>)org.mockito.Mockito.mock(org.springframework.beans.factory.ObjectProvider.class);
org.mockito.Mockito.when(business.orderedStream()).thenAnswer(c->java.util.stream.Stream.empty());
var adapter=new AppointmentWorkFactAdapter(null,business,null,providers,null,"token");
var unused=adapter.openRequirements();org.mockito.Mockito.verifyNoInteractions(providers);
var source=new ContainerSource("1","CNT",java.time.Instant.parse("2026-10-01T00:00:00Z"),null,null,"v1",Flow.UNKNOWN,null,List.of());
var f=new WorkTestFixture();
org.mockito.Mockito.when(f.gateway.openRequirements()).thenAnswer(c->adapter.openRequirements());
org.mockito.Mockito.when(f.gateway.discover(null,10)).thenReturn(new SourcePage(List.of(source),null,true,"scope"));
f.sync.synchronize(new SyncRequest(null,10));assertEquals(Type.ROUTE,f.store.all().get(0).getType());
org.mockito.Mockito.verifyNoInteractions(providers);
assertThrows(ResponseStatusException.class,()->unused.read(source,Flow.TRANSFER));
var provider=org.mockito.Mockito.mock(RequirementProvider.class);
var delegate=org.mockito.Mockito.mock(RequirementReader.class);
org.mockito.Mockito.when(providers.getIfAvailable()).thenReturn(provider);
org.mockito.Mockito.when(provider.openRequirements()).thenReturn(delegate);
var requirement=new Requirement("INBOUND:1",null,null,"FBA",null,"ONT8",null,List.of(),List.of(),List.of());
org.mockito.Mockito.when(delegate.read(source,Flow.TRANSFER)).thenReturn(List.of(requirement),List.of(requirement),List.of());
var reader=adapter.openRequirements();
assertEquals(List.of(requirement),reader.read(source,Flow.TRANSFER));
assertEquals(List.of(requirement),reader.read(source,Flow.TRANSFER));
assertThrows(ResponseStatusException.class,()->reader.read(source,Flow.TRANSFER));
org.mockito.Mockito.verify(provider,org.mockito.Mockito.times(1)).openRequirements();
}
@Test void incompleteAndDuplicateAuthorityResultsAreFailuresNotEmptySuccess() {
var f=new WorkTestFixture(); var state=f.state("1",Type.EMPTY_RETURN,Flow.DIRECT,null);
var request=new FactRequest(state.getWorkItemKey(),state.getType(),state.getFlow(),"1",null,null,null,List.of(),state.getContainers(),List.of());
@@ -79,7 +79,7 @@ class AppointmentWorkPersistenceTest {
var requirement=new AppointmentWorkFactGateway.Requirement(AppointmentWorkPolicy.customerRequirement(later.batchKey(),"B"),later.batchKey(),"AP-2","FBA","海外仓","ONT8",null,List.of(),List.of(),List.of(new SourceReference("B","ORDER:2","FBA:ONT8")));
var source=new AppointmentWorkFactGateway.ContainerSource("1","CNT",null,LocalDateTime.of(2026,10,1,12,0),null,"v1",Flow.TRANSFER,null,List.of(requirement));
var facts=org.mockito.Mockito.mock(AppointmentWorkFactGateway.class);
org.mockito.Mockito.when(facts.requirements(source,Flow.TRANSFER)).thenReturn(List.of(requirement));
org.mockito.Mockito.when(facts.openRequirements()).thenAnswer(c->(AppointmentWorkFactGateway.RequirementReader)(row,flow)->List.of(requirement));
tx.executeWithoutResult(status->new AppointmentWorkSyncService(store,facts).generate(source,Flow.TRANSFER,List.of(),ACTOR));
assertEquals(persisted.owner(),store.get(key).getOwner());assertEquals(persisted.nextFollowupDate(),store.get(key).getFollowupDate());
var third=new com.baiyue.lastleg.inboundplan.receiving.FbaAppointmentWorkPort.Member("FBA_APPOINTMENT:3","B");
@@ -12,6 +12,50 @@ import static com.baiyue.lastleg.appointmentwork.AppointmentWorkModels.*;
import static com.baiyue.lastleg.appointmentwork.AppointmentWorkFactGateway.*;
class AppointmentWorkSyncServiceTest {
/** 真实需求提供链百柜只读一次目录,下页和命令生成独立重读;来源查询仍逐次执行。 */
@Test void hundredFbaContainersShareOnlyCatalogWithinOneGenerationScope() {
var f=new WorkTestFixture();
var orders=mock(com.baiyue.lastleg.mapper.LastlegOrderMapper.class);
var shipping=mock(com.baiyue.lastleg.shipping.mapper.ContainerShippingGateMapper.class);
var external=mock(com.baiyue.lastleg.appointmentplanning.AppointmentExternalSourceGateway.class);
var warehouse=mock(com.baiyue.lastleg.appointmentplanning.AppointmentWarehouseClient.class);
when(warehouse.catalog("token")).thenReturn(com.baiyue.common.core.api.ApiResponse.success(List.of(
new com.baiyue.lastleg.appointmentplanning.AppointmentWarehouseClient.Warehouse(100L,"US-WH","美国仓","OVERSEAS","US"),
new com.baiyue.lastleg.appointmentplanning.AppointmentWarehouseClient.Warehouse(200L,"ONT8","ONT8","FBA","US"))));
when(orders.selectBatchIds(anyCollection())).thenAnswer(c->c.<Collection<Long>>getArgument(0).stream().map(id->{
var order=new com.baiyue.lastleg.entity.LastlegOrder();order.setId(id);order.setCustomerId("C"+id);
order.setOrderType("FIRST_LEG");order.setSourceContainerBookingId(id.toString());order.setOverseasWarehouseId("100");
order.setDestinationType("FBA");order.setFbaWarehouseCode("ONT8");order.setPackageCount(1);return order;
}).toList());
when(shipping.selectBatchIds(anyCollection())).thenAnswer(c->c.<Collection<String>>getArgument(0).stream().map(id->{
var gate=new com.baiyue.lastleg.shipping.entity.ContainerShippingGate();gate.setContainerBookingId(id);gate.setNodeSequence(40);return gate;
}).toList());
var sources=new com.baiyue.lastleg.appointmentplanning.AppointmentSourceService(orders,shipping,external,warehouse,"token");
var cargo=mock(com.baiyue.lastleg.appointmentplanning.AppointmentCargoVerificationService.class);
var allocation=mock(com.baiyue.lastleg.appointmentplanning.AppointmentCargoVerificationService.Context.class);
when(allocation.availablePackages()).thenReturn(1L);when(cargo.inspect(any(com.baiyue.lastleg.appointmentplanning.AppointmentSourceService.Source.class))).thenReturn(allocation);
var mapping=mock(com.baiyue.lastleg.inboundplan.work.AppointmentWorkSourceMapper.class);
when(mapping.containerSources(anyString())).thenAnswer(c->{String id=c.getArgument(0);return List.of(
new com.baiyue.lastleg.inboundplan.work.AppointmentWorkSourceMapper.Source(Long.valueOf(id),"C"+id,id,id,"FBA","ONT8",1));});
var provider=new com.baiyue.lastleg.inboundplan.work.AppointmentWorkRequirementProvider(sources,cargo,
mock(com.baiyue.lastleg.inboundplan.service.InboundFbaAppointmentService.class),mapping);
when(f.gateway.openRequirements()).thenAnswer(c->provider.openRequirements());
var rows=java.util.stream.IntStream.rangeClosed(1,100).mapToObj(i->new ContainerSource(""+i,"CNT"+i,
Instant.parse("2026-10-01T00:00:00Z"),null,null,"v1",Flow.TRANSFER,null,
List.of(new Requirement("INBOUND:"+i,null,null,"FBA",null,"ONT8",null,List.of(),List.of(),List.of())))).toList();
when(f.gateway.discover(null,100)).thenReturn(new SourcePage(rows,null,true,"scope"));
f.sync.synchronize(new SyncRequest(null,100));verify(warehouse,times(1)).catalog("token");
verify(orders,times(100)).selectBatchIds(anyCollection());
var before=f.store.all().stream().map(f.store::write).toList();
f.sync.synchronize(new SyncRequest(null,100));verify(warehouse,times(2)).catalog("token");
assertEquals(before,f.store.all().stream().map(f.store::write).toList());
f.sync.generate(rows.get(0),Flow.TRANSFER,f.store.container("1"),ACTOR);
verify(warehouse,times(3)).catalog("token");
f.sync.generate(rows.get(0),Flow.TRANSFER,f.store.container("1"),ACTOR);
verify(warehouse,times(4)).catalog("token");
verify(orders,times(202)).selectBatchIds(anyCollection());
}
/** 百柜同页只读三次完整工作集;新路线、既有历史及重复同步都不丢失。 */
@Test void hundredContainersReuseOneGenerationWorkingSetAndKeepUnknownRoutesAndHistory() {
var f=new WorkTestFixture();
@@ -57,6 +101,9 @@ class AppointmentWorkSyncServiceTest {
var cargo=mock(com.baiyue.lastleg.appointmentplanning.AppointmentCargoVerificationService.class);
var appointments=mock(com.baiyue.lastleg.inboundplan.service.InboundFbaAppointmentService.class);
var mapping=mock(com.baiyue.lastleg.inboundplan.work.AppointmentWorkSourceMapper.class);
var scope=mock(com.baiyue.lastleg.appointmentplanning.AppointmentSourceService.ReadScope.class);
when(sources.openReadScope()).thenReturn(scope);
when(scope.resolveBatch(anyList())).thenAnswer(c->sources.resolveBatch(c.getArgument(0)));
var provider=new com.baiyue.lastleg.inboundplan.work.AppointmentWorkRequirementProvider(sources,cargo,appointments,mapping);
when(mapping.containerSources("1")).thenReturn(List.of(new com.baiyue.lastleg.inboundplan.work.AppointmentWorkSourceMapper.Source(1L,"A","1","1","FBA","ONT8",1),new com.baiyue.lastleg.inboundplan.work.AppointmentWorkSourceMapper.Source(2L,"B","2","1","FBA","ONT8",1)));
when(sources.canonicalOrders(anyList())).thenReturn(refs.stream().map(r->new com.baiyue.lastleg.appointmentplanning.AppointmentCoverageService.SourceRef(r.customerId(),r.sourceKey(),r.destinationKey())).toList());
@@ -70,7 +117,7 @@ class AppointmentWorkSyncServiceTest {
when(appointments.relatedBatchSnapshots(anyList())).thenReturn(List.of(snapshot));
var root=new Requirement("INBOUND:1",null,null,"FBA",null,"ONT8",null,List.of(),List.of(),refs);
var source=new ContainerSource("1","CNT",null,LocalDateTime.parse("2026-10-01T10:00:00"),null,"v1",Flow.TRANSFER,null,List.of(root));
doAnswer(c->provider.requirements(c.getArgument(0),c.getArgument(1))).when(f.gateway).requirements(any(),eq(Flow.TRANSFER));
when(f.gateway.openRequirements()).thenAnswer(c->provider.openRequirements());
when(f.gateway.discover(null,10)).thenReturn(new SourcePage(List.of(source),null,true,"scope"));
f.guard.setRequirementVersion(1);
f.sync.synchronize(new SyncRequest(null,10));
@@ -38,7 +38,7 @@ final class WorkTestFixture {
when(gateway.facts(anyList())).thenAnswer(c->{ var result=new LinkedHashMap<String,Fact>(); for(var state:c.<List<AppointmentWorkState>>getArgument(0)) if(state.getType()!=Type.ROUTE) result.put(state.getWorkItemKey(),facts.getOrDefault(state.getWorkItemKey(),fact(state,null,null,ExecutionStatus.PENDING,null))); return result; });
when(gateway.discover(isNull(),anyInt())).thenReturn(new SourcePage(List.of(),null,true,"scope"));
when(gateway.owner(eq("1"),any())).thenReturn(new Owner("1","运营甲"));
when(gateway.requirements(any(),any())).thenAnswer(c->c.getArgument(0,ContainerSource.class).requirements());
when(gateway.openRequirements()).thenAnswer(c->(RequirementReader)(source,flow)->source.requirements());
}
AppointmentWorkState state(String container,Type type,Flow flow,String requirement) {
var state=new AppointmentWorkState(); state.setWorkItemKey(key(container,type,requirement)); state.setContainerBookingId(container); state.setContainerNo("柜"+container);
@@ -15,7 +15,7 @@ class AppointmentWorkRequirementProviderTest {
@Test void cancelledEmptyHistoryDoesNotBlockCurrentBatchOrRestoreAlreadyAllocatedCargo() {
var sources=mock(AppointmentSourceService.class);var cargo=mock(AppointmentCargoVerificationService.class);
var appointments=mock(InboundFbaAppointmentService.class);var mapping=mock(AppointmentWorkSourceMapper.class);
var provider=new AppointmentWorkRequirementProvider(sources,cargo,appointments,mapping);
scoped(sources);var provider=new AppointmentWorkRequirementProvider(sources,cargo,appointments,mapping);
var requirement=new Requirement("INBOUND:1",null,null,"FBA",null,"ONT8",null,List.of(),List.of(),List.of());
var container=new ContainerSource("1","CNT",null,null,LocalDateTime.parse("2026-10-15T11:00:00"),"1",Flow.TRANSFER,null,List.of(requirement));
when(mapping.containerSources("1")).thenReturn(List.of(new AppointmentWorkSourceMapper.Source(11L,"C","10","1","FBA","ONT8",60)));
@@ -33,29 +33,29 @@ class AppointmentWorkRequirementProviderTest {
when(current.batchKey()).thenReturn("FBA_APPOINTMENT:2");when(current.appointmentNo()).thenReturn("AP-2");
when(current.fbaWarehouseCode()).thenReturn("ONT8");when(current.items()).thenReturn(List.of(item));when(current.replacementBatchKeys()).thenReturn(List.of());
when(appointments.relatedBatchSnapshots(anyList())).thenReturn(List.of(cancelled,current));
var actual=provider.requirements(container,Flow.TRANSFER);
var actual=provider.openRequirements().read(container,Flow.TRANSFER);
assertEquals(List.of("FBA_APPOINTMENT:2|CUSTOMER:C"),actual.stream().map(Requirement::requirementKey).toList());
assertEquals(List.of(new SourceReference("C","ORDER:11","FBA:ONT8")),actual.get(0).sources());
assertTrue(actual.get(0).replacesRequirementKeys().contains("FBA_APPOINTMENT:1|CUSTOMER:C"));
when(allocation.availablePackages()).thenReturn(20L);
actual=provider.requirements(container,Flow.TRANSFER);
actual=provider.openRequirements().read(container,Flow.TRANSFER);
assertEquals(List.of("INBOUND:1","FBA_APPOINTMENT:2|CUSTOMER:C"),actual.stream().map(Requirement::requirementKey).toList());
assertEquals(List.of("FBA_APPOINTMENT:1","FBA_APPOINTMENT:1|CUSTOMER:C"),actual.get(0).replacesRequirementKeys());
when(appointments.relatedBatchSnapshots(anyList())).thenReturn(List.of(cancelled));
when(allocation.availablePackages()).thenReturn(60L);
actual=provider.requirements(container,Flow.TRANSFER);
actual=provider.openRequirements().read(container,Flow.TRANSFER);
assertEquals(List.of("INBOUND:1"),actual.stream().map(Requirement::requirementKey).toList());
assertEquals(List.of(new SourceReference("C","ORDER:11","FBA:ONT8")),actual.get(0).sources());
assertTrue(actual.get(0).replacesRequirementKeys().contains("FBA_APPOINTMENT:1|CUSTOMER:C"));
// 非取消的空批次仍属缺失事实,不能静默跳过。
when(cancelled.appointmentStatus()).thenReturn("PENDING");
assertEquals(503,assertThrows(org.springframework.web.server.ResponseStatusException.class,()->provider.requirements(container,Flow.TRANSFER)).getStatusCode().value());
assertEquals(503,assertThrows(org.springframework.web.server.ResponseStatusException.class,()->provider.openRequirements().read(container,Flow.TRANSFER)).getStatusCode().value());
}
@Test void cancelledAndFailedBookingsRetainTheirAllocatedBatchAndOnlyAuthorityDefinesRemainder() {
var sources=mock(AppointmentSourceService.class);var cargo=mock(AppointmentCargoVerificationService.class);
var appointments=mock(InboundFbaAppointmentService.class);var mapping=mock(AppointmentWorkSourceMapper.class);
var provider=new AppointmentWorkRequirementProvider(sources,cargo,appointments,mapping);
scoped(sources);var provider=new AppointmentWorkRequirementProvider(sources,cargo,appointments,mapping);
var requirement=new Requirement("INBOUND:1",null,null,"FBA",null,"ONT8",null,List.of(),List.of(),List.of());
var container=new ContainerSource("1","CNT",null,null,LocalDateTime.parse("2026-10-15T11:00:00"),"1",Flow.TRANSFER,null,List.of(requirement));
when(mapping.containerSources("1")).thenReturn(List.of(new AppointmentWorkSourceMapper.Source(11L,"C","10","1","FBA","ONT8",60)));
@@ -72,55 +72,74 @@ class AppointmentWorkRequirementProviderTest {
when(appointments.relatedBatchSnapshots(anyList())).thenReturn(List.of(batch));
for(var status:List.of("CANCELLED","FAILED")) {
when(batch.appointmentStatus()).thenReturn(status);
var result=provider.requirements(container,Flow.TRANSFER);
var result=provider.openRequirements().read(container,Flow.TRANSFER);
assertEquals(List.of("FBA_APPOINTMENT:1|CUSTOMER:C"),result.stream().map(Requirement::requirementKey).toList());
assertEquals(List.of(new SourceReference("C","ORDER:11","FBA:ONT8")),result.get(0).sources());
assertTrue(result.get(0).replacesRequirementKeys().contains("FBA_APPOINTMENT:1"));
}
when(allocation.availablePackages()).thenReturn(20L);
assertEquals(List.of("INBOUND:1","FBA_APPOINTMENT:1|CUSTOMER:C"),provider.requirements(container,Flow.TRANSFER).stream().map(Requirement::requirementKey).toList());
assertEquals(List.of("INBOUND:1","FBA_APPOINTMENT:1|CUSTOMER:C"),provider.openRequirements().read(container,Flow.TRANSFER).stream().map(Requirement::requirementKey).toList());
when(batch.replacementBatchKeys()).thenReturn(List.of("FBA_APPOINTMENT:2"));
assertEquals(List.of("INBOUND:1"),provider.requirements(container,Flow.TRANSFER).stream().map(Requirement::requirementKey).toList());
assertEquals(List.of("INBOUND:1"),provider.openRequirements().read(container,Flow.TRANSFER).stream().map(Requirement::requirementKey).toList());
}
@Test void unbookableSourceKeepsItsPhysicalRequirementWhileAuthorityFailuresStillAbort() {
var sources=mock(AppointmentSourceService.class);var cargo=mock(AppointmentCargoVerificationService.class);
var appointments=mock(InboundFbaAppointmentService.class);var mapping=mock(AppointmentWorkSourceMapper.class);
var provider=new AppointmentWorkRequirementProvider(sources,cargo,appointments,mapping);
scoped(sources);var provider=new AppointmentWorkRequirementProvider(sources,cargo,appointments,mapping);
var requirement=new Requirement("INBOUND:1",null,null,"FBA",null,"ONT8",null,List.of(),List.of(),List.of());
var container=new ContainerSource("1","CNT",null,null,LocalDateTime.parse("2026-10-15T11:00:00"),"1",Flow.TRANSFER,null,List.of(requirement));
when(mapping.containerSources("1")).thenReturn(List.of(new AppointmentWorkSourceMapper.Source(11L,"C","10","1","FBA","ONT8",4)));
var refs=List.of(new AppointmentCoverageService.SourceRef("C","ORDER:11",null));
when(sources.canonicalOrders(anyList())).thenReturn(refs);
when(sources.resolveBatch(refs)).thenThrow(new org.springframework.web.server.ResponseStatusException(org.springframework.http.HttpStatus.BAD_REQUEST,"当前货源类型不支持FBA预约"));
var actual=provider.requirements(container,Flow.TRANSFER);
var actual=provider.openRequirements().read(container,Flow.TRANSFER);
assertEquals(1,actual.size());assertEquals("INBOUND:1",actual.get(0).requirementKey());
assertEquals(List.of(new SourceReference("C","ORDER:11",null)),actual.get(0).sources());
verifyNoInteractions(cargo,appointments);
doThrow(new org.springframework.web.server.ResponseStatusException(org.springframework.http.HttpStatus.SERVICE_UNAVAILABLE,"仓库目录暂不可用,请稍后重新核对")).when(sources).resolveBatch(refs);
assertThrows(org.springframework.web.server.ResponseStatusException.class,()->provider.requirements(container,Flow.TRANSFER));
assertThrows(org.springframework.web.server.ResponseStatusException.class,()->provider.openRequirements().read(container,Flow.TRANSFER));
}
@Test void privateDispatchKeepsPartialRemainderAndRestoresCancelledQuantity() {
var sources=mock(AppointmentSourceService.class);var cargo=mock(AppointmentCargoVerificationService.class);var appointments=mock(InboundFbaAppointmentService.class);var mapping=mock(AppointmentWorkSourceMapper.class);
var provider=new AppointmentWorkRequirementProvider(sources,cargo,appointments,mapping);
scoped(sources);var provider=new AppointmentWorkRequirementProvider(sources,cargo,appointments,mapping);
var requirement=new Requirement("INBOUND:1",null,null,"PRIVATE",null,"客户仓",null,List.of(),List.of(),List.of());
var container=new ContainerSource("1","CNT",null,null,LocalDateTime.parse("2026-10-15T11:00:00"),"1",Flow.TRANSFER,null,List.of(requirement));
when(mapping.containerSources("1")).thenReturn(List.of(new AppointmentWorkSourceMapper.Source(11L,"C","10","1","PRIVATE",null,10)));
var batch=new AppointmentWorkSourceMapper.DispatchSource(20,"D-20","PRE_ARRANGED",11,"C","ORDER:11","CUSTOMER:11",4);
when(mapping.privateDispatches("1")).thenReturn(List.of(batch));when(mapping.privateBatch("DISPATCH:20")).thenReturn(List.of(batch));
var manifest=provider.requirements(container,Flow.TRANSFER);
var manifest=provider.openRequirements().read(container,Flow.TRANSFER);
assertEquals(List.of("INBOUND:1","DISPATCH:20"),manifest.stream().map(Requirement::requirementKey).toList());
assertEquals("D-20",manifest.get(1).batchNo());assertEquals("CUSTOMER:11",manifest.get(1).sources().get(0).destinationKey());
when(mapping.privateDispatches("1")).thenReturn(List.of(new AppointmentWorkSourceMapper.DispatchSource(20,"D-20","CANCELLED",11,"C","ORDER:11","CUSTOMER:11",4)));
var restored=provider.requirements(container,Flow.TRANSFER);assertEquals(1,restored.size());assertEquals(List.of("DISPATCH:20"),restored.get(0).replacesRequirementKeys());
var restored=provider.openRequirements().read(container,Flow.TRANSFER);assertEquals(1,restored.size());assertEquals(List.of("DISPATCH:20"),restored.get(0).replacesRequirementKeys());
verifyNoInteractions(sources,cargo,appointments);
}
@Test void chosenDirectRoutePreservesInboundKeyBeforeAnyActualUnloading() {
var sources=mock(AppointmentSourceService.class); var cargo=mock(AppointmentCargoVerificationService.class);
var appointments=mock(InboundFbaAppointmentService.class); var mapping=mock(AppointmentWorkSourceMapper.class);
var provider=new AppointmentWorkRequirementProvider(sources,cargo,appointments,mapping);
scoped(sources);var provider=new AppointmentWorkRequirementProvider(sources,cargo,appointments,mapping);
var requirement=new Requirement("INBOUND:1",null,null,"FBA",null,"ONT8",null,List.of(),List.of(),List.of());
var source=new ContainerSource("1","CNT",null,null,LocalDateTime.parse("2026-10-15T11:00:00"),"1",Flow.UNKNOWN,null,List.of(requirement));
assertEquals(List.of(requirement),provider.requirements(source,Flow.DIRECT));
assertEquals(List.of(requirement),provider.openRequirements().read(source,Flow.DIRECT));
verifyNoInteractions(sources,cargo,appointments,mapping);
}
/** 无尾程映射时保留原需求,不读取或依赖仓库目录。 */
@Test void missingLastlegSourcesDoNotOpenDirectoryScope() {
var sources=mock(AppointmentSourceService.class);var cargo=mock(AppointmentCargoVerificationService.class);
var appointments=mock(InboundFbaAppointmentService.class);var mapping=mock(AppointmentWorkSourceMapper.class);
var provider=new AppointmentWorkRequirementProvider(sources,cargo,appointments,mapping);
var requirement=new Requirement("INBOUND:1",null,null,"FBA",null,"ONT8",null,List.of(),List.of(),List.of());
var source=new ContainerSource("1","CNT",null,null,LocalDateTime.parse("2026-10-15T11:00:00"),"1",Flow.TRANSFER,null,List.of(requirement));
var reader=provider.openRequirements();verifyNoInteractions(sources,cargo,appointments,mapping);
assertEquals(List.of(requirement),reader.read(source,Flow.TRANSFER));verifyNoInteractions(sources,cargo,appointments);
}
/** 保留原来源模拟,显式接入本次读取作用域。 */
private static void scoped(AppointmentSourceService sources) {
when(sources.openReadScope()).thenAnswer(call->{
var scope=mock(AppointmentSourceService.ReadScope.class);
when(scope.resolveBatch(anyList())).thenAnswer(c->sources.resolveBatch(c.getArgument(0)));
return scope;
});
}
}