From 831cd71ce2caef02d8cb055235b6a12e316491eb Mon Sep 17 00:00:00 2001 From: chenchuchuan <13554600537@163.com> Date: Wed, 4 Nov 2026 09:19:13 +0800 Subject: [PATCH] =?UTF-8?q?test(lingxing):=20=E6=96=B0=E5=A2=9E=E4=BB=8E?= =?UTF-8?q?=20ChannelSdk=20=E8=B0=83=E7=94=A8=E9=A2=86=E6=98=9F=E7=9A=84?= =?UTF-8?q?=E7=94=A8=E6=B3=95=E6=BC=94=E7=A4=BA=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 六个步骤对应操作手册的调用顺序: 1. 构造 ChannelAccount(provider/platform/omsStoreId + externalStoreId + platformCode) 2. 构造 ChannelConnection(端点 + appId/appSecret) 3. 构建 ChannelSdk(accountRepository 必需;工厂或适配器实例注册) 4. capabilities() 不发网络请求 5. pull 时打印并断言真实请求:先换 token,再拉订单,含 platform_code/store_id/offset/length/时间窗 6. 三类快速失败:账户未配置、能力缺失(在调用动作时报错)、平台无适配器 注意:领星 appId 同时作为签名 AES 密钥,测试里必须是 16/24/32 字节。 Co-Authored-By: Claude --- .../ecommerce/lingxing/SdkCallGuideTest.java | 213 +++++++++++++++++++++ 1 file changed, 213 insertions(+) create mode 100644 lingxing/src/test/java/common/ecommerce/lingxing/SdkCallGuideTest.java diff --git a/lingxing/src/test/java/common/ecommerce/lingxing/SdkCallGuideTest.java b/lingxing/src/test/java/common/ecommerce/lingxing/SdkCallGuideTest.java new file mode 100644 index 0000000..d4ec1e3 --- /dev/null +++ b/lingxing/src/test/java/common/ecommerce/lingxing/SdkCallGuideTest.java @@ -0,0 +1,213 @@ +package common.ecommerce.lingxing; + +import common.ecommerce.capability.CapabilityReport; +import common.ecommerce.error.ChannelException; +import common.ecommerce.http.HttpRequest; +import common.ecommerce.http.HttpResponse; +import common.ecommerce.http.HttpTransport; +import common.ecommerce.lingxing.adapter.LingxingAdapterFactory; +import common.ecommerce.model.Capability; +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.order.SalesOrderQuery; +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.InMemoryIdempotencyStore; +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 java.time.ZoneOffset; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * 从 ChannelSdk 调用领星的完整写法演示。 + * + *

这不是契约测试,而是用法说明:每一段代码对应操作手册里的一节。 + * 网络层用 {@link RecordingTransport} 记录请求后中断,不联网、不伪造响应。

+ */ +class SdkCallGuideTest { + + private static final String ENDPOINT = "https://openapi.lingxing.com"; + private static final String OMS_STORE_ID = "oms-wm-001"; + /** 领星 appId 同时作为签名 AES 密钥,长度必须是 16/24/32 字节。 */ + private static final String APP_KEY = "test-app-id-1234"; + private static final String APP_SECRET = "test-app-secret"; + + /** ---------------------------------------------------------------- 第 1 步:账户 */ + private static ChannelAccount walmartAccount() { + return ChannelAccount.builder() + .key(AccountKey.of(ChannelProvider.LINGXING, Platform.WALMART_SC, OMS_STORE_ID)) + .externalStoreId("1108413237731") // 领星 store_id + .platformCode(10008) // 领星 platform_code:10008 = Walmart + .credentialRef("kms://test/lingxing") // 只存引用(本 SDK 目前不用它取密钥) + .marketplaceZone("America/Los_Angeles") // 必须是合法 IANA 时区 + .currency("USD") + .build(); + } + + /** ---------------------------------------------------------------- 第 2 步:连接信息 */ + private static ChannelConnection connection() { + return ChannelConnection.of(ENDPOINT, + ChannelSecret.builder().appKey(APP_KEY).appSecret(APP_SECRET).build()); + } + + /** ---------------------------------------------------------------- 第 3 步:构建 SDK */ + private static ChannelSdk buildSdk(HttpTransport transport, ChannelAccount account) { + InMemoryAccounts accounts = new InMemoryAccounts().put(account); + + ChannelSdk.Builder builder = ChannelSdk.builder() + .accountRepository(accounts) // 必需:按 AccountKey 查账户 + .idempotencyStore(new InMemoryIdempotencyStore()) // 写操作用;本用例不写 + .idempotencyOwner("test-node") + .clock(Clock.fixed(Instant.parse("2026-11-04T00:00:00Z"), ZoneOffset.UTC)); + + // 工厂内部会 new OkHttpTransport,所以这里直接注册适配器实例以便注入测试传输层。 + // 生产环境用 .register(new LingxingAdapterFactory(tokenStore, lockProvider, clock))。 + return builder.register(new common.ecommerce.lingxing.adapter.LingxingAdapter( + transport, + new common.ecommerce.http.JacksonJsonCodec(), + new InMemoryTokenStore(), + new InMemoryLockProvider(), + Clock.systemUTC())).build(); + } + + @Test + void step3_factoryStyleRegistration() { + // 生产写法:注册工厂,SDK 在 build() 时调用 factory.create(...) + LingxingAdapterFactory factory = new LingxingAdapterFactory( + new InMemoryTokenStore(), new InMemoryLockProvider(), Clock.systemUTC()); + + assertNotNull(factory); + assertEquals("lingxing", factory.adapterId()); + assertEquals(ChannelProvider.LINGXING, factory.provider()); + assertTrue(factory.platforms().contains(Platform.WALMART_SC)); + } + + /** ---------------------------------------------------------------- 第 4 步:查能力(不发请求) */ + @Test + void step4_capabilitiesDoNotCallNetwork() { + RecordingTransport transport = new RecordingTransport(); + ChannelSdk sdk = buildSdk(transport, walmartAccount()); + + AccountKey key = AccountKey.of(ChannelProvider.LINGXING, Platform.WALMART_SC, OMS_STORE_ID); + CapabilityReport caps = sdk.capabilities(key); + + assertTrue(caps.supports(Capability.SALES_ORDER)); + assertTrue(caps.supports(Capability.PRODUCT)); + assertFalse(caps.supports(Capability.REFUND)); + assertTrue(transport.recorded.isEmpty(), "capabilities() 不应发起网络请求"); + } + + /** ---------------------------------------------------------------- 第 5 步:拉订单 */ + @Test + void step5_pullOrdersShowsRequestParameters() { + RecordingTransport transport = new RecordingTransport(); + ChannelSdk sdk = buildSdk(transport, walmartAccount()); + AccountKey key = AccountKey.of(ChannelProvider.LINGXING, Platform.WALMART_SC, OMS_STORE_ID); + + // 请求构造完成后 RecordingTransport 会抛 StopException 中断,这里接住 + assertThrows(RecordingTransport.Stop.class, () -> sdk.salesOrders(key, connection()).pull( + SalesOrderQuery.builder() + .updatedAfter(Instant.parse("2026-11-01T00:00:00Z")) + .updatedBefore(Instant.parse("2026-11-02T00:00:00Z")) + .pageSize(50) + .build())); + + // 两次请求:先换 token,再拉订单 + assertEquals(2, transport.recorded.size()); + HttpRequest tokenReq = transport.recorded.get(0); + HttpRequest orderReq = transport.recorded.get(1); + + assertTrue(tokenReq.url().endsWith("/api/auth-server/oauth/access-token"), tokenReq.url()); + assertTrue(tokenReq.body().contains("name=\"appId\"\r\n\r\n" + APP_KEY), tokenReq.body()); + assertTrue(tokenReq.body().contains("name=\"appSecret\"\r\n\r\n" + APP_SECRET), tokenReq.body()); + + assertTrue(orderReq.url().startsWith(ENDPOINT + "/pb/mp/order/v2/list"), orderReq.url()); + assertTrue(orderReq.url().contains("app_key=" + APP_KEY), orderReq.url()); + assertTrue(orderReq.url().contains("sign="), orderReq.url()); + assertTrue(orderReq.body().contains("\"platform_code\":[10008]"), orderReq.body()); + assertTrue(orderReq.body().contains("\"store_id\":[\"1108413237731\"]"), orderReq.body()); + assertTrue(orderReq.body().contains("\"offset\":0"), orderReq.body()); + assertTrue(orderReq.body().contains("\"length\":50"), orderReq.body()); + assertTrue(orderReq.body().contains("\"start_time\":1793491200"), orderReq.body()); + assertEquals("Bearer ", orderReq.headers().get("Authorization").substring(0, 7)); + } + + /** ---------------------------------------------------------------- 第 6 步:参数不对时的报错 */ + @Test + void step6_unknownAccountFailsFast() { + ChannelSdk sdk = buildSdk(new RecordingTransport(), walmartAccount()); + AccountKey missing = AccountKey.of(ChannelProvider.LINGXING, Platform.WALMART_SC, "oms-not-exists"); + + IllegalStateException ex = assertThrows(IllegalStateException.class, + () -> sdk.salesOrders(missing, connection())); + assertTrue(ex.getMessage().contains("channel account not configured"), ex.getMessage()); + } + + @Test + void step6_unsupportedCapabilityFailsFast() { + ChannelSdk sdk = buildSdk(new RecordingTransport(), walmartAccount()); + AccountKey key = AccountKey.of(ChannelProvider.LINGXING, Platform.WALMART_SC, OMS_STORE_ID); + + // 能力检查发生在调用动作时(pull/submit),不是取视图时 + ChannelException.UnsupportedCapabilityException ex = + assertThrows(ChannelException.UnsupportedCapabilityException.class, + () -> sdk.settlements(key, connection()).pull( + common.ecommerce.model.settlement.SettlementQuery.builder().build())); + assertEquals(Capability.BILLING, ex.capability()); + } + + @Test + void step6_unregisteredPlatformFailsFast() { + ChannelSdk sdk = buildSdk(new RecordingTransport(), walmartAccount()); + // 账户仓库里只有沃尔玛 SC 的账户,用 EBAY 的 key 会先卡在账户查询 + AccountKey ebay = AccountKey.of(ChannelProvider.LINGXING, Platform.EBAY, OMS_STORE_ID); + + IllegalStateException ex = assertThrows(IllegalStateException.class, + () -> sdk.salesOrders(ebay, connection())); + assertTrue(ex.getMessage().contains("channel account not configured"), ex.getMessage()); + } + + /** + * 记录请求后中断的传输层。 + * + *

令牌请求返回一个最小的成功响应,让流程能走到业务请求; + * 业务请求记录后中断,用来看请求是怎么构造的。

+ */ + static final class RecordingTransport implements HttpTransport { + + final List recorded = new ArrayList(); + + @Override + public HttpResponse execute(HttpRequest request) { + recorded.add(request); + if (request.url().contains("/api/auth-server/oauth/access-token")) { + return HttpResponse.of(200, java.util.Collections.emptyMap(), + "{\"code\":\"200\",\"msg\":\"OK\",\"data\":{" + + "\"access_token\":\"tok-1\",\"refresh_token\":\"ref-1\"," + + "\"expires_in\":7199}}", 1); + } + throw new Stop(); + } + + /** 用于中断调用链,不是错误。 */ + static final class Stop extends RuntimeException { + private static final long serialVersionUID = 1L; + } + } +}