Browse Source
按官方文档落三条此前只能靠猜的契约: - 鉴权头确认是 x-api-key(原来猜的 token) - 响应信封确认是 statusCode/status/code/message,code=200 成功; 空响应体或缺失 code 不再当作成功处理 - 商品路径确认是 /v1/product/save 新增 YqnProductMapper:统一 WarehouseProduct → SaveProductRequest。 customs 按“国家 + HS + 申报价值”整组下发,币种固定 USD; 没给全时整个 customs 不发。WarehouseProduct 在构造期就挡住半组字段。 WarehouseProduct 按 API 115 的必填项重写:packingType / 长宽高 / 重量 / SN 改为必填,单位写进字段名(cm、g),避免适配器之间靠注释约定换算。 出库相关的字段仍待打样核对(Spec §8.6 第 2/5/9 项)。 Co-Authored-By: Claude <noreply@anthropic.com>feature/supplier-lingxing
9 changed files with 738 additions and 72 deletions
@ -0,0 +1,105 @@ |
|||||||
|
package common.ecommerce.yqn.adapter; |
||||||
|
|
||||||
|
import common.ecommerce.shared.support.Money; |
||||||
|
import common.ecommerce.warehouse.capability.WarehouseProduct; |
||||||
|
import common.ecommerce.yqn.dto.YqnDtos; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.List; |
||||||
|
import java.util.Locale; |
||||||
|
|
||||||
|
/** |
||||||
|
* 运去哪商品报文映射(API 115 / {@code /v1/product/save})。 |
||||||
|
* |
||||||
|
* <p>平台字段名只出现在本类与 {@link YqnDtos}。</p> |
||||||
|
*/ |
||||||
|
public final class YqnProductMapper { |
||||||
|
|
||||||
|
/** 运去哪清关信息只接受美元。 */ |
||||||
|
private static final String CUSTOMS_CURRENCY = "USD"; |
||||||
|
|
||||||
|
private YqnProductMapper() { |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 统一商品 → 运去哪保存请求。 |
||||||
|
* |
||||||
|
* <p>运去哪必填的长宽高与重量在 {@link WarehouseProduct} 构造期已校验, |
||||||
|
* 这里只做字段搬运与单位对齐(cm / g 与平台一致,不换算)。</p> |
||||||
|
*/ |
||||||
|
public static YqnDtos.SaveProductRequest toRequest(WarehouseProduct product) { |
||||||
|
YqnDtos.SaveProductRequest req = new YqnDtos.SaveProductRequest(); |
||||||
|
req.sku = skuOf(product); |
||||||
|
req.customItemNo = product.customItemNo(); |
||||||
|
req.upc = product.barcode(); |
||||||
|
req.productType = product.productType() > 0 ? product.productType() : null; |
||||||
|
req.name = product.name(); |
||||||
|
req.nameEn = product.nameEn(); |
||||||
|
req.description = product.description(); |
||||||
|
req.packingType = product.packingType(); |
||||||
|
req.length = product.lengthCm(); |
||||||
|
req.width = product.widthCm(); |
||||||
|
req.height = product.heightCm(); |
||||||
|
req.weight = product.weightGrams(); |
||||||
|
req.itemLength = positiveOrNull(product.itemLengthCm()); |
||||||
|
req.itemWidth = positiveOrNull(product.itemWidthCm()); |
||||||
|
req.itemHeight = positiveOrNull(product.itemHeightCm()); |
||||||
|
req.isRequireSn = product.requireSn(); |
||||||
|
req.packageStyle = product.packageStyle() >= 0 ? product.packageStyle() : null; |
||||||
|
req.returnDealMethod = product.returnDealMethod() >= 0 ? product.returnDealMethod() : null; |
||||||
|
req.customName = product.declaredName(); |
||||||
|
req.customNameEn = product.declaredNameEn(); |
||||||
|
req.link = product.link(); |
||||||
|
req.remark = product.remark(); |
||||||
|
|
||||||
|
Money cost = product.cost(); |
||||||
|
if (cost != null) { |
||||||
|
req.cost = cost.amount().doubleValue(); |
||||||
|
req.currency = cost.currencyCode(); |
||||||
|
} |
||||||
|
req.customs = toCustoms(product.declaredValue(), product.hsCode(), product.countryOfOrigin()); |
||||||
|
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(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 海关信息:国家编码 + HS Code + 进口申报价值/币种 构成一条。 |
||||||
|
* |
||||||
|
* <p>三项没有整组给时返回 null(不发 customs), |
||||||
|
* {@link WarehouseProduct} 已在构造期挡掉「只给一半」的情况。</p> |
||||||
|
*/ |
||||||
|
static List<YqnDtos.SaveProductRequest.Customs> toCustoms(Money declaredValue, |
||||||
|
String hsCode, String countryCode) { |
||||||
|
if (declaredValue == null || isBlank(hsCode) || isBlank(countryCode)) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
YqnDtos.SaveProductRequest.Customs customs = new YqnDtos.SaveProductRequest.Customs(); |
||||||
|
customs.countryCode = countryCode.trim().toUpperCase(Locale.ROOT); |
||||||
|
customs.customCode = hsCode.trim(); |
||||||
|
customs.importDeclaredValue = declaredValue.amount().doubleValue(); |
||||||
|
customs.importDeclaredCurrency = CUSTOMS_CURRENCY; |
||||||
|
customs.exportDeclaredValue = declaredValue.amount().doubleValue(); |
||||||
|
customs.exportDeclaredCurrency = CUSTOMS_CURRENCY; |
||||||
|
|
||||||
|
List<YqnDtos.SaveProductRequest.Customs> out = |
||||||
|
new ArrayList<YqnDtos.SaveProductRequest.Customs>(1); |
||||||
|
out.add(customs); |
||||||
|
return out; |
||||||
|
} |
||||||
|
|
||||||
|
private static Double positiveOrNull(double v) { |
||||||
|
return v > 0 ? v : null; |
||||||
|
} |
||||||
|
|
||||||
|
private static boolean isBlank(String v) { |
||||||
|
return v == null || v.trim().isEmpty(); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,152 @@ |
|||||||
|
package common.ecommerce.yqn.adapter; |
||||||
|
|
||||||
|
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.WarehouseCapability; |
||||||
|
import common.ecommerce.warehouse.WarehouseProvider; |
||||||
|
import common.ecommerce.warehouse.capability.WarehouseProduct; |
||||||
|
import common.ecommerce.warehouse.sdk.WarehouseConnection; |
||||||
|
import common.ecommerce.warehouse.sdk.WarehouseContext; |
||||||
|
import common.ecommerce.yqn.YqnConstants; |
||||||
|
import org.junit.jupiter.api.Test; |
||||||
|
|
||||||
|
import java.time.Clock; |
||||||
|
import java.time.Instant; |
||||||
|
import java.time.ZoneOffset; |
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows; |
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue; |
||||||
|
|
||||||
|
/** |
||||||
|
* 商品保存(API 115)契约测试。 |
||||||
|
*/ |
||||||
|
class YqnProductTest { |
||||||
|
|
||||||
|
private static final Clock CLOCK = Clock.fixed(Instant.parse("2026-11-09T00:00:00Z"), ZoneOffset.UTC); |
||||||
|
|
||||||
|
private static WarehouseContext ctx() { |
||||||
|
return WarehouseContext.of("YQN-RF_NY_US", WarehouseProvider.YQN, "RF_NY_US", |
||||||
|
WarehouseConnection.of("https://pr-hwc-open-api.yqn.com", |
||||||
|
ChannelSecret.builder().appSecret("key-1").build()), |
||||||
|
"req-1", CLOCK); |
||||||
|
} |
||||||
|
|
||||||
|
private static WarehouseProduct product() { |
||||||
|
return WarehouseProduct.builder() |
||||||
|
.omsSku("DM00001") |
||||||
|
.name("测试商品") |
||||||
|
.nameEn("Test Product") |
||||||
|
.packingType(1) |
||||||
|
.lengthCm(30) |
||||||
|
.widthCm(20) |
||||||
|
.heightCm(10) |
||||||
|
.weightGrams(500) |
||||||
|
.requireSn(0) |
||||||
|
.build(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void savesProductWithDocumentedShape() { |
||||||
|
RecordingTransport transport = RecordingTransport.create() |
||||||
|
.route(YqnConstants.PATH_PRODUCT_SAVE, req -> Responses.ok( |
||||||
|
"{\"statusCode\":200,\"status\":0,\"code\":200,\"message\":\"产品创建成功\"}")); |
||||||
|
YqnAdapter adapter = new YqnAdapter(transport, new JacksonJsonCodec()); |
||||||
|
|
||||||
|
common.ecommerce.warehouse.capability.WarehouseProductCapable.ProductResult result = |
||||||
|
adapter.createOrUpdate(ctx(), product()); |
||||||
|
|
||||||
|
assertEquals("DM00001", result.providerSku()); |
||||||
|
assertEquals("SAVED", result.status()); |
||||||
|
assertEquals("产品创建成功", result.rawStatus()); |
||||||
|
|
||||||
|
String body = transport.lastDataRequest().body(); |
||||||
|
assertTrue(body.contains("\"sku\":\"DM00001\""), body); |
||||||
|
assertTrue(body.contains("\"nameEn\":\"Test Product\""), body); |
||||||
|
assertTrue(body.contains("\"packingType\":1"), body); |
||||||
|
assertTrue(body.contains("\"weight\":500.0"), body); |
||||||
|
assertTrue(body.contains("\"isRequireSn\":0"), body); |
||||||
|
// 未给的可选项不应出现
|
||||||
|
assertTrue(!body.contains("customs"), body); |
||||||
|
assertTrue(!body.contains("\"cost\""), body); |
||||||
|
assertEquals("key-1", transport.recorded().get(0).headers().get("x-api-key")); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void providerSkuWinsWhenMapped() { |
||||||
|
RecordingTransport transport = RecordingTransport.create() |
||||||
|
.route(YqnConstants.PATH_PRODUCT_SAVE, req -> Responses.ok( |
||||||
|
"{\"statusCode\":200,\"status\":0,\"code\":200,\"message\":\"ok\"}")); |
||||||
|
YqnAdapter adapter = new YqnAdapter(transport, new JacksonJsonCodec()); |
||||||
|
|
||||||
|
WarehouseProduct mapped = WarehouseProduct.builder() |
||||||
|
.omsSku("OMS-1") |
||||||
|
.providerSku("YQN-1") |
||||||
|
.name("测试商品") |
||||||
|
.nameEn("Test") |
||||||
|
.packingType(0) |
||||||
|
.lengthCm(1).widthCm(1).heightCm(1).weightGrams(1) |
||||||
|
.requireSn(0) |
||||||
|
.build(); |
||||||
|
adapter.createOrUpdate(ctx(), mapped); |
||||||
|
|
||||||
|
assertTrue(transport.lastDataRequest().body().contains("\"sku\":\"YQN-1\"")); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void customsIsSentOnlyWhenAllThreeGiven() { |
||||||
|
WarehouseProduct withCustoms = WarehouseProduct.builder() |
||||||
|
.omsSku("DM00002") |
||||||
|
.name("测试商品") |
||||||
|
.nameEn("Test Product") |
||||||
|
.packingType(0) |
||||||
|
.lengthCm(10).widthCm(10).heightCm(10).weightGrams(100) |
||||||
|
.requireSn(0) |
||||||
|
.countryOfOrigin("us") |
||||||
|
.hsCode("1234567890") |
||||||
|
.declaredValue(Money.of("12.50", "USD")) |
||||||
|
.build(); |
||||||
|
|
||||||
|
RecordingTransport transport = RecordingTransport.create() |
||||||
|
.route(YqnConstants.PATH_PRODUCT_SAVE, req -> Responses.ok( |
||||||
|
"{\"statusCode\":200,\"status\":0,\"code\":200,\"message\":\"ok\"}")); |
||||||
|
new YqnAdapter(transport, new JacksonJsonCodec()).createOrUpdate(ctx(), withCustoms); |
||||||
|
|
||||||
|
String body = transport.lastDataRequest().body(); |
||||||
|
assertTrue(body.contains("\"countryCode\":\"US\""), body); |
||||||
|
assertTrue(body.contains("\"customCode\":\"1234567890\""), body); |
||||||
|
assertTrue(body.contains("\"importDeclaredValue\":12.5"), body); |
||||||
|
assertTrue(body.contains("\"importDeclaredCurrency\":\"USD\""), body); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void halfCustomsIsRejectedAtBuildTime() { |
||||||
|
assertThrows(IllegalArgumentException.class, () -> WarehouseProduct.builder() |
||||||
|
.omsSku("X").name("n").nameEn("n").packingType(0) |
||||||
|
.lengthCm(1).widthCm(1).heightCm(1).weightGrams(1).requireSn(0) |
||||||
|
.hsCode("123") |
||||||
|
.build()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void requiredFieldsAreEnforced() { |
||||||
|
assertThrows(IllegalArgumentException.class, () -> WarehouseProduct.builder() |
||||||
|
.omsSku("X").name("n").nameEn("n") |
||||||
|
// 缺 packingType
|
||||||
|
.lengthCm(1).widthCm(1).heightCm(1).weightGrams(1).requireSn(0) |
||||||
|
.build()); |
||||||
|
assertThrows(IllegalArgumentException.class, () -> WarehouseProduct.builder() |
||||||
|
.omsSku("X").name("n").nameEn("n").packingType(0) |
||||||
|
.lengthCm(0).widthCm(1).heightCm(1).weightGrams(1).requireSn(0) |
||||||
|
.build()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void productCapabilityIsDeclared() { |
||||||
|
YqnAdapter adapter = new YqnAdapter(RecordingTransport.create(), new JacksonJsonCodec()); |
||||||
|
assertTrue(adapter.capabilities().contains(WarehouseCapability.PRODUCT)); |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue