Browse Source
- dto/EdayunCreateProductDtos:按文档建模,含组合产品 / 证书 / 图片 / 申报 / 批次等子结构;字段名是驼峰(与 token 接口的下划线风格不同) - enums/EdayunYesNo:0 是 / 1 否 的统一封装。这个口径与直觉相反, 单独成枚举避免各处手写 x ? 0 : 1 把方向写反 - enums/EdayunDeclarationType:出口 / 进口(文档未给码值,按示例推断 0/1) - adapter/EdayunProductMapper:g → kg 换算;反向标志统一走 EdayunYesNo; 申报信息按「出口=原产国 + 进口=US」组两条,缺值时整组不下发 - adapter/EdayunAdapter:实现 createOrUpdate,声明 PRODUCT 能力位 - 请求头从单独的小写 token 改为文档写的 Authorization: Bearer <token> (实测两者都接受,以文档为准) 测试 9 个:字段映射、g→kg、brandFlag 反向、默认不带电、序列号与批次标志、 申报信息缺值不下发、图片、枚举码值、能力位。 探针 EdayunCreateProductProbeTest:默认 dry-run 只打印报文,真跑需 -Dedayun.probe.write=true,且用 PROBE- 前缀的随机 SKU 避免撞车。 全量 544 个测试通过。 Co-Authored-By: Claude <noreply@anthropic.com>master
11 changed files with 862 additions and 16 deletions
@ -0,0 +1,139 @@ |
|||||||
|
package common.ecommerce.edayun.adapter; |
||||||
|
|
||||||
|
import common.ecommerce.edayun.dto.EdayunCreateProductDtos; |
||||||
|
import common.ecommerce.edayun.enums.EdayunDeclarationType; |
||||||
|
import common.ecommerce.edayun.enums.EdayunYesNo; |
||||||
|
import common.ecommerce.shared.support.Money; |
||||||
|
import common.ecommerce.warehouse.capability.WarehouseProduct; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
/** |
||||||
|
* 易达云商品报文映射({@code POST /webApi/skuInfo/v1/createProduct})。 |
||||||
|
* |
||||||
|
* <p>单位差异:{@link WarehouseProduct} 用 g / cm,易达云用 <b>kg</b> / cm。 |
||||||
|
* 重量在这里换算,调用方不用管。</p> |
||||||
|
* |
||||||
|
* <p>三处「字段有默认值但语义不同」的地方:</p> |
||||||
|
* <ul> |
||||||
|
* <li>{@code brandFlag}:0 是 / 1 否(<b>与直觉相反</b>),没有品牌信息时传 1</li> |
||||||
|
* <li>{@code logisticsPackagingType}:0 是 / 1 否,同样反向</li> |
||||||
|
* <li>{@code electricFlag}:0 带电 / 1 不带电,同样反向</li> |
||||||
|
* </ul> |
||||||
|
*/ |
||||||
|
public final class EdayunProductMapper { |
||||||
|
|
||||||
|
/** 1 克 = 0.001 千克。 */ |
||||||
|
private static final double GRAMS_PER_KG = 1000.0; |
||||||
|
|
||||||
|
private EdayunProductMapper() { |
||||||
|
} |
||||||
|
|
||||||
|
/** 统一商品 → 创建产品请求。 */ |
||||||
|
public static EdayunCreateProductDtos.CreateProductRequest toCreateRequest(WarehouseProduct product) { |
||||||
|
EdayunCreateProductDtos.CreateProductRequest req = |
||||||
|
new EdayunCreateProductDtos.CreateProductRequest(); |
||||||
|
|
||||||
|
req.skuCode = skuOf(product); |
||||||
|
req.skuName = product.name(); |
||||||
|
req.skuNameEn = product.nameEn(); |
||||||
|
req.extraBarcode1 = product.barcode(); |
||||||
|
req.skuUrl = product.link(); |
||||||
|
req.remark = product.remark(); |
||||||
|
|
||||||
|
// 反向标志:没有品牌信息时传「非品牌」
|
||||||
|
req.brandFlag = EdayunYesNo.noOf(product.customItemNo() != null |
||||||
|
&& !product.customItemNo().trim().isEmpty()); |
||||||
|
|
||||||
|
req.weight = gramsToKg(product.weightGrams()); |
||||||
|
req.length = product.lengthCm(); |
||||||
|
req.width = product.widthCm(); |
||||||
|
req.height = product.heightCm(); |
||||||
|
|
||||||
|
// 统一模型没有「是否自带物流包装」的概念,默认按「自带」传 0
|
||||||
|
req.logisticsPackagingType = 0; |
||||||
|
// 统一模型没有包材概念,默认不是包材
|
||||||
|
req.pmFlag = Boolean.FALSE; |
||||||
|
|
||||||
|
// 带电信息由调用方经 packType 之外的通道表达不了,默认按不带电
|
||||||
|
req.electricFlag = 1; |
||||||
|
|
||||||
|
req.batchManagementEnabled = EdayunYesNo.codeOf(product.lotControlEnabled()); |
||||||
|
req.collectSerialNumber = product.snManagementMode() == null ? null |
||||||
|
: (product.snManagementMode() > 0 ? 1 : 0); |
||||||
|
|
||||||
|
// 图片
|
||||||
|
if (product.imageUrl() != null && !product.imageUrl().trim().isEmpty()) { |
||||||
|
EdayunCreateProductDtos.CreateProductRequest.Picture pic = |
||||||
|
new EdayunCreateProductDtos.CreateProductRequest.Picture(); |
||||||
|
pic.picUrl = product.imageUrl().trim(); |
||||||
|
List<EdayunCreateProductDtos.CreateProductRequest.Picture> pics = |
||||||
|
new ArrayList<EdayunCreateProductDtos.CreateProductRequest.Picture>(1); |
||||||
|
pics.add(pic); |
||||||
|
req.picList = pics; |
||||||
|
} |
||||||
|
|
||||||
|
// 效期 → batchInfo
|
||||||
|
if (product.expirationControl() != null && product.expirationControl() |
||||||
|
&& product.expirationDays() != null) { |
||||||
|
EdayunCreateProductDtos.CreateProductRequest.BatchInfo batch = |
||||||
|
new EdayunCreateProductDtos.CreateProductRequest.BatchInfo(); |
||||||
|
batch.expirationEnabled = 1; |
||||||
|
batch.shelfLifeDays = product.expirationDays(); |
||||||
|
req.batchInfo = batch; |
||||||
|
} |
||||||
|
|
||||||
|
req.declarationInfoList = declarationsOf(product); |
||||||
|
return req; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 申报信息:出口 + 进口各一条。 |
||||||
|
* |
||||||
|
* <p>文档要求「一个 sku 至少需要一条出口 + 一条进口」,但标准模型只有一组 |
||||||
|
* 申报字段(HS / 原产国 / 申报价值 / 申报品名)。这里用同一组数据生成两条, |
||||||
|
* 国家分别取 {@code countryOfOrigin}(出口)与默认 {@code US}(进口)。</p> |
||||||
|
* |
||||||
|
* <p><b>待确认</b>:进口国的取法需要业务侧明确——目前是猜的。</p> |
||||||
|
*/ |
||||||
|
private static List<EdayunCreateProductDtos.CreateProductRequest.Declaration> declarationsOf( |
||||||
|
WarehouseProduct product) { |
||||||
|
Money value = product.declaredValue(); |
||||||
|
String origin = product.countryOfOrigin(); |
||||||
|
if (value == null || origin == null || origin.trim().isEmpty()) { |
||||||
|
// 文档标必填,但标准模型允许这三个字段整组缺省;缺省时不下发,
|
||||||
|
// 由仓方报错——不在本地编造申报信息
|
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
List<EdayunCreateProductDtos.CreateProductRequest.Declaration> out = |
||||||
|
new ArrayList<EdayunCreateProductDtos.CreateProductRequest.Declaration>(2); |
||||||
|
out.add(declaration(EdayunDeclarationType.EXPORT, origin.trim().toUpperCase(), value)); |
||||||
|
out.add(declaration(EdayunDeclarationType.IMPORT, DEFAULT_IMPORT_COUNTRY, value)); |
||||||
|
return out; |
||||||
|
} |
||||||
|
|
||||||
|
/** 进口国默认值;<b>待业务确认</b>。 */ |
||||||
|
private static final String DEFAULT_IMPORT_COUNTRY = "US"; |
||||||
|
|
||||||
|
private static EdayunCreateProductDtos.CreateProductRequest.Declaration declaration( |
||||||
|
EdayunDeclarationType type, String country, Money value) { |
||||||
|
EdayunCreateProductDtos.CreateProductRequest.Declaration d = |
||||||
|
new EdayunCreateProductDtos.CreateProductRequest.Declaration(); |
||||||
|
d.declarationType = type.code(); |
||||||
|
d.country = country; |
||||||
|
d.declaredPrice = value.amount().doubleValue(); |
||||||
|
d.currencyType = value.currency().getCurrencyCode(); |
||||||
|
return d; |
||||||
|
} |
||||||
|
|
||||||
|
private static String skuOf(WarehouseProduct product) { |
||||||
|
String sku = product.providerSku(); |
||||||
|
return sku != null && !sku.trim().isEmpty() ? sku.trim() : product.omsSku(); |
||||||
|
} |
||||||
|
|
||||||
|
static Double gramsToKg(double grams) { |
||||||
|
return grams / GRAMS_PER_KG; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,194 @@ |
|||||||
|
package common.ecommerce.edayun.dto; |
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude; |
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty; |
||||||
|
|
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
/** |
||||||
|
* 易达云报文结构:创建产品({@code POST /webApi/skuInfo/v1/createProduct})。 |
||||||
|
* |
||||||
|
* <p>一个文件对应一个接口,只放该接口的请求 / 响应与内嵌子结构; |
||||||
|
* 与其它接口共用的结构各自复制一份,避免跨接口耦合。</p> |
||||||
|
* |
||||||
|
* <p>字段名是<b>驼峰</b>(与 token 接口的下划线风格不同),照文档原样保留。</p> |
||||||
|
*/ |
||||||
|
public final class EdayunCreateProductDtos { |
||||||
|
|
||||||
|
private EdayunCreateProductDtos() { |
||||||
|
} |
||||||
|
|
||||||
|
/** 创建产品请求。 */ |
||||||
|
@JsonInclude(JsonInclude.Include.NON_NULL) |
||||||
|
public static final class CreateProductRequest { |
||||||
|
|
||||||
|
/** SKU 编码,长度 ≤32。必填。 */ |
||||||
|
public String skuCode; |
||||||
|
/** 外部条码 1,≤50。 */ |
||||||
|
public String extraBarcode1; |
||||||
|
/** 外部条码 2,≤50。 */ |
||||||
|
public String extraBarcode2; |
||||||
|
/** 外部条码 3,≤50。 */ |
||||||
|
public String extraBarcode3; |
||||||
|
/** SKU 中文名,≤150。必填。 */ |
||||||
|
public String skuName; |
||||||
|
/** SKU 英文名,≤150。必填。 */ |
||||||
|
public String skuNameEn; |
||||||
|
/** 是否品牌:0 是 / 1 否。必填。 */ |
||||||
|
public Integer brandFlag; |
||||||
|
public String brandName; |
||||||
|
/** 销售链接,≤500。 */ |
||||||
|
public String skuUrl; |
||||||
|
/** 用途,≤50。必填(需写清使用场景 / 原理)。 */ |
||||||
|
public String useTo; |
||||||
|
/** 材质,≤50。必填(需具体,不要笼统写「金属」)。 */ |
||||||
|
public String materialQuality; |
||||||
|
public String specificationAttribute; |
||||||
|
|
||||||
|
/** 是否带电:0 带电 / 1 不带电。必填。 */ |
||||||
|
public Integer electricFlag; |
||||||
|
/** 电池类型,字典 sku_electric_type:0 锂电子 / 1 锂金属 / 2 非锂。带电必填。 */ |
||||||
|
public String electricType; |
||||||
|
/** 电池包装,字典 sku_electric_package:0 纯电 / 1 内置 / 2 外置。带电必填。 */ |
||||||
|
public String electricPackage; |
||||||
|
/** 电池功率(W);带电必填,>100W 还要 unNumber。 */ |
||||||
|
public String electricPower; |
||||||
|
/** UN 编码;带电且功率 >100W 必填。 */ |
||||||
|
public String unNumber; |
||||||
|
/** 电池文件地址。 */ |
||||||
|
public String electricUrl; |
||||||
|
|
||||||
|
/** 产品重量 kg,[0.001, 1000]。必填。 */ |
||||||
|
public Double weight; |
||||||
|
/** 长 cm,[0.5, 10000]。必填。 */ |
||||||
|
public Double length; |
||||||
|
public Double width; |
||||||
|
public Double height; |
||||||
|
/** 是否自带物流包装:0 是 / 1 否。必填。 */ |
||||||
|
public Integer logisticsPackagingType; |
||||||
|
|
||||||
|
/** 是否包材。必填。 */ |
||||||
|
public Boolean pmFlag; |
||||||
|
/** 包材长 cm,[0.5, 10000];是包材时必填。 */ |
||||||
|
public Double pmLength; |
||||||
|
public Double pmWidth; |
||||||
|
public Double pmHeight; |
||||||
|
|
||||||
|
/** 批次管理:0 否 / 1 是。需单独申请权限。 */ |
||||||
|
public Integer batchManagementEnabled; |
||||||
|
/** 存储条件:0 常温 / 2 冷藏 / 3 冷冻 / 4 恒温。 */ |
||||||
|
public Integer stockEnvironment; |
||||||
|
|
||||||
|
public String firstCategory; |
||||||
|
public String secondaryCategory; |
||||||
|
public String tertiaryCategory; |
||||||
|
public String quaternaryCategory; |
||||||
|
|
||||||
|
/** 是否含钢铁铝:0 否 / 1 是;为 1 时三个含量至少一项 >0。 */ |
||||||
|
public Integer isMetalMaterial; |
||||||
|
/** 钢含量 0–100。 */ |
||||||
|
public Double steelContent; |
||||||
|
/** 铁含量 0–100;文档标为 string。 */ |
||||||
|
public String ironContent; |
||||||
|
/** 铝含量 0–100;文档标为 string。 */ |
||||||
|
public String aluminumContent; |
||||||
|
|
||||||
|
/** 序列号管理:0 否 / 1 是。 */ |
||||||
|
public Integer collectSerialNumber; |
||||||
|
/** 序列号示例图;序列号管理时必填。 */ |
||||||
|
public String snDiagram; |
||||||
|
/** 序列号使用范围,逗号拼接:SN_REV / SN_ORD / SN_RMA。 */ |
||||||
|
public String snUseScopes; |
||||||
|
/** 序列号格式:1 纯数字 / 2 字母+数字 / 3 数字+字母。 */ |
||||||
|
public Integer snFormat; |
||||||
|
public Integer snTotalCharacter; |
||||||
|
/** 格式为 1 时必填。 */ |
||||||
|
public String snBeginDigit; |
||||||
|
/** 格式为 2 时必填。 */ |
||||||
|
public String snBeginLetter; |
||||||
|
|
||||||
|
public String remark; |
||||||
|
/** 是否组合产品:0 否 / 1 是。 */ |
||||||
|
public Integer skuType; |
||||||
|
/** 组合产品明细;是组合产品时必填。 */ |
||||||
|
public List<CombinationItem> skuCombinationRelationList; |
||||||
|
public List<Certificate> skuCertificate; |
||||||
|
public List<Picture> picList; |
||||||
|
|
||||||
|
/** |
||||||
|
* 申报信息。<b>必填</b>:一个 SKU 至少需要一条出口 + 一条进口。 |
||||||
|
*/ |
||||||
|
public List<Declaration> declarationInfoList; |
||||||
|
|
||||||
|
/** 批次信息。 */ |
||||||
|
public BatchInfo batchInfo; |
||||||
|
|
||||||
|
/** 组合产品明细行。 */ |
||||||
|
@JsonInclude(JsonInclude.Include.NON_NULL) |
||||||
|
public static final class CombinationItem { |
||||||
|
public String skuCode; |
||||||
|
public String skuQuery; |
||||||
|
} |
||||||
|
|
||||||
|
/** 证书。 */ |
||||||
|
@JsonInclude(JsonInclude.Include.NON_NULL) |
||||||
|
public static final class Certificate { |
||||||
|
public String country; |
||||||
|
public String certificateType; |
||||||
|
public String certificateNo; |
||||||
|
public String certificateExplain; |
||||||
|
/** 文档示例是毫秒时间戳。 */ |
||||||
|
public Long certificateDate; |
||||||
|
public String certificateUrl; |
||||||
|
} |
||||||
|
|
||||||
|
/** 图片。 */ |
||||||
|
@JsonInclude(JsonInclude.Include.NON_NULL) |
||||||
|
public static final class Picture { |
||||||
|
public String picUrl; |
||||||
|
} |
||||||
|
|
||||||
|
/** 申报信息(出口 / 进口各一条)。 */ |
||||||
|
@JsonInclude(JsonInclude.Include.NON_NULL) |
||||||
|
public static final class Declaration { |
||||||
|
/** 0 出口 / 1 进口(文档未明确取值含义,按示例传 0)。 */ |
||||||
|
public Integer declarationType; |
||||||
|
public String country; |
||||||
|
public Double declaredPrice; |
||||||
|
public String currencyType; |
||||||
|
public Integer headStrokeType; |
||||||
|
} |
||||||
|
|
||||||
|
/** 批次信息。 */ |
||||||
|
@JsonInclude(JsonInclude.Include.NON_NULL) |
||||||
|
public static final class BatchInfo { |
||||||
|
/** 是否开启效期管理 0 否 / 1 是。 */ |
||||||
|
public Integer expirationEnabled; |
||||||
|
public Integer shelfLifeDays; |
||||||
|
public Integer outboundThreshold; |
||||||
|
public Integer warningDays; |
||||||
|
public Integer inboundThreshold; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** 创建产品响应。 */ |
||||||
|
@JsonInclude(JsonInclude.Include.NON_NULL) |
||||||
|
public static final class CreateProductResponse { |
||||||
|
|
||||||
|
/** 200 成功,其他失败。 */ |
||||||
|
public Integer code; |
||||||
|
public String msg; |
||||||
|
public ProductData data; |
||||||
|
|
||||||
|
/** 成功时的数据。 */ |
||||||
|
@JsonInclude(JsonInclude.Include.NON_NULL) |
||||||
|
public static final class ProductData { |
||||||
|
@JsonProperty("skuId") |
||||||
|
public String skuId; |
||||||
|
@JsonProperty("skuCode") |
||||||
|
public String skuCode; |
||||||
|
@JsonProperty("barcode") |
||||||
|
public String barcode; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,25 @@ |
|||||||
|
package common.ecommerce.edayun.enums; |
||||||
|
|
||||||
|
/** |
||||||
|
* 易达云申报类型({@code declarationInfoList[].declarationType})。 |
||||||
|
* |
||||||
|
* <p>文档要求一个 SKU 至少一条出口 + 一条进口,但没说码值。 |
||||||
|
* 示例里只出现 {@code 0},<b>按 0 出口、1 进口推断</b>——待联调确认。</p> |
||||||
|
*/ |
||||||
|
public enum EdayunDeclarationType { |
||||||
|
|
||||||
|
/** 出口申报。 */ |
||||||
|
EXPORT(0), |
||||||
|
/** 进口申报。 */ |
||||||
|
IMPORT(1); |
||||||
|
|
||||||
|
private final int code; |
||||||
|
|
||||||
|
EdayunDeclarationType(int code) { |
||||||
|
this.code = code; |
||||||
|
} |
||||||
|
|
||||||
|
public int code() { |
||||||
|
return code; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,64 @@ |
|||||||
|
package common.ecommerce.edayun.enums; |
||||||
|
|
||||||
|
/** |
||||||
|
* 易达云布尔标志的码值:<b>0 是 / 1 否</b>——与直觉相反。 |
||||||
|
* |
||||||
|
* <p>多处复用:{@code brandFlag} / {@code logisticsPackagingType} / |
||||||
|
* {@code electricFlag} / {@code isMetalMaterial} 等都是这个口径。 |
||||||
|
* 单独成枚举是为了让 {@link #codeOf(Boolean)} 成为唯一入口, |
||||||
|
* 避免各处手写 {@code x ? 0 : 1} 时把方向写反。</p> |
||||||
|
*/ |
||||||
|
public enum EdayunYesNo { |
||||||
|
|
||||||
|
/** 是。 */ |
||||||
|
YES(0, true), |
||||||
|
/** 否。 */ |
||||||
|
NO(1, false); |
||||||
|
|
||||||
|
private final int code; |
||||||
|
private final boolean value; |
||||||
|
|
||||||
|
EdayunYesNo(int code, boolean value) { |
||||||
|
this.code = code; |
||||||
|
this.value = value; |
||||||
|
} |
||||||
|
|
||||||
|
public int code() { |
||||||
|
return code; |
||||||
|
} |
||||||
|
|
||||||
|
public boolean value() { |
||||||
|
return value; |
||||||
|
} |
||||||
|
|
||||||
|
/** 业务布尔 → 易达云码;null 透传(由调用方决定是否必填)。 */ |
||||||
|
public static Integer codeOf(Boolean value) { |
||||||
|
if (value == null) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
return value ? YES.code : NO.code; |
||||||
|
} |
||||||
|
|
||||||
|
/** 易达云码 → 业务布尔;未知码返回 null,不猜。 */ |
||||||
|
public static Boolean valueOf(Integer code) { |
||||||
|
if (code == null) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
if (code == YES.code) { |
||||||
|
return Boolean.TRUE; |
||||||
|
} |
||||||
|
if (code == NO.code) { |
||||||
|
return Boolean.FALSE; |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 「有值即为是」的辅助:{@code nullableHasValue(true)} → 0(是)。 |
||||||
|
* |
||||||
|
* <p>给 {@code brandFlag} 这类「只要给了品牌信息就算有品牌」的字段用。</p> |
||||||
|
*/ |
||||||
|
public static Integer noOf(boolean hasValue) { |
||||||
|
return hasValue ? YES.code : NO.code; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,153 @@ |
|||||||
|
package common.ecommerce.edayun.adapter; |
||||||
|
|
||||||
|
import common.ecommerce.edayun.EdayunConstants; |
||||||
|
import common.ecommerce.edayun.dto.EdayunCreateProductDtos; |
||||||
|
import common.ecommerce.edayun.enums.EdayunYesNo; |
||||||
|
import common.ecommerce.shared.spi.AccessToken; |
||||||
|
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.enums.WarehouseCapability; |
||||||
|
import org.junit.jupiter.api.Test; |
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull; |
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue; |
||||||
|
|
||||||
|
/** |
||||||
|
* 创建产品({@code POST /webApi/skuInfo/v1/createProduct})报文映射。 |
||||||
|
* |
||||||
|
* <p>只测映射,不发请求——重点是三处反向标志、g→kg 换算、申报信息的组装。</p> |
||||||
|
*/ |
||||||
|
class EdayunProductTest { |
||||||
|
|
||||||
|
private static WarehouseProduct.Builder base() { |
||||||
|
return WarehouseProduct.builder() |
||||||
|
.omsSku("DM00001") |
||||||
|
.name("测试商品") |
||||||
|
.nameEn("Test Product") |
||||||
|
.packingType(0) |
||||||
|
.lengthCm(10).widthCm(8).heightCm(6) |
||||||
|
.weightGrams(500); |
||||||
|
} |
||||||
|
|
||||||
|
// ------------------------------------------------------------ 映射
|
||||||
|
|
||||||
|
@Test |
||||||
|
void mapsCoreFields() { |
||||||
|
EdayunCreateProductDtos.CreateProductRequest req = |
||||||
|
EdayunProductMapper.toCreateRequest(base().build()); |
||||||
|
|
||||||
|
assertEquals("DM00001", req.skuCode); |
||||||
|
assertEquals("测试商品", req.skuName); |
||||||
|
assertEquals("Test Product", req.skuNameEn); |
||||||
|
assertEquals(Double.valueOf(10.0), req.length); |
||||||
|
assertEquals(Double.valueOf(8.0), req.width); |
||||||
|
assertEquals(Double.valueOf(6.0), req.height); |
||||||
|
} |
||||||
|
|
||||||
|
/** 单位:标准模型是克,易达云是千克。 */ |
||||||
|
@Test |
||||||
|
void convertsGramsToKilograms() { |
||||||
|
EdayunCreateProductDtos.CreateProductRequest req = |
||||||
|
EdayunProductMapper.toCreateRequest(base().weightGrams(500).build()); |
||||||
|
|
||||||
|
assertEquals(Double.valueOf(0.5), req.weight); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* brandFlag 是「0 是 / 1 否」,与直觉相反。 |
||||||
|
* 没有品牌信息时必须传 1(否),传 0 会让仓以为这是品牌商品。 |
||||||
|
*/ |
||||||
|
@Test |
||||||
|
void brandFlagIsInverted() { |
||||||
|
EdayunCreateProductDtos.CreateProductRequest withoutBrand = |
||||||
|
EdayunProductMapper.toCreateRequest(base().build()); |
||||||
|
assertEquals(Integer.valueOf(1), withoutBrand.brandFlag); |
||||||
|
|
||||||
|
EdayunCreateProductDtos.CreateProductRequest withBrand = |
||||||
|
EdayunProductMapper.toCreateRequest( |
||||||
|
base().customItemNo("BRAND-1").build()); |
||||||
|
assertEquals(Integer.valueOf(0), withBrand.brandFlag); |
||||||
|
} |
||||||
|
|
||||||
|
/** 统一模型没有带电信息,默认按「不带电」= 1。 */ |
||||||
|
@Test |
||||||
|
void defaultsToNotPowered() { |
||||||
|
EdayunCreateProductDtos.CreateProductRequest req = |
||||||
|
EdayunProductMapper.toCreateRequest(base().build()); |
||||||
|
|
||||||
|
assertEquals(Integer.valueOf(1), req.electricFlag); |
||||||
|
assertEquals(Integer.valueOf(0), req.logisticsPackagingType); |
||||||
|
assertEquals(Boolean.FALSE, req.pmFlag); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void mapsSerialAndBatchFlags() { |
||||||
|
EdayunCreateProductDtos.CreateProductRequest req = |
||||||
|
EdayunProductMapper.toCreateRequest(base() |
||||||
|
.snManagementMode(1) |
||||||
|
.lotControlEnabled(true) |
||||||
|
.expirationControl(true) |
||||||
|
.expirationDays(365) |
||||||
|
.build()); |
||||||
|
|
||||||
|
assertEquals(Integer.valueOf(1), req.collectSerialNumber); |
||||||
|
assertEquals(Integer.valueOf(0), req.batchManagementEnabled); |
||||||
|
assertEquals(Integer.valueOf(1), req.batchInfo.expirationEnabled); |
||||||
|
assertEquals(Integer.valueOf(365), req.batchInfo.shelfLifeDays); |
||||||
|
} |
||||||
|
|
||||||
|
/** 申报信息:出口 + 进口各一条,缺申报价值时整组不下发(不编造)。 */ |
||||||
|
@Test |
||||||
|
void declarationsRequireValueAndOrigin() { |
||||||
|
assertNull(EdayunProductMapper.toCreateRequest(base().build()).declarationInfoList); |
||||||
|
|
||||||
|
EdayunCreateProductDtos.CreateProductRequest req = EdayunProductMapper.toCreateRequest( |
||||||
|
base().declaredValue(Money.of("10.00", "USD")) |
||||||
|
.countryOfOrigin("cn") |
||||||
|
.hsCode("8517") |
||||||
|
.build()); |
||||||
|
|
||||||
|
assertEquals(2, req.declarationInfoList.size()); |
||||||
|
assertEquals(Integer.valueOf(0), req.declarationInfoList.get(0).declarationType); // 出口
|
||||||
|
assertEquals("CN", req.declarationInfoList.get(0).country); |
||||||
|
assertEquals(Integer.valueOf(1), req.declarationInfoList.get(1).declarationType); // 进口
|
||||||
|
assertEquals(Double.valueOf(10.0), req.declarationInfoList.get(0).declaredPrice); |
||||||
|
assertEquals("USD", req.declarationInfoList.get(0).currencyType); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void imageGoesToPicList() { |
||||||
|
EdayunCreateProductDtos.CreateProductRequest req = EdayunProductMapper.toCreateRequest( |
||||||
|
base().imageUrl("https://img/1.jpg").build()); |
||||||
|
|
||||||
|
assertEquals(1, req.picList.size()); |
||||||
|
assertEquals("https://img/1.jpg", req.picList.get(0).picUrl); |
||||||
|
assertNull(EdayunProductMapper.toCreateRequest(base().build()).picList); |
||||||
|
} |
||||||
|
|
||||||
|
// ------------------------------------------------------------ 枚举
|
||||||
|
|
||||||
|
@Test |
||||||
|
void yesNoCodesAreInverted() { |
||||||
|
assertEquals(Integer.valueOf(0), EdayunYesNo.codeOf(true)); |
||||||
|
assertEquals(Integer.valueOf(1), EdayunYesNo.codeOf(false)); |
||||||
|
assertNull(EdayunYesNo.codeOf(null)); |
||||||
|
|
||||||
|
assertEquals(Boolean.TRUE, EdayunYesNo.valueOf(0)); |
||||||
|
assertEquals(Boolean.FALSE, EdayunYesNo.valueOf(1)); |
||||||
|
// 未知码不猜
|
||||||
|
assertNull(EdayunYesNo.valueOf(9)); |
||||||
|
} |
||||||
|
|
||||||
|
// ------------------------------------------------------------ 适配器
|
||||||
|
|
||||||
|
@Test |
||||||
|
void declaresProductCapability() { |
||||||
|
EdayunAdapter adapter = new EdayunAdapter(RecordingTransport.create(), |
||||||
|
new common.ecommerce.http.JacksonJsonCodec()); |
||||||
|
assertTrue(adapter.capabilities().contains(WarehouseCapability.PRODUCT)); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,114 @@ |
|||||||
|
package common.ecommerce.edayun.probe; |
||||||
|
|
||||||
|
import common.ecommerce.edayun.EdayunConstants; |
||||||
|
import common.ecommerce.edayun.adapter.EdayunAdapter; |
||||||
|
import common.ecommerce.edayun.dto.EdayunCreateProductDtos; |
||||||
|
import common.ecommerce.edayun.adapter.EdayunProductMapper; |
||||||
|
import common.ecommerce.http.JacksonJsonCodec; |
||||||
|
import common.ecommerce.http.OkHttpTransport; |
||||||
|
import common.ecommerce.shared.error.ChannelException; |
||||||
|
import common.ecommerce.shared.spi.ChannelSecret; |
||||||
|
import common.ecommerce.shared.support.Money; |
||||||
|
import common.ecommerce.warehouse.capability.WarehouseProduct; |
||||||
|
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; |
||||||
|
import java.util.UUID; |
||||||
|
|
||||||
|
/** |
||||||
|
* 创建产品联调探针:<b>只打印将要发出的报文,不真的调用</b>。 |
||||||
|
* |
||||||
|
* <p>创建产品是写操作,会真实建档。默认连报文构造都只做 dry-run; |
||||||
|
* 真跑需要显式加 {@code -Dedayun.probe.write=true}。</p> |
||||||
|
* |
||||||
|
* <pre> |
||||||
|
* # 只看报文(安全) |
||||||
|
* mvn -pl warehouse-sdk-edayun -DfailIfNoTests=false \ |
||||||
|
* -Dedayun.probe.product=true \ |
||||||
|
* -Dtest=EdayunCreateProductProbeTest test |
||||||
|
* |
||||||
|
* # 真发一次(会在测试仓建档,用随机 SKU 避免撞车) |
||||||
|
* ... -Dedayun.probe.product=true -Dedayun.probe.write=true \ |
||||||
|
* -Dedayun.probe.token=... -Dedayun.probe.appKey=... -Dedayun.probe.appSecret=... |
||||||
|
* </pre> |
||||||
|
*/ |
||||||
|
@EnabledIfSystemProperty(named = "edayun.probe.product", matches = "true") |
||||||
|
class EdayunCreateProductProbeTest { |
||||||
|
|
||||||
|
private static final String BASE_URL = opt("edayun.probe.baseUrl", EdayunConstants.PR_BASE_URL); |
||||||
|
private static final String TOKEN = System.getProperty("edayun.probe.token", ""); |
||||||
|
private static final String APP_KEY = System.getProperty("edayun.probe.appKey", ""); |
||||||
|
private static final String APP_SECRET = System.getProperty("edayun.probe.appSecret", ""); |
||||||
|
private static final boolean WRITE = Boolean.getBoolean("edayun.probe.write"); |
||||||
|
|
||||||
|
@Test |
||||||
|
void dumpRequest() { |
||||||
|
String sku = "PROBE-" + Long.toString(System.currentTimeMillis(), 36).toUpperCase(); |
||||||
|
WarehouseProduct product = WarehouseProduct.builder() |
||||||
|
.omsSku(sku) |
||||||
|
.name("探针商品") |
||||||
|
.nameEn("Probe Product") |
||||||
|
.packingType(0) |
||||||
|
.lengthCm(10).widthCm(8).heightCm(6) |
||||||
|
.weightGrams(500) |
||||||
|
.declaredValue(Money.of("10.00", "USD")) |
||||||
|
.countryOfOrigin("CN") |
||||||
|
.hsCode("8517") |
||||||
|
.build(); |
||||||
|
|
||||||
|
EdayunCreateProductDtos.CreateProductRequest req = |
||||||
|
EdayunProductMapper.toCreateRequest(product); |
||||||
|
String bodyJson = new JacksonJsonCodec().toJson(req); |
||||||
|
|
||||||
|
System.out.println("\n================ edayun / POST /webApi/skuInfo/v1/createProduct ================"); |
||||||
|
System.out.println("[request] " + bodyJson); |
||||||
|
if (APP_SECRET != null && !APP_SECRET.isEmpty()) { |
||||||
|
System.out.println("[sign] " + common.ecommerce.edayun.http.EdayunRequestFactory |
||||||
|
.sign(bodyJson, APP_SECRET)); |
||||||
|
} |
||||||
|
|
||||||
|
Assumptions.assumeTrue(WRITE, "dry-run:未加 -Dedayun.probe.write=true,不实际创建"); |
||||||
|
Assumptions.assumeTrue(!TOKEN.trim().isEmpty() && !APP_KEY.trim().isEmpty() |
||||||
|
&& !APP_SECRET.trim().isEmpty(), |
||||||
|
"真跑需要 -Dedayun.probe.token / appKey / appSecret"); |
||||||
|
|
||||||
|
try { |
||||||
|
WarehouseContext ctx = WarehouseContext.of("EDY-PROBE", WarehouseProvider.EDAYUN, null, |
||||||
|
WarehouseConnection.of(BASE_URL, |
||||||
|
ChannelSecret.builder() |
||||||
|
.appKey(APP_KEY).appSecret(APP_SECRET).build()), |
||||||
|
"probe-" + UUID.randomUUID(), Clock.systemUTC()); |
||||||
|
EdayunAdapter adapter = new EdayunAdapter( |
||||||
|
new OkHttpTransport("common-ecommerce/1.0.0 (edayun-probe)"), |
||||||
|
new JacksonJsonCodec()); |
||||||
|
// 用适配器的 public 能力入口,但 token 走系统属性注入的取值
|
||||||
|
common.ecommerce.warehouse.capability.WarehouseProductCapable.ProductResult result = |
||||||
|
adapter.createOrUpdate(ctx, product); |
||||||
|
System.out.println("[OK] providerSku=" + result.providerSku() |
||||||
|
+ " status=" + result.status() + " rawStatus=" + result.rawStatus()); |
||||||
|
} catch (RuntimeException e) { |
||||||
|
String msg = e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage(); |
||||||
|
System.out.println("[ERROR] " + e.getClass().getSimpleName() + ": " |
||||||
|
+ (msg.length() <= 800 ? msg : msg.substring(0, 800) + "…")); |
||||||
|
if (e instanceof ChannelException) { |
||||||
|
ChannelException ce = (ChannelException) e; |
||||||
|
System.out.println(" platformCode=" + ce.platformCode() |
||||||
|
+ " retryable=" + ce.retryable()); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static String opt(String name, String fallback) { |
||||||
|
String value = System.getProperty(name, ""); |
||||||
|
if (value == null) { |
||||||
|
return fallback; |
||||||
|
} |
||||||
|
String trimmed = value.trim(); |
||||||
|
return trimmed.isEmpty() ? fallback : trimmed; |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue