Files
baiyue-firstleg/lastleg-service/src/test/java/com/baiyue/lastleg/service/LastlegOrderServiceTest.java
T

238 lines
12 KiB
Java
Raw Normal View History

2026-08-19 16:24:02 +08:00
package com.baiyue.lastleg.service;
import com.baiyue.lastleg.dto.LastlegOrderRequests;
import com.baiyue.lastleg.entity.LastlegOrder;
import com.baiyue.lastleg.entity.LastlegOrderDetail;
import com.baiyue.lastleg.entity.LastlegOrderStatusHistory;
import com.baiyue.lastleg.inboundplan.entity.InboundPlanCarton;
import com.baiyue.lastleg.inboundplan.mapper.InboundPlanCartonMapper;
2026-08-28 14:20:48 +08:00
import com.baiyue.lastleg.inventory.command.service.InventoryCommandService;
2026-08-19 16:24:02 +08:00
import com.baiyue.lastleg.mapper.LastlegOrderDetailMapper;
import com.baiyue.lastleg.mapper.LastlegOrderMapper;
import com.baiyue.lastleg.mapper.LastlegOrderStatusHistoryMapper;
2026-08-26 11:05:26 +08:00
import com.baiyue.lastleg.mapper.LastlegOrderFeeMapper;
import com.baiyue.lastleg.mapper.LastlegOrderOperationLogMapper;
2026-08-19 16:24:02 +08:00
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.test.util.ReflectionTestUtils;
import java.util.List;
2026-08-26 11:05:26 +08:00
import java.math.BigDecimal;
2026-08-19 16:24:02 +08:00
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
2026-08-28 14:20:48 +08:00
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyLong;
2026-08-19 16:24:02 +08:00
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class LastlegOrderServiceTest {
@Mock LastlegOrderMapper orders;
@Mock LastlegOrderDetailMapper details;
@Mock LastlegOrderStatusHistoryMapper histories;
2026-08-26 11:05:26 +08:00
@Mock LastlegOrderFeeMapper fees;
@Mock LastlegOrderOperationLogMapper operations;
@Mock InboundPlanCartonMapper cartons;
2026-08-28 14:20:48 +08:00
@Mock InventoryCommandService inventory;
2026-08-19 16:24:02 +08:00
LastlegOrderService service;
@BeforeEach
void setUp() {
service = new LastlegOrderService();
ReflectionTestUtils.setField(service, "orderMapper", orders);
ReflectionTestUtils.setField(service, "detailMapper", details);
ReflectionTestUtils.setField(service, "historyMapper", histories);
2026-08-26 11:05:26 +08:00
ReflectionTestUtils.setField(service, "feeMapper", fees);
ReflectionTestUtils.setField(service, "operationLogMapper", operations);
ReflectionTestUtils.setField(service, "cartonMapper", cartons);
2026-08-28 14:20:48 +08:00
ReflectionTestUtils.setField(service, "inventory", inventory);
2026-08-19 16:24:02 +08:00
}
@Test
2026-08-20 18:02:38 +08:00
void detailDoesNotExposeDropshipCartonsOrInternalEventIds() {
2026-08-19 16:24:02 +08:00
LastlegOrder order = order(11L, "100", "PENDING_ASSIGNMENT", null);
order.setOrderType("ONE_CLICK_DROPSHIP");
when(orders.selectOne(any())).thenReturn(order);
when(details.selectList(any())).thenReturn(List.of());
when(histories.selectList(any())).thenReturn(List.of());
2026-08-26 11:05:26 +08:00
when(fees.selectList(any())).thenReturn(List.of());
when(operations.selectList(any())).thenReturn(List.of());
2026-08-19 16:24:02 +08:00
var response = service.detail("100", 11L);
2026-08-20 18:02:38 +08:00
assertEquals(11L, response.order().id());
2026-08-21 10:43:25 +08:00
assertEquals("100", response.order().customerId());
2026-08-22 12:35:32 +08:00
assertEquals("ONE_CLICK_DROPSHIP", response.order().orderType());
assertEquals("一件代发", response.order().orderTypeName());
2026-08-20 18:02:38 +08:00
assertTrue(java.util.Arrays.stream(response.getClass().getRecordComponents())
.noneMatch(component -> "dropshipPallets".equals(component.getName())));
assertTrue(java.util.Arrays.stream(com.baiyue.lastleg.dto.LastlegOrderDetailResponse.History.class.getRecordComponents())
.noneMatch(component -> "eventId".equals(component.getName())));
2026-08-19 16:24:02 +08:00
}
@Test
void transferUpdatesOrderAndEveryCargoSnapshot() {
LastlegOrder order = order(11L, "100", "PENDING_ASSIGNMENT", null);
when(orders.selectList(any())).thenReturn(List.of(order));
var request = new LastlegOrderRequests.TransferDestination(
List.of(11L), "wh-2", "LAX-02", "Los Angeles 02", null);
assertEquals(1, service.transferDestination("100", request));
assertEquals("wh-2", order.getTransferWarehouseId());
assertEquals("LAX-02", order.getTransferWarehouseCode());
assertEquals("DESTINATION_ASSIGNED", order.getCurrentStatus());
ArgumentCaptor<LastlegOrderDetail> cargo = ArgumentCaptor.forClass(LastlegOrderDetail.class);
verify(details).update(cargo.capture(), any());
assertEquals("LAX-02", cargo.getValue().getDestinationWarehouseCode());
ArgumentCaptor<InboundPlanCarton> carton = ArgumentCaptor.forClass(InboundPlanCarton.class);
verify(cartons).update(carton.capture(), any());
assertEquals("wh-2", carton.getValue().getDestinationWarehouseId());
assertEquals("LAX-02", carton.getValue().getDestinationWarehouseCode());
2026-08-19 16:24:02 +08:00
ArgumentCaptor<LastlegOrderStatusHistory> history = ArgumentCaptor.forClass(LastlegOrderStatusHistory.class);
verify(histories).insert(history.capture());
2026-08-24 11:55:50 +08:00
assertEquals("100", history.getValue().getCustomerId());
2026-08-19 16:24:02 +08:00
assertEquals("PENDING_ASSIGNMENT", history.getValue().getFromStatus());
assertEquals("DESTINATION_ASSIGNED", history.getValue().getToStatus());
}
2026-08-26 11:05:26 +08:00
@Test
void createFeePersistsFeeAndOperationForOwnedOrder() {
when(orders.selectOne(any())).thenReturn(order(11L, "100", "PENDING_ASSIGNMENT", null));
var request = new LastlegOrderRequests.CreateFee(
"清关费", "USD", new BigDecimal("420.00"), "PENDING_REVIEW", "报关代理");
var fee = service.createFee("100", 11L, request, "platform_admin");
assertEquals("100", fee.getCustomerId());
assertEquals("清关费", fee.getFeeItem());
assertEquals(new BigDecimal("420.00"), fee.getAmount());
2026-08-26 13:51:19 +08:00
verify(fees).insert(any(com.baiyue.lastleg.entity.LastlegOrderFee.class));
verify(operations).insert(any(com.baiyue.lastleg.entity.LastlegOrderOperationLog.class));
2026-08-26 11:05:26 +08:00
}
2026-08-28 14:20:48 +08:00
@Test
void palletMergeMarkIsPersistedAndAuditedForPdaReminder() {
LastlegOrder order = order(11L, "100", "INBOUND_PLANNED", "IB-1");
when(orders.selectOne(any())).thenReturn(order);
when(orders.lockById("100", 11L)).thenReturn(order);
when(orders.updatePalletMergeFlag(any(), anyLong(), any(), any())).thenReturn(1);
service.setPalletMergeRequired("100", 11L,
new LastlegOrderRequests.SetPalletMergeRequired(true), "platform_admin");
verify(orders).updatePalletMergeFlag(org.mockito.ArgumentMatchers.eq("100"),
org.mockito.ArgumentMatchers.eq(11L), org.mockito.ArgumentMatchers.eq("REQUIRED"), any());
ArgumentCaptor<com.baiyue.lastleg.entity.LastlegOrderOperationLog> operation =
ArgumentCaptor.forClass(com.baiyue.lastleg.entity.LastlegOrderOperationLog.class);
verify(operations).insert(operation.capture());
assertEquals("标记合托", operation.getValue().getOperationNode());
assertTrue(operation.getValue().getResult().contains("PDA"));
}
@Test
void completedPhysicalMergeCannotBeUnmarked() {
LastlegOrder order = order(11L, "100", "INBOUND_PLANNED", "IB-1");
order.setPalletMergeFlag("MERGED");
when(orders.selectOne(any())).thenReturn(order);
when(orders.lockById("100", 11L)).thenReturn(order);
assertThrows(ResponseStatusException.class, () -> service.setPalletMergeRequired("100", 11L,
new LastlegOrderRequests.SetPalletMergeRequired(false), "platform_admin"));
verify(orders, never()).updatePalletMergeFlag(any(), anyLong(), any(), any());
}
2026-08-19 16:24:02 +08:00
@Test
void transferRejectsAnOrderAlreadyBoundToInboundPlan() {
LastlegOrder order = order(11L, "100", "DESTINATION_ASSIGNED", "IB-1");
when(orders.selectList(any())).thenReturn(List.of(order));
var request = new LastlegOrderRequests.TransferDestination(
List.of(11L), "wh-2", "LAX-02", "Los Angeles 02", null);
assertThrows(ResponseStatusException.class, () -> service.transferDestination("100", request));
verify(orders, never()).updateById(any(LastlegOrder.class));
verify(details, never()).update(any(), any());
verify(cartons, never()).update(any(), any());
2026-08-19 16:24:02 +08:00
}
@Test
void batchRejectsMissingOrCrossCustomerIdsBeforeWriting() {
when(orders.selectList(any())).thenReturn(List.of(order(11L, "100", "PENDING_ASSIGNMENT", null)));
var request = new LastlegOrderRequests.TransferDestination(
List.of(11L, 22L), "wh-2", "LAX-02", "Los Angeles 02", null);
ResponseStatusException error = assertThrows(ResponseStatusException.class,
() -> service.transferDestination("100", request));
assertTrue(error.getStatusCode().is4xxClientError());
verify(orders, never()).updateById(any(LastlegOrder.class));
}
2026-08-21 14:42:40 +08:00
@Test
void pageUsesPaginationAndPlatformFiltersWithoutCustomerScope() {
var query = new LastlegOrderRequests.Query(3, 25, "LL-", "FIRST_LEG", "IN-", null,
"", "PENDING_ASSIGNMENT", null, "LAX", null, null, null, null, null);
var resultPage = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<LastlegOrder>(3, 25);
resultPage.setTotal(0);
when(orders.selectPage(any(), any())).thenReturn(resultPage);
service.page(null, query);
ArgumentCaptor<com.baomidou.mybatisplus.extension.plugins.pagination.Page<LastlegOrder>> page =
ArgumentCaptor.forClass(com.baomidou.mybatisplus.extension.plugins.pagination.Page.class);
verify(orders).selectPage(page.capture(), any());
assertEquals(3, page.getValue().getCurrent());
assertEquals(25, page.getValue().getSize());
}
2026-08-28 14:20:48 +08:00
@Test
void holdingBeforeInboundPersistsTheOrderMarkerAndOperationRemark() {
LastlegOrder order = order(11L, "100", "PENDING_INBOUND", null);
when(orders.selectOne(any())).thenReturn(order);
when(orders.lockById("100", 11L)).thenReturn(order);
service.setDetained("100", 11L, new LastlegOrderRequests.SetDetained(true, "等待质检"), "operator");
assertTrue(order.getDetained());
verify(orders).updateById(order);
ArgumentCaptor<com.baiyue.lastleg.entity.LastlegOrderOperationLog> operation =
ArgumentCaptor.forClass(com.baiyue.lastleg.entity.LastlegOrderOperationLog.class);
verify(operations).insert(operation.capture());
assertEquals("暂扣", operation.getValue().getOperationNode());
assertEquals("等待质检", operation.getValue().getResult());
verify(inventory, never()).releaseInboundOrderHold(any(), any(), anyLong(), any(), any());
}
@Test
void releasingAfterInboundMovesAllOrderHeldInventoryToAvailable() {
LastlegOrder order = order(11L, "100", "INBOUND_COMPLETED", "IP-1");
order.setOrderNo("LM-1");
order.setDetained(true);
when(orders.selectOne(any())).thenReturn(order);
when(orders.lockById("100", 11L)).thenReturn(order);
service.setDetained("100", 11L, new LastlegOrderRequests.SetDetained(false, "质检通过"), "operator");
verify(inventory).releaseInboundOrderHold("100", "operator", 11L, "LM-1", "质检通过");
assertEquals(false, order.getDetained());
}
2026-08-19 16:24:02 +08:00
private static LastlegOrder order(long id, String customerId, String status, String inboundOrderNo) {
LastlegOrder order = new LastlegOrder();
order.setId(id);
order.setCustomerId(customerId);
order.setCurrentStatus(status);
order.setInboundOrderNo(inboundOrderNo);
return order;
}
}