feat(appointments): unify pre-inbound planning and confirmed FBA bookings

This commit is contained in:
2026-10-10 10:38:06 +08:00
parent 24f5ccf7ff
commit c4c1bddeec
54 changed files with 1623 additions and 744 deletions
@@ -27,9 +27,15 @@ class InboundPlanSchemaContractTest {
assertTrue(schema.contains("create table if not exists ll_inbound_pallet_merge"));
assertTrue(schema.contains("create table if not exists ll_inbound_fba_appointment"));
assertTrue(schema.contains("unique key uk_ll_fba_appointment_no (appointment_no)"));
assertTrue(schema.contains("expected_loading_at"));
String appointment = schema.substring(schema.indexOf("create table if not exists ll_inbound_fba_appointment ("), schema.indexOf("create table if not exists ll_inbound_fba_appointment_item ("));
assertFalse(appointment.contains("customer_id"));
assertFalse(appointment.contains("expected_loading_at"));
assertTrue(appointment.contains("confirmation_no varchar(128) null"));
assertTrue(appointment.contains("planned_date date null"));
assertTrue(appointment.contains("appointment_start_utc datetime(3) null"));
assertTrue(schema.contains("unique key uk_ll_fba_active_source (active_source_key)"));
assertTrue(schema.contains("unique key uk_ll_fba_active_order (active_order_id)"));
assertTrue(schema.contains("create table if not exists ll_fba_appointment_command"));
String items = schema.substring(schema.indexOf("create table if not exists ll_inbound_fba_appointment_item ("));
assertTrue(items.substring(0, items.indexOf("engine=innodb")).contains("customer_id varchar(128) not null"));
assertTrue(schema.contains("planned_arrival_date"));
@@ -55,12 +55,11 @@ class InboundPlanControllerTest {
@Test
void platformAppointmentUpdateUsesSharedAppointmentNumber() {
UpdateFbaAppointmentRequest request = new UpdateFbaAppointmentRequest(
LocalDateTime.of(2026, 8, 20, 10, 30), LocalDateTime.of(2026, 8, 21, 8, 0),
"PENDING", "dock", null, 0);
new com.baiyue.lastleg.inboundplan.dto.FbaAppointmentDetails(null,LocalDateTime.of(2026,8,20,10,30),null,null,null,null,"PENDING","dock",null,null),0,"update-key");
controller.updateFbaAppointment(platform(), "APT-1", request);
verify(appointmentService).update("APT-1", request);
verify(appointmentService).update("7", "APT-1", request);
}
@Test
@@ -25,8 +25,8 @@ class InboundPlanDispatchStatusPersistenceTest {
db.execute("CREATE TABLE ll_inbound_plan(id BIGINT,plan_no VARCHAR(50),customer_id VARCHAR(20),overseas_warehouse_id BIGINT,assigned_flag BOOLEAN,merge_required BOOLEAN,urgent_flag BOOLEAN,status VARCHAR(30),created_at TIMESTAMP,version INT)");
db.execute("CREATE TABLE ll_inbound_plan_line(id BIGINT,plan_id BIGINT,order_id BIGINT,inbound_code VARCHAR(50),destination_warehouse_id VARCHAR(30),destination_warehouse_code VARCHAR(30),destination_warehouse_name VARCHAR(50),fba_warehouse_code VARCHAR(30),required_package_count INT,volume_cbm DECIMAL(18,3),planned_pallet_count INT)");
db.execute("CREATE TABLE ll_order(id BIGINT,customer_id VARCHAR(20),lx_code VARCHAR(50),order_type VARCHAR(30),overseas_warehouse_code VARCHAR(30),overseas_warehouse_name VARCHAR(50),customer_name VARCHAR(50))");
db.execute("CREATE TABLE ll_inbound_fba_appointment_item(id BIGINT,line_id BIGINT,appointment_id BIGINT,customer_id VARCHAR(20))");
db.execute("CREATE TABLE ll_inbound_fba_appointment(id BIGINT,created_at TIMESTAMP,appointment_no VARCHAR(50),appointment_status VARCHAR(30),appointment_at TIMESTAMP,expected_loading_at TIMESTAMP)");
db.execute("CREATE TABLE ll_inbound_fba_appointment_item(id BIGINT,order_id BIGINT,appointment_id BIGINT,customer_id VARCHAR(20),fba_warehouse_code VARCHAR(30),active BOOLEAN)");
db.execute("CREATE TABLE ll_inbound_fba_appointment(id BIGINT,created_at TIMESTAMP,appointment_no VARCHAR(50),appointment_status VARCHAR(30),appointment_at TIMESTAMP)");
db.execute("CREATE TABLE ll_inbound_plan_pallet_allocation(line_id BIGINT,picked_package_count INT)");
db.execute("CREATE TABLE ll_order_detail(order_id BIGINT,customer_id VARCHAR(20),gross_weight_kg DECIMAL(18,3),net_weight_kg DECIMAL(18,3))");
db.execute("CREATE TABLE ll_inbound_order(id BIGINT,customer_id VARCHAR(20),gross_weight_kg DECIMAL(18,3),net_weight_kg DECIMAL(18,3))");
@@ -83,8 +83,8 @@ class InboundPlanDispatchStatusPersistenceTest {
db.update("INSERT INTO ll_inbound_plan VALUES(4,'LIP-2','B',90,TRUE,FALSE,FALSE,'RECEIVED',CURRENT_TIMESTAMP,0)");
db.update("INSERT INTO ll_inbound_plan_line VALUES(5,4,6,'IN-2','91','ABE4','ABE4','ABE4',1,1,1)");
db.update("INSERT INTO ll_order VALUES(6,'B','LX-2','FIRST_LEG','LA','洛杉矶仓','客户B')");
db.update("INSERT INTO ll_inbound_fba_appointment VALUES(7,CURRENT_TIMESTAMP,'APT-SHARED','SUCCESS',CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)");
db.update("INSERT INTO ll_inbound_fba_appointment_item VALUES(8,2,7,'A'),(9,5,7,'B')");
db.update("INSERT INTO ll_inbound_fba_appointment VALUES(7,CURRENT_TIMESTAMP,'APT-SHARED','SUCCESS',CURRENT_TIMESTAMP)");
db.update("INSERT INTO ll_inbound_fba_appointment_item VALUES(8,3,7,'A','ABE4',TRUE),(9,6,7,'B','ABE4',TRUE)");
assertThat(mapper.rows(null, query(null), 0, 20)).hasSize(2)
.extracting(InboundPlanListResponse::appointmentNo).containsOnly("APT-SHARED");
for (String customer : new String[]{"A", "B"}) {
@@ -0,0 +1,163 @@
package com.baiyue.lastleg.inboundplan.service;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
import com.baiyue.lastleg.appointmentplanning.*;
import com.baiyue.lastleg.inboundplan.dto.*;
import com.baiyue.lastleg.inboundplan.mapper.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
import org.h2.jdbcx.JdbcDataSource;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.*;
import javax.sql.DataSource;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
class FbaAppointmentPersistenceTest {
interface OrderVersionProbe {
@org.apache.ibatis.annotations.Select("SELECT version FROM ll_order WHERE id=#{id}")
int version(long id);
}
@Test void lockingOrdersInvalidatesFactsReadBeforeTheLock() {
try(var context=new AnnotationConfigApplicationContext(Config.class)) {
var transaction=new org.springframework.transaction.support.TransactionTemplate(context.getBean(DataSourceTransactionManager.class));
transaction.execute(status->{
var probe=context.getBean(SqlSessionTemplate.class).getMapper(OrderVersionProbe.class);
assertEquals(0,probe.version(1L));
new JdbcTemplate(context.getBean(DataSource.class)).update("UPDATE ll_order SET version=1 WHERE id=1");
assertEquals(0,probe.version(1L));
context.getBean(InboundFbaAppointmentQueryMapper.class).lockOrders(List.of(1L));
assertEquals(1,probe.version(1L));
return null;
});
}
}
private static List<AppointmentSourceService.Source> cargo(Integer secondPackages) {
return List.of(new AppointmentSourceService.Source("ORDER:1","A",1L,"IN-A",null,null,"100","美国仓","FBA:ONT8","ONT8",10,"ORDER:1",true,List.of()),
new AppointmentSourceService.Source("ORDER:2","B",2L,"IN-B",null,null,"100","美国仓","FBA:ONT8","ONT8",secondPackages,"ORDER:1",true,List.of()));
}
private static SourceFbaAppointmentRequest request(String key) {
return new SourceFbaAppointmentRequest(List.of(new AppointmentCoverageService.SourceRef("A","ORDER:1","FBA:ONT8"),new AppointmentCoverageService.SourceRef("B","ORDER:2","FBA:ONT8")),
"100","ONT8",new FbaAppointmentDetails(null,null,null,null,null,null,"PENDING",null,null,null),key);
}
@Test void failureAfterHeaderAndFirstItemRollsBackTheCommandAndEntireAggregate() {
try(var context=new AnnotationConfigApplicationContext(Config.class)) {
var service=context.getBean(InboundFbaAppointmentService.class);assertTrue(org.springframework.aop.support.AopUtils.isAopProxy(service));
var db=new JdbcTemplate(context.getBean(DataSource.class));
db.execute("ALTER TABLE ll_inbound_fba_appointment_item ADD CONSTRAINT fail_second_item CHECK(package_count<>13)");
when(context.getBean(AppointmentSourceService.class).resolveBatch(anyList())).thenReturn(cargo(13));
assertThrows(RuntimeException.class,()->service.createFromSources("operator",request("retry-after-rollback")));
for(var table:List.of("ll_inbound_fba_appointment","ll_inbound_fba_appointment_item","ll_fba_appointment_command")) assertEquals(0,db.queryForObject("SELECT COUNT(*) FROM "+table,Integer.class));
db.execute("ALTER TABLE ll_inbound_fba_appointment_item DROP CONSTRAINT fail_second_item");
var saved=service.createFromSources("operator",request("retry-after-rollback"));
assertEquals(saved,service.createFromSources("operator",request("retry-after-rollback")));
assertEquals(1,db.queryForObject("SELECT COUNT(*) FROM ll_inbound_fba_appointment",Integer.class));
assertEquals(2,db.queryForObject("SELECT COUNT(*) FROM ll_inbound_fba_appointment_item",Integer.class));
assertEquals(1,service.unscheduled(1,20).total());
}
}
@Test void concurrentDifferentCommandsCannotReserveTheSameCrossCustomerCargoTwice() throws Exception {
try(var context=new AnnotationConfigApplicationContext(Config.class)) {
var service=context.getBean(InboundFbaAppointmentService.class);var start=new CountDownLatch(1);var executor=Executors.newFixedThreadPool(2);
try {
var first=executor.submit(()->attempt(service,start,"first"));var second=executor.submit(()->attempt(service,start,"second"));start.countDown();
assertEquals(1,(first.get(10,TimeUnit.SECONDS)?1:0)+(second.get(10,TimeUnit.SECONDS)?1:0));
var db=new JdbcTemplate(context.getBean(DataSource.class));
assertEquals(1,db.queryForObject("SELECT COUNT(*) FROM ll_inbound_fba_appointment",Integer.class));
assertEquals(2,db.queryForObject("SELECT COUNT(*) FROM ll_inbound_fba_appointment_item WHERE active=1",Integer.class));
assertEquals(1,db.queryForObject("SELECT COUNT(*) FROM ll_fba_appointment_command",Integer.class));
} finally {executor.shutdownNow();}
}
}
@Test void cancellationReleasesGeneratedClaimsWhileReplayKeepsItsOriginalSnapshot() {
try(var context=new AnnotationConfigApplicationContext(Config.class)) {
var service=context.getBean(InboundFbaAppointmentService.class);var first=service.createFromSources("operator",request("first"));
service.delete("operator",first.appointmentNo(),0);
var second=service.createFromSources("operator",request("second"));assertNotEquals(first.appointmentNo(),second.appointmentNo());
assertEquals("CANCELLED",service.detail(first.appointmentNo()).appointmentStatus());
assertEquals(first,service.createFromSources("operator",request("first")));
var db=new JdbcTemplate(context.getBean(DataSource.class));assertEquals(4,db.queryForObject("SELECT COUNT(*) FROM ll_inbound_fba_appointment_item",Integer.class));
assertEquals(2,db.queryForObject("SELECT COUNT(*) FROM ll_inbound_fba_appointment_item WHERE active=1",Integer.class));
}
}
@Test void unknownQuantityOnReviewDoesNotSilentlyKeepThePreviousKnownQuantity() {
try(var context=new AnnotationConfigApplicationContext(Config.class)) {
var service=context.getBean(InboundFbaAppointmentService.class);var saved=service.createFromSources("operator",request("create"));
when(context.getBean(AppointmentSourceService.class).resolveBatch(anyList())).thenReturn(cargo(null));
var updated=service.update("operator",saved.appointmentNo(),new UpdateFbaAppointmentRequest(request("unused").details(),0,"review"));
assertNull(updated.items().get(1).packageCount());
assertNull(new JdbcTemplate(context.getBean(DataSource.class)).queryForObject("SELECT package_count FROM ll_inbound_fba_appointment_item WHERE source_key='ORDER:2'",Integer.class));
}
}
@Test void actualIncomingBindsTheOriginalBookingInItsOwnTransactionWithoutChangingPlannedFacts() {
try(var context=new AnnotationConfigApplicationContext(Config.class)) {
var service=context.getBean(InboundFbaAppointmentService.class);var linker=context.getBean(AppointmentSourceLinker.class);
var before=new AppointmentSourceService.Source("BOOKING:B-1","C",null,null,"B-1",null,"100","美国仓","FBA:ONT8","ONT8",null,"BOOKING:1",true,List.of());
when(context.getBean(AppointmentSourceService.class).resolveBatch(anyList())).thenReturn(List.of(before));
var request=new SourceFbaAppointmentRequest(List.of(new AppointmentCoverageService.SourceRef("C","BOOKING:B-1","FBA:ONT8")),"100","ONT8",request("unused").details(),"before-incoming");
var saved=service.createFromSources("operator",request);
assertNull(saved.items().get(0).orderId());
assertThrows(org.springframework.transaction.IllegalTransactionStateException.class,()->linker.materialized("C","B-1",100L));
var transaction=new org.springframework.transaction.support.TransactionTemplate(context.getBean(DataSourceTransactionManager.class));
var db=new JdbcTemplate(context.getBean(DataSource.class));
assertThrows(IllegalStateException.class,()->transaction.execute(status->{db.update("INSERT INTO ll_order(id) VALUES(100)");linker.materialized("C","B-1",100L);throw new IllegalStateException("incoming failed after link");}));
assertNull(service.detail(saved.appointmentNo()).items().get(0).orderId());
assertEquals(0,db.queryForObject("SELECT COUNT(*) FROM ll_order WHERE id=100",Integer.class));
transaction.execute(status->{db.update("INSERT INTO ll_order(id) VALUES(100)");linker.materialized("C","B-1",100L);linker.materialized("C","B-1",100L);return null;});
var linked=service.detail(saved.appointmentNo());assertEquals(100L,linked.items().get(0).orderId());assertEquals("WT-REAL",linked.items().get(0).inboundCode());
assertNull(linked.items().get(0).packageCount());assertEquals("BOOKING:1",linked.items().get(0).sourceVersion());assertEquals("PENDING",linked.appointmentStatus());
assertEquals(saved,service.createFromSources("operator",request));
assertEquals(1,db.queryForObject("SELECT COUNT(*) FROM ll_inbound_fba_appointment_item",Integer.class));
}
}
private static boolean attempt(InboundFbaAppointmentService service,CountDownLatch start,String key) throws Exception {
start.await();try{service.createFromSources("operator",request(key));return true;}catch(org.springframework.web.server.ResponseStatusException error){assertEquals(409,error.getStatusCode().value());return false;}
}
@Configuration @EnableTransactionManagement
static class Config {
@Bean DataSource dataSource() throws Exception {
var ds=new JdbcDataSource();ds.setURL("jdbc:h2:mem:appointment-"+UUID.randomUUID()+";MODE=MySQL;DB_CLOSE_DELAY=-1;LOCK_TIMEOUT=5000");
String baseline;try(var input=FbaAppointmentPersistenceTest.class.getResourceAsStream("/db/lastleg.sql")){baseline=new String(Objects.requireNonNull(input).readAllBytes(),StandardCharsets.UTF_8);}
try(var c=ds.getConnection();var statement=c.createStatement()) {
for(var table:List.of("ll_inbound_fba_appointment","ll_inbound_fba_appointment_item","ll_fba_appointment_command")) {
int start=baseline.indexOf("CREATE TABLE IF NOT EXISTS "+table+" (");assertTrue(start>=0);
String sql=baseline.substring(start,baseline.indexOf(';',start)+1).replace(" STORED","").replaceAll(" ENGINE=InnoDB[^;]*;",";");statement.execute(sql);
}
statement.execute("CREATE TABLE ll_order(id BIGINT PRIMARY KEY,version INT DEFAULT 0)");statement.execute("INSERT INTO ll_order(id) VALUES(1),(2)");
statement.execute("CREATE TABLE ll_dispatch_item(order_id BIGINT,customer_id VARCHAR(128),dispatch_order_id BIGINT)");
statement.execute("CREATE TABLE ll_dispatch_order(id BIGINT,customer_id VARCHAR(128),status VARCHAR(32))");
}return ds;
}
@Bean DataSourceTransactionManager transactionManager(DataSource dataSource){return new DataSourceTransactionManager(dataSource);}
@Bean SqlSessionFactory sessionFactory(DataSource dataSource) {
var config=new MybatisConfiguration();config.setEnvironment(new Environment("test",new SpringManagedTransactionFactory(),dataSource));config.setMapUnderscoreToCamelCase(true);
config.addMapper(InboundFbaAppointmentMapper.class);config.addMapper(InboundFbaAppointmentItemMapper.class);config.addMapper(InboundFbaAppointmentQueryMapper.class);config.addMapper(FbaAppointmentCommandMapper.class);
config.addMapper(OrderVersionProbe.class);
return new MybatisSqlSessionFactoryBuilder().build(config);
}
@Bean SqlSessionTemplate template(SqlSessionFactory factory){return new SqlSessionTemplate(factory);}
@Bean InboundFbaAppointmentMapper appointmentMapper(SqlSessionTemplate session){return session.getMapper(InboundFbaAppointmentMapper.class);}
@Bean InboundFbaAppointmentItemMapper itemMapper(SqlSessionTemplate session){return session.getMapper(InboundFbaAppointmentItemMapper.class);}
@Bean InboundFbaAppointmentQueryMapper queryMapper(SqlSessionTemplate session){return session.getMapper(InboundFbaAppointmentQueryMapper.class);}
@Bean FbaAppointmentCommandMapper commands(SqlSessionTemplate session){return session.getMapper(FbaAppointmentCommandMapper.class);}
@Bean AppointmentSourceService sources(){var mock=mock(AppointmentSourceService.class);when(mock.resolveBatch(anyList())).thenReturn(cargo(20));when(mock.destinationTimezone("ONT8")).thenReturn("America/Los_Angeles");return mock;}
@Bean AppointmentBatchPreviewMapper flags(){var mock=mock(AppointmentBatchPreviewMapper.class);when(mock.flags(anyList())).thenReturn(List.of(new AppointmentBatchPreviewMapper.Flags(1L,false,false,false),new AppointmentBatchPreviewMapper.Flags(2L,false,false,false)));return mock;}
@Bean ObjectMapper json(){return new ObjectMapper().findAndRegisterModules();}
@Bean AppointmentCoverageMapper destinationFacts(){return mock(AppointmentCoverageMapper.class);}
@Bean com.baiyue.lastleg.mapper.LastlegOrderMapper orders(){var mock=mock(com.baiyue.lastleg.mapper.LastlegOrderMapper.class);var row=new com.baiyue.lastleg.entity.LastlegOrder();row.setId(100L);row.setCustomerId("C");row.setInboundCode("WT-REAL");when(mock.selectById(100L)).thenReturn(row);return mock;}
@Bean AppointmentExternalSourceGateway bookings(){var mock=mock(AppointmentExternalSourceGateway.class);when(mock.resolve(anyList())).thenReturn(List.of(new AppointmentExternalSourceGateway.Source("C","B-1","TR-1","RECEIVED","100","FBA:ONT8",100L,10,2L)));return mock;}
@Bean AppointmentSourceLinker linker(com.baiyue.lastleg.mapper.LastlegOrderMapper orders,InboundFbaAppointmentItemMapper items,AppointmentExternalSourceGateway bookings){return new AppointmentSourceLinker(orders,items,bookings);}
@Bean InboundFbaAppointmentService service(){return new InboundFbaAppointmentService();}
}
}
@@ -1,286 +1,175 @@
package com.baiyue.lastleg.inboundplan.service;
import com.baiyue.lastleg.inboundplan.dto.BatchFbaAppointmentRequest;
import com.baiyue.lastleg.inboundplan.dto.UpdateFbaAppointmentRequest;
import com.baiyue.lastleg.inboundplan.entity.InboundFbaAppointment;
import com.baiyue.lastleg.inboundplan.entity.InboundFbaAppointmentItem;
import com.baiyue.lastleg.inboundplan.mapper.InboundFbaAppointmentItemMapper;
import com.baiyue.lastleg.inboundplan.mapper.InboundFbaAppointmentMapper;
import com.baiyue.lastleg.inboundplan.mapper.InboundFbaAppointmentQueryMapper;
import com.baiyue.lastleg.inboundplan.dto.*;
import com.baiyue.lastleg.inboundplan.entity.*;
import com.baiyue.lastleg.inboundplan.mapper.*;
import com.baiyue.lastleg.inboundplan.model.FbaAppointmentLineSnapshot;
import com.baiyue.lastleg.appointmentplanning.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;
import java.time.*;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.time.LocalDateTime;
import java.time.LocalDate;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class InboundFbaAppointmentServiceTest {
@Mock InboundFbaAppointmentMapper appointmentMapper;
@Mock InboundFbaAppointmentItemMapper itemMapper;
@Mock InboundFbaAppointmentQueryMapper queryMapper;
InboundFbaAppointmentMapper headers;
InboundFbaAppointmentItemMapper items;
InboundFbaAppointmentQueryMapper query;
FbaAppointmentCommandMapper commands;
AppointmentSourceService sources;
AppointmentBatchPreviewMapper flags;
InboundFbaAppointmentService service;
final Map<String,AppointmentSourceService.Source> cargo=new LinkedHashMap<>();
final List<InboundFbaAppointmentItem> storedItems=new ArrayList<>();
final List<InboundFbaAppointment> storedHeaders=new ArrayList<>();
final Map<String,FbaAppointmentCommandMapper.Command> journal=new HashMap<>();
final LocalDateTime at=LocalDateTime.parse("2026-10-15T09:00:00");
@BeforeEach void setup() {
headers=mock(InboundFbaAppointmentMapper.class);items=mock(InboundFbaAppointmentItemMapper.class);query=mock(InboundFbaAppointmentQueryMapper.class);
commands=mock(FbaAppointmentCommandMapper.class);sources=mock(AppointmentSourceService.class);flags=mock(AppointmentBatchPreviewMapper.class);
service=new InboundFbaAppointmentService();
ReflectionTestUtils.setField(service,"appointmentMapper",headers);ReflectionTestUtils.setField(service,"itemMapper",items);ReflectionTestUtils.setField(service,"queryMapper",query);
ReflectionTestUtils.setField(service,"commands",commands);ReflectionTestUtils.setField(service,"sources",sources);ReflectionTestUtils.setField(service,"flags",flags);ReflectionTestUtils.setField(service,"json",new ObjectMapper().findAndRegisterModules());
when(commands.find(anyString())).thenAnswer(c->journal.get(c.getArgument(0)));
when(commands.begin(anyString(),anyString(),anyString(),anyString(),any())).thenAnswer(c->{journal.put(c.getArgument(0),new FbaAppointmentCommandMapper.Command(c.getArgument(1),null));return 1;});
when(commands.finish(anyString(),anyString(),anyString())).thenAnswer(c->{String key=c.getArgument(0);journal.put(key,new FbaAppointmentCommandMapper.Command(journal.get(key).requestHash(),c.getArgument(2)));return 1;});
when(sources.resolveBatch(anyList())).thenAnswer(c->{List<AppointmentCoverageService.SourceRef> refs=c.getArgument(0);return refs.stream().map(r->cargo.get(r.sourceKey())).toList();});
when(sources.destinationTimezone(anyString())).thenReturn("America/Los_Angeles");
when(query.lockOrders(anyList())).thenAnswer(c->c.getArgument(0));
when(flags.flags(anyList())).thenAnswer(c->{List<Long> ids=c.getArgument(0);return ids.stream().map(id->new AppointmentBatchPreviewMapper.Flags(id,false,false,false)).toList();});
when(headers.insert(any(InboundFbaAppointment.class))).thenAnswer(c->{storedHeaders.add(c.getArgument(0));return 1;});
when(headers.selectOne(any())).thenAnswer(c->storedHeaders.isEmpty()?null:storedHeaders.get(0));
when(headers.update(any(InboundFbaAppointment.class),any())).thenReturn(1);
when(items.insert(any(InboundFbaAppointmentItem.class))).thenAnswer(c->{storedItems.add(c.getArgument(0));return 1;});
when(items.selectList(any())).thenAnswer(c->List.copyOf(storedItems));
add("ORDER:1","A",1L,"100","ONT8",10);add("ORDER:2","B",2L,"100","ONT8",20);
}
void add(String key,String customer,Long order,String origin,String destination,Integer packages) {
cargo.put(key,new AppointmentSourceService.Source(key,customer,order,order==null?null:"IN-"+order,key.startsWith("BOOKING:")?key.substring(8):null,null,origin,"美国仓","FBA:"+destination,destination,packages,"ORDER:1",true,List.of()));
}
List<AppointmentCoverageService.SourceRef> refs(String... keys) {return Arrays.stream(keys).map(key->new AppointmentCoverageService.SourceRef(cargo.get(key).customerId(),key,cargo.get(key).destinationKey())).toList();}
FbaAppointmentDetails pending(String remark) {return new FbaAppointmentDetails(LocalDate.of(2026,10,15),null,null,null,null,null,"PENDING",remark,null,null);}
FbaAppointmentDetails confirmed() {return new FbaAppointmentDetails(LocalDate.of(2026,10,14),null,null,"EXT-ONT8-1",at,at.plusHours(1),"SUCCESS","已核实",null,null);}
SourceFbaAppointmentRequest request(String key,FbaAppointmentDetails details,String... sources) {return new SourceFbaAppointmentRequest(refs(sources),"100","ONT8",details,key);}
FbaAppointmentResponse create(String key) {return service.createFromSources("operator",request(key,pending(null),"ORDER:1","ORDER:2"));}
private InboundFbaAppointmentService service;
private final LocalDateTime appointmentAt = LocalDateTime.of(2026, 8, 20, 10, 30);
private final LocalDateTime expectedLoadingAt = LocalDateTime.of(2026, 8, 21, 8, 0);
@BeforeEach
void setUp() {
service = new InboundFbaAppointmentService();
ReflectionTestUtils.setField(service, "appointmentMapper", appointmentMapper);
ReflectionTestUtils.setField(service, "itemMapper", itemMapper);
ReflectionTestUtils.setField(service, "queryMapper", queryMapper);
lenient().when(itemMapper.selectList(any())).thenReturn(List.of());
lenient().when(appointmentMapper.update(any(InboundFbaAppointment.class), any())).thenReturn(1);
@Test void refusesOriginOrDestinationChangedAfterSelection() {
var intent=request("origin-changed",pending(null),"ORDER:1");
add("ORDER:1","A",1L,"200","ONT8",10);
assertEquals(409,assertThrows(ResponseStatusException.class,()->service.createFromSources("operator",intent)).getStatusCode().value());
add("ORDER:1","A",1L,"100","LAX9",10);
var changed=new SourceFbaAppointmentRequest(refs("ORDER:1"),"100","ONT8",pending(null),"destination-changed");
assertEquals(409,assertThrows(ResponseStatusException.class,()->service.createFromSources("operator",changed)).getStatusCode().value());
verify(headers,never()).insert(any(InboundFbaAppointment.class));
}
@Test
void createsOneBatchForSelectedLinesAndFreezesFbaWarehouse() throws Exception {
assertEquals(true, InboundFbaAppointmentService.class
.getMethod("batchCreate", String.class, BatchFbaAppointmentRequest.class)
.getAnnotation(Transactional.class) != null);
when(queryMapper.lockLines(List.of(11L, 12L))).thenReturn(List.of(
line(11L, "IN-11", "ONT8"), line(12L, "IN-12", "ONT8")));
var result = service.batchCreate("7", request(List.of(11L, 12L), "APT-1"));
assertEquals("APT-1", result.appointmentNo());
assertEquals("ONT8", result.fbaWarehouseCode());
assertEquals(expectedLoadingAt, result.expectedLoadingAt());
assertEquals(2, result.selectedCount());
var captured = org.mockito.ArgumentCaptor.forClass(InboundFbaAppointmentItem.class);
verify(itemMapper, org.mockito.Mockito.times(2)).insert(captured.capture());
assertEquals(List.of("CUSTOMER-11", "CUSTOMER-12"), captured.getAllValues().stream()
.map(InboundFbaAppointmentItem::getCustomerId).toList());
verify(appointmentMapper).insert(any(InboundFbaAppointment.class));
verify(itemMapper, org.mockito.Mockito.times(2)).insert(any(InboundFbaAppointmentItem.class));
@Test void savesCrossCustomerArrangementBeforeAnyInboundPlanOrExternalNumberExists() {
var result=create("create");assertTrue(result.appointmentNo().startsWith("AP-"));assertNull(result.confirmationNo());assertNull(result.appointmentAt());
assertEquals(LocalDate.of(2026,10,15),result.plannedDate());assertEquals(2,result.selectedCount());assertEquals("PENDING",result.appointmentStatus());
assertEquals(List.of("A","B"),result.items().stream().map(FbaAppointmentResponse.Item::customerId).toList());verify(query,never()).selectedLines(anyList());
}
@Test
void rejectsMixedFbaWarehouses() {
when(queryMapper.lockLines(List.of(11L, 12L))).thenReturn(List.of(
line(11L, "IN-11", "ONT8"), line(12L, "IN-12", "LAX9")));
assertThrows(ResponseStatusException.class,
() -> service.batchCreate("7", request(List.of(11L, 12L), "APT-1")));
verify(appointmentMapper, never()).insert(any(InboundFbaAppointment.class));
@Test void unknownQuantityAndUnmaterializedBookingRemainUnknownInPendingArrangement() {
add("BOOKING:B-1","B",null,"100","ONT8",null);
var result=service.createFromSources("operator",request("unknown",new FbaAppointmentDetails(null,null,null,null,null,null,"PENDING",null,null,null),"ORDER:1","BOOKING:B-1"));
assertNull(result.plannedDate());assertNull(result.items().get(1).orderId());assertNull(result.items().get(1).inboundCode());assertNull(result.items().get(1).packageCount());
verify(query,never()).countMissingShipmentReferences(anyList());
}
@Test
void rejectsMissingSelectedLine() {
when(queryMapper.lockLines(List.of(11L, 12L))).thenReturn(List.of(line(11L, "IN-11", "ONT8")));
assertThrows(ResponseStatusException.class,
() -> service.batchCreate("7", request(List.of(11L, 12L), "APT-1")));
@Test void inboundEntryCanonicalizesTheOriginalBookingInsteadOfCreatingAnOrderAlias() {
add("BOOKING:B-1","B",2L,"100","ONT8",20);
when(query.selectedLines(List.of(11L))).thenReturn(List.of(new FbaAppointmentLineSnapshot(11L,100L,2L,"IN-2","ONT8","B")));
when(sources.canonicalOrders(anyList())).thenReturn(refs("BOOKING:B-1"));
var result=service.batchCreate("operator",new BatchFbaAppointmentRequest(List.of(11L),"100","ONT8",pending(null),"lines"));
assertEquals("BOOKING:B-1",result.items().get(0).sourceKey());verify(sources).canonicalOrders(List.of(new AppointmentExternalSourceGateway.OrderRef("B",2L)));
}
@Test
void duplicateLineIdsAreRejectedBeforeDatabaseAccess() {
assertThrows(ResponseStatusException.class,
() -> service.batchCreate("7", request(List.of(11L, 11L), "APT-1")));
verify(queryMapper, never()).lockLines(any());
@Test void mixedOriginsAndDestinationsAreNotSilentlyGrouped() {
add("ORDER:2","B",2L,"200","ONT8",20);
assertThrows(ResponseStatusException.class,()->create("mixed-origin"));
add("ORDER:2","B",2L,"100","LAX9",20);
assertThrows(ResponseStatusException.class,()->create("mixed-destination"));assertTrue(storedHeaders.isEmpty());
}
@Test
void sameAppointmentReplayReturnsExistingResult() {
InboundFbaAppointment existing = appointment(99L, "APT-1", "ONT8");
InboundFbaAppointmentItem item = new InboundFbaAppointmentItem();
item.setLineId(11L);
item.setInboundCode("IN-11");
item.setFbaWarehouseCode("ONT8");
when(appointmentMapper.selectOne(any())).thenReturn(existing);
when(itemMapper.selectList(any())).thenReturn(List.of(item));
var result = service.batchCreate("7", request(List.of(11L), "APT-1"));
assertEquals(99L, result.id());
verify(appointmentMapper, never()).insert(any(InboundFbaAppointment.class));
@Test void missingAndDuplicateInboundSelectionsAreRejected() {
assertThrows(ResponseStatusException.class,()->service.batchCreate("operator",new BatchFbaAppointmentRequest(List.of(11L,11L),"100","ONT8",pending(null),"duplicates")));
verify(query,never()).selectedLines(anyList());
when(query.selectedLines(List.of(11L))).thenReturn(List.of());
assertThrows(ResponseStatusException.class,()->service.batchCreate("operator",new BatchFbaAppointmentRequest(List.of(11L),"100","ONT8",pending(null),"missing")));
assertTrue(storedHeaders.isEmpty());
}
@Test
void sameAppointmentNumberWithDifferentSelectionConflicts() {
when(appointmentMapper.selectOne(any())).thenReturn(appointment(99L, "APT-1", "ONT8"));
InboundFbaAppointmentItem item = new InboundFbaAppointmentItem();
item.setLineId(11L);
when(itemMapper.selectList(any())).thenReturn(List.of(item));
assertThrows(ResponseStatusException.class,
() -> service.batchCreate("7", request(List.of(12L), "APT-1")));
@Test void exactCreateReplayReturnsOriginalResultEvenAfterLaterEditing() {
var request=request("original",pending(null),"ORDER:1","ORDER:2");var first=service.createFromSources("operator",request);
var edited=service.update("operator",first.appointmentNo(),new UpdateFbaAppointmentRequest(pending("新备注"),0,"edit"));assertEquals(1,edited.version());
assertEquals(first,service.createFromSources("operator",request));verify(headers,times(1)).insert(any(InboundFbaAppointment.class));
}
@Test
void sameAppointmentNumberWithDifferentExpectedLoadingTimeConflicts() {
when(appointmentMapper.selectOne(any())).thenReturn(appointment(99L, "APT-1", "ONT8"));
InboundFbaAppointmentItem item = new InboundFbaAppointmentItem();
item.setLineId(11L);
when(itemMapper.selectList(any())).thenReturn(List.of(item));
BatchFbaAppointmentRequest changed = new BatchFbaAppointmentRequest(List.of(11L), "APT-1",
appointmentAt, expectedLoadingAt.plusHours(1), "SUCCESS", "dock", null);
ResponseStatusException exception = assertThrows(ResponseStatusException.class,
() -> service.batchCreate("7", changed));
assertEquals(409, exception.getStatusCode().value());
@Test void sameKeyWithDifferentCargoDetailsOrOperatorIsRejected() {
create("original");
assertEquals(409,assertThrows(ResponseStatusException.class,()->service.createFromSources("operator",request("original",pending(null),"ORDER:1"))).getStatusCode().value());
assertThrows(ResponseStatusException.class,()->service.createFromSources("operator",request("original",pending("different"),"ORDER:1","ORDER:2")));
assertThrows(ResponseStatusException.class,()->service.createFromSources("other",request("original",pending(null),"ORDER:1","ORDER:2")));
verify(headers,times(1)).insert(any(InboundFbaAppointment.class));
}
@Test
void stringSnowflakeLineIdsDeserializeWithoutPrecisionLoss() throws Exception {
String json = "{\"lineIds\":[\"9007199254740993\"],"
+ "\"appointmentNo\":\"APT-1\",\"appointmentAt\":\"2026-08-20T10:30:00\","
+ "\"expectedLoadingAt\":\"2026-08-21T08:00:00\",\"appointmentStatus\":\"PENDING\"}";
BatchFbaAppointmentRequest request = new ObjectMapper().findAndRegisterModules()
.readValue(json, BatchFbaAppointmentRequest.class);
assertEquals(List.of(9007199254740993L), request.lineIds());
assertEquals(expectedLoadingAt, request.expectedLoadingAt());
@Test void confirmationNeedsExternalNumberAndLocationTimezone() {
var missing=new FbaAppointmentDetails(null,null,null,null,at,null,"SUCCESS",null,null,null);
assertThrows(ResponseStatusException.class,()->service.createFromSources("operator",request("no-number",missing,"ORDER:1")));
when(sources.destinationTimezone("ONT8")).thenReturn(null);
assertThrows(ResponseStatusException.class,()->service.createFromSources("operator",request("no-zone",confirmed(),"ORDER:1")));assertTrue(storedHeaders.isEmpty());
}
@Test
void updatesAppointmentDetailsWithoutReplacingSelectedLines() {
InboundFbaAppointment existing = appointment(99L, "APT-1", "ONT8");
when(appointmentMapper.selectOne(any())).thenReturn(existing);
LocalDateTime loadingAt = expectedLoadingAt.plusHours(2);
var result = service.update("APT-1", new UpdateFbaAppointmentRequest(
appointmentAt.plusDays(1), loadingAt, "PENDING", "updated", null, 0));
assertEquals(loadingAt, result.expectedLoadingAt());
assertEquals("PENDING", result.appointmentStatus());
verify(appointmentMapper).update(org.mockito.ArgumentMatchers.eq(existing), any());
verify(itemMapper, never()).insert(any(InboundFbaAppointmentItem.class));
@Test void confirmationRequiresActualShipmentReferencesAndKnownCargoWithoutFakingMaterialization() {
when(query.countMissingShipmentReferences(List.of(1L))).thenReturn(1L);
assertThrows(ResponseStatusException.class,()->service.createFromSources("operator",request("missing-materials",confirmed(),"ORDER:1")));
add("BOOKING:B-1","B",null,"100","ONT8",10);
assertThrows(ResponseStatusException.class,()->service.createFromSources("operator",request("unmaterialized",confirmed(),"BOOKING:B-1")));
add("ORDER:2","B",2L,"100","ONT8",null);
assertThrows(ResponseStatusException.class,()->service.createFromSources("operator",request("unknown",confirmed(),"ORDER:2")));assertTrue(storedHeaders.isEmpty());
}
@Test
void preventsAssigningTheSameCargoToTwoActiveAppointments() {
when(queryMapper.lockLines(List.of(11L))).thenReturn(List.of(line(11L, "IN-11", "ONT8")));
when(queryMapper.countActiveAppointments(List.of(11L), null)).thenReturn(1L);
var exception = assertThrows(ResponseStatusException.class,
() -> service.batchCreate("7", request(List.of(11L), "SECOND-APT")));
assertEquals(409, exception.getStatusCode().value());
verify(appointmentMapper, never()).insert(any(InboundFbaAppointment.class));
@Test void confirmationPersistsExternalNumberAndUtcSeparatelyFromPlannedDate() {
var response=service.createFromSources("operator",request("confirm",confirmed(),"ORDER:1"));
assertEquals("EXT-ONT8-1",response.confirmationNo());assertNotEquals(response.appointmentNo(),response.confirmationNo());
assertEquals(Instant.parse("2026-10-15T16:00:00Z"),response.appointmentStartUtc());assertEquals(LocalDate.of(2026,10,14),response.plannedDate());
}
@Test
void cancelledAppointmentCannotBeReactivatedAfterItsCargoWasBookedElsewhere() {
InboundFbaAppointment existing = appointment(99L, "APT-1", "ONT8");
existing.setAppointmentStatus("CANCELLED");
when(appointmentMapper.selectOne(any())).thenReturn(existing);
InboundFbaAppointmentItem item = new InboundFbaAppointmentItem();
item.setLineId(11L);
when(itemMapper.selectList(any())).thenReturn(List.of(item));
when(queryMapper.countActiveAppointments(List.of(11L), 99L)).thenReturn(1L);
assertThrows(ResponseStatusException.class, () -> service.update("APT-1",
new UpdateFbaAppointmentRequest(appointmentAt, expectedLoadingAt, "SUCCESS", "", null, 0)));
verify(queryMapper).lockLines(List.of(11L));
verify(appointmentMapper, never()).update(any(InboundFbaAppointment.class), any());
@Test void invalidWindowsAndNonexistentDaylightSavingTimeAreRejected() {
var invalid=new FbaAppointmentDetails(null,null,null,"EXT",at,at.minusMinutes(1),"SUCCESS",null,null,null);
assertThrows(ResponseStatusException.class,()->service.createFromSources("operator",request("bad-window",invalid,"ORDER:1")));
var gap=new FbaAppointmentDetails(null,null,null,"EXT",LocalDateTime.parse("2026-03-08T02:30:00"),null,"SUCCESS",null,null,null);
assertThrows(ResponseStatusException.class,()->service.createFromSources("operator",request("dst-gap",gap,"ORDER:1")));
}
@Test
void rejectsAnAppointmentWindowWhoseEndIsNotAfterItsStart() {
var request = new BatchFbaAppointmentRequest(List.of(11L), "BAD-WINDOW", appointmentAt,
expectedLoadingAt, "PENDING", "", appointmentAt.minusMinutes(1));
assertThrows(ResponseStatusException.class, () -> service.batchCreate("7", request));
verify(queryMapper, never()).lockLines(any());
@Test void duplicateActiveSourceOrDatabaseClaimRaceCannotCreateAnotherAppointment() {
when(query.countActiveSources(anyList(),anyList(),isNull())).thenReturn(1L);
assertEquals(409,assertThrows(ResponseStatusException.class,()->create("occupied")).getStatusCode().value());assertTrue(storedHeaders.isEmpty());
when(query.countActiveSources(anyList(),anyList(),isNull())).thenReturn(0L);
when(items.insert(any(InboundFbaAppointmentItem.class))).thenThrow(new org.springframework.dao.DuplicateKeyException("claimed"));
assertEquals(409,assertThrows(ResponseStatusException.class,()->create("raced")).getStatusCode().value());
}
@Test
void rejectsStaleEditBeforeWritingTheSharedAppointment() {
var existing = appointment(99L, "APT-1", "ONT8");
existing.setVersion(3);
when(appointmentMapper.selectOne(any())).thenReturn(existing);
var request = new UpdateFbaAppointmentRequest(appointmentAt, expectedLoadingAt,
"PENDING", "", appointmentAt.plusHours(2), 2);
var error = assertThrows(ResponseStatusException.class, () -> service.update("APT-1", request));
assertEquals(409, error.getStatusCode().value());
verify(appointmentMapper, never()).update(any(InboundFbaAppointment.class), any());
@Test void staleUpdateAndStaleCancellationDoNotWrite() {
var result=create("original");
assertThrows(ResponseStatusException.class,()->service.update("operator",result.appointmentNo(),new UpdateFbaAppointmentRequest(pending(null),2,"stale-update")));
assertThrows(ResponseStatusException.class,()->service.delete("operator",result.appointmentNo(),2));
verify(headers,never()).update(any(InboundFbaAppointment.class),any());verify(headers,never()).updateById(any(InboundFbaAppointment.class));
}
@Test
void cannotDeleteAnAppointmentWhoseGoodsAlreadyHaveAnActiveDispatch() {
when(appointmentMapper.selectOne(any())).thenReturn(appointment(99L, "APT-1", "ONT8"));
when(queryMapper.countActiveDispatches(99L)).thenReturn(1L);
assertThrows(ResponseStatusException.class, () -> service.delete("APT-1", 0));
verify(appointmentMapper, never()).delete(any(com.baomidou.mybatisplus.core.conditions.Wrapper.class));
verify(itemMapper, never()).delete(any(com.baomidou.mybatisplus.core.conditions.Wrapper.class));
@Test void activeDispatchBlocksChangingOrCancellingItsAppointment() {
var result=create("original");when(query.countActiveDispatches(result.id())).thenReturn(1L);
assertThrows(ResponseStatusException.class,()->service.update("operator",result.appointmentNo(),new UpdateFbaAppointmentRequest(pending("change"),0,"blocked-update")));
assertThrows(ResponseStatusException.class,()->service.delete("operator",result.appointmentNo(),0));assertTrue(storedItems.stream().allMatch(InboundFbaAppointmentItem::getActive));
}
@Test
void staleDeleteKeepsAllCargoAssociations() {
when(appointmentMapper.selectOne(any())).thenReturn(appointment(99L, "APT-1", "ONT8"));
when(appointmentMapper.delete(any(com.baomidou.mybatisplus.core.conditions.Wrapper.class))).thenReturn(0);
assertThrows(ResponseStatusException.class, () -> service.delete("APT-1", 0));
verify(itemMapper, never()).delete(any(com.baomidou.mybatisplus.core.conditions.Wrapper.class));
@Test void cancellationPreservesConfirmedFactsAndHistoryButCannotRestoreOverAnotherActiveClaim() {
var result=service.createFromSources("operator",request("confirmed",confirmed(),"ORDER:1"));
service.delete("operator",result.appointmentNo(),0);
assertEquals("CANCELLED",storedHeaders.get(0).getAppointmentStatus());assertEquals(at,storedHeaders.get(0).getAppointmentAt());assertFalse(storedItems.get(0).getActive());
assertEquals(result,service.createFromSources("operator",request("confirmed",confirmed(),"ORDER:1")));
when(query.countActiveSources(anyList(),anyList(),eq(result.id()))).thenReturn(1L);
assertThrows(ResponseStatusException.class,()->service.update("operator",result.appointmentNo(),new UpdateFbaAppointmentRequest(confirmed(),1,"restore")));
}
private BatchFbaAppointmentRequest request(List<Long> lineIds, String number) {
return new BatchFbaAppointmentRequest(lineIds, number, appointmentAt, expectedLoadingAt,
"SUCCESS", "dock", null);
@Test void snowflakeLineSelectorsDeserializeWithoutPrecisionLoss() throws Exception {
var request=new ObjectMapper().findAndRegisterModules().readValue("{\"lineIds\":[\"9007199254740993\"],\"details\":{\"appointmentStatus\":\"PENDING\"},\"idempotencyKey\":\"key\"}",BatchFbaAppointmentRequest.class);
assertEquals(List.of(9007199254740993L),request.lineIds());assertNull(request.details().appointmentAt());
}
@Test
void calendarRejectsInvalidOrUnboundedDateRangesBeforeQuerying() {
LocalDate from = LocalDate.of(2026, 9, 1);
for (LocalDate to : List.of(from, from.minusDays(1), from.plusDays(43))) {
assertThrows(ResponseStatusException.class, () -> service.calendar(from, to));
}
verify(appointmentMapper, never()).selectList(any());
}
@Test
void emptyCalendarDoesNotQueryAppointmentItems() {
when(appointmentMapper.selectList(any())).thenReturn(List.of());
assertEquals(List.of(), service.calendar(LocalDate.of(2026, 9, 1), LocalDate.of(2026, 10, 1)));
verify(itemMapper, never()).selectList(any());
}
@Test
void calendarLoadsAllItemsInOneBatchAndKeepsAppointmentAssociation() {
var first = appointment(99L, "APT-1", "ONT8");
var second = appointment(100L, "APT-2", "LAX9");
InboundFbaAppointmentItem item = new InboundFbaAppointmentItem();
item.setAppointmentId(100L);
item.setLineId(11L);
item.setInboundCode("IN-11");
when(appointmentMapper.selectList(any())).thenReturn(List.of(first, second));
when(itemMapper.selectList(any())).thenReturn(List.of(item));
var result = service.calendar(LocalDate.of(2026, 9, 1), LocalDate.of(2026, 10, 1));
assertEquals(2, result.size());
assertEquals(0, result.get(0).items().size());
assertEquals("IN-11", result.get(1).items().get(0).inboundCode());
verify(itemMapper).selectList(any());
}
private FbaAppointmentLineSnapshot line(Long id, String inboundCode, String fba) {
return new FbaAppointmentLineSnapshot(id, 1000L, id + 100L, inboundCode, fba, "CUSTOMER-" + id);
}
private InboundFbaAppointment appointment(Long id, String number, String fba) {
InboundFbaAppointment value = new InboundFbaAppointment();
value.setId(id);
value.setAppointmentNo(number);
value.setAppointmentAt(appointmentAt);
value.setExpectedLoadingAt(expectedLoadingAt);
value.setAppointmentStatus("SUCCESS");
value.setAppointmentRemark("dock");
value.setFbaWarehouseCode(fba);
value.setSelectedCount(1);
value.setVersion(0);
return value;
@Test void calendarRejectsInvalidRangesAndAvoidsPerRowDetailQueries() {
var from=LocalDate.of(2026,9,1);for(var to:List.of(from,from.minusDays(1),from.plusDays(43)))assertThrows(ResponseStatusException.class,()->service.calendar(from,to));
when(headers.selectList(any())).thenReturn(List.of());assertTrue(service.calendar(from,from.plusDays(30)).isEmpty());verify(items,never()).selectList(any());
var one=create("first");when(headers.selectList(any())).thenReturn(List.copyOf(storedHeaders));clearInvocations(items);
var calendar=service.calendar(from,from.plusDays(30));assertEquals(one.items(),calendar.get(0).items());verify(items,times(1)).selectList(any());
}
}