fix(inbound): expose original command for resource confirmation recovery

This commit is contained in:
2026-10-05 02:13:22 +08:00
parent b000b056d1
commit a0a4e03da9
7 changed files with 126 additions and 9 deletions
@@ -9,6 +9,34 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class TailPlanCommandOpenApiContractTest {
@Test
void previewDocumentsTheOriginalResourceRetrySeparatelyFromItsCurrentSnapshot() throws Exception {
try (var stream = getClass().getClassLoader().getResourceAsStream("openapi/overseas-business-v1.yaml")) {
java.util.Map<?, ?> document = new org.yaml.snakeyaml.Yaml().load(stream);
var components = (java.util.Map<?, ?>) document.get("components");
var schemas = (java.util.Map<?, ?>) components.get("schemas");
var preview = (java.util.Map<?, ?>) schemas.get("ContainerInboundPlanPreview");
var properties = (java.util.Map<?, ?>) preview.get("properties");
var retry = (java.util.Map<?, ?>) properties.get("resourceRetry");
org.junit.jupiter.api.Assertions.assertEquals(true, retry.get("nullable"));
org.junit.jupiter.api.Assertions.assertEquals(java.util.List.of("idempotencyKey", "snapshotVersion", "snapshotHash"), retry.get("required"));
assertTrue(properties.containsKey("snapshotVersion") && properties.containsKey("snapshotHash"));
}
}
@Test
void platformPreviewUsesTheCoordinatorThatCanRestorePendingResources() {
var controller = new com.baiyue.lastleg.inboundplan.controller.InboundPlanController();
var coordinator = org.mockito.Mockito.mock(com.baiyue.lastleg.inboundplan.service.ContainerInboundPlanConversionCoordinator.class);
var principal = org.mockito.Mockito.mock(com.baiyue.common.security.AuthenticatedUser.class);
var expected = org.mockito.Mockito.mock(com.baiyue.lastleg.inboundplan.dto.ContainerInboundPlanPreviewResponse.class);
org.springframework.test.util.ReflectionTestUtils.setField(controller, "containerConversion", coordinator);
org.mockito.Mockito.when(principal.tenantId()).thenReturn(0L);
org.mockito.Mockito.when(principal.appCode()).thenReturn("PLATFORM");
org.mockito.Mockito.when(coordinator.preview("BOOKING-1")).thenReturn(expected);
org.junit.jupiter.api.Assertions.assertSame(expected, controller.previewContainer(principal, "BOOKING-1").data());
}
@Test
void exposesOnlyLatestManualPlanHoldAndEightRowNodeContracts() throws Exception {
try (var stream = getClass().getClassLoader()
@@ -30,6 +30,7 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyLong;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.doReturn;
@@ -266,6 +267,59 @@ class ContainerInboundPlanConversionCoordinatorTest {
verify(warehouse).confirmPalletBatch(any());
}
@Test
void reopenedPreviewRetainsCurrentDisplayButResumesTheOriginalCommittedCommand() throws Exception {
var committed = command("FIRST-KEY", "LOCAL_COMMITTED", request());
committed.setResultJson("[]");
setOnePreparedItem(committed);
var current = preview(8L, "b".repeat(64));
when(plans.previewContainer(BOOKING_ID)).thenReturn(current);
when(commands.findByBooking(BOOKING_ID)).thenReturn(committed);
var reopened = coordinator.preview(BOOKING_ID);
assertEquals(8L, reopened.snapshotVersion());
assertEquals("b".repeat(64), reopened.snapshotHash());
assertEquals(current.groups(), reopened.groups());
assertEquals("FIRST-KEY", reopened.resourceRetry().idempotencyKey());
assertEquals(7L, reopened.resourceRetry().snapshotVersion());
assertEquals(SNAPSHOT_HASH, reopened.resourceRetry().snapshotHash());
when(commands.find(BOOKING_ID, "FIRST-KEY")).thenReturn(committed);
assertEquals(409, assertThrows(ResponseStatusException.class, () -> coordinator.confirm(
new ConvertContainerInboundPlanRequest(BOOKING_ID, reopened.snapshotVersion(), reopened.snapshotHash()),
"FIRST-KEY")).getStatusCode().value());
when(warehouse.confirmPalletBatch(any())).thenReturn(new WarehouseResourceGateway.PalletBatchResponse(
41L, "BATCH-1", "CONFIRMED", 1, false, List.of(reserved("CONFIRMED"))));
when(commands.updateState(eq(1L), eq("LOCAL_COMMITTED"), any(String.class), eq("COMPLETED"), eq("[]"),
org.mockito.ArgumentMatchers.isNull(), any())).thenReturn(1);
assertEquals(List.of(), coordinator.confirm(new ConvertContainerInboundPlanRequest(BOOKING_ID,
reopened.resourceRetry().snapshotVersion(), reopened.resourceRetry().snapshotHash()),
reopened.resourceRetry().idempotencyKey()));
verify(plans, never()).convertContainerReserved(any(), anyLong(), any(), any());
verify(warehouse, never()).ensurePalletBatch(any());
verify(warehouse).confirmPalletBatch(any());
}
@Test
void nonCommittedStatesDoNotExposeAResourceRetryCommand() throws Exception {
when(plans.previewContainer(BOOKING_ID)).thenReturn(preview(8L, "b".repeat(64)));
for (String status : List.of("PREPARED", "APPLYING", "FAILED", "COMPLETED")) {
when(commands.findByBooking(BOOKING_ID)).thenReturn(command("FIRST-KEY", status, request()));
org.junit.jupiter.api.Assertions.assertNull(coordinator.preview(BOOKING_ID).resourceRetry());
}
when(commands.findByBooking(BOOKING_ID)).thenReturn(null);
org.junit.jupiter.api.Assertions.assertNull(coordinator.preview(BOOKING_ID).resourceRetry());
}
@Test
void missingCommittedSnapshotFailsClosedInsteadOfInventingAReplacement() throws Exception {
var committed = command("FIRST-KEY", "LOCAL_COMMITTED", request());
committed.setSnapshotHash(null);
when(plans.previewContainer(BOOKING_ID)).thenReturn(preview(8L, "b".repeat(64)));
when(commands.findByBooking(BOOKING_ID)).thenReturn(committed);
assertEquals(409, assertThrows(ResponseStatusException.class,
() -> coordinator.preview(BOOKING_ID)).getStatusCode().value());
verify(warehouse, never()).confirmPalletBatch(any());
}
@Test
void preparationCarriesUnderlyingFbaCodeForDropshipWithoutClassifyingOverseasDropshipAsFba() {
ConvertContainerInboundPlanRequest request = request();
@@ -276,7 +330,7 @@ class ContainerInboundPlanConversionCoordinatorTest {
List.of(new ContainerInboundPlanPreviewResponse.Group("ONE_CLICK_DROPSHIP", null, "ABE3", "Amazon ABE3",
1, 1, 1, 1, 0, 0, 1, 0, List.of(fba)),
new ContainerInboundPlanPreviewResponse.Group("ONE_CLICK_DROPSHIP", "31", "US-LAX-01", "Los Angeles",
1, 1, 1, 1, 0, 0, 1, 0, List.of(overseas))));
1, 1, 1, 1, 0, 0, 1, 0, List.of(overseas))), null);
when(plans.previewContainer(BOOKING_ID)).thenReturn(preview);
when(numbers.next(eq("INBOUND_PLAN"), any(String.class), any(Long.class)))
.thenReturn("IP-FBA", "IP-OVERSEAS");
@@ -309,7 +363,7 @@ class ContainerInboundPlanConversionCoordinatorTest {
var preview = new ContainerInboundPlanPreviewResponse(BOOKING_ID, "MSCU6639870", "31", "US-LAX-01",
"Los Angeles", 7L, SNAPSHOT_HASH, 1, 1, 0, 1, 2, 0, 2, 0, 1, 0, true,
List.of(new ContainerInboundPlanPreviewResponse.Group("FIRST_LEG", "31", "US-LAX-01", "Los Angeles",
1, 1, 2, 0, 2, 0, 2, 0, List.of(overseas))));
1, 1, 2, 0, 2, 0, 2, 0, List.of(overseas))), null);
when(plans.previewContainer(BOOKING_ID)).thenReturn(preview);
when(numbers.next(eq("INBOUND_PLAN"), any(String.class), any(Long.class))).thenReturn("IP-OVERSEAS");
when(plans.sourcePalletNos("200", 12L, "FIRST_LEG")).thenReturn(List.of());
@@ -341,7 +395,7 @@ class ContainerInboundPlanConversionCoordinatorTest {
var preview = new ContainerInboundPlanPreviewResponse(BOOKING_ID, "MSCU6639870", "31", "US-LAX-01",
"Los Angeles", 7L, SNAPSHOT_HASH, 1, 1, 0, 1, 3, 2, 1, 0, 3, 0, true,
List.of(new ContainerInboundPlanPreviewResponse.Group("FBA", null, "ACY2", "Amazon ACY2",
1, 1, 3, 2, 1, 0, 3, 0, List.of(order))));
1, 1, 3, 2, 1, 0, 3, 0, List.of(order))), null);
when(plans.previewContainer(BOOKING_ID)).thenReturn(preview);
when(numbers.next(eq("INBOUND_PLAN"), any(String.class), any(Long.class))).thenReturn("IP-ACY2");
when(plans.sourcePalletNos("100", 11L, "FIRST_LEG")).thenReturn(List.of(
@@ -373,7 +427,7 @@ class ContainerInboundPlanConversionCoordinatorTest {
var preview = new ContainerInboundPlanPreviewResponse(BOOKING_ID, "MSCU6639870", "31", "US-LAX-01",
"Los Angeles", 7L, SNAPSHOT_HASH, 1, 1, 0, 1, 3, 2, 1, 3, 3, 3, true,
List.of(new ContainerInboundPlanPreviewResponse.Group("FBA", null, "ACY2", "Amazon ACY2",
1, 1, 3, 2, 1, 3, 3, 3, List.of(order))));
1, 1, 3, 2, 1, 3, 3, 3, List.of(order))), null);
when(plans.previewContainer(BOOKING_ID)).thenReturn(preview);
when(plans.sourcePalletNos("100", 11L, "FIRST_LEG")).thenReturn(List.of(
"D10000-101-001", "D10000-101-002"));
@@ -428,7 +482,7 @@ class ContainerInboundPlanConversionCoordinatorTest {
"Los Angeles", 7L, SNAPSHOT_HASH, orders.size(), orders.size(), 0, pending, pending,
pending, 0, 0, orders.size(), 1, true,
List.of(new ContainerInboundPlanPreviewResponse.Group("FBA", null, "ACY2", "Amazon ACY2",
orders.size(), pending, pending, pending, 0, 0, orders.size(), 1, orders)));
orders.size(), pending, pending, pending, 0, 0, orders.size(), 1, orders)), null);
}
private InboundPlanResponse plan(String planNo, List<InboundPlanResponse.Pallet> pallets) {
@@ -476,6 +530,6 @@ class ContainerInboundPlanConversionCoordinatorTest {
private ContainerInboundPlanPreviewResponse preview(long version, String hash) {
return new ContainerInboundPlanPreviewResponse(BOOKING_ID, "MSCU6639870", "31", "US-LAX-01",
"Los Angeles", version, hash, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, true, List.of());
"Los Angeles", version, hash, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, true, List.of(), null);
}
}