Browse Source
API 139 的创建响应有 80 多个字段,之前只取了 9 个,调拨失败时 errorCode / errorReason 直接丢失——status 只告诉你「已驳回」或 「取消异常」,具体原因查不到。补上: - errorCode / errorReason + hasError() 便捷判断 - orgWarehouseTitle / inOrgWarehouseTitle(调出/调入仓名称) - carrierCode / totalWeight / totalVolume - TransferOrder 新增字段全部加 Javadoc 说明「缺失时为 null」 顺带修一个真实 bug:响应不回填 sourceNo 时,toOrder 直接把 null 传给 TransferOrder.transferRequestNo,构造期抛 IllegalArgumentException。 sourceNo 同时是幂等键,不能为空——改为回落到请求侧的 transferRequestNo。 新增 YqnTransferTest 覆盖:出库调拨带 shipTo、响应字段映射、 异常字段、未知状态归 UNKNOWN、type 取自请求侧。 单元体积单位运去哪未标明,按 kg / m³ 命名但不做换算,Javadoc 注明。 Co-Authored-By: Claude <noreply@anthropic.com>feature/supplier-lingxing
6 changed files with 341 additions and 4 deletions
@ -0,0 +1,203 @@ |
|||||||
|
package common.ecommerce.yqn.adapter; |
||||||
|
|
||||||
|
import common.ecommerce.http.JacksonJsonCodec; |
||||||
|
import common.ecommerce.shared.spi.ChannelSecret; |
||||||
|
import common.ecommerce.shared.testkit.RecordingTransport; |
||||||
|
import common.ecommerce.shared.testkit.Responses; |
||||||
|
import common.ecommerce.warehouse.enums.TransferStatus; |
||||||
|
import common.ecommerce.warehouse.enums.TransferType; |
||||||
|
import common.ecommerce.warehouse.enums.WarehouseProvider; |
||||||
|
import common.ecommerce.warehouse.model.ShipTo; |
||||||
|
import common.ecommerce.warehouse.model.TransferCommand; |
||||||
|
import common.ecommerce.warehouse.model.TransferOrder; |
||||||
|
import common.ecommerce.warehouse.sdk.WarehouseConnection; |
||||||
|
import common.ecommerce.warehouse.sdk.WarehouseContext; |
||||||
|
import common.ecommerce.yqn.YqnConstants; |
||||||
|
import org.junit.jupiter.api.Test; |
||||||
|
|
||||||
|
import java.time.Clock; |
||||||
|
import java.time.Instant; |
||||||
|
import java.time.LocalDate; |
||||||
|
import java.time.ZoneOffset; |
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse; |
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue; |
||||||
|
|
||||||
|
/** |
||||||
|
* 创建调拨单(API 139)契约测试。 |
||||||
|
*/ |
||||||
|
class YqnTransferTest { |
||||||
|
|
||||||
|
private static final Clock CLOCK = Clock.fixed(Instant.parse("2026-11-10T00:00:00Z"), ZoneOffset.UTC); |
||||||
|
|
||||||
|
private static WarehouseContext ctx() { |
||||||
|
return WarehouseContext.of("YQN-RF_NY_US", WarehouseProvider.YQN, "RF_NY_US", |
||||||
|
WarehouseConnection.of(YqnConstants.PR_BASE_URL, |
||||||
|
ChannelSecret.builder().appSecret("key-1").build()), |
||||||
|
"req-transfer", CLOCK); |
||||||
|
} |
||||||
|
|
||||||
|
private static TransferCommand command() { |
||||||
|
return TransferCommand.builder() |
||||||
|
.transferRequestNo("TR-001") |
||||||
|
.type(TransferType.WAREHOUSE_TO_WAREHOUSE) |
||||||
|
.fromWarehouseCode("RF_NY_US") |
||||||
|
.toWarehouseCode("RF_LA_US") |
||||||
|
.inventoryType(0) |
||||||
|
.carrierType(1) |
||||||
|
.transportationType(2) |
||||||
|
.estimateOutboundDate(LocalDate.of(2026, 11, 15)) |
||||||
|
.estimateInboundDate(LocalDate.of(2026, 11, 20)) |
||||||
|
.remark("补货") |
||||||
|
.line("DM00001", 10) |
||||||
|
.build(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void sendsDocumentedShape() { |
||||||
|
RecordingTransport transport = RecordingTransport.create() |
||||||
|
.route(YqnConstants.PATH_TRANSFER_CREATE, req -> Responses.ok( |
||||||
|
"{\"statusCode\":200,\"status\":0,\"code\":200,\"message\":\"ok\",\"data\":{" |
||||||
|
+ "\"transferOutboundPlanNo\":\"TP123\"," |
||||||
|
+ "\"transferOutboundPlanStatus\":5,\"sourceNo\":\"TR-001\"}}")); |
||||||
|
YqnAdapter adapter = new YqnAdapter(transport, new JacksonJsonCodec()); |
||||||
|
|
||||||
|
TransferOrder order = adapter.create(ctx(), command()); |
||||||
|
|
||||||
|
String body = transport.lastDataRequest().body(); |
||||||
|
assertTrue(body.contains("\"type\":2"), body); |
||||||
|
assertTrue(body.contains("\"orgWarehouseCode\":\"RF_NY_US\""), body); |
||||||
|
assertTrue(body.contains("\"inOrgWarehouseCode\":\"RF_LA_US\""), body); |
||||||
|
assertTrue(body.contains("\"sourceNo\":\"TR-001\""), body); |
||||||
|
assertTrue(body.contains("\"estimateOutboundAt\":\"2026-11-15\""), body); |
||||||
|
assertTrue(body.contains("\"sku\":\"DM00001\""), body); |
||||||
|
assertTrue(body.contains("\"qty\":10"), body); |
||||||
|
|
||||||
|
assertEquals("TR-001", order.transferRequestNo()); |
||||||
|
assertEquals("TP123", order.providerTransferNo()); |
||||||
|
assertEquals(TransferStatus.PENDING_REVIEW, order.status()); |
||||||
|
assertEquals("5", order.rawStatus()); |
||||||
|
assertEquals(WarehouseProvider.YQN, order.sourceProvider()); |
||||||
|
} |
||||||
|
|
||||||
|
/** 出库类型必须带收货地址;仓到仓可以不填。 */ |
||||||
|
private static TransferCommand outboundCommand() { |
||||||
|
return TransferCommand.builder() |
||||||
|
.transferRequestNo("TR-OUT-1") |
||||||
|
.type(TransferType.OUTBOUND) |
||||||
|
.fromWarehouseCode("RF_NY_US") |
||||||
|
.toWarehouseCode("Other") |
||||||
|
.inventoryType(0) |
||||||
|
.carrierType(1) |
||||||
|
.transportationType(3) |
||||||
|
.estimateOutboundDate(LocalDate.of(2026, 11, 15)) |
||||||
|
.estimateInboundDate(LocalDate.of(2026, 11, 20)) |
||||||
|
.shipTo(ShipTo.builder() |
||||||
|
.name("李四") |
||||||
|
.phone("+1-555-0200") |
||||||
|
.countryCode("US") |
||||||
|
.stateOrProvince("CA") |
||||||
|
.city("Los Angeles") |
||||||
|
.addressLine1("2 Main St") |
||||||
|
.postalCode("90001") |
||||||
|
.build()) |
||||||
|
.line("DM00001", 5) |
||||||
|
.build(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void sendsShipToFieldsForOutbound() { |
||||||
|
RecordingTransport transport = RecordingTransport.create() |
||||||
|
.route(YqnConstants.PATH_TRANSFER_CREATE, req -> Responses.ok( |
||||||
|
"{\"statusCode\":200,\"status\":0,\"code\":200,\"data\":{" |
||||||
|
+ "\"transferOutboundPlanNo\":\"TP1\"}}")); |
||||||
|
new YqnAdapter(transport, new JacksonJsonCodec()).create(ctx(), outboundCommand()); |
||||||
|
|
||||||
|
String body = transport.lastDataRequest().body(); |
||||||
|
assertTrue(body.contains("\"type\":3"), body); |
||||||
|
assertTrue(body.contains("\"shipToCountry\":\"US\""), body); |
||||||
|
assertTrue(body.contains("\"shipToState\":\"CA\""), body); |
||||||
|
assertTrue(body.contains("\"shipToCity\":\"Los Angeles\""), body); |
||||||
|
assertTrue(body.contains("\"shipToPostal\":\"90001\""), body); |
||||||
|
assertTrue(body.contains("\"shipToAddress1\":\"2 Main St\""), body); |
||||||
|
assertTrue(body.contains("\"shipToContact\":\"李四\""), body); |
||||||
|
assertTrue(body.contains("\"shipToTelephone\":\"+1-555-0200\""), body); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void mapsTitlesCarrierAndSizes() { |
||||||
|
RecordingTransport transport = RecordingTransport.create() |
||||||
|
.route(YqnConstants.PATH_TRANSFER_CREATE, req -> Responses.ok( |
||||||
|
"{\"statusCode\":200,\"status\":0,\"code\":200,\"data\":{" |
||||||
|
+ "\"transferOutboundPlanNo\":\"TP1\"," |
||||||
|
+ "\"transferOutboundPlanStatus\":7," |
||||||
|
+ "\"orgWarehouseCode\":\"RF_NY_US\"," |
||||||
|
+ "\"inOrgWarehouseCode\":\"RF_LA_US\"," |
||||||
|
+ "\"orgWarehouseTitle\":\"纽约仓\"," |
||||||
|
+ "\"inOrgWarehouseTitle\":\"洛杉矶仓\"," |
||||||
|
+ "\"carrierCode\":\"UPS\"," |
||||||
|
+ "\"totalWeight\":12.5," |
||||||
|
+ "\"totalVolume\":0.8," |
||||||
|
+ "\"totalQuantity\":10," |
||||||
|
+ "\"sourceNo\":\"TR-001\"}}")); |
||||||
|
TransferOrder order = new YqnAdapter(transport, new JacksonJsonCodec()) |
||||||
|
.create(ctx(), command()); |
||||||
|
|
||||||
|
assertEquals("纽约仓", order.fromWarehouseTitle()); |
||||||
|
assertEquals("洛杉矶仓", order.toWarehouseTitle()); |
||||||
|
assertEquals("UPS", order.carrierCode()); |
||||||
|
assertEquals(Double.valueOf(12.5), order.totalWeightKg()); |
||||||
|
assertEquals(Double.valueOf(0.8), order.totalVolumeCbm()); |
||||||
|
assertEquals(10, order.totalQuantity()); |
||||||
|
assertFalse(order.hasError()); |
||||||
|
} |
||||||
|
|
||||||
|
/** 调拨失败时 errorCode / errorReason 是唯一能定位原因的地方。 */ |
||||||
|
@Test |
||||||
|
void mapsErrorFields() { |
||||||
|
RecordingTransport transport = RecordingTransport.create() |
||||||
|
.route(YqnConstants.PATH_TRANSFER_CREATE, req -> Responses.ok( |
||||||
|
"{\"statusCode\":200,\"status\":0,\"code\":200,\"data\":{" |
||||||
|
+ "\"transferOutboundPlanNo\":\"TP1\"," |
||||||
|
+ "\"transferOutboundPlanStatus\":3," |
||||||
|
+ "\"sourceNo\":\"TR-001\"," |
||||||
|
+ "\"errorCode\":\"INSUFFICIENT_STOCK\"," |
||||||
|
+ "\"errorReason\":\"可售库存不足\"}}")); |
||||||
|
TransferOrder order = new YqnAdapter(transport, new JacksonJsonCodec()) |
||||||
|
.create(ctx(), command()); |
||||||
|
|
||||||
|
assertEquals(TransferStatus.REJECTED, order.status()); |
||||||
|
assertEquals("INSUFFICIENT_STOCK", order.errorCode()); |
||||||
|
assertEquals("可售库存不足", order.errorReason()); |
||||||
|
assertTrue(order.hasError()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void unknownStatusIsNotGuessed() { |
||||||
|
RecordingTransport transport = RecordingTransport.create() |
||||||
|
.route(YqnConstants.PATH_TRANSFER_CREATE, req -> Responses.ok( |
||||||
|
"{\"statusCode\":200,\"status\":0,\"code\":200,\"data\":{" |
||||||
|
+ "\"transferOutboundPlanNo\":\"TP1\"," |
||||||
|
+ "\"transferOutboundPlanStatus\":4242," |
||||||
|
+ "\"sourceNo\":\"TR-001\"}}")); |
||||||
|
TransferOrder order = new YqnAdapter(transport, new JacksonJsonCodec()) |
||||||
|
.create(ctx(), command()); |
||||||
|
|
||||||
|
assertEquals(TransferStatus.UNKNOWN, order.status()); |
||||||
|
assertEquals("4242", order.rawStatus()); |
||||||
|
} |
||||||
|
|
||||||
|
/** 响应不回传 type,从请求侧带过来,不猜。 */ |
||||||
|
@Test |
||||||
|
void typeComesFromRequestNotResponse() { |
||||||
|
RecordingTransport transport = RecordingTransport.create() |
||||||
|
.route(YqnConstants.PATH_TRANSFER_CREATE, req -> Responses.ok( |
||||||
|
"{\"statusCode\":200,\"status\":0,\"code\":200,\"data\":{" |
||||||
|
+ "\"transferOutboundPlanNo\":\"TP1\",\"sourceNo\":\"TR-001\"}}")); |
||||||
|
TransferOrder order = new YqnAdapter(transport, new JacksonJsonCodec()) |
||||||
|
.create(ctx(), command()); |
||||||
|
|
||||||
|
assertEquals(TransferType.WAREHOUSE_TO_WAREHOUSE, order.type()); |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue