Browse Source
ChannelSdk: - 类注释改成「构建需要三样 + 每次调用需要两样」+ 调用链图 - 每个字段、每个 Builder 方法、每个门面方法补注释(含异常说明) - 明确能力检查发生在 pull() 调用时,不是取视图时 - 明确 clock 的作用(查询终点 + 幂等时间戳)与生产是否要传 ChannelConnection / ChannelSecret: - endpointBaseUrl / appKey / appSecret / clientId / clientSecret / refreshToken 逐个注明是哪个平台用 新增 ProductCallExampleTest(拉商品): - 三段配置分别是什么、拼出的请求长什么样、结果如何映射 - 传输层返回一页固定响应,不联网 Co-Authored-By: Claude <noreply@anthropic.com>feature/supplier-lingxing
4 changed files with 331 additions and 21 deletions
@ -0,0 +1,182 @@ |
|||||||
|
package common.ecommerce.lingxing; |
||||||
|
|
||||||
|
import common.ecommerce.http.HttpResponse; |
||||||
|
import common.ecommerce.http.HttpTransport; |
||||||
|
import common.ecommerce.lingxing.adapter.LingxingAdapter; |
||||||
|
import common.ecommerce.model.ChannelProvider; |
||||||
|
import common.ecommerce.model.Platform; |
||||||
|
import common.ecommerce.model.account.AccountKey; |
||||||
|
import common.ecommerce.model.account.ChannelAccount; |
||||||
|
import common.ecommerce.model.product.ProductListing; |
||||||
|
import common.ecommerce.model.product.ProductQuery; |
||||||
|
import common.ecommerce.model.support.Page; |
||||||
|
import common.ecommerce.sdk.ChannelConnection; |
||||||
|
import common.ecommerce.sdk.ChannelSdk; |
||||||
|
import common.ecommerce.spi.ChannelSecret; |
||||||
|
import common.ecommerce.testkit.InMemoryAccounts; |
||||||
|
import common.ecommerce.testkit.InMemoryLockProvider; |
||||||
|
import common.ecommerce.testkit.InMemoryTokenStore; |
||||||
|
import org.junit.jupiter.api.Test; |
||||||
|
|
||||||
|
import java.time.Clock; |
||||||
|
import java.time.Instant; |
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull; |
||||||
|
|
||||||
|
/** |
||||||
|
* 从 ChannelSdk 拉商品的完整示例。 |
||||||
|
* |
||||||
|
* <p>这是<b>用法文档</b>,不是契约测试。传输层返回一页固定的领星 Listing 响应, |
||||||
|
* 目的是让你看清:三段配置分别是什么、拼出来的请求长什么样、结果如何映射成统一模型。</p> |
||||||
|
*/ |
||||||
|
class ProductCallExampleTest { |
||||||
|
|
||||||
|
/** 领星生产域名。SDK 不保存它,每次调用通过 ChannelConnection 传入。 */ |
||||||
|
private static final String ENDPOINT = "https://openapi.lingxing.com"; |
||||||
|
|
||||||
|
// ==================== 第 1 段:账户 ====================
|
||||||
|
|
||||||
|
/** 你自己的店铺 ID,业务归属维度。 */ |
||||||
|
private static final String OMS_STORE_ID = "oms-wm-001"; |
||||||
|
|
||||||
|
/** 领星侧的店铺 ID(store_id)。 */ |
||||||
|
private static final String LINGXING_STORE_ID = "1108413237731"; |
||||||
|
|
||||||
|
/** 领星平台码:10008 = Walmart,10001 = Amazon SC。 */ |
||||||
|
private static final int PLATFORM_CODE = 10008; |
||||||
|
|
||||||
|
/** |
||||||
|
* 领星 AppID。同时作为签名用的 AES 密钥,长度必须是 16/24/32 字节, |
||||||
|
* 否则 LingxingSigner 会直接拒绝。 |
||||||
|
*/ |
||||||
|
private static final String APP_KEY = "test-app-id-1234"; |
||||||
|
|
||||||
|
/** 领星 AppSecret。 */ |
||||||
|
private static final String APP_SECRET = "test-app-secret"; |
||||||
|
|
||||||
|
/** 账户:告诉 SDK「这是哪个店铺」,SDK 用它校验归属、拼请求参数。 */ |
||||||
|
private static ChannelAccount account() { |
||||||
|
return ChannelAccount.builder() |
||||||
|
// provider + platform + omsStoreId:三个一起决定用哪个适配器
|
||||||
|
.key(AccountKey.of(ChannelProvider.LINGXING, Platform.WALMART_SC, OMS_STORE_ID)) |
||||||
|
// 领星 store_id,会拼进请求体的 store_ids
|
||||||
|
.externalStoreId(LINGXING_STORE_ID) |
||||||
|
// 领星 platform_code,会拼进请求体的 platform_code
|
||||||
|
.platformCode(PLATFORM_CODE) |
||||||
|
// 密钥引用;本 SDK 目前不使用(密钥从 ChannelConnection 传)
|
||||||
|
.credentialRef("kms://test/lingxing") |
||||||
|
// 市场时区,必须能被 ZoneId.of 解析;影响返回时间的当地视图
|
||||||
|
.marketplaceZone("America/Los_Angeles") |
||||||
|
// 店铺币种
|
||||||
|
.currency("USD") |
||||||
|
.build(); |
||||||
|
} |
||||||
|
|
||||||
|
// ==================== 第 2 段:连接 ====================
|
||||||
|
|
||||||
|
/** 连接:告诉 SDK「这次连哪里、用谁的身份」。每次调用都可以不同(换环境、换 app)。 */ |
||||||
|
private static ChannelConnection connection() { |
||||||
|
return ChannelConnection.of(ENDPOINT, ChannelSecret.builder() |
||||||
|
.appKey(APP_KEY) // 领星 AppID
|
||||||
|
.appSecret(APP_SECRET) // 领星 AppSecret
|
||||||
|
.build()); |
||||||
|
} |
||||||
|
|
||||||
|
// ==================== 第 3 段:构建 SDK ====================
|
||||||
|
|
||||||
|
/** 构建 SDK:账户仓库 + 适配器。写操作才需要幂等存储。 */ |
||||||
|
private static ChannelSdk buildSdk(HttpTransport transport) { |
||||||
|
InMemoryAccounts accounts = new InMemoryAccounts().put(account()); |
||||||
|
|
||||||
|
// 生产写法是 .register(new LingxingAdapterFactory(tokenStore, lockProvider, clock)),
|
||||||
|
// 工厂内部会 new OkHttpTransport;这里为了接管网络层,直接注册适配器实例。
|
||||||
|
LingxingAdapter adapter = new LingxingAdapter(transport, |
||||||
|
new common.ecommerce.http.JacksonJsonCodec(), |
||||||
|
new InMemoryTokenStore(), new InMemoryLockProvider(), Clock.systemUTC()); |
||||||
|
|
||||||
|
return ChannelSdk.builder() |
||||||
|
.accountRepository(accounts) // 必需:按 AccountKey 找账户
|
||||||
|
.register(adapter) // 必需:至少一个适配器
|
||||||
|
.build(); |
||||||
|
} |
||||||
|
|
||||||
|
// ==================== 调用 ====================
|
||||||
|
|
||||||
|
@Test |
||||||
|
void pullProductsThroughSdk() { |
||||||
|
FixedResponseTransport transport = new FixedResponseTransport(); |
||||||
|
ChannelSdk sdk = buildSdk(transport); |
||||||
|
|
||||||
|
// 每次调用都传 AccountKey + ChannelConnection
|
||||||
|
AccountKey key = AccountKey.of(ChannelProvider.LINGXING, Platform.WALMART_SC, OMS_STORE_ID); |
||||||
|
|
||||||
|
Page<ProductListing> page = sdk.products(key, connection()).pull(ProductQuery.builder() |
||||||
|
.updatedAfter(Instant.parse("2026-11-01T00:00:00Z")) // 可选
|
||||||
|
.updatedBefore(Instant.parse("2026-11-04T00:00:00Z")) // 可选,不传则用 SDK 时钟
|
||||||
|
.pageSize(20) // 平台上限内自动截断
|
||||||
|
.build()); |
||||||
|
|
||||||
|
// 解析结果:领星字段 → 统一模型
|
||||||
|
assertEquals(1, page.size()); |
||||||
|
ProductListing p = page.items().get(0); |
||||||
|
assertEquals("wal-0726-102", p.platformSku()); // ← msku
|
||||||
|
assertEquals("FBSPORT Round Inflatable", p.title()); // ← title
|
||||||
|
assertEquals(Platform.WALMART_SC, p.platform()); |
||||||
|
assertEquals(OMS_STORE_ID, p.omsStoreId()); |
||||||
|
assertEquals("USD", p.currency()); // ← currency_icon "$" 映射
|
||||||
|
assertEquals("139.90", p.price().amount().toPlainString()); |
||||||
|
assertNotNull(p.attributes().get("store_name")); // 未标准化的原始字段
|
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void requestBodyCarriesAccountParameters() { |
||||||
|
FixedResponseTransport transport = new FixedResponseTransport(); |
||||||
|
ChannelSdk sdk = buildSdk(transport); |
||||||
|
AccountKey key = AccountKey.of(ChannelProvider.LINGXING, Platform.WALMART_SC, OMS_STORE_ID); |
||||||
|
|
||||||
|
sdk.products(key, connection()).pull(ProductQuery.builder().pageSize(20).build()); |
||||||
|
|
||||||
|
// 第一次是换 token,第二次是拉商品
|
||||||
|
assertEquals(2, transport.requests.size()); |
||||||
|
String productBody = transport.requests.get(1).body(); |
||||||
|
|
||||||
|
// 账户里的四个参数分别落到请求里
|
||||||
|
assertTrueContains(productBody, "\"store_ids\":[\"" + LINGXING_STORE_ID + "\"]"); |
||||||
|
assertTrueContains(productBody, "\"offset\":0"); |
||||||
|
assertTrueContains(productBody, "\"length\":20"); |
||||||
|
// 时间窗按 UTC 日期发送
|
||||||
|
assertTrueContains(transport.requests.get(1).url(), "/basicOpen/multiplatform/walmart/list"); |
||||||
|
} |
||||||
|
|
||||||
|
private static void assertTrueContains(String haystack, String needle) { |
||||||
|
org.junit.jupiter.api.Assertions.assertTrue(haystack.contains(needle), |
||||||
|
"期望包含 " + needle + ",实际:" + haystack); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 固定响应传输层:令牌请求返回最小 token 响应,商品请求返回一页沃尔玛 Listing。 |
||||||
|
*/ |
||||||
|
static final class FixedResponseTransport implements HttpTransport { |
||||||
|
|
||||||
|
final java.util.List<common.ecommerce.http.HttpRequest> requests = |
||||||
|
new java.util.ArrayList<common.ecommerce.http.HttpRequest>(); |
||||||
|
|
||||||
|
@Override |
||||||
|
public HttpResponse execute(common.ecommerce.http.HttpRequest request) { |
||||||
|
requests.add(request); |
||||||
|
if (request.url().contains("/oauth/access-token")) { |
||||||
|
return HttpResponse.of(200, java.util.Collections.<String, String>emptyMap(), |
||||||
|
"{\"code\":\"200\",\"msg\":\"OK\",\"data\":{\"access_token\":\"tok-1\"," |
||||||
|
+ "\"refresh_token\":\"ref-1\",\"expires_in\":7199}}", 1); |
||||||
|
} |
||||||
|
return HttpResponse.of(200, java.util.Collections.<String, String>emptyMap(), |
||||||
|
"{\"code\":0,\"message\":\"success\",\"total\":1,\"data\":{\"list\":[{" |
||||||
|
+ "\"item_id\":\"wal-0726-102\",\"msku\":\"wal-0726-102\"," |
||||||
|
+ "\"title\":\"FBSPORT Round Inflatable\",\"picture_url\":\"\"," |
||||||
|
+ "\"status_name\":\"PUBLISHED\",\"store_id\":\"" + LINGXING_STORE_ID + "\"," |
||||||
|
+ "\"store_name\":\"测试店铺\",\"currency_icon\":\"$\",\"price\":\"139.90\"," |
||||||
|
+ "\"gtin\":\"00723849594300\"}]}}", 1); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue