Browse Source
- 新增 LingxingSdkListingCallTest:从 ChannelSdk 门面调领星 listing, 覆盖能力检查、snake_case 搜索参数、Target 时间窗三字段、审计记录 - 各平台契约测试同步实测结论:沃尔玛/eBay 改断言 snake_case、 多值搜索显式失败;Target 补时间窗与多值校验;Temu 补 30 天截断; SHEIN/TikTok 补本地 SKU 编码 8 Co-Authored-By: Claude <noreply@anthropic.com>feature/supplier-lingxing
7 changed files with 316 additions and 6 deletions
@ -0,0 +1,164 @@ |
|||||||
|
package common.ecommerce.lingxing.adapter; |
||||||
|
|
||||||
|
import common.ecommerce.audit.CallObserver; |
||||||
|
import common.ecommerce.audit.CallOutcome; |
||||||
|
import common.ecommerce.audit.CallRecord; |
||||||
|
import common.ecommerce.http.JacksonJsonCodec; |
||||||
|
import common.ecommerce.lingxing.LingxingApi; |
||||||
|
import common.ecommerce.lingxing.support.LingxingTestSupport; |
||||||
|
import common.ecommerce.model.Platform; |
||||||
|
import common.ecommerce.model.account.ChannelAccount; |
||||||
|
import common.ecommerce.model.product.ProductListing; |
||||||
|
import common.ecommerce.model.product.ProductQuery; |
||||||
|
import common.ecommerce.model.product.ProductSearchField; |
||||||
|
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 common.ecommerce.testkit.RecordingTransport; |
||||||
|
import common.ecommerce.testkit.Responses; |
||||||
|
import org.junit.jupiter.api.Test; |
||||||
|
|
||||||
|
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.assertTrue; |
||||||
|
|
||||||
|
/** |
||||||
|
* 从 {@link ChannelSdk} 门面调用领星 listing 的端到端测试。 |
||||||
|
* |
||||||
|
* <p>用 {@link RecordingTransport} 替掉网络,走真实的 {@link LingxingAdapter}, |
||||||
|
* 验证「门面 → 子平台 → Source」这条链路:能力检查、请求路径、SKU 搜索参数、 |
||||||
|
* 统一模型映射和调用日志。</p> |
||||||
|
*/ |
||||||
|
class LingxingSdkListingCallTest { |
||||||
|
|
||||||
|
/** 沃尔玛的商品页;响应里 total 在顶层,data 是数组。 */ |
||||||
|
private static final String WALMART_JSON = |
||||||
|
"{\"code\":0,\"message\":\"success\",\"total\":1,\"data\":[{" |
||||||
|
+ "\"item_id\":\"wal-1\",\"msku\":\"WM-MSKU-1\",\"title\":\"Walmart Item\"," |
||||||
|
+ "\"local_sku\":\"WM-LOCAL-1\",\"status_name\":\"PUBLISHED\"," |
||||||
|
+ "\"store_id\":\"store-1\",\"currency_icon\":\"$\",\"price\":\"19.99\"}]}"; |
||||||
|
|
||||||
|
/** Target 的商品页;响应结构是 data.list。 */ |
||||||
|
private static final String TARGET_JSON = |
||||||
|
"{\"code\":0,\"message\":\"success\",\"data\":{\"count\":1,\"list\":[{" |
||||||
|
+ "\"product_unique_id\":\"prod-1\",\"msku\":\"TGT-MSKU-1\",\"title\":\"Target Item\"," |
||||||
|
+ "\"local_sku\":\"\",\"store_id\":\"store-1\",\"price\":\"9.99\"," |
||||||
|
+ "\"currency_icon\":\"$\",\"status\":1,\"status_name\":\"active\"}]}}"; |
||||||
|
|
||||||
|
private static final class CollectingObserver implements CallObserver { |
||||||
|
|
||||||
|
private final List<CallRecord> records = new ArrayList<CallRecord>(); |
||||||
|
|
||||||
|
@Override |
||||||
|
public void onCall(CallRecord record) { |
||||||
|
records.add(record); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static ChannelSdk sdk(RecordingTransport transport, ChannelAccount account, |
||||||
|
CallObserver observer) { |
||||||
|
LingxingAdapter adapter = new LingxingAdapter( |
||||||
|
transport, new JacksonJsonCodec(), |
||||||
|
new InMemoryTokenStore(), new InMemoryLockProvider(), |
||||||
|
LingxingTestSupport.CLOCK); |
||||||
|
return ChannelSdk.builder() |
||||||
|
.accountRepository(new InMemoryAccounts().put(account)) |
||||||
|
.clock(LingxingTestSupport.CLOCK) |
||||||
|
.register(adapter) |
||||||
|
.callObserver(observer) |
||||||
|
.build(); |
||||||
|
} |
||||||
|
|
||||||
|
private static ChannelConnection connection() { |
||||||
|
return ChannelConnection.of(LingxingTestSupport.ENDPOINT, |
||||||
|
ChannelSecret.builder() |
||||||
|
.appKey(LingxingTestSupport.APP_KEY) |
||||||
|
.appSecret(LingxingTestSupport.APP_SECRET) |
||||||
|
.build()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void pullsWalmartListingThroughFacade() { |
||||||
|
RecordingTransport transport = RecordingTransport.create() |
||||||
|
.route(LingxingApi.LISTING_WALMART.path(), r -> Responses.ok(WALMART_JSON)); |
||||||
|
ChannelAccount account = LingxingTestSupport.account(Platform.WALMART_SC, 10008, "store-1"); |
||||||
|
CollectingObserver observer = new CollectingObserver(); |
||||||
|
|
||||||
|
Page<ProductListing> page = sdk(transport, account, observer) |
||||||
|
.products(account.key(), connection()) |
||||||
|
.pull(ProductQuery.builder() |
||||||
|
.pageSize(10) |
||||||
|
.searchField(ProductSearchField.PLATFORM_SKU) |
||||||
|
.searchValue("WM-MSKU-1") |
||||||
|
.build()); |
||||||
|
|
||||||
|
assertEquals(1, page.size()); |
||||||
|
ProductListing p = page.items().get(0); |
||||||
|
assertEquals(Platform.WALMART_SC, p.platform()); |
||||||
|
assertEquals("WM-MSKU-1", p.platformSku()); |
||||||
|
// omsStoreId 是宿主的店铺标识(这里取 AccountKey 的 storeId),不是领星 store_id
|
||||||
|
assertEquals(account.omsStoreId(), p.omsStoreId()); |
||||||
|
assertEquals("PUBLISHED", p.rawStatus()); |
||||||
|
|
||||||
|
// 实测:搜索参数必须是 snake_case,否则平台静默忽略
|
||||||
|
String body = transport.lastDataRequest().body(); |
||||||
|
assertTrue(body.contains("\"search_field\":1"), body); |
||||||
|
assertTrue(body.contains("\"search_single_value\":\"WM-MSKU-1\""), body); |
||||||
|
assertFalse(body.contains("searchField"), body); |
||||||
|
|
||||||
|
// 门面审计记录:operation / 平台 / store / 条数
|
||||||
|
assertEquals(1, observer.records.size()); |
||||||
|
CallRecord record = observer.records.get(0); |
||||||
|
assertEquals("products.pull", record.operation()); |
||||||
|
assertEquals(CallOutcome.SUCCESS, record.outcome()); |
||||||
|
assertEquals(Platform.WALMART_SC, record.platform()); |
||||||
|
assertEquals("store-1", record.externalStoreId()); |
||||||
|
assertEquals(1, record.resultCount()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void pullsTargetListingThroughFacade() { |
||||||
|
RecordingTransport transport = RecordingTransport.create() |
||||||
|
.route(LingxingApi.LISTING_SHOPIFY.path(), r -> Responses.ok(TARGET_JSON)); |
||||||
|
ChannelAccount account = LingxingTestSupport.account(Platform.TARGET, 0, "store-1"); |
||||||
|
|
||||||
|
Page<ProductListing> page = sdk(transport, account, CallObserver.NOOP) |
||||||
|
.products(account.key(), connection()) |
||||||
|
.pull(ProductQuery.builder() |
||||||
|
.pageSize(5) |
||||||
|
.updatedAfter(java.time.Instant.parse("2026-10-01T00:00:00Z")) |
||||||
|
.updatedBefore(java.time.Instant.parse("2026-10-08T00:00:00Z")) |
||||||
|
.build()); |
||||||
|
|
||||||
|
assertEquals(1, page.size()); |
||||||
|
assertEquals("TGT-MSKU-1", page.items().get(0).platformSku()); |
||||||
|
assertEquals("prod-1", page.items().get(0).listingId()); |
||||||
|
|
||||||
|
String body = transport.lastDataRequest().body(); |
||||||
|
assertTrue(body.contains("\"store_ids\":[\"store-1\"]"), body); |
||||||
|
assertTrue(body.contains("\"listing_time_field\":2"), body); |
||||||
|
assertTrue(body.contains("\"listing_start_time\":\"2026-10-01\""), body); |
||||||
|
assertTrue(body.contains("\"listing_end_time\":\"2026-10-08\""), body); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void capabilityReportIncludesProductPerSubPlatform() { |
||||||
|
RecordingTransport transport = RecordingTransport.create(); |
||||||
|
ChannelAccount account = LingxingTestSupport.account(Platform.TARGET, 0, "store-1"); |
||||||
|
|
||||||
|
// Target 有商品能力;没有商品接口的长尾平台(如 Mirakl)没有
|
||||||
|
assertTrue(sdk(transport, account, CallObserver.NOOP) |
||||||
|
.capabilities(account.key()).supports(common.ecommerce.model.Capability.PRODUCT)); |
||||||
|
|
||||||
|
ChannelAccount mirakl = LingxingTestSupport.account(Platform.MIRAKL, 10034, "store-1"); |
||||||
|
assertFalse(sdk(transport, mirakl, CallObserver.NOOP) |
||||||
|
.capabilities(mirakl.key()).supports(common.ecommerce.model.Capability.PRODUCT)); |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue