package com.baiyue.lastleg.appointmentplanning; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.Test; import org.springframework.http.HttpStatus; import org.springframework.web.server.ResponseStatusException; import java.util.*; import java.util.stream.IntStream; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; class AppointmentCargoVerificationBatchTest { @Test void inspectionUsesTwoBoundedBatchesAndRetainsPerSourcePrerequisitesAndAllocations() { var sources=mock(AppointmentSourceService.class);var mapper=mock(AppointmentCargoVerificationMapper.class); var service=new AppointmentCargoVerificationService(sources,mapper,new ObjectMapper(),new AppointmentCargoReferenceRepository(mapper)); var refs=IntStream.rangeClosed(1,200).mapToObj(i->new AppointmentCoverageService.SourceRef("C","ORDER:"+i,"FBA:ONT8")).toList(); when(sources.inspectBatch(anyList())).thenAnswer(call->call.>getArgument(0).stream().map(ref-> ref.sourceKey().equals("ORDER:2")?new AppointmentSourceService.Resolution(ref,null,new ResponseStatusException(HttpStatus.BAD_REQUEST,"接仓点不在启用的美国仓目录中")): new AppointmentSourceService.Resolution(ref,new AppointmentSourceService.Source(ref.sourceKey(),ref.customerId(),Long.valueOf(ref.sourceKey().substring(6)),"IN",null,"CONTAINER","100","仓",ref.destinationKey(),"ONT8",10,"ORDER:1",true,List.of()),null)).toList()); when(mapper.allocatedBatch(anyList())).thenAnswer(call->call.>getArgument(0).stream().filter(r->r.sourceKey().equals("ORDER:1")) .map(r->new AppointmentCargoVerificationMapper.SourceAllocation(r.customerId(),r.sourceKey(),4,0)).toList()); var result=service.inspectBatch(refs); assertEquals(200,result.size());assertEquals(6L,result.get(0).requireContext().availablePackages()); assertEquals("接仓点不在启用的美国仓目录中",assertThrows(ResponseStatusException.class,()->result.get(1).requireContext()).getReason()); assertEquals(10L,result.get(199).requireContext().availablePackages()); verify(sources,times(2)).inspectBatch(argThat(batch->batch.size()==100)); verify(mapper,times(2)).latestBatch(anyList());verify(mapper,times(2)).allocatedBatch(anyList());verify(mapper,times(2)).originalShipmentReferences(anyList());verifyNoMoreInteractions(mapper,sources); when(sources.inspectBatch(anyList())).thenThrow(new ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE,"目录不可用")); assertEquals(503,assertThrows(ResponseStatusException.class,()->service.inspectBatch(refs)).getStatusCode().value()); } }