Browse Source
- 新增 HengjianProductSaveDtos + HengjianProductMapper,声明 PRODUCT 能力位 - 保存语义:同 SKU 再调即更新,不区分 create/update;响应无 data, status 固定 SAVED、rawStatus 取仓方 message,不编 ADDED/UPDATED - 恒健必填项(nameEn / description / packingType / 外箱四项 / packedQty) 在本地拒绝,缺项一个请求都不发 - 三组尺寸(产品 / 单品 / 外箱)各归各位,不做回退填充 - 不发送恒健没有语义对应的字段:imageUrl(恒健是测量图)、packageStyle、 SN / 批次 / HS / 原产国 / 链接 / 客退 / 品类 - 新增写探针(默认跳过,另需 -Dhengjian.probe.write=true)验证保存幂等 - 文档补商品保存的字段口径与本地校验清单 验证:mvn test 全量通过,恒健 48 个用例 + 2 个探针跳过。 Co-Authored-By: Claude Code <noreply@anthropic.com>master
9 changed files with 886 additions and 22 deletions
@ -0,0 +1,165 @@
@@ -0,0 +1,165 @@
|
||||
package common.ecommerce.hengjian.adapter; |
||||
|
||||
import common.ecommerce.hengjian.dto.HengjianProductSaveDtos; |
||||
import common.ecommerce.shared.support.Money; |
||||
import common.ecommerce.warehouse.capability.WarehouseProduct; |
||||
|
||||
/** |
||||
* 恒健商品报文映射({@code POST /v1/product/save})。 |
||||
* |
||||
* <p>平台字段名只出现在本类与 {@link HengjianProductSaveDtos}。 |
||||
* 单位与统一模型一致(cm / g),不做换算。</p> |
||||
* |
||||
* <h2>必填项在本地拒绝</h2> |
||||
* <p>恒健把 <b>产品尺寸 / 外箱尺寸 / 装箱数量 / 英文名 / 描述 / 发货包装</b> 都标为必填。 |
||||
* 统一模型不强制这些(运去哪、信仓的要求各不相同),所以由本映射器按恒健的协议校验: |
||||
* 缺了直接抛 {@link IllegalArgumentException},<b>不拿其它字段顶替</b>—— |
||||
* 发出去被仓方拒,排查成本比本地报错高得多。</p> |
||||
* |
||||
* <h2>不发送的字段</h2> |
||||
* <table border="1"> |
||||
* <tr><th>恒健字段</th><th>为什么不发</th></tr> |
||||
* <tr><td>{@code imageUrls}</td> |
||||
* <td>文档写明是「产品测量图片」,用于复核长宽高,<b>不是商品主图</b>。 |
||||
* 统一模型的 {@code imageUrl} 是商品图片,语义不同,不能拿来顶替。</td></tr> |
||||
* <tr><td>{@code isSpecialOuterPackaging}</td> |
||||
* <td>「特殊外包装」与统一模型的 {@code packageStyle}(裸包装 / 销售包装 / 物流包装) |
||||
* 不是同一套码,映射需要业务确认,暂时不发。</td></tr> |
||||
* </table> |
||||
* |
||||
* <p>统一模型里的 SN、批次、HS Code、原产国、商品链接、客退处理方式、产品品类, |
||||
* 恒健文档都没有对应字段,一律不发。</p> |
||||
*/ |
||||
public final class HengjianProductMapper { |
||||
|
||||
/** 保价开关的开 / 关取值(恒健用 Y / N,不是 0 / 1)。 */ |
||||
private static final String INSURED_YES = "Y"; |
||||
private static final String INSURED_NO = "N"; |
||||
|
||||
private HengjianProductMapper() { |
||||
} |
||||
|
||||
/** |
||||
* 统一商品 → 恒健保存请求。 |
||||
* |
||||
* @param product 已通过 {@link WarehouseProduct} 构造期校验的商品 |
||||
* @throws IllegalArgumentException 恒健要求但统一模型未给的非空字段缺失 |
||||
*/ |
||||
public static HengjianProductSaveDtos.SaveProductRequest toRequest(WarehouseProduct product) { |
||||
String sku = skuOf(product); |
||||
|
||||
HengjianProductSaveDtos.SaveProductRequest req = new HengjianProductSaveDtos.SaveProductRequest(); |
||||
req.sku = sku; |
||||
req.upc = trimToNull(product.barcode()); |
||||
req.productType = product.productType() > 0 ? product.productType() : null; |
||||
|
||||
req.name = require(product.name(), "name", sku); |
||||
req.nameEn = require(product.nameEn(), "nameEn", sku); |
||||
req.description = require(product.description(), "description", sku); |
||||
req.packingType = requirePackingType(product, sku); |
||||
|
||||
// 产品尺寸与重量:WarehouseProduct 构造期已保证为正
|
||||
req.length = product.lengthCm(); |
||||
req.width = product.widthCm(); |
||||
req.height = product.heightCm(); |
||||
req.weight = product.weightGrams(); |
||||
|
||||
// 外箱五项同为必填,缺一不可
|
||||
req.packedLength = requirePacked(product.packedLengthCm(), "packedLengthCm", sku); |
||||
req.packedWidth = requirePacked(product.packedWidthCm(), "packedWidthCm", sku); |
||||
req.packedHeight = requirePacked(product.packedHeightCm(), "packedHeightCm", sku); |
||||
req.packedWeight = requirePacked(product.packedWeightGrams(), "packedWeightGrams", sku); |
||||
req.packedQty = requirePackedQty(product, sku); |
||||
|
||||
// 单品尺寸选填;未给(0)不发
|
||||
req.itemLength = positiveOrNull(product.itemLengthCm()); |
||||
req.itemWidth = positiveOrNull(product.itemWidthCm()); |
||||
req.itemHeight = positiveOrNull(product.itemHeightCm()); |
||||
|
||||
req.expirationControl = flagOf(product.expirationControl()); |
||||
// 没开保质期管理就不该带天数,避免仓方按「已设保质期但未启用」存脏数据
|
||||
req.expirationDate = Boolean.TRUE.equals(product.expirationControl()) |
||||
? product.expirationDays() : null; |
||||
|
||||
Money cost = product.cost(); |
||||
if (cost != null) { |
||||
req.cost = cost.amount().doubleValue(); |
||||
req.currency = cost.currency().getCurrencyCode(); |
||||
} |
||||
|
||||
req.isInsuranceFlag = insuranceFlag(product.insured()); |
||||
if (product.insuranceFee() != null) { |
||||
req.isInsuranceRate = product.insuranceFee().amount().doubleValue(); |
||||
} |
||||
|
||||
req.remark = trimToNull(product.remark()); |
||||
req.customName = trimToNull(product.declaredName()); |
||||
req.customNameEn = trimToNull(product.declaredNameEn()); |
||||
return req; |
||||
} |
||||
|
||||
/** 仓侧 SKU 优先;没有映射时用 OMS 主 SKU。 */ |
||||
public static String skuOf(WarehouseProduct product) { |
||||
String providerSku = product.providerSku(); |
||||
if (providerSku != null && !providerSku.trim().isEmpty()) { |
||||
return providerSku.trim(); |
||||
} |
||||
return product.omsSku(); |
||||
} |
||||
|
||||
/** 0 原包装发货 / 1 重新打包;未给(-1)在恒健是必填,直接拒绝。 */ |
||||
private static Integer requirePackingType(WarehouseProduct product, String sku) { |
||||
int packingType = product.packingType(); |
||||
if (packingType < 0) { |
||||
throw new IllegalArgumentException( |
||||
"hengjian product " + sku + " requires packingType (0 原包装发货 / 1 重新打包)"); |
||||
} |
||||
return packingType; |
||||
} |
||||
|
||||
private static Double requirePacked(Double value, String field, String sku) { |
||||
if (value == null || value <= 0) { |
||||
throw new IllegalArgumentException("hengjian product " + sku + " requires " + field |
||||
+ " (外箱尺寸与重量是必填项)"); |
||||
} |
||||
return value; |
||||
} |
||||
|
||||
private static Integer requirePackedQty(WarehouseProduct product, String sku) { |
||||
Integer qty = product.packedQuantity(); |
||||
if (qty == null || qty <= 0) { |
||||
throw new IllegalArgumentException("hengjian product " + sku |
||||
+ " requires packedQuantity (装箱数量是必填项)"); |
||||
} |
||||
return qty; |
||||
} |
||||
|
||||
/** 0/1 开关 → Integer;null 不发(由仓方按默认处理)。 */ |
||||
private static Integer flagOf(Boolean flag) { |
||||
return flag == null ? null : (flag ? 1 : 0); |
||||
} |
||||
|
||||
/** 保价开关 → 恒健的 {@code Y / N};null 不发。 */ |
||||
private static String insuranceFlag(Boolean insured) { |
||||
return insured == null ? null : (insured ? INSURED_YES : INSURED_NO); |
||||
} |
||||
|
||||
private static Double positiveOrNull(double v) { |
||||
return v > 0 ? v : null; |
||||
} |
||||
|
||||
private static String require(String value, String field, String sku) { |
||||
if (value == null || value.trim().isEmpty()) { |
||||
throw new IllegalArgumentException("hengjian product " + sku + " requires " + field); |
||||
} |
||||
return value.trim(); |
||||
} |
||||
|
||||
private static String trimToNull(String v) { |
||||
if (v == null) { |
||||
return null; |
||||
} |
||||
String trimmed = v.trim(); |
||||
return trimmed.isEmpty() ? null : trimmed; |
||||
} |
||||
} |
||||
@ -0,0 +1,123 @@
@@ -0,0 +1,123 @@
|
||||
package common.ecommerce.hengjian.dto; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 恒健报文结构:创建 / 编辑产品({@code POST /v1/product/save})。 |
||||
* |
||||
* <p>字段名照抄文档。注意<b>尺寸与重量分三组,含义不同</b>:</p> |
||||
* <ul> |
||||
* <li>{@code length / width / height / weight}:产品(发货单元)尺寸与重量;</li> |
||||
* <li>{@code itemLength / itemWidth / itemHeight}:单品尺寸,选填;</li> |
||||
* <li>{@code packedLength / packedWidth / packedHeight / packedWeight / packedQty}: |
||||
* 外箱尺寸、外箱重量与装箱数量,<b>文档标必填</b>。</li> |
||||
* </ul> |
||||
* <p>三组不能互相顶替:把单品尺寸填进 {@code length} 会让仓方的计费重与货架位算错。</p> |
||||
*/ |
||||
public final class HengjianProductSaveDtos { |
||||
|
||||
private HengjianProductSaveDtos() { |
||||
} |
||||
|
||||
/** 保存请求;响应没有 data,只有一个 {@code message}。 */ |
||||
@JsonInclude(JsonInclude.Include.NON_NULL) |
||||
public static final class SaveProductRequest { |
||||
|
||||
/** 产品 SKU;必填。 */ |
||||
public String sku; |
||||
|
||||
/** UPC 编码;选填。 */ |
||||
public String upc; |
||||
|
||||
/** 产品类型:1 成品 / 2 配件 / 3 耗材;不传由仓方按默认处理。 */ |
||||
public Integer productType; |
||||
|
||||
/** 中文名称;必填。 */ |
||||
public String name; |
||||
|
||||
/** 英文名称;必填。 */ |
||||
public String nameEn; |
||||
|
||||
/** 产品描述;必填。 */ |
||||
public String description; |
||||
|
||||
/** 产品发货包装:0 原包装发货 / 1 重新打包;必填。 */ |
||||
public Integer packingType; |
||||
|
||||
/** 产品长(cm);必填。 */ |
||||
public Double length; |
||||
|
||||
/** 产品宽(cm);必填。 */ |
||||
public Double width; |
||||
|
||||
/** 产品高(cm);必填。 */ |
||||
public Double height; |
||||
|
||||
/** 产品重量(g);必填。 */ |
||||
public Double weight; |
||||
|
||||
/** 外箱长(cm);必填。 */ |
||||
public Double packedLength; |
||||
|
||||
/** 外箱宽(cm);必填。 */ |
||||
public Double packedWidth; |
||||
|
||||
/** 外箱高(cm);必填。 */ |
||||
public Double packedHeight; |
||||
|
||||
/** 外箱重量(g);必填。 */ |
||||
public Double packedWeight; |
||||
|
||||
/** 装箱数量;必填。 */ |
||||
public Integer packedQty; |
||||
|
||||
/** 单品长(cm);选填。 */ |
||||
public Double itemLength; |
||||
|
||||
/** 单品宽(cm);选填。 */ |
||||
public Double itemWidth; |
||||
|
||||
/** 单品高(cm);选填。 */ |
||||
public Double itemHeight; |
||||
|
||||
/** 保质期管理:0 否 / 1 是;不填默认为否。 */ |
||||
public Integer expirationControl; |
||||
|
||||
/** 保质期天数;{@code expirationControl=1} 时才有意义。 */ |
||||
public Integer expirationDate; |
||||
|
||||
/** 成本。 */ |
||||
public Double cost; |
||||
|
||||
/** 币种,如 {@code USD}。 */ |
||||
public String currency; |
||||
|
||||
/** 是否保价:{@code Y} / {@code N};不填默认为否。 */ |
||||
public String isInsuranceFlag; |
||||
|
||||
/** 保价费用。 */ |
||||
public Double isInsuranceRate; |
||||
|
||||
/** 备注。 */ |
||||
public String remark; |
||||
|
||||
/** 报关中文名。 */ |
||||
public String customName; |
||||
|
||||
/** 报关英文名。 */ |
||||
public String customNameEn; |
||||
|
||||
/** 是否特殊外包装:0 否 / 1 是。 */ |
||||
public Integer isSpecialOuterPackaging; |
||||
|
||||
/** |
||||
* 产品测量图片地址列表。 |
||||
* |
||||
* <p>文档说明「有变更产品长/宽/高重量时传入」——它不是商品主图, |
||||
* 而是仓方复核尺寸用的测量照片。</p> |
||||
*/ |
||||
public List<String> imageUrls; |
||||
} |
||||
} |
||||
@ -0,0 +1,338 @@
@@ -0,0 +1,338 @@
|
||||
package common.ecommerce.hengjian.adapter; |
||||
|
||||
import common.ecommerce.hengjian.HengjianConstants; |
||||
import common.ecommerce.http.JacksonJsonCodec; |
||||
import common.ecommerce.shared.spi.ChannelSecret; |
||||
import common.ecommerce.shared.support.Money; |
||||
import common.ecommerce.shared.testkit.RecordingTransport; |
||||
import common.ecommerce.shared.testkit.Responses; |
||||
import common.ecommerce.warehouse.capability.WarehouseProduct; |
||||
import common.ecommerce.warehouse.capability.WarehouseProductCapable; |
||||
import common.ecommerce.warehouse.enums.WarehouseCapability; |
||||
import common.ecommerce.warehouse.enums.WarehouseProvider; |
||||
import common.ecommerce.warehouse.sdk.WarehouseConnection; |
||||
import common.ecommerce.warehouse.sdk.WarehouseContext; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import java.time.Clock; |
||||
import java.time.Instant; |
||||
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.assertThrows; |
||||
import static org.junit.jupiter.api.Assertions.assertTrue; |
||||
|
||||
/** |
||||
* 商品创建 / 编辑({@code POST /v1/product/save})契约测试。 |
||||
* |
||||
* <p>重点在三件容易出错的事:必填项本地拒绝、三组尺寸不互相顶替、 |
||||
* 不在报文里编造恒健没有的字段。</p> |
||||
*/ |
||||
class HengjianProductSaveTest { |
||||
|
||||
private static final Clock CLOCK = Clock.fixed(Instant.parse("2026-11-12T00:00:00Z"), ZoneOffset.UTC); |
||||
private static final String SAVE_BODY = |
||||
"{\"statusCode\":200,\"status\":0,\"code\":200,\"message\":\"产品创建成功\"}"; |
||||
|
||||
private static WarehouseContext ctx() { |
||||
return WarehouseContext.of("HJ-EFCA", WarehouseProvider.HENGJIAN, "EFCA", |
||||
WarehouseConnection.of(HengjianConstants.TEST_BASE_URL, |
||||
ChannelSecret.builder().appSecret("token-1").build()), |
||||
"req-1", CLOCK); |
||||
} |
||||
|
||||
private static HengjianAdapter adapter(RecordingTransport transport) { |
||||
return new HengjianAdapter(transport, new JacksonJsonCodec()); |
||||
} |
||||
|
||||
private static RecordingTransport saving() { |
||||
return RecordingTransport.create() |
||||
.route(HengjianConstants.PATH_PRODUCT_SAVE, req -> Responses.ok(SAVE_BODY)); |
||||
} |
||||
|
||||
/** 一份满足恒健全部必填项的商品。 */ |
||||
private static WarehouseProduct.Builder product() { |
||||
return WarehouseProduct.builder() |
||||
.omsSku("DM00001") |
||||
.name("测试商品") |
||||
.nameEn("Test Product") |
||||
.description("用于契约测试的商品") |
||||
.packingType(1) |
||||
.lengthCm(30) |
||||
.widthCm(20) |
||||
.heightCm(10) |
||||
.weightGrams(500) |
||||
.packedQuantity(6) |
||||
.packedLengthCm(60.0) |
||||
.packedWidthCm(40.0) |
||||
.packedHeightCm(30.0) |
||||
.packedWeightGrams(3200.0); |
||||
} |
||||
|
||||
// ---------------------------------------------------------------- 请求形状
|
||||
|
||||
@Test |
||||
void sendsPostToProductSave() { |
||||
RecordingTransport transport = saving(); |
||||
adapter(transport).createOrUpdate(ctx(), product().build()); |
||||
|
||||
assertEquals("POST", transport.lastDataRequest().method()); |
||||
assertEquals(HengjianConstants.TEST_BASE_URL + "/v1/product/save", |
||||
transport.lastDataRequest().url()); |
||||
} |
||||
|
||||
/** 文档必填的字段必须全部出现在报文里。 */ |
||||
@Test |
||||
void sendsAllMandatoryFields() { |
||||
RecordingTransport transport = saving(); |
||||
adapter(transport).createOrUpdate(ctx(), product().build()); |
||||
String body = transport.lastDataRequest().body(); |
||||
|
||||
String[] mandatory = {"sku", "name", "nameEn", "description", "packingType", |
||||
"length", "width", "height", "weight", |
||||
"packedLength", "packedWidth", "packedHeight", "packedWeight", "packedQty"}; |
||||
for (String field : mandatory) { |
||||
assertTrue(body.contains("\"" + field + "\""), "缺少必填字段 " + field + ",body=" + body); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 三组尺寸各归各位:产品尺寸、单品尺寸、外箱尺寸不能互相顶替。 |
||||
* 顶替会让仓方的计费重与货架位算错。 |
||||
*/ |
||||
@Test |
||||
void keepsThreeDimensionGroupsSeparate() { |
||||
RecordingTransport transport = saving(); |
||||
adapter(transport).createOrUpdate(ctx(), product() |
||||
.itemLengthCm(12.5) |
||||
.itemWidthCm(8) |
||||
.itemHeightCm(3.5) |
||||
.build()); |
||||
String body = transport.lastDataRequest().body(); |
||||
|
||||
assertTrue(body.contains("\"length\":30.0"), body); |
||||
assertTrue(body.contains("\"width\":20.0"), body); |
||||
assertTrue(body.contains("\"height\":10.0"), body); |
||||
assertTrue(body.contains("\"itemLength\":12.5"), body); |
||||
assertTrue(body.contains("\"itemWidth\":8.0"), body); |
||||
assertTrue(body.contains("\"itemHeight\":3.5"), body); |
||||
assertTrue(body.contains("\"packedLength\":60.0"), body); |
||||
assertTrue(body.contains("\"packedWidth\":40.0"), body); |
||||
assertTrue(body.contains("\"packedHeight\":30.0"), body); |
||||
assertTrue(body.contains("\"packedWeight\":3200.0"), body); |
||||
assertTrue(body.contains("\"packedQty\":6"), body); |
||||
} |
||||
|
||||
/** 单品尺寸选填:没给就不出现在报文里,不能填 0 占位。 */ |
||||
@Test |
||||
void omitsOptionalItemDimensionsWhenAbsent() { |
||||
RecordingTransport transport = saving(); |
||||
adapter(transport).createOrUpdate(ctx(), product().build()); |
||||
String body = transport.lastDataRequest().body(); |
||||
|
||||
assertFalse(body.contains("itemLength"), body); |
||||
assertFalse(body.contains("itemWidth"), body); |
||||
assertFalse(body.contains("itemHeight"), body); |
||||
} |
||||
|
||||
/** 没开保质期管理就不带天数,避免仓方存「已设天数但未启用」的脏数据。 */ |
||||
@Test |
||||
void expirationDaysOnlySentWhenControlOn() { |
||||
RecordingTransport transport = saving(); |
||||
adapter(transport).createOrUpdate(ctx(), product().expirationDays(365).build()); |
||||
assertFalse(transport.lastDataRequest().body().contains("expirationDate"), |
||||
"未启用保质期管理时不应发送天数"); |
||||
|
||||
RecordingTransport on = saving(); |
||||
adapter(on).createOrUpdate(ctx(), product().expirationControl(true).expirationDays(365).build()); |
||||
String body = on.lastDataRequest().body(); |
||||
assertTrue(body.contains("\"expirationControl\":1"), body); |
||||
assertTrue(body.contains("\"expirationDate\":365"), body); |
||||
} |
||||
|
||||
/** 保价开关用 Y / N,不是 0 / 1。 */ |
||||
@Test |
||||
void insuranceFlagUsesYn() { |
||||
RecordingTransport transport = saving(); |
||||
adapter(transport).createOrUpdate(ctx(), product() |
||||
.insured(true) |
||||
.insuranceFee(Money.of("2.50", "USD")) |
||||
.build()); |
||||
String body = transport.lastDataRequest().body(); |
||||
|
||||
assertTrue(body.contains("\"isInsuranceFlag\":\"Y\""), body); |
||||
assertTrue(body.contains("\"isInsuranceRate\":2.5"), body); |
||||
|
||||
RecordingTransport no = saving(); |
||||
adapter(no).createOrUpdate(ctx(), product().insured(false).build()); |
||||
assertTrue(no.lastDataRequest().body().contains("\"isInsuranceFlag\":\"N\"")); |
||||
} |
||||
|
||||
/** 未指定保价时整组不发,由仓方按默认(否)处理。 */ |
||||
@Test |
||||
void insuranceFieldsOmittedWhenUnspecified() { |
||||
RecordingTransport transport = saving(); |
||||
adapter(transport).createOrUpdate(ctx(), product().build()); |
||||
String body = transport.lastDataRequest().body(); |
||||
|
||||
assertFalse(body.contains("isInsuranceFlag"), body); |
||||
assertFalse(body.contains("isInsuranceRate"), body); |
||||
} |
||||
|
||||
@Test |
||||
void mapsCostCurrencyAndCustomsNames() { |
||||
RecordingTransport transport = saving(); |
||||
adapter(transport).createOrUpdate(ctx(), product() |
||||
.cost(Money.of("12.30", "USD")) |
||||
.declaredName("测试品") |
||||
.declaredNameEn("Test Item") |
||||
.build()); |
||||
String body = transport.lastDataRequest().body(); |
||||
|
||||
assertTrue(body.contains("\"cost\":12.3"), body); |
||||
assertTrue(body.contains("\"currency\":\"USD\""), body); |
||||
assertTrue(body.contains("\"customName\":\"测试品\""), body); |
||||
assertTrue(body.contains("\"customNameEn\":\"Test Item\""), body); |
||||
} |
||||
|
||||
/** 仓侧 SKU 优先。 */ |
||||
@Test |
||||
void providerSkuWins() { |
||||
RecordingTransport transport = saving(); |
||||
adapter(transport).createOrUpdate(ctx(), product().providerSku("HJ-SKU-1").build()); |
||||
|
||||
assertTrue(transport.lastDataRequest().body().contains("\"sku\":\"HJ-SKU-1\"")); |
||||
} |
||||
|
||||
/** |
||||
* 恒健没有的字段不能编:SN、批次、HS Code、原产国、商品链接、客退处理、品类, |
||||
* 以及语义不同的 {@code imageUrls} / {@code isSpecialOuterPackaging}。 |
||||
*/ |
||||
@Test |
||||
void doesNotInventFieldsHengjianDoesNotHave() { |
||||
RecordingTransport transport = saving(); |
||||
adapter(transport).createOrUpdate(ctx(), product() |
||||
.imageUrl("https://example.com/main.jpg") |
||||
.productCategory(5) |
||||
.packageStyle(1) |
||||
.requireSn(1) |
||||
.link("https://example.com/p/1") |
||||
.hsCode("9503.00") |
||||
.countryOfOrigin("CN") |
||||
.lotControlEnabled(true) |
||||
.lotAttributeCode("LOT") |
||||
.build()); |
||||
String body = transport.lastDataRequest().body(); |
||||
|
||||
String[] absent = {"imageUrls", "productCategory", "packageStyle", "isRequireSn", |
||||
"snMgmtMode", "link", "hsCode", "countryOfOrigin", "customs", |
||||
"lotControl", "returnDealMethod", "isSpecialOuterPackaging"}; |
||||
for (String field : absent) { |
||||
assertFalse(body.contains("\"" + field + "\""), "不应发送恒健没有的字段 " + field + ",body=" + body); |
||||
} |
||||
} |
||||
|
||||
// ---------------------------------------------------------------- 本地必填校验
|
||||
|
||||
@Test |
||||
void rejectsMissingNameEn() { |
||||
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, |
||||
() -> adapter(saving()).createOrUpdate(ctx(), product().nameEn(null).build())); |
||||
assertTrue(ex.getMessage().contains("nameEn"), ex.getMessage()); |
||||
} |
||||
|
||||
@Test |
||||
void rejectsMissingDescription() { |
||||
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, |
||||
() -> adapter(saving()).createOrUpdate(ctx(), product().description(null).build())); |
||||
assertTrue(ex.getMessage().contains("description"), ex.getMessage()); |
||||
} |
||||
|
||||
@Test |
||||
void rejectsMissingPackingType() { |
||||
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, |
||||
() -> adapter(saving()).createOrUpdate(ctx(), product().packingType(-1).build())); |
||||
assertTrue(ex.getMessage().contains("packingType"), ex.getMessage()); |
||||
} |
||||
|
||||
/** 外箱五项缺任何一个都本地拒绝,不静默补值。 */ |
||||
@Test |
||||
void rejectsMissingPackedDimensions() { |
||||
String[] fields = {"packedLengthCm", "packedWidthCm", "packedHeightCm", "packedWeightGrams"}; |
||||
for (String field : fields) { |
||||
WarehouseProduct.Builder b = product(); |
||||
if ("packedLengthCm".equals(field)) { |
||||
b.packedLengthCm(null); |
||||
} else if ("packedWidthCm".equals(field)) { |
||||
b.packedWidthCm(null); |
||||
} else if ("packedHeightCm".equals(field)) { |
||||
b.packedHeightCm(null); |
||||
} else { |
||||
b.packedWeightGrams(null); |
||||
} |
||||
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, |
||||
() -> adapter(saving()).createOrUpdate(ctx(), b.build()), field); |
||||
assertTrue(ex.getMessage().contains("packedLengthCm") || ex.getMessage().contains("外箱"), |
||||
ex.getMessage()); |
||||
} |
||||
} |
||||
|
||||
@Test |
||||
void rejectsMissingPackedQuantity() { |
||||
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, |
||||
() -> adapter(saving()).createOrUpdate(ctx(), product().packedQuantity(null).build())); |
||||
assertTrue(ex.getMessage().contains("packedQuantity"), ex.getMessage()); |
||||
} |
||||
|
||||
/** 校验失败时一个请求都不该发出去。 */ |
||||
@Test |
||||
void noRequestSentWhenValidationFails() { |
||||
RecordingTransport transport = saving(); |
||||
assertThrows(IllegalArgumentException.class, |
||||
() -> adapter(transport).createOrUpdate(ctx(), product().nameEn(null).build())); |
||||
|
||||
assertTrue(transport.dataRequests().isEmpty(), "本地校验失败不应发出请求"); |
||||
} |
||||
|
||||
// ---------------------------------------------------------------- 响应
|
||||
|
||||
@Test |
||||
void resultCarriesSkuAndProviderMessage() { |
||||
WarehouseProductCapable.ProductResult result = |
||||
adapter(saving()).createOrUpdate(ctx(), product().build()); |
||||
|
||||
assertEquals("DM00001", result.providerSku()); |
||||
assertEquals("SAVED", result.status()); |
||||
assertEquals("产品创建成功", result.rawStatus(), "仓方没有 data,只回 message"); |
||||
} |
||||
|
||||
/** 空 body 不能被当成成功。 */ |
||||
@Test |
||||
void emptyResponseBodyIsRejected() { |
||||
RecordingTransport transport = RecordingTransport.create() |
||||
.route(HengjianConstants.PATH_PRODUCT_SAVE, req -> Responses.ok("")); |
||||
|
||||
assertThrows(RuntimeException.class, |
||||
() -> adapter(transport).createOrUpdate(ctx(), product().build())); |
||||
} |
||||
|
||||
@Test |
||||
void businessErrorIsPropagated() { |
||||
RecordingTransport transport = RecordingTransport.create() |
||||
.route(HengjianConstants.PATH_PRODUCT_SAVE, req -> Responses.ok( |
||||
"{\"statusCode\":500,\"status\":1,\"code\":500,\"message\":\"sku 已存在\"}")); |
||||
|
||||
RuntimeException ex = assertThrows(RuntimeException.class, |
||||
() -> adapter(transport).createOrUpdate(ctx(), product().build())); |
||||
assertTrue(ex.getMessage().contains("sku 已存在"), ex.getMessage()); |
||||
} |
||||
|
||||
// ---------------------------------------------------------------- 能力位
|
||||
|
||||
@Test |
||||
void declaresProductCapability() { |
||||
assertTrue(adapter(RecordingTransport.create()).capabilities() |
||||
.contains(WarehouseCapability.PRODUCT)); |
||||
} |
||||
} |
||||
@ -0,0 +1,126 @@
@@ -0,0 +1,126 @@
|
||||
package common.ecommerce.hengjian.probe; |
||||
|
||||
import common.ecommerce.hengjian.HengjianConstants; |
||||
import common.ecommerce.hengjian.adapter.HengjianAdapter; |
||||
import common.ecommerce.http.JacksonJsonCodec; |
||||
import common.ecommerce.http.OkHttpTransport; |
||||
import common.ecommerce.shared.spi.ChannelSecret; |
||||
import common.ecommerce.shared.support.Money; |
||||
import common.ecommerce.warehouse.capability.WarehouseProduct; |
||||
import common.ecommerce.warehouse.capability.WarehouseProductCapable; |
||||
import common.ecommerce.warehouse.enums.WarehouseProvider; |
||||
import common.ecommerce.warehouse.sdk.WarehouseConnection; |
||||
import common.ecommerce.warehouse.sdk.WarehouseContext; |
||||
import org.junit.jupiter.api.Assumptions; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.condition.EnabledIfSystemProperty; |
||||
|
||||
import java.time.Clock; |
||||
|
||||
/** |
||||
* 恒健<b>写接口</b>探针:真实创建商品,用于核对 {@code /v1/product/save} 的报文。 |
||||
* |
||||
* <p>需要两个开关同时打开,避免误跑在正式环境:</p> |
||||
* <pre> |
||||
* mvn -pl warehouse-sdk-hengjian -DfailIfNoTests=false \ |
||||
* -Dhengjian.probe=true -Dhengjian.probe.write=true \ |
||||
* -Dhengjian.probe.token=你的组织token \ |
||||
* -Dhengjian.probe.sku=PROBE-SKU-001 \ |
||||
* -Dtest=HengjianWriteProbeTest test |
||||
* </pre> |
||||
* |
||||
* <p><b>会在仓方测试环境留下真实商品数据</b>:恒健文档里没有删除产品的接口, |
||||
* 跑完需要人工在 Efulfill 后台清理。SKU 默认带时间戳,重跑不会撞唯一约束。</p> |
||||
* |
||||
* <p>输出只打印结果与关键字段,不打印 token。</p> |
||||
*/ |
||||
@EnabledIfSystemProperty(named = "hengjian.probe.write", matches = "true") |
||||
class HengjianWriteProbeTest { |
||||
|
||||
private static final String TOKEN = System.getProperty("hengjian.probe.token", ""); |
||||
private static final String BASE_URL = opt("hengjian.probe.baseUrl", HengjianConstants.TEST_BASE_URL); |
||||
private static final String WAREHOUSE_CODE = opt("hengjian.probe.warehouseCode", "HJ-PROBE"); |
||||
private static final String PROVIDER_WAREHOUSE = opt("hengjian.probe.providerWarehouseId", "EFCA"); |
||||
private static final String SKU = |
||||
opt("hengjian.probe.sku", "PROBE-" + System.currentTimeMillis()); |
||||
|
||||
private static String opt(String key, String fallback) { |
||||
String v = System.getProperty(key); |
||||
return v == null || v.trim().isEmpty() ? fallback : v.trim(); |
||||
} |
||||
|
||||
private static WarehouseContext ctx() { |
||||
return WarehouseContext.of(WAREHOUSE_CODE, WarehouseProvider.HENGJIAN, PROVIDER_WAREHOUSE, |
||||
WarehouseConnection.of(BASE_URL, |
||||
ChannelSecret.builder().appSecret(TOKEN.trim()).build()), |
||||
"probe-" + System.currentTimeMillis(), Clock.systemUTC()); |
||||
} |
||||
|
||||
private static HengjianAdapter adapter() { |
||||
return new HengjianAdapter(new OkHttpTransport("common-ecommerce-hengjian-probe"), |
||||
new JacksonJsonCodec()); |
||||
} |
||||
|
||||
private static void assumeToken() { |
||||
Assumptions.assumeTrue(!TOKEN.trim().isEmpty(), |
||||
"未提供 -Dhengjian.probe.token,跳过恒健写探针"); |
||||
} |
||||
|
||||
/** 一份满足恒健全部必填项的商品。 */ |
||||
private static WarehouseProduct product(String sku) { |
||||
return WarehouseProduct.builder() |
||||
.omsSku(sku) |
||||
.name("探针商品") |
||||
.nameEn("Probe Product") |
||||
.description("SDK 探针创建,可删除") |
||||
.packingType(1) |
||||
.lengthCm(30) |
||||
.widthCm(20) |
||||
.heightCm(10) |
||||
.weightGrams(500) |
||||
.packedQuantity(6) |
||||
.packedLengthCm(60.0) |
||||
.packedWidthCm(40.0) |
||||
.packedHeightCm(30.0) |
||||
.packedWeightGrams(3200.0) |
||||
.cost(Money.of("1.00", "USD")) |
||||
.build(); |
||||
} |
||||
|
||||
/** |
||||
* 建商品:验证必填项组合与保存语义。 |
||||
* |
||||
* <p>同 SKU 连跑两次可确认 {@code save} 是幂等保存(第二次应为更新而非报重复)。</p> |
||||
*/ |
||||
@Test |
||||
void probeProductSave() { |
||||
assumeToken(); |
||||
System.out.println("\n================ hengjian / v1/product/save ================"); |
||||
System.out.println(" sku=" + SKU + " base=" + BASE_URL + " warehouse=" + PROVIDER_WAREHOUSE); |
||||
try { |
||||
WarehouseProductCapable.ProductResult result = |
||||
adapter().createOrUpdate(ctx(), product(SKU)); |
||||
System.out.println(" [OK] status=" + result.status() |
||||
+ " providerSku=" + result.providerSku() |
||||
+ " rawStatus=" + result.rawStatus()); |
||||
} catch (RuntimeException e) { |
||||
System.out.println(" [FAIL] " + e.getClass().getSimpleName() + ": " + e.getMessage()); |
||||
} |
||||
} |
||||
|
||||
/** 再存一次:确认保存语义(更新)不会因 SKU 重复而失败。 */ |
||||
@Test |
||||
void probeProductSaveTwice() { |
||||
assumeToken(); |
||||
System.out.println("\n================ hengjian / v1/product/save (second save) ================"); |
||||
try { |
||||
WarehouseProductCapable.ProductResult result = |
||||
adapter().createOrUpdate(ctx(), product(SKU)); |
||||
System.out.println(" [OK] 第二次保存 status=" + result.status() |
||||
+ " rawStatus=" + result.rawStatus()); |
||||
} catch (RuntimeException e) { |
||||
System.out.println(" [FAIL] " + e.getClass().getSimpleName() + ": " + e.getMessage() |
||||
+ " ← 若非幂等保存,这里会报 SKU 已存在"); |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue