fix(firstleg): reuse source manifest within work fact batches

This commit is contained in:
2026-10-15 17:52:56 +08:00
parent 994215d981
commit 23ce486539
2 changed files with 37 additions and 2 deletions
@@ -7,6 +7,8 @@ import org.junit.jupiter.api.Test;
import org.springframework.web.server.ResponseStatusException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.IntStream;
import com.baiyue.firstleg.portschedule.dto.AppointmentWorkSourceDtos.*;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
@@ -42,4 +44,32 @@ class AppointmentWorkSourceServiceTest {
assertEquals(401,assertThrows(ResponseStatusException.class,()->guarded.discover(null,null,20)).getStatusCode().value());
verifyNoInteractions(mapper);
}
@Test void factBatchReadsManifestOnceAndPreservesEachContainerIdentity() {
when(mapper.sources()).thenReturn(IntStream.rangeClosed(1,100).mapToObj(id->source(id,1)).toList());
when(mapper.cargo(anyLong())).thenReturn(List.of());
var fact=mock(Fact.class);
when(facts.read(any(),any())).thenAnswer(invocation->{
FactRequest request=invocation.getArgument(0); ContainerSource source=invocation.getArgument(1);
assertEquals(request.containerBookingId(),source.containerBookingId());
return fact;
});
var requests=IntStream.rangeClosed(1,100).boxed().flatMap(id->List.of(
request("PICKUP-"+id,"PICKUP",id),request("RETURN-"+id,"EMPTY_RETURN",id)).stream()).toList();
assertEquals(200,service.facts(requests).size());
verify(mapper,times(1)).sources();
for(long id=1;id<=100;id++) verify(mapper,times(1)).cargo(id);
verify(facts,times(200)).read(any(),any());
}
@Test void nextFactBatchReadsFreshManifestAndRejectsRemovedContainer() {
when(mapper.sources()).thenReturn(List.of(source(1,1)),List.of());
when(mapper.cargo(1)).thenReturn(List.of());
var request=request("PICKUP-1","PICKUP",1);
service.facts(List.of(request));
assertEquals(404,assertThrows(ResponseStatusException.class,()->service.facts(List.of(request))).getStatusCode().value());
verify(mapper,times(2)).sources();
verify(facts,times(1)).read(any(),any());
}
private FactRequest request(String key,String type,long id) {
return new FactRequest(key,type,"TRANSIT",Long.toString(id),null,null,null,List.of(),List.of(),List.of());
}
}