package com.baiyue.lastleg.appointmentplanning; import com.baiyue.common.core.api.ApiResponse; import org.junit.jupiter.api.Test; import org.springframework.web.server.ResponseStatusException; import java.util.List; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; class AppointmentCargoServiceTest { /** 选项取完整权威美国FBA目录,不依赖当前页货物或新增仓库主档权限。 */ @Test void destinationOptionsUseTheSameAmericanFbaScopeWithoutQueryingCargoRows() { var mapper = mock(AppointmentCargoMapper.class); var warehouses = mock(AppointmentWarehouseClient.class); when(warehouses.catalog("token")).thenReturn(ApiResponse.success(List.of( new AppointmentWarehouseClient.Warehouse(1L,"US-WH","美国仓","OVERSEAS","US"), new AppointmentWarehouseClient.Warehouse(2L,"ONT8","ONT8","FBA","US"), new AppointmentWarehouseClient.Warehouse(3L,"ABE4","ABE4","FBA","US"), new AppointmentWarehouseClient.Warehouse(4L,"YYZ1","YYZ1","FBA","CA"), new AppointmentWarehouseClient.Warehouse(5L,"ONT8","ONT8","FBA","US")))); assertEquals(List.of("ABE4","ONT8"),new AppointmentCargoService(mapper,warehouses,"token").destinations()); verifyNoInteractions(mapper); when(warehouses.catalog("token")).thenThrow(new IllegalStateException("offline")); assertEquals(503,assertThrows(ResponseStatusException.class,()->new AppointmentCargoService(mapper,warehouses,"token").destinations()).getStatusCode().value()); } @Test void authenticUsDirectoryScopesTheDatabaseQueryAndPreservesServerPagination() { var mapper = mock(AppointmentCargoMapper.class); var warehouses = mock(AppointmentWarehouseClient.class); when(warehouses.catalog("token")).thenReturn(ApiResponse.success(List.of( new AppointmentWarehouseClient.Warehouse(1L,"US-WH","美国仓","OVERSEAS","US"), new AppointmentWarehouseClient.Warehouse(2L,"CA-WH","加拿大仓","OVERSEAS","CA"), new AppointmentWarehouseClient.Warehouse(3L,"ONT8","ONT8","FBA","US"), new AppointmentWarehouseClient.Warehouse(4L,"YYZ1","YYZ1","FBA","CA")))); var row = new AppointmentCargo(); row.setOrderId(99L); row.setSourceKey("ORDER:99"); row.setHasInboundPlan(false); when(mapper.count(List.of("1"),List.of("ONT8"),null,null,"WT")).thenReturn(21L); when(mapper.page(List.of("1"),List.of("ONT8"),null,null,"WT",20,20)).thenReturn(List.of(row)); var result = new AppointmentCargoService(mapper,warehouses,"token").page(2,20,null,null," WT "); assertEquals(21,result.total()); assertEquals(2,result.pages()); assertEquals(1,result.records().size()); assertFalse(result.records().get(0).isHasInboundPlan()); verify(warehouses,times(1)).catalog("token"); } @Test void directoryFailureIsNotReportedAsAnEmptyAmericanCargoList() { var mapper = mock(AppointmentCargoMapper.class); var warehouses = mock(AppointmentWarehouseClient.class); when(warehouses.catalog("token")).thenThrow(new IllegalStateException("offline")); var error = assertThrows(ResponseStatusException.class, () -> new AppointmentCargoService(mapper,warehouses,"token").page(1,20,null,null,null)); assertEquals(503,error.getStatusCode().value()); verifyNoInteractions(mapper); } @Test void invalidPaginationAndNonUsFiltersNeverQueryCargo() { var mapper = mock(AppointmentCargoMapper.class); var warehouses = mock(AppointmentWarehouseClient.class); var service = new AppointmentCargoService(mapper,warehouses,"token"); assertThrows(ResponseStatusException.class, () -> service.page(0,20,null,null,null)); verifyNoInteractions(warehouses); when(warehouses.catalog("token")).thenReturn(ApiResponse.success(List.of())); assertThrows(ResponseStatusException.class, () -> service.page(1,20,"unknown",null,null)); verifyNoInteractions(mapper); } }