Browse Source
core 新增 ProductQuery(SKU 过滤 + 分页)与 WarehouseProductCapable.queryProducts(default 拒绝,两家覆盖), 门面加 queryProducts。WarehouseProduct 补查询结果才有的字段 (仓侧 id / 仓方实测尺寸 / 创建更新时间 / 状态原值 / 体积), 并加 allowUnknownDimensions 开关:查询结果允许尺寸重量为 0 表示「仓方未给」,建档仍必须为正数——不伪造 0.01 之类的假值。 恒健 /v2/product/queryPage: - 文档把 pageSize / pageNum 的说明写反(pageSize 写「页码」), 按响应里的含义实现:pageSize=每页条数、pageNum=页码;请求侧是 STRING - 响应 data 是分页对象而非数组,按 total 判断是否还有下一页 - 文档漏了 length(只有 width/height/itemLength),映射时优先 length、 缺失退回 itemLength - measurement* 存到仓方实测字段,与申报尺寸分开 信仓 api.productlist: - 字段表按 Apifox「OMS接口文档V2」全量映射(token/state/page/limit 四项必填) - 固定 state=1 查正式产品:临时产品是建档中间态,混进业务列表会让人 以为能下单;建档查重仍走 state=0 - key 是单值:传多个 SKU 时拒绝,不静默只取第一个 - 厘米与英寸两套尺寸都给,直接用厘米那套,避免二次换算舍入 - 数值全是字符串,空串转 null 不转 0;按 rst.count 判断下一页 - PRODUCTION_BASE_URL 确定为 https://oms.xincang-tech.com/oms(去掉末尾斜杠) 验证:mvn test 全量通过(恒健 +11、信仓 +10)。 Co-Authored-By: Claude Code <noreply@anthropic.com>feature/Walmart
16 changed files with 1760 additions and 23 deletions
@ -0,0 +1,100 @@
@@ -0,0 +1,100 @@
|
||||
package common.ecommerce.warehouse.model; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 商品查询条件。 |
||||
* |
||||
* <p>与 {@code WarehouseProductCapable.createOrUpdate} 的区别:那个是写,这个是读。 |
||||
* 各仓支持的维度不同:不支持的维度由适配器<b>显式拒绝</b>,不静默忽略—— |
||||
* 忽略一个过滤条件会让调用方以为「仓里就这些商品」。</p> |
||||
* |
||||
* <p>目前两家仓(恒健 {@code /v2/product/queryPage}、信仓 {@code api.productlist}) |
||||
* 都只支持按 SKU 过滤 + 分页,所以模型只放这两个维度。</p> |
||||
*/ |
||||
public final class ProductQuery { |
||||
|
||||
/** |
||||
* 仓方 SKU 列表;空表示不限。 |
||||
* |
||||
* <p>恒健是 {@code skus} 数组(一次可传多个);信仓是 {@code key} 单值(精确匹配)。 |
||||
* 信仓收到多个 SKU 时由适配器<b>拒绝</b>而不是只取第一个。</p> |
||||
*/ |
||||
private final List<String> providerSkus; |
||||
/** 每页条数;0 表示用仓方默认。 */ |
||||
private final int pageSize; |
||||
/** 页码游标;null 表示第一页。 */ |
||||
private final String cursor; |
||||
|
||||
private ProductQuery(Builder b) { |
||||
this.providerSkus = Collections.unmodifiableList(new ArrayList<String>(b.providerSkus)); |
||||
this.pageSize = b.pageSize; |
||||
this.cursor = b.cursor; |
||||
} |
||||
|
||||
public static Builder builder() { |
||||
return new Builder(); |
||||
} |
||||
|
||||
/** 仓方 SKU 列表;空表示不限。不可变。 */ |
||||
public List<String> providerSkus() { |
||||
return providerSkus; |
||||
} |
||||
|
||||
/** 每页条数;0 表示用仓方默认。 */ |
||||
public int pageSize() { |
||||
return pageSize; |
||||
} |
||||
|
||||
/** 页码游标;null 表示第一页。 */ |
||||
public String cursor() { |
||||
return cursor; |
||||
} |
||||
|
||||
public Builder toBuilder() { |
||||
Builder b = builder().pageSize(pageSize).cursor(cursor); |
||||
b.providerSkus.addAll(providerSkus); |
||||
return b; |
||||
} |
||||
|
||||
public static final class Builder { |
||||
private final List<String> providerSkus = new ArrayList<String>(); |
||||
private int pageSize; |
||||
private String cursor; |
||||
|
||||
public Builder providerSkus(List<String> v) { |
||||
this.providerSkus.clear(); |
||||
if (v != null) { |
||||
for (String sku : v) { |
||||
if (sku != null && !sku.trim().isEmpty()) { |
||||
this.providerSkus.add(sku.trim()); |
||||
} |
||||
} |
||||
} |
||||
return this; |
||||
} |
||||
|
||||
public Builder providerSku(String v) { |
||||
if (v != null && !v.trim().isEmpty()) { |
||||
this.providerSkus.add(v.trim()); |
||||
} |
||||
return this; |
||||
} |
||||
|
||||
public Builder pageSize(int v) { |
||||
this.pageSize = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder cursor(String v) { |
||||
this.cursor = v; |
||||
return this; |
||||
} |
||||
|
||||
public ProductQuery build() { |
||||
return new ProductQuery(this); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,248 @@
@@ -0,0 +1,248 @@
|
||||
package common.ecommerce.hengjian.adapter; |
||||
|
||||
import common.ecommerce.hengjian.dto.HengjianProductQueryDtos; |
||||
import common.ecommerce.shared.support.Money; |
||||
import common.ecommerce.shared.support.Page; |
||||
import common.ecommerce.warehouse.capability.WarehouseProduct; |
||||
import common.ecommerce.warehouse.model.ProductQuery; |
||||
|
||||
import java.math.BigDecimal; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 恒健商品查询映射({@code POST /v2/product/queryPage})。 |
||||
* |
||||
* <h2>分页字段名容易搞反</h2> |
||||
* <p>请求的 {@code pageSize} / {@code pageNum} 文档标 STRING,且<b>字段说明写反了</b> |
||||
* (pageSize 写「页码」)。按响应里的含义实现:{@code pageSize} = 每页条数、 |
||||
* {@code pageNum} = 页码。</p> |
||||
* |
||||
* <h2>长度字段</h2> |
||||
* <p>响应文档漏了 {@code length}(只有 {@code width} / {@code height})。 |
||||
* 映射时优先 {@code length},缺失退回 {@code itemLength},不返回 null。</p> |
||||
* |
||||
* <h2>数值都是字符串</h2> |
||||
* <p>恒健这个接口把 DOUBLE 也按 JSON 数字返回,但 DTO 用 String 接(Jackson 会转)。 |
||||
* 空值 / 非法值转 null,不转 0。</p> |
||||
*/ |
||||
public final class HengjianProductQueryMapper { |
||||
|
||||
/** 每页默认与上限;文档没写,取 200 并拒绝超标(不静默截断)。 */ |
||||
static final int DEFAULT_PAGE_SIZE = 100; |
||||
static final int MAX_PAGE_SIZE = 200; |
||||
|
||||
private HengjianProductQueryMapper() { |
||||
} |
||||
|
||||
/** |
||||
* 统一查询 → 恒健请求。 |
||||
* |
||||
* @throws IllegalArgumentException 页大小超上限 / 游标不是页码 |
||||
*/ |
||||
public static HengjianProductQueryDtos.QueryRequest toRequest(ProductQuery query) { |
||||
HengjianProductQueryDtos.QueryRequest req = new HengjianProductQueryDtos.QueryRequest(); |
||||
// 文档标 STRING,按字符串下发
|
||||
req.pageNum = String.valueOf(pageOf(query.cursor())); |
||||
req.pageSize = String.valueOf(pageSizeOf(query.pageSize())); |
||||
if (!query.providerSkus().isEmpty()) { |
||||
req.skus = new ArrayList<String>(query.providerSkus()); |
||||
} |
||||
return req; |
||||
} |
||||
|
||||
/** 页码:游标是字符串化页码,空游标为第 1 页。 */ |
||||
static int pageOf(String cursor) { |
||||
if (cursor == null || cursor.trim().isEmpty()) { |
||||
return 1; |
||||
} |
||||
try { |
||||
int page = Integer.parseInt(cursor.trim()); |
||||
return page < 1 ? 1 : page; |
||||
} catch (NumberFormatException e) { |
||||
throw new IllegalArgumentException( |
||||
"hengjian product cursor must be a page number, got " + cursor); |
||||
} |
||||
} |
||||
|
||||
/** 每页条数:0 用默认;超上限拒绝不截断。 */ |
||||
static int pageSizeOf(int requested) { |
||||
if (requested <= 0) { |
||||
return DEFAULT_PAGE_SIZE; |
||||
} |
||||
if (requested > MAX_PAGE_SIZE) { |
||||
throw new IllegalArgumentException( |
||||
"hengjian product pageSize must not exceed " + MAX_PAGE_SIZE + ", got " + requested); |
||||
} |
||||
return requested; |
||||
} |
||||
|
||||
// ================================================================ 响应
|
||||
|
||||
/** 响应 → 分页商品。 */ |
||||
public static Page<WarehouseProduct> toPage(HengjianProductQueryDtos.PageData data, |
||||
String requestedCursor, int requestedPageSize) { |
||||
if (data == null || data.data == null || data.data.isEmpty()) { |
||||
return Page.last(new ArrayList<WarehouseProduct>()); |
||||
} |
||||
List<WarehouseProduct> out = new ArrayList<WarehouseProduct>(data.data.size()); |
||||
for (HengjianProductQueryDtos.ProductRow row : data.data) { |
||||
if (row == null) { |
||||
continue; |
||||
} |
||||
WarehouseProduct product = toProduct(row); |
||||
if (product != null) { |
||||
out.add(product); |
||||
} |
||||
} |
||||
return nextPage(out, data.total, requestedCursor, requestedPageSize); |
||||
} |
||||
|
||||
/** 按 {@code total} 判断是否还有下一页。 */ |
||||
static Page<WarehouseProduct> nextPage(List<WarehouseProduct> items, Long totalRaw, |
||||
String requestedCursor, int requestedPageSize) { |
||||
int page = pageOf(requestedCursor); |
||||
if (totalRaw == null) { |
||||
return items.size() < requestedPageSize |
||||
? Page.last(items) : Page.of(items, String.valueOf(page + 1)); |
||||
} |
||||
if ((long) page * requestedPageSize >= totalRaw) { |
||||
return Page.last(items); |
||||
} |
||||
return Page.of(items, String.valueOf(page + 1)); |
||||
} |
||||
|
||||
/** 一条商品 → 统一模型;{@code sku} 缺失时返回 null(跳过脏数据,不整页失败)。 */ |
||||
static WarehouseProduct toProduct(HengjianProductQueryDtos.ProductRow row) { |
||||
String sku = trimToNull(row.sku); |
||||
if (sku == null) { |
||||
return null; |
||||
} |
||||
WarehouseProduct.Builder b = WarehouseProduct.builder() |
||||
// 查询结果允许尺寸 / 重量缺失(用 0 表示),不伪造
|
||||
.allowUnknownDimensions(true) |
||||
.omsSku(sku) |
||||
.providerSku(sku) |
||||
.name(firstNonBlank(row.name, row.nameEn, sku)) |
||||
.nameEn(trimToNull(row.nameEn)) |
||||
.description(trimToNull(row.description)) |
||||
.barcode(trimToNull(row.upc)) |
||||
.remark(trimToNull(row.remark)) |
||||
// 长:优先 length,文档漏了它时退回 itemLength
|
||||
.lengthCm(doubleOrZero(firstNonBlank(row.length, row.itemLength))) |
||||
.widthCm(doubleOrZero(row.width)) |
||||
.heightCm(doubleOrZero(row.height)) |
||||
.weightGrams(doubleOrZero(row.weight)) |
||||
.itemLengthCm(doubleOrZero(row.itemLength)) |
||||
.itemWidthCm(doubleOrZero(row.itemWidth)) |
||||
.itemHeightCm(doubleOrZero(row.itemHeight)) |
||||
// 仓库实测,与申报尺寸分开存
|
||||
.measuredLengthCm(doubleOrNull(row.measurementLength)) |
||||
.measuredWidthCm(doubleOrNull(row.measurementWidth)) |
||||
.measuredHeightCm(doubleOrNull(row.measurementHeight)) |
||||
.measuredWeightGrams(doubleOrNull(row.measurementWeight)) |
||||
// 外箱
|
||||
.packedQuantity(intOrNull(row.packedQty)) |
||||
.packedLengthCm(doubleOrNull(row.packedLength)) |
||||
.packedWidthCm(doubleOrNull(row.packedWidth)) |
||||
.packedHeightCm(doubleOrNull(row.packedHeight)) |
||||
.packedWeightGrams(doubleOrNull(row.packedWeight)) |
||||
// 保质期
|
||||
.expirationControl(oneZeroToBoolean(row.expirationControl)) |
||||
.expirationDays(intOrNull(row.expirationDate)); |
||||
|
||||
Integer productType = intOrNull(row.productType); |
||||
if (productType != null) { |
||||
b.productType(productType); |
||||
} |
||||
// 保价:Y / N
|
||||
String insured = trimToNull(row.isInsuranceFlag); |
||||
if (insured != null) { |
||||
b.insured("Y".equalsIgnoreCase(insured)); |
||||
} |
||||
// 成本:带币种
|
||||
BigDecimal cost = decimalOrNull(row.cost); |
||||
String currency = trimToNull(row.currency); |
||||
if (cost != null && currency != null) { |
||||
b.cost(Money.of(cost, currency)); |
||||
} |
||||
BigDecimal rate = decimalOrNull(row.isInsuranceRate); |
||||
if (rate != null && currency != null) { |
||||
b.insuranceFee(Money.of(rate, currency)); |
||||
} |
||||
return b.build(); |
||||
} |
||||
|
||||
// ================================================================ 工具
|
||||
|
||||
private static Double doubleOrNull(String raw) { |
||||
BigDecimal v = decimalOrNull(raw); |
||||
return v == null ? null : v.doubleValue(); |
||||
} |
||||
|
||||
/** 尺寸 / 重量:仓方没给或非正数时返回 0(查询结果里 0 = 未给)。 */ |
||||
private static double doubleOrZero(String raw) { |
||||
BigDecimal v = decimalOrNull(raw); |
||||
if (v == null || v.signum() <= 0) { |
||||
return 0; |
||||
} |
||||
return v.doubleValue(); |
||||
} |
||||
|
||||
static BigDecimal decimalOrNull(String raw) { |
||||
String v = trimToNull(raw); |
||||
if (v == null) { |
||||
return null; |
||||
} |
||||
try { |
||||
return new BigDecimal(v); |
||||
} catch (NumberFormatException e) { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
static Integer intOrNull(String raw) { |
||||
String v = trimToNull(raw); |
||||
if (v == null) { |
||||
return null; |
||||
} |
||||
try { |
||||
return Integer.valueOf(v.trim()); |
||||
} catch (NumberFormatException e) { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
/** 0 / 1 → false / true;其它值返回 null(不猜)。 */ |
||||
static Boolean oneZeroToBoolean(String raw) { |
||||
Integer v = intOrNull(raw); |
||||
if (v == null) { |
||||
return null; |
||||
} |
||||
if (v == 1) { |
||||
return Boolean.TRUE; |
||||
} |
||||
if (v == 0) { |
||||
return Boolean.FALSE; |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
private static String firstNonBlank(String... values) { |
||||
for (String v : values) { |
||||
String t = trimToNull(v); |
||||
if (t != null) { |
||||
return t; |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
private static String trimToNull(String v) { |
||||
if (v == null) { |
||||
return null; |
||||
} |
||||
String t = v.trim(); |
||||
return t.isEmpty() ? null : t; |
||||
} |
||||
} |
||||
@ -0,0 +1,167 @@
@@ -0,0 +1,167 @@
|
||||
package common.ecommerce.hengjian.dto; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
import com.fasterxml.jackson.annotation.JsonInclude; |
||||
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 恒健报文结构:分页查询 SKU({@code POST /v2/product/queryPage})。 |
||||
* |
||||
* <p>真机确认(2026-11-17):无 token 时直接 {@code 403 Unauthorized}, |
||||
* <b>鉴权先于参数校验</b>,所以必填项按文档实现。</p> |
||||
* |
||||
* <h2>请求</h2> |
||||
* <p>{@code pageSize} / {@code pageNum} 文档标 <b>STRING</b>(其它列表接口是 INTEGER, |
||||
* 这个页不一样);{@code skus} 选填。</p> |
||||
* |
||||
* <p><b>文档的字段说明写反了</b>:{@code pageSize} 写「页码」、{@code pageNum} 写「页数」, |
||||
* 与响应里的 {@code pageSize}(每页条数)/ {@code pageNum}(页码)自相矛盾。 |
||||
* 按<b>响应</b>的含义实现:{@code pageSize} = 每页条数,{@code pageNum} = 页码。</p> |
||||
* |
||||
* <h2>响应</h2> |
||||
* <p>{@code data} 是<b>分页对象</b>(不是数组):{@code total} / {@code pageSize} / |
||||
* {@code pageNum} / {@code data[]}。</p> |
||||
* |
||||
* <p><b>文档漏了 {@code length}</b>:商品主体长宽高里只列了 {@code width} / {@code height} |
||||
* 与 {@code itemLength},但建档接口有 {@code length}。映射时优先取 {@code length}, |
||||
* 缺失时退回 {@code itemLength}——两者语义不同(主体 vs 单品), |
||||
* 这里做兜底是为了不让「查回来没有长」变成 null。</p> |
||||
*/ |
||||
public final class HengjianProductQueryDtos { |
||||
|
||||
private HengjianProductQueryDtos() { |
||||
} |
||||
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL) |
||||
public static final class QueryRequest { |
||||
|
||||
/** 每页条数;文档标 STRING。 */ |
||||
public String pageSize; |
||||
|
||||
/** 页码;文档标 STRING。 */ |
||||
public String pageNum; |
||||
|
||||
/** 要查的 SKU 列表;选填。 */ |
||||
public List<String> skus; |
||||
} |
||||
|
||||
/** 响应外层。 */ |
||||
@JsonIgnoreProperties(ignoreUnknown = true) |
||||
public static final class QueryResponse { |
||||
|
||||
@JsonProperty("code") |
||||
public Integer code; |
||||
|
||||
@JsonProperty("message") |
||||
public String message; |
||||
|
||||
@JsonProperty("data") |
||||
public PageData data; |
||||
} |
||||
|
||||
/** 分页对象。 */ |
||||
@JsonIgnoreProperties(ignoreUnknown = true) |
||||
public static final class PageData { |
||||
|
||||
@JsonProperty("total") |
||||
public Long total; |
||||
|
||||
/** 每页条数。 */ |
||||
@JsonProperty("pageSize") |
||||
public Integer pageSize; |
||||
|
||||
/** 页码。 */ |
||||
@JsonProperty("pageNum") |
||||
public Integer pageNum; |
||||
|
||||
@JsonProperty("data") |
||||
public List<ProductRow> data; |
||||
} |
||||
|
||||
/** 一条商品。 */ |
||||
@JsonIgnoreProperties(ignoreUnknown = true) |
||||
public static final class ProductRow { |
||||
|
||||
@JsonProperty("sku") |
||||
public String sku; |
||||
@JsonProperty("upc") |
||||
public String upc; |
||||
/** 1 成品 / 2 配件 / 3 耗材。 */ |
||||
@JsonProperty("productType") |
||||
public String productType; |
||||
@JsonProperty("name") |
||||
public String name; |
||||
@JsonProperty("nameEn") |
||||
public String nameEn; |
||||
@JsonProperty("description") |
||||
public String description; |
||||
|
||||
/** 产品宽(cm)。 */ |
||||
@JsonProperty("width") |
||||
public String width; |
||||
/** 产品高(cm)。 */ |
||||
@JsonProperty("height") |
||||
public String height; |
||||
/** |
||||
* 产品长(cm)。 |
||||
* |
||||
* <p><b>文档的响应表里没有这一项</b>,但建档接口有;按实际响应为准。</p> |
||||
*/ |
||||
@JsonProperty("length") |
||||
public String length; |
||||
/** 产品重(g)。 */ |
||||
@JsonProperty("weight") |
||||
public String weight; |
||||
|
||||
/** 单品长 / 宽 / 高(cm)。 */ |
||||
@JsonProperty("itemLength") |
||||
public String itemLength; |
||||
@JsonProperty("itemWidth") |
||||
public String itemWidth; |
||||
@JsonProperty("itemHeight") |
||||
public String itemHeight; |
||||
|
||||
/** 保质期管理:0 否 / 1 是。 */ |
||||
@JsonProperty("expirationControl") |
||||
public String expirationControl; |
||||
/** 保质期天数。 */ |
||||
@JsonProperty("expirationDate") |
||||
public String expirationDate; |
||||
|
||||
/** 装箱数量。 */ |
||||
@JsonProperty("packedQty") |
||||
public String packedQty; |
||||
@JsonProperty("packedLength") |
||||
public String packedLength; |
||||
@JsonProperty("packedWidth") |
||||
public String packedWidth; |
||||
@JsonProperty("packedHeight") |
||||
public String packedHeight; |
||||
@JsonProperty("packedWeight") |
||||
public String packedWeight; |
||||
|
||||
/** 仓库实测长 / 宽 / 高(cm)/ 重(g)。 */ |
||||
@JsonProperty("measurementLength") |
||||
public String measurementLength; |
||||
@JsonProperty("measurementWidth") |
||||
public String measurementWidth; |
||||
@JsonProperty("measurementHeight") |
||||
public String measurementHeight; |
||||
@JsonProperty("measurementWeight") |
||||
public String measurementWeight; |
||||
|
||||
@JsonProperty("cost") |
||||
public String cost; |
||||
@JsonProperty("currency") |
||||
public String currency; |
||||
/** 产品是否保价:N 否 / Y 是。 */ |
||||
@JsonProperty("isInsuranceFlag") |
||||
public String isInsuranceFlag; |
||||
@JsonProperty("isInsuranceRate") |
||||
public String isInsuranceRate; |
||||
@JsonProperty("remark") |
||||
public String remark; |
||||
} |
||||
} |
||||
@ -0,0 +1,196 @@
@@ -0,0 +1,196 @@
|
||||
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.Page; |
||||
import common.ecommerce.shared.testkit.RecordingTransport; |
||||
import common.ecommerce.shared.testkit.Responses; |
||||
import common.ecommerce.warehouse.capability.WarehouseProduct; |
||||
import common.ecommerce.warehouse.enums.WarehouseProvider; |
||||
import common.ecommerce.warehouse.model.ProductQuery; |
||||
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.assertNull; |
||||
import static org.junit.jupiter.api.Assertions.assertThrows; |
||||
import static org.junit.jupiter.api.Assertions.assertTrue; |
||||
|
||||
/** |
||||
* 恒健商品查询({@code POST /v2/product/queryPage})契约测试。 |
||||
*/ |
||||
class HengjianProductQueryTest { |
||||
|
||||
private static final WarehouseContext CTX = WarehouseContext.of( |
||||
"HJ-EFCA", WarehouseProvider.HENGJIAN, "EFCA", |
||||
WarehouseConnection.of(HengjianConstants.TEST_BASE_URL, |
||||
ChannelSecret.builder().appSecret("token-1").build()), |
||||
"req-1", Clock.fixed(Instant.parse("2026-11-17T00:00:00Z"), ZoneOffset.UTC)); |
||||
|
||||
private static HengjianAdapter adapter(RecordingTransport transport) { |
||||
return new HengjianAdapter(transport, new JacksonJsonCodec()); |
||||
} |
||||
|
||||
private static String body(String rows, long total) { |
||||
return "{\"code\":200,\"message\":\"success\",\"data\":{" |
||||
+ "\"total\":" + total + ",\"pageSize\":100,\"pageNum\":1," |
||||
+ "\"data\":[" + rows + "]}}"; |
||||
} |
||||
|
||||
private static RecordingTransport listing(String body) { |
||||
return RecordingTransport.create() |
||||
.route(HengjianConstants.PATH_PRODUCT_QUERY_PAGE, req -> Responses.ok(body)); |
||||
} |
||||
|
||||
private static final String ONE_ROW = |
||||
"{\"sku\":\"SKU-A\",\"upc\":\"UPC-A\",\"productType\":1," |
||||
+ "\"name\":\"小部件\",\"nameEn\":\"Widget\",\"description\":\"desc\"," |
||||
+ "\"length\":30,\"width\":20,\"height\":10,\"weight\":1500," |
||||
+ "\"itemLength\":29,\"itemWidth\":19,\"itemHeight\":9," |
||||
+ "\"packedQty\":6,\"packedLength\":60,\"packedWidth\":40,\"packedHeight\":30," |
||||
+ "\"packedWeight\":9000," |
||||
+ "\"measurementLength\":30.5,\"measurementWidth\":20.5," |
||||
+ "\"measurementHeight\":10.5,\"measurementWeight\":1480," |
||||
+ "\"cost\":12.34,\"currency\":\"USD\"," |
||||
+ "\"isInsuranceFlag\":\"Y\",\"isInsuranceRate\":1.5," |
||||
+ "\"expirationControl\":1,\"expirationDate\":365," |
||||
+ "\"remark\":\"备注\"}"; |
||||
|
||||
// ---------------------------------------------------------------- 请求
|
||||
|
||||
/** pageSize / pageNum 文档标 STRING,按字符串下发。 */ |
||||
@Test |
||||
void sendsPagingAsString() { |
||||
RecordingTransport transport = listing(body(ONE_ROW, 1)); |
||||
adapter(transport).queryProducts(CTX, ProductQuery.builder().pageSize(50).build()); |
||||
String json = transport.lastDataRequest().body(); |
||||
|
||||
assertTrue(json.contains("\"pageSize\":\"50\""), json); |
||||
assertTrue(json.contains("\"pageNum\":\"1\""), json); |
||||
} |
||||
|
||||
@Test |
||||
void sendsSkusOnlyWhenGiven() { |
||||
RecordingTransport absent = listing(body("", 0)); |
||||
adapter(absent).queryProducts(CTX, ProductQuery.builder().build()); |
||||
assertFalse(absent.lastDataRequest().body().contains("\"skus\"")); |
||||
|
||||
RecordingTransport present = listing(body("", 0)); |
||||
adapter(present).queryProducts(CTX, ProductQuery.builder() |
||||
.providerSkus(java.util.Arrays.asList("A", "B")).build()); |
||||
String json = present.lastDataRequest().body(); |
||||
assertTrue(json.contains("\"skus\":[\"A\",\"B\"]"), json); |
||||
} |
||||
|
||||
@Test |
||||
void cursorBecomesPageNum() { |
||||
RecordingTransport transport = listing(body(ONE_ROW, 1000)); |
||||
adapter(transport).queryProducts(CTX, ProductQuery.builder().cursor("3").pageSize(10).build()); |
||||
|
||||
assertTrue(transport.lastDataRequest().body().contains("\"pageNum\":\"3\"")); |
||||
} |
||||
|
||||
@Test |
||||
void rejectsPageSizeAboveMax() { |
||||
assertThrows(IllegalArgumentException.class, |
||||
() -> adapter(listing(body("", 0))).queryProducts(CTX, |
||||
ProductQuery.builder().pageSize(500).build())); |
||||
} |
||||
|
||||
// ---------------------------------------------------------------- 响应
|
||||
|
||||
@Test |
||||
void mapsProductFields() { |
||||
Page<WarehouseProduct> page = adapter(listing(body(ONE_ROW, 1))) |
||||
.queryProducts(CTX, ProductQuery.builder().build()); |
||||
|
||||
assertEquals(1, page.size()); |
||||
WarehouseProduct p = page.items().get(0); |
||||
assertEquals("SKU-A", p.omsSku()); |
||||
assertEquals("小部件", p.name()); |
||||
assertEquals("Widget", p.nameEn()); |
||||
assertEquals("UPC-A", p.barcode()); |
||||
assertEquals(30.0, p.lengthCm(), 0.001); |
||||
assertEquals(20.0, p.widthCm(), 0.001); |
||||
assertEquals(10.0, p.heightCm(), 0.001); |
||||
assertEquals(1500.0, p.weightGrams(), 0.001); |
||||
assertEquals(1, p.productType()); |
||||
assertEquals(Integer.valueOf(6), p.packedQuantity()); |
||||
assertEquals(60.0, p.packedLengthCm(), 0.001); |
||||
assertEquals(9000.0, p.packedWeightGrams(), 0.001); |
||||
assertEquals(Boolean.TRUE, p.expirationControl()); |
||||
assertEquals(Integer.valueOf(365), p.expirationDays()); |
||||
assertEquals(Boolean.TRUE, p.insured()); |
||||
assertEquals("备注", p.remark()); |
||||
} |
||||
|
||||
/** 仓库实测尺寸与申报尺寸分开存。 */ |
||||
@Test |
||||
void keepsMeasuredDimensionsSeparate() { |
||||
WarehouseProduct p = adapter(listing(body(ONE_ROW, 1))) |
||||
.queryProducts(CTX, ProductQuery.builder().build()).items().get(0); |
||||
|
||||
assertEquals(30.0, p.lengthCm(), 0.001); |
||||
assertEquals(30.5, p.measuredLengthCm(), 0.001); |
||||
assertEquals(1480.0, p.measuredWeightGrams(), 0.001); |
||||
} |
||||
|
||||
/** 文档响应表漏了 length:缺失时退回 itemLength,不返回 null。 */ |
||||
@Test |
||||
void fallsBackToItemLengthWhenLengthMissing() { |
||||
String row = "{\"sku\":\"SKU-B\",\"width\":20,\"height\":10,\"weight\":100," |
||||
+ "\"itemLength\":29,\"itemWidth\":19,\"itemHeight\":9}"; |
||||
WarehouseProduct p = adapter(listing(body(row, 1))) |
||||
.queryProducts(CTX, ProductQuery.builder().build()).items().get(0); |
||||
|
||||
assertEquals(29.0, p.lengthCm(), 0.001, "没有 length 时用 itemLength"); |
||||
} |
||||
|
||||
/** total 决定是否还有下一页。 */ |
||||
@Test |
||||
void pagingUsesTotal() { |
||||
Page<WarehouseProduct> more = adapter(listing(body(ONE_ROW, 500))) |
||||
.queryProducts(CTX, ProductQuery.builder().pageSize(100).build()); |
||||
assertTrue(more.hasMore()); |
||||
assertEquals("2", more.nextCursor()); |
||||
|
||||
Page<WarehouseProduct> done = adapter(listing(body(ONE_ROW, 1))) |
||||
.queryProducts(CTX, ProductQuery.builder().pageSize(100).build()); |
||||
assertFalse(done.hasMore()); |
||||
assertNull(done.nextCursor()); |
||||
} |
||||
|
||||
@Test |
||||
void emptyPageIsLast() { |
||||
Page<WarehouseProduct> page = adapter(listing(body("", 0))) |
||||
.queryProducts(CTX, ProductQuery.builder().build()); |
||||
assertTrue(page.items().isEmpty()); |
||||
assertFalse(page.hasMore()); |
||||
} |
||||
|
||||
/** 缺 sku 的脏数据跳过。 */ |
||||
@Test |
||||
void skipsRowsWithoutSku() { |
||||
String row = "{\"name\":\"无SKU\"}"; |
||||
assertTrue(adapter(listing(body(row, 1))) |
||||
.queryProducts(CTX, ProductQuery.builder().build()).items().isEmpty()); |
||||
} |
||||
|
||||
/** 尺寸缺失用 0 表示「仓方未给」。 */ |
||||
@Test |
||||
void missingDimensionsBecomeZero() { |
||||
String row = "{\"sku\":\"SKU-C\"}"; |
||||
WarehouseProduct p = adapter(listing(body(row, 1))) |
||||
.queryProducts(CTX, ProductQuery.builder().build()).items().get(0); |
||||
|
||||
assertEquals(0.0, p.lengthCm(), 0.001); |
||||
assertFalse(p.hasDimensions()); |
||||
} |
||||
} |
||||
@ -0,0 +1,262 @@
@@ -0,0 +1,262 @@
|
||||
package common.ecommerce.xincang.adapter; |
||||
|
||||
import common.ecommerce.shared.support.Money; |
||||
import common.ecommerce.shared.support.Page; |
||||
import common.ecommerce.warehouse.capability.WarehouseProduct; |
||||
import common.ecommerce.warehouse.model.ProductQuery; |
||||
import common.ecommerce.xincang.XincangConstants; |
||||
import common.ecommerce.xincang.dto.XincangProductListDtos; |
||||
|
||||
import java.math.BigDecimal; |
||||
import java.math.RoundingMode; |
||||
import java.util.ArrayList; |
||||
import java.util.LinkedHashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* 信仓商品查询映射({@code GET api.productlist})。 |
||||
* |
||||
* <p>字段表来自 Apifox「OMS接口文档V2」→ 商品 → 获取商品列表(2026-11 核对)。</p> |
||||
* |
||||
* <h2>请求</h2> |
||||
* <p>{@code token} / {@code state} / {@code page} / {@code limit} 四项<b>必填</b>。 |
||||
* 适配器的 {@code state} 固定传 <b>1(正式产品)</b>——商品查询是给业务用的, |
||||
* 临时产品是建档过程的中间态,混进列表会让调用方以为能下单。</p> |
||||
* |
||||
* <h2>单位</h2> |
||||
* <p>响应同时给厘米与英寸两套尺寸、千克与磅两套重量。统一模型是 cm / g, |
||||
* 所以<b>直接用厘米那套</b>、重量用 {@code weight_kg × 1000}, |
||||
* 不做「英寸换算回来」的二次舍入。</p> |
||||
* |
||||
* <h2>数值全是字符串</h2> |
||||
* <p>信仓全站这个风格。空串转 null,不转 0——「仓方没给」和「就是 0」意义不同。</p> |
||||
*/ |
||||
public final class XincangProductQueryMapper { |
||||
|
||||
/** 查询固定查正式产品:临时产品是建档中间态,业务不该看到。 */ |
||||
static final int STATE_FORMAL = 1; |
||||
|
||||
/** 分页默认与上限(文档:limit 默认 20、最大 100)。 */ |
||||
static final int DEFAULT_LIMIT = 20; |
||||
static final int MAX_LIMIT = 100; |
||||
|
||||
private XincangProductQueryMapper() { |
||||
} |
||||
|
||||
/** |
||||
* 统一查询 → query 参数。 |
||||
* |
||||
* @throws IllegalArgumentException 传了多个 SKU(信仓 {@code key} 只收单值) |
||||
*/ |
||||
public static Map<String, Object> toParams(ProductQuery query) { |
||||
int page = pageOf(query.cursor()); |
||||
if (query.providerSkus().size() > 1) { |
||||
// 静默只取第一个会让调用方以为其余 SKU 也查了——而且它们会缺席列表,
|
||||
// 看起来像「仓里没有这个商品」
|
||||
throw new IllegalArgumentException( |
||||
"xincang product list accepts a single sku (key), got " |
||||
+ query.providerSkus().size() + "; query them one by one"); |
||||
} |
||||
Map<String, Object> params = new LinkedHashMap<String, Object>(); |
||||
params.put("state", STATE_FORMAL); |
||||
params.put("page", page); |
||||
params.put("limit", limitOf(query.pageSize())); |
||||
if (!query.providerSkus().isEmpty()) { |
||||
params.put("key", query.providerSkus().get(0)); |
||||
} |
||||
return params; |
||||
} |
||||
|
||||
/** 页码:游标是字符串化页码,空游标为第 1 页。 */ |
||||
static int pageOf(String cursor) { |
||||
if (cursor == null || cursor.trim().isEmpty()) { |
||||
return 1; |
||||
} |
||||
try { |
||||
int page = Integer.parseInt(cursor.trim()); |
||||
return page < 1 ? 1 : page; |
||||
} catch (NumberFormatException e) { |
||||
throw new IllegalArgumentException( |
||||
"xincang product list cursor must be a page number, got " + cursor); |
||||
} |
||||
} |
||||
|
||||
/** 每页条数:0 用仓方默认;超上限拒绝不截断。 */ |
||||
static int limitOf(int requested) { |
||||
if (requested <= 0) { |
||||
return DEFAULT_LIMIT; |
||||
} |
||||
if (requested > MAX_LIMIT) { |
||||
throw new IllegalArgumentException( |
||||
"xincang product list limit must not exceed " + MAX_LIMIT + ", got " + requested); |
||||
} |
||||
return requested; |
||||
} |
||||
|
||||
// ================================================================ 响应
|
||||
|
||||
/** 响应 → 分页商品。 */ |
||||
public static Page<WarehouseProduct> toPage(XincangProductListDtos.ProductListResult rst, |
||||
String requestedCursor, int requestedLimit) { |
||||
if (rst == null || rst.data == null || rst.data.isEmpty()) { |
||||
return Page.last(new ArrayList<WarehouseProduct>()); |
||||
} |
||||
List<WarehouseProduct> out = new ArrayList<WarehouseProduct>(rst.data.size()); |
||||
for (XincangProductListDtos.ProductItem item : rst.data) { |
||||
if (item == null) { |
||||
continue; |
||||
} |
||||
WarehouseProduct product = toProduct(item); |
||||
if (product != null) { |
||||
out.add(product); |
||||
} |
||||
} |
||||
return nextPage(out, rst.count, requestedCursor, requestedLimit); |
||||
} |
||||
|
||||
/** |
||||
* 是否还有下一页。 |
||||
* |
||||
* <p>按 {@code rst.count}(总数)判断,不按「本页是否满」——服务端可能对 |
||||
* {@code limit} 做上限收窄,满页不代表还有。</p> |
||||
*/ |
||||
static Page<WarehouseProduct> nextPage(List<WarehouseProduct> items, String totalRaw, |
||||
String requestedCursor, int requestedLimit) { |
||||
int page = pageOf(requestedCursor); |
||||
Integer total = intOrNull(totalRaw); |
||||
if (total == null) { |
||||
// 仓方没给总数就按「本页是否满」保守判断
|
||||
return items.size() < requestedLimit |
||||
? Page.last(items) : Page.of(items, String.valueOf(page + 1)); |
||||
} |
||||
if ((long) page * requestedLimit >= total) { |
||||
return Page.last(items); |
||||
} |
||||
return Page.of(items, String.valueOf(page + 1)); |
||||
} |
||||
|
||||
/** |
||||
* 一条商品 → 统一模型。 |
||||
* |
||||
* <p>{@code product_no} 缺失时返回 null(无法定位的商品,跳过而不是抛—— |
||||
* 列表里一条脏数据不该让整页失败)。</p> |
||||
*/ |
||||
static WarehouseProduct toProduct(XincangProductListDtos.ProductItem item) { |
||||
String sku = trimToNull(item.productNo); |
||||
if (sku == null) { |
||||
return null; |
||||
} |
||||
WarehouseProduct.Builder b = WarehouseProduct.builder() |
||||
// 查询结果允许尺寸 / 重量为 0(仓方没给),不伪造最小值
|
||||
.allowUnknownDimensions(true) |
||||
.omsSku(sku) |
||||
.providerSku(sku) |
||||
.name(firstNonBlank(item.chnName, item.enName, sku)) |
||||
.nameEn(trimToNull(item.enName)) |
||||
.barcode(trimToNull(item.barCode)) |
||||
// 尺寸直接用厘米那套,不从英寸反算
|
||||
.lengthCm(doubleOrZero(item.sizeLCm)) |
||||
.widthCm(doubleOrZero(item.sizeWCm)) |
||||
.heightCm(doubleOrZero(item.sizeHCm)) |
||||
// weight_kg → g
|
||||
.weightGrams(gramsOf(item)) |
||||
// 查询结果才有
|
||||
.providerProductId(trimToNull(item.id)) |
||||
.rawState(trimToNull(item.state)) |
||||
.stateName(trimToNull(item.stateName)) |
||||
.volumeCm3(decimalOrNull(item.volumeCm)) |
||||
.createdAt(XincangOrderDetailMapper.parseTime(item.gmtCreate)) |
||||
.updatedAt(XincangOrderDetailMapper.parseTime(item.gmtModify)) |
||||
.imageUrl(trimToNull(item.imageUrl)) |
||||
.remark(null); |
||||
|
||||
// packing_rate → 装箱数量
|
||||
Integer packing = intOrNull(item.packingRate); |
||||
if (packing != null) { |
||||
b.packedQuantity(packing); |
||||
} |
||||
// amount → 申报价值(信仓固定美元)
|
||||
BigDecimal amount = decimalOrNull(item.amount); |
||||
if (amount != null) { |
||||
b.declaredValue(Money.of(amount, "USD")); |
||||
} |
||||
// 统一模型没有「长度/重量单位」以外的差异;外箱尺寸信仓不提供,保持 null
|
||||
return b.build(); |
||||
} |
||||
|
||||
/** |
||||
* 重量换算成克。 |
||||
* |
||||
* <p>优先 {@code weight_kg × 1000};没有千克字段时退回 {@code weight_lbs × 453.59237}。</p> |
||||
*/ |
||||
static double gramsOf(XincangProductListDtos.ProductItem item) { |
||||
BigDecimal kg = decimalOrNull(item.weightKg); |
||||
if (kg != null) { |
||||
return kg.multiply(BigDecimal.valueOf(1000)).doubleValue(); |
||||
} |
||||
BigDecimal lbs = decimalOrNull(item.weightLbs); |
||||
if (lbs != null) { |
||||
return lbs.multiply(BigDecimal.valueOf(XincangConstants.GRAMS_PER_POUND)).doubleValue(); |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
// ================================================================ 工具
|
||||
|
||||
/** |
||||
* 尺寸 / 重量:仓方没给或给了非正数时返回 0。 |
||||
* |
||||
* <p>0 在查询结果的模型里表示「仓方未给」(见 |
||||
* {@code WarehouseProduct.hasDimensions()}),不伪造一个假的最小值。</p> |
||||
*/ |
||||
private static double doubleOrZero(String raw) { |
||||
BigDecimal v = decimalOrNull(raw); |
||||
if (v == null || v.signum() <= 0) { |
||||
return 0; |
||||
} |
||||
return v.doubleValue(); |
||||
} |
||||
|
||||
static BigDecimal decimalOrNull(String raw) { |
||||
String v = trimToNull(raw); |
||||
if (v == null) { |
||||
return null; |
||||
} |
||||
try { |
||||
return new BigDecimal(v).setScale(4, RoundingMode.HALF_UP); |
||||
} catch (NumberFormatException e) { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
static Integer intOrNull(String raw) { |
||||
String v = trimToNull(raw); |
||||
if (v == null) { |
||||
return null; |
||||
} |
||||
try { |
||||
return Integer.valueOf(v); |
||||
} catch (NumberFormatException e) { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
private static String firstNonBlank(String... values) { |
||||
for (String v : values) { |
||||
String t = trimToNull(v); |
||||
if (t != null) { |
||||
return t; |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
private static String trimToNull(String v) { |
||||
if (v == null) { |
||||
return null; |
||||
} |
||||
String t = v.trim(); |
||||
return t.isEmpty() ? null : t; |
||||
} |
||||
} |
||||
@ -0,0 +1,215 @@
@@ -0,0 +1,215 @@
|
||||
package common.ecommerce.xincang.adapter; |
||||
|
||||
import common.ecommerce.http.HttpRequest; |
||||
import common.ecommerce.http.JacksonJsonCodec; |
||||
import common.ecommerce.shared.spi.ChannelSecret; |
||||
import common.ecommerce.shared.support.Page; |
||||
import common.ecommerce.shared.testkit.RecordingTransport; |
||||
import common.ecommerce.shared.testkit.Responses; |
||||
import common.ecommerce.warehouse.capability.WarehouseProduct; |
||||
import common.ecommerce.warehouse.model.ProductQuery; |
||||
import common.ecommerce.warehouse.sdk.WarehouseConnection; |
||||
import common.ecommerce.warehouse.sdk.WarehouseContext; |
||||
import common.ecommerce.xincang.XincangConstants; |
||||
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.assertNull; |
||||
import static org.junit.jupiter.api.Assertions.assertThrows; |
||||
import static org.junit.jupiter.api.Assertions.assertTrue; |
||||
|
||||
/** |
||||
* 信仓商品查询({@code GET api.productlist})契约测试。 |
||||
* |
||||
* <p>字段表来自 Apifox「OMS接口文档V2」→ 商品 → 获取商品列表。</p> |
||||
*/ |
||||
class XincangProductQueryTest { |
||||
|
||||
private static final Clock CLOCK = Clock.fixed(Instant.parse("2026-11-17T00:00:00Z"), ZoneOffset.UTC); |
||||
|
||||
private static WarehouseContext ctx() { |
||||
return WarehouseContext.of("XC-1", common.ecommerce.warehouse.enums.WarehouseProvider.XINCANG, |
||||
"LAX", |
||||
WarehouseConnection.of(XincangConstants.TEST_BASE_URL, |
||||
ChannelSecret.builder().appKey("k").appSecret("s").build()), |
||||
"req-1", CLOCK); |
||||
} |
||||
|
||||
private static XincangAdapter adapter(RecordingTransport transport) { |
||||
return new XincangAdapter(transport, new JacksonJsonCodec(), |
||||
new common.ecommerce.shared.testkit.InMemoryTokenStore(), |
||||
new common.ecommerce.shared.testkit.InMemoryLockProvider(), |
||||
Clock.fixed(Instant.parse("2026-11-17T00:00:00Z"), ZoneOffset.UTC)); |
||||
} |
||||
|
||||
/** 产品列表成功响应(登录也走同一 transport,这里单独 route 登录)。 */ |
||||
private static RecordingTransport productListing(String body) { |
||||
return RecordingTransport.create() |
||||
.route(XincangConstants.PATH_LOGIN, req -> Responses.ok( |
||||
"{\"rst\":{\"id\":\"c-1\",\"token\":\"tk-1\",\"token_expire_time\":\"" |
||||
+ java.time.LocalDateTime.ofInstant(Instant.parse("2026-11-17T02:00:00Z"), |
||||
XincangConstants.SYSTEM_ZONE) |
||||
.format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) |
||||
+ "\"},\"errno\":0,\"err\":\"\"}")) |
||||
.route(XincangConstants.PATH_PRODUCT_LIST, req -> Responses.ok(body)); |
||||
} |
||||
|
||||
/** 取最近一次 productlist 请求的 URL。 */ |
||||
private static String productListUrl(RecordingTransport transport) { |
||||
for (HttpRequest req : transport.recorded()) { |
||||
if (req.url().contains(XincangConstants.PATH_PRODUCT_LIST)) { |
||||
return req.url(); |
||||
} |
||||
} |
||||
throw new AssertionError("no productlist request recorded"); |
||||
} |
||||
|
||||
private static String listBody(String rowsJson, String count) { |
||||
return "{\"rst\":{\"code\":\"0\",\"msg\":\"\",\"data\":[" + rowsJson + "]," |
||||
+ "\"count\":\"" + count + "\"},\"errno\":0,\"err\":\"\"}"; |
||||
} |
||||
|
||||
private static final String ONE_ROW = |
||||
"{\"id\":\"9001\",\"product_no\":\"SKU-A\",\"bar_code\":\"BC-A\"," |
||||
+ "\"en_name\":\"Widget\",\"chn_name\":\"小部件\"," |
||||
+ "\"customer_id\":\"7\",\"customer_name\":\"C007\"," |
||||
+ "\"amount\":\"12.34\",\"packing_rate\":\"6\"," |
||||
+ "\"weight_kg\":\"1.5\",\"weight_lbs\":\"3.3069\"," |
||||
+ "\"size_l_cm\":\"30\",\"size_w_cm\":\"20\",\"size_h_cm\":\"10\"," |
||||
+ "\"size_l_ln\":\"11.81\",\"size_w_ln\":\"7.87\",\"size_h_ln\":\"3.94\"," |
||||
+ "\"volume_cm\":\"6000\",\"volume_ln\":\"366.1\",\"volume_m\":\"0.006\"," |
||||
+ "\"gmt_create\":\"2026-11-01 10:00:00\"," |
||||
+ "\"gmt_modify\":\"2026-11-02 11:30:00\"," |
||||
+ "\"state\":\"1\",\"state_name\":\"正式产品\"," |
||||
+ "\"image_url\":\"https://oss/x.png\"," |
||||
+ "\"ckm\":\"LAX\"}"; |
||||
|
||||
// ---------------------------------------------------------------- 请求
|
||||
|
||||
@Test |
||||
void sendsMandatoryQueryParams() { |
||||
RecordingTransport transport = productListing(listBody(ONE_ROW, "1")); |
||||
adapter(transport).queryProducts(ctx(), ProductQuery.builder().build()); |
||||
|
||||
String url = productListUrl(transport); |
||||
// 四项必填
|
||||
assertTrue(url.contains("state=1"), url); |
||||
assertTrue(url.contains("page=1"), url); |
||||
assertTrue(url.contains("limit="), url); |
||||
assertTrue(url.contains("token="), url); |
||||
} |
||||
|
||||
/** 固定查正式产品:临时产品是建档中间态,不该混进业务列表。 */ |
||||
@Test |
||||
void alwaysQueriesFormalState() { |
||||
RecordingTransport transport = productListing(listBody("", "0")); |
||||
adapter(transport).queryProducts(ctx(), ProductQuery.builder().build()); |
||||
|
||||
assertTrue(productListUrl(transport).contains("state=1")); |
||||
} |
||||
|
||||
/** key 是单值:传多个 SKU 时拒绝,不静默只取第一个。 */ |
||||
@Test |
||||
void rejectsMultipleSkus() { |
||||
RecordingTransport transport = productListing(listBody("", "0")); |
||||
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, |
||||
() -> adapter(transport).queryProducts(ctx(), ProductQuery.builder() |
||||
.providerSkus(java.util.Arrays.asList("A", "B")).build())); |
||||
assertTrue(ex.getMessage().contains("single sku"), ex.getMessage()); |
||||
} |
||||
|
||||
@Test |
||||
void sendsSingleSkuAsKey() { |
||||
RecordingTransport transport = productListing(listBody(ONE_ROW, "1")); |
||||
adapter(transport).queryProducts(ctx(), ProductQuery.builder().providerSku("SKU-A").build()); |
||||
|
||||
assertTrue(productListUrl(transport).contains("key=SKU-A")); |
||||
} |
||||
|
||||
@Test |
||||
void rejectsLimitAboveMax() { |
||||
RecordingTransport transport = productListing(listBody("", "0")); |
||||
assertThrows(IllegalArgumentException.class, |
||||
() -> adapter(transport).queryProducts(ctx(), ProductQuery.builder().pageSize(500).build())); |
||||
} |
||||
|
||||
// ---------------------------------------------------------------- 响应
|
||||
|
||||
@Test |
||||
void mapsProductFields() { |
||||
Page<WarehouseProduct> page = adapter(productListing(listBody(ONE_ROW, "1"))) |
||||
.queryProducts(ctx(), ProductQuery.builder().build()); |
||||
|
||||
assertEquals(1, page.size()); |
||||
WarehouseProduct p = page.items().get(0); |
||||
assertEquals("SKU-A", p.omsSku()); |
||||
assertEquals("SKU-A", p.providerSku()); |
||||
assertEquals("小部件", p.name()); |
||||
assertEquals("Widget", p.nameEn()); |
||||
assertEquals("BC-A", p.barcode()); |
||||
// 尺寸直接用厘米那套,不从英寸反算
|
||||
assertEquals(30.0, p.lengthCm(), 0.001); |
||||
assertEquals(20.0, p.widthCm(), 0.001); |
||||
assertEquals(10.0, p.heightCm(), 0.001); |
||||
// weight_kg × 1000
|
||||
assertEquals(1500.0, p.weightGrams(), 0.001); |
||||
assertEquals(Integer.valueOf(6), p.packedQuantity()); |
||||
assertEquals("USD", p.declaredValue().currencyCode()); |
||||
assertEquals(0, p.declaredValue().amount().compareTo(new java.math.BigDecimal("12.34"))); |
||||
assertEquals("9001", p.providerProductId()); |
||||
assertEquals("1", p.rawState()); |
||||
assertEquals("正式产品", p.stateName()); |
||||
assertEquals("https://oss/x.png", p.imageUrl()); |
||||
// 北京时间 → UTC:10:00 北京 = 02:00 UTC
|
||||
assertEquals(Instant.parse("2026-11-01T02:00:00Z"), p.createdAt()); |
||||
} |
||||
|
||||
/** count 决定是否还有下一页,不看本页是否满。 */ |
||||
@Test |
||||
void pagingUsesCount() { |
||||
Page<WarehouseProduct> more = adapter(productListing(listBody(ONE_ROW, "50"))) |
||||
.queryProducts(ctx(), ProductQuery.builder().pageSize(20).build()); |
||||
assertTrue(more.hasMore()); |
||||
assertEquals("2", more.nextCursor()); |
||||
|
||||
Page<WarehouseProduct> done = adapter(productListing(listBody(ONE_ROW, "1"))) |
||||
.queryProducts(ctx(), ProductQuery.builder().pageSize(20).build()); |
||||
assertFalse(done.hasMore()); |
||||
assertNull(done.nextCursor()); |
||||
} |
||||
|
||||
@Test |
||||
void emptyDataIsLastPage() { |
||||
Page<WarehouseProduct> page = adapter(productListing(listBody("", "0"))) |
||||
.queryProducts(ctx(), ProductQuery.builder().build()); |
||||
assertTrue(page.items().isEmpty()); |
||||
assertFalse(page.hasMore()); |
||||
} |
||||
|
||||
/** 没有尺寸的商品用 0 表示「仓方未给」,不伪造。 */ |
||||
@Test |
||||
void missingDimensionsBecomeZero() { |
||||
String row = "{\"product_no\":\"SKU-B\",\"state\":\"1\",\"weight_kg\":\"\"}"; |
||||
WarehouseProduct p = adapter(productListing(listBody(row, "1"))) |
||||
.queryProducts(ctx(), ProductQuery.builder().build()).items().get(0); |
||||
|
||||
assertEquals(0.0, p.lengthCm(), 0.001); |
||||
assertEquals(0.0, p.weightGrams(), 0.001); |
||||
assertFalse(p.hasDimensions()); |
||||
} |
||||
|
||||
/** 缺 product_no 的脏数据跳过,不让整页失败。 */ |
||||
@Test |
||||
void skipsRowsWithoutSku() { |
||||
String row = "{\"id\":\"1\",\"chn_name\":\"无 SKU\"}"; |
||||
Page<WarehouseProduct> page = adapter(productListing(listBody(row, "1"))) |
||||
.queryProducts(ctx(), ProductQuery.builder().build()); |
||||
|
||||
assertTrue(page.items().isEmpty()); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue