fix(pda): return business error when outbound order is missing

This commit is contained in:
zgw
2026-09-08 12:01:15 +08:00
parent c387ba0b50
commit 827083e7d6
2 changed files with 115 additions and 0 deletions

View File

@@ -337,6 +337,9 @@ public class PdaController extends BaseController {
public AjaxResult OutFillPkDetail(@RequestBody OutStockout outStockout) {
List<OutStockout> outStockouts = outStockoutService.selectOutStockoutList(outStockout);
if (StringUtils.isEmpty(outStockouts)) {
return AjaxResult.error("未查询到匹配的出库单,请核对出库单号及查询条件后重试。");
}
return AjaxResult.success(outStockoutDetailService.selectOutStockoutDetailByStockoutGuid(outStockouts.get(0).getStockoutGuid()));
}

View File

@@ -0,0 +1,112 @@
package com.ruoyi.pda.controller;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.out.outStockout.domain.OutStockout;
import com.ruoyi.out.outStockout.service.impl.OutStockoutServiceImpl;
import com.ruoyi.out.outStockoutDetail.domain.OutStockoutDetail;
import com.ruoyi.out.outStockoutDetail.service.impl.OutStockoutDetailServiceImpl;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.http.MediaType;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import java.util.Collections;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
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 PdaControllerTest {
private final OutStockoutServiceImpl orders = mock(OutStockoutServiceImpl.class);
private final OutStockoutDetailServiceImpl details = mock(OutStockoutDetailServiceImpl.class);
private final PdaController controller = new PdaController();
private MockMvc mvc;
@BeforeEach
void setUp() {
ReflectionTestUtils.setField(controller, "outStockoutService", orders);
ReflectionTestUtils.setField(controller, "outStockoutDetailService", details);
mvc = MockMvcBuilders.standaloneSetup(controller).build();
}
@Test
void missingOrderReturnsBusinessMessageWithoutThrowingAnException() throws Exception {
when(orders.selectOutStockoutList(any(OutStockout.class))).thenReturn(Collections.emptyList());
mvc.perform(post("/pda/OutFillPkDetail")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"stockoutId\":\"CKD-NOT-FOUND\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(500))
.andExpect(jsonPath("$.msg").value("未查询到匹配的出库单,请核对出库单号及查询条件后重试。"))
.andExpect(jsonPath("$.data").doesNotExist())
.andExpect(result -> assertThat(result.getResolvedException()).isNull());
verifyNoInteractions(details);
}
@Test
void nullOrderListAlsoReturnsBusinessError() {
OutStockout query = new OutStockout();
when(orders.selectOutStockoutList(query)).thenReturn(null);
AjaxResult result = controller.OutFillPkDetail(query);
assertThat(result.isError()).isTrue();
assertThat(result.get("msg")).isEqualTo("未查询到匹配的出库单,请核对出库单号及查询条件后重试。");
verifyNoInteractions(details);
}
@Test
void matchingOrderStillReturnsItsDetails() throws Exception {
OutStockout order = new OutStockout();
order.setStockoutGuid("order-guid");
OutStockoutDetail detail = new OutStockoutDetail();
detail.setStockoutDetailId("CKD-EXISTING-1");
when(orders.selectOutStockoutList(any(OutStockout.class))).thenReturn(List.of(order));
when(details.selectOutStockoutDetailByStockoutGuid("order-guid")).thenReturn(List.of(detail));
mvc.perform(post("/pda/OutFillPkDetail")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"stockoutId\":\"CKD-EXISTING\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(200))
.andExpect(jsonPath("$.data[0].stockoutDetailId").value("CKD-EXISTING-1"));
verify(details).selectOutStockoutDetailByStockoutGuid("order-guid");
}
@Test
void existingOrderWithoutDetailsRetainsSuccessfulEmptyResponse() {
OutStockout order = new OutStockout();
order.setStockoutGuid("order-guid");
when(orders.selectOutStockoutList(order)).thenReturn(List.of(order));
when(details.selectOutStockoutDetailByStockoutGuid("order-guid")).thenReturn(Collections.emptyList());
AjaxResult result = controller.OutFillPkDetail(order);
assertThat(result.isSuccess()).isTrue();
assertThat(result.get("data")).isEqualTo(Collections.emptyList());
}
@Test
void databaseFailureIsNotMisreportedAsMissingOrder() {
OutStockout query = new OutStockout();
DataAccessResourceFailureException failure = new DataAccessResourceFailureException("Database unavailable");
when(orders.selectOutStockoutList(query)).thenThrow(failure);
assertThatThrownBy(() -> controller.OutFillPkDetail(query)).isSameAs(failure);
verifyNoInteractions(details);
}
}