fix: return A0404 for missing PDA pallets

This commit is contained in:
2026-09-04 14:07:18 +08:00
parent f6247ce232
commit 3399ae7488
13 changed files with 205 additions and 7 deletions
@@ -6,8 +6,10 @@ import org.springframework.http.MediaType;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestClient;
import org.springframework.web.server.ResponseStatusException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
@@ -57,4 +59,17 @@ class HttpWarehouseResourceGatewayTest {
server.verify();
}
@Test
void resolveMapsBusinessNotFoundEnvelopeToStableNotFound() {
server.expect(requestTo("http://warehouse/internal/v1/warehouse-pallets/resolve/MISSING"))
.andRespond(withSuccess("""
{"code":"A0404","message":"Not found","data":null}
""", MediaType.APPLICATION_JSON));
assertThatThrownBy(() -> gateway.resolvePallet("MISSING"))
.isInstanceOf(ResponseStatusException.class)
.hasMessageContaining("404");
server.verify();
}
}
@@ -0,0 +1,40 @@
package com.baiyue.lastleg.palletops.gateway;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestClient;
import org.springframework.web.server.ResponseStatusException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
class HttpPalletPhysicalGatewayTest {
private HttpPalletPhysicalGateway gateway;
private MockRestServiceServer server;
@BeforeEach
void setUp() {
RestClient.Builder builder = RestClient.builder();
server = MockRestServiceServer.bindTo(builder).build();
gateway = new HttpPalletPhysicalGateway();
ReflectionTestUtils.setField(gateway, "client", builder.baseUrl("http://warehouse").build());
ReflectionTestUtils.setField(gateway, "internalToken", "test-token");
}
@Test
void resolveMapsBusinessNotFoundEnvelopeToStableNotFound() {
server.expect(requestTo("http://warehouse/internal/v1/warehouse-pallets/resolve/MISSING"))
.andRespond(withSuccess("""
{"code":"A0404","message":"Not found","data":null}
""", MediaType.APPLICATION_JSON));
assertThatThrownBy(() -> gateway.resolve("MISSING"))
.isInstanceOf(ResponseStatusException.class)
.hasMessageContaining("404");
server.verify();
}
}
@@ -63,6 +63,16 @@ class PdaPalletOperationServiceTest {
verifyNoInteractions(mapper);
}
@Test void resolveReportsMissingOnSitePalletAsNotFound() {
when(physical.resolve("MISSING")).thenReturn(null);
ResponseStatusException failure = assertThrows(ResponseStatusException.class, () -> service.resolve(identity(),
new PalletOperationRequests.Scan("MISSING")));
assertEquals(404, failure.getStatusCode().value());
verifyNoInteractions(mapper);
}
@Test void localConcurrencyFailureCompensatesEveryAppliedPhysicalTransitionInReverseSafeForm() {
var source = pallet(10L, "SOURCE", 88L, 1);
var target = pallet(20L, "TARGET", 88L, 2);
@@ -82,6 +82,7 @@ class PdaOperationsOpenApiContractTest {
assertTrue(yaml.contains("enum: [LOCATION, PALLET]"));
assertTrue(yaml.contains("enum: [APPROVE, REWORK]"));
assertTrue(yaml.contains("enum: [DAMAGED, MISSING_LABEL, LABEL_MISMATCH, UNREADABLE, OTHER]"));
assertEquals(3, occurrences(yaml, "body.code=A0404"));
assertFalse(yaml.contains("/api/pda/v1/last-leg/delivery"));
for (String response : List.of("StocktakeSummaryPageResponse", "StocktakeDetailResponse",
"StocktakeItemResponse", "InventoryMoveResponse", "InventoryScanResponse", "LocationResponse",
@@ -101,4 +102,8 @@ class PdaOperationsOpenApiContractTest {
return new String(input.readAllBytes(), StandardCharsets.UTF_8);
}
}
private static int occurrences(String value, String token) {
return (value.length() - value.replace(token, "").length()) / token.length();
}
}
@@ -108,6 +108,16 @@ class PdaInventoryMoveServiceTest {
verify(physical, never()).apply(anyLong(), any(), anyString(), anyLong(), anyString(), any());
}
@Test
void missingSourcePalletIsReportedAsNotFound() {
when(warehouse.resolvePallet("MISSING")).thenReturn(null);
assertThatThrownBy(() -> service.move(900L, "operator-1",
new PdaInventoryMoveRequest("MISSING", "B-01", 2, "move-1", "库位优化")))
.isInstanceOf(ResponseStatusException.class).hasMessageContaining("404");
verify(physical, never()).apply(anyLong(), any(), anyString(), anyLong(), anyString(), any());
}
@Test
void resumesWhenWarehouseCommittedButLastlegTransactionRolledBack() {
when(warehouse.resolvePallet("PAL-1")).thenReturn(pallet(900L, 21L, 3));
@@ -0,0 +1,77 @@
package com.baiyue.lastleg.pda.warehouse.controller;
import com.baiyue.common.security.AuthenticatedUser;
import com.baiyue.common.web.GlobalExceptionHandler;
import com.baiyue.lastleg.pda.warehouse.dto.PdaWarehouseRequests;
import com.baiyue.lastleg.pda.warehouse.service.PdaWarehouseService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.server.ResponseStatusException;
import java.util.Set;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
class PdaWarehouseControllerTest {
private PdaWarehouseService service;
private MockMvc mockMvc;
@BeforeEach
void setUp() {
service = mock(PdaWarehouseService.class);
PdaWarehouseController controller = new PdaWarehouseController();
ReflectionTestUtils.setField(controller, "service", service);
mockMvc = MockMvcBuilders.standaloneSetup(controller)
.setControllerAdvice(new GlobalExceptionHandler())
.setCustomArgumentResolvers(new AuthenticatedUserResolver())
.build();
}
@Test
void missingPalletReturnsHttp200WithStableA0404Body() throws Exception {
when(service.pallet(900L, new PdaWarehouseRequests.Scan("MISSING")))
.thenThrow(new ResponseStatusException(HttpStatus.NOT_FOUND, "Warehouse pallet was not found"));
mockMvc.perform(post("/api/pda/v1/last-leg/pallets/resolve")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"code\":\"MISSING\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value("A0404"))
.andExpect(jsonPath("$.message").value("Warehouse pallet was not found"));
}
private static AuthenticatedUser user() {
return new AuthenticatedUser(42L, 0L, 0L, 8L, "picker", "PLATFORM", "PDA",
Set.of("lastleg:pda:operate", "ROLE_OVERSEAS_WAREHOUSE_PDA_OPERATOR"),
false, null, null, false, 900L);
}
private static final class AuthenticatedUserResolver implements HandlerMethodArgumentResolver {
@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.hasParameterAnnotation(AuthenticationPrincipal.class)
&& parameter.getParameterType() == AuthenticatedUser.class;
}
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container,
NativeWebRequest request, WebDataBinderFactory binderFactory) {
return user();
}
}
}
@@ -48,6 +48,14 @@ class PdaWarehouseServiceTest {
.isInstanceOf(ResponseStatusException.class).hasMessageContaining("409");
}
@Test
void missingPalletIsReportedAsNotFound() {
when(warehouse.resolvePallet("MISSING")).thenReturn(null);
assertThatThrownBy(() -> service.pallet(900L, new PdaWarehouseRequests.Scan("MISSING")))
.isInstanceOf(ResponseStatusException.class).hasMessageContaining("404");
}
@Test
void locationCombinesPhysicalCapacityAndInventoryOccupation() {
when(warehouse.resolveLocation(900L, "A-01")).thenReturn(