Browse Source
- ListingSmokeProbeTest:从 ChannelSdk 门面逐平台拉一页 listing, 打印统一模型关键字段并用样本回查平台 SKU / 本地 SKU - ListingParamProbeTest:核对 Target 时间窗上限、Temu 窗口边界等参数形态 - SearchFieldProbeTest / ShopListProbeTest:去掉硬编码的真实凭据, 改从 -Dlingxing.probe.appKey / -Dlingxing.probe.appSecret 读取; 未提供时 assumeTrue 跳过,避免本地与 CI 默认打真机 - SearchFieldProbeTest 的 TARGET_STORE_ID 置空(store_ids 非必填)、 样本为空不再断言失败、单个编码报错不中断整轮扫描 所有探针仍默认跳过且只做查询。 Co-Authored-By: Claude <noreply@anthropic.com>feature/supplier-lingxing
4 changed files with 420 additions and 16 deletions
@ -0,0 +1,165 @@
@@ -0,0 +1,165 @@
|
||||
package common.ecommerce.lingxing.probe; |
||||
|
||||
import common.ecommerce.http.HttpRequest; |
||||
import common.ecommerce.http.HttpResponse; |
||||
import common.ecommerce.http.HttpTransport; |
||||
import common.ecommerce.http.JacksonJsonCodec; |
||||
import common.ecommerce.http.OkHttpTransport; |
||||
import common.ecommerce.lingxing.LingxingApi; |
||||
import common.ecommerce.lingxing.LingxingConstants; |
||||
import common.ecommerce.lingxing.auth.LingxingTokenClient; |
||||
import common.ecommerce.lingxing.http.LingxingEnvelope; |
||||
import common.ecommerce.lingxing.http.LingxingRateLimiter; |
||||
import common.ecommerce.lingxing.http.LingxingRequestFactory; |
||||
import common.ecommerce.lingxing.platform.LingxingCall; |
||||
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.sdk.ChannelConnection; |
||||
import common.ecommerce.sdk.ChannelContext; |
||||
import common.ecommerce.spi.AccessToken; |
||||
import common.ecommerce.spi.ChannelSecret; |
||||
import com.fasterxml.jackson.core.JsonProcessingException; |
||||
import com.fasterxml.jackson.databind.JsonNode; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.condition.EnabledIfSystemProperty; |
||||
|
||||
import java.time.Clock; |
||||
import java.util.Collections; |
||||
import java.util.LinkedHashMap; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* 只读探针(收尾第三轮):Target 时间窗上限与单边窗口。 |
||||
* |
||||
* <p>默认跳过;用 {@code -Dlingxing.probe=true} 显式开启。<b>只做查询。</b></p> |
||||
*/ |
||||
@EnabledIfSystemProperty(named = "lingxing.probe", matches = "true") |
||||
class ListingParamProbeTest { |
||||
|
||||
// 真实凭据不入库:本地跑探针时用 -Dlingxing.probe.appKey=... -Dlingxing.probe.appSecret=...
|
||||
private static final String APP_KEY = System.getProperty("lingxing.probe.appKey", ""); |
||||
private static final String APP_SECRET = System.getProperty("lingxing.probe.appSecret", ""); |
||||
private static final String BASE_URL = LingxingConstants.PRODUCTION_BASE_URL; |
||||
|
||||
private static final JacksonJsonCodec JSON = new JacksonJsonCodec(); |
||||
private final HttpTransport transport = new OkHttpTransport("common-ecommerce-param-probe"); |
||||
|
||||
private static final String TARGET_STORE = "110593321260223488"; |
||||
private static final String TEMU_STORE = "110544498784182784"; |
||||
|
||||
@Test |
||||
void targetWindowLimits() { |
||||
requireCredentials(); |
||||
LingxingCall call = call(Platform.TARGET, 0, TARGET_STORE); |
||||
System.out.println("\n===== TARGET 时间窗上限 ====="); |
||||
for (int days : new int[]{30, 31, 60, 90, 180, 365}) { |
||||
Map<String, Object> body = base(); |
||||
body.put("listing_time_field", 2); |
||||
body.put("listing_start_time", minusDays(days)); |
||||
body.put("listing_end_time", "2026-11-06"); |
||||
probe(call, LingxingApi.LISTING_SHOPIFY, days + " 天", body); |
||||
} |
||||
System.out.println("\n===== TARGET 单边窗口(field=2)====="); |
||||
Map<String, Object> startOnly = base(); |
||||
startOnly.put("listing_time_field", 2); |
||||
startOnly.put("listing_start_time", "2026-10-01"); |
||||
probe(call, LingxingApi.LISTING_SHOPIFY, "只 start", startOnly); |
||||
|
||||
Map<String, Object> endOnly = base(); |
||||
endOnly.put("listing_time_field", 2); |
||||
endOnly.put("listing_end_time", "2026-11-06"); |
||||
probe(call, LingxingApi.LISTING_SHOPIFY, "只 end", endOnly); |
||||
|
||||
System.out.println("\n===== TARGET time_field 取值 ====="); |
||||
for (int field : new int[]{1, 2, 3}) { |
||||
Map<String, Object> body = base(); |
||||
body.put("listing_time_field", field); |
||||
body.put("listing_start_time", "2021-01-01"); |
||||
body.put("listing_end_time", "2023-12-31"); |
||||
probe(call, LingxingApi.LISTING_SHOPIFY, "field=" + field + " 2021-2023", body); |
||||
} |
||||
} |
||||
|
||||
@Test |
||||
void temuWindowBoundary() { |
||||
requireCredentials(); |
||||
LingxingCall call = call(Platform.TEMU, 10024, TEMU_STORE); |
||||
System.out.println("\n===== TEMU 窗口边界(数据时间未知,只看是否报错)====="); |
||||
for (int days : new int[]{1, 7, 30}) { |
||||
Map<String, Object> body = new LinkedHashMap<String, Object>(); |
||||
body.put("offset", 0); |
||||
body.put("length", 3); |
||||
body.put("useCursor", Boolean.FALSE); |
||||
body.put("storeIds", Collections.singletonList(TEMU_STORE)); |
||||
body.put("updateStartTime", minusDays(days) + " 00:00:00"); |
||||
body.put("updateEndTime", "2026-11-06 00:00:00"); |
||||
probe(call, LingxingApi.LISTING_TEMU, days + " 天", body); |
||||
} |
||||
} |
||||
|
||||
// ---------------------------------------------------------------- 工具
|
||||
|
||||
private static Map<String, Object> base() { |
||||
Map<String, Object> body = new LinkedHashMap<String, Object>(); |
||||
body.put("offset", 0); |
||||
body.put("length", 10); |
||||
body.put("store_ids", Collections.singletonList(TARGET_STORE)); |
||||
return body; |
||||
} |
||||
|
||||
private static String minusDays(int days) { |
||||
return java.time.LocalDate.parse("2026-11-06").minusDays(days).toString(); |
||||
} |
||||
|
||||
/** 没有真实凭据时跳过,避免本地/CI 默认跑真机。 */ |
||||
private static void requireCredentials() { |
||||
org.junit.jupiter.api.Assumptions.assumeTrue( |
||||
!APP_KEY.trim().isEmpty() && !APP_SECRET.trim().isEmpty(), |
||||
"未提供 -Dlingxing.probe.appKey / -Dlingxing.probe.appSecret,跳过探针"); |
||||
} |
||||
|
||||
private void probe(LingxingCall call, LingxingApi api, String label, Map<String, Object> body) { |
||||
try { |
||||
HttpRequest req = call.signed(api, body, "probe.param"); |
||||
HttpResponse resp = call.execute(req); |
||||
LingxingEnvelope env = call.envelope(resp, "probe.param"); |
||||
JsonNode root = JSON.mapper().readTree(env.body()); |
||||
JsonNode data = root == null ? null : root.get("data"); |
||||
int items = 0; |
||||
if (data != null) { |
||||
items = data.isArray() ? data.size() : data.path("list").size(); |
||||
} |
||||
System.out.println(" [" + label + "] items=" + items); |
||||
} catch (JsonProcessingException e) { |
||||
System.out.println(" [" + label + "] 解析失败"); |
||||
} catch (RuntimeException e) { |
||||
System.out.println(" [" + label + "] 失败: " + shortMsg(e)); |
||||
} |
||||
} |
||||
|
||||
private static String shortMsg(RuntimeException e) { |
||||
String msg = e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage(); |
||||
return msg.length() <= 150 ? msg : msg.substring(0, 150); |
||||
} |
||||
|
||||
private LingxingCall call(Platform platform, int code, String storeId) { |
||||
ChannelAccount account = ChannelAccount.builder() |
||||
.key(AccountKey.of(ChannelProvider.LINGXING, platform, "oms-probe")) |
||||
.externalStoreId(storeId == null || storeId.isEmpty() ? "probe" : storeId) |
||||
.platformCode(code == 0 ? null : code) |
||||
.credentialRef("probe") |
||||
.marketplaceZone("America/Los_Angeles") |
||||
.currency("USD") |
||||
.build(); |
||||
Clock clock = Clock.systemUTC(); |
||||
ChannelSecret secret = ChannelSecret.builder().appKey(APP_KEY).appSecret(APP_SECRET).build(); |
||||
ChannelConnection conn = ChannelConnection.of(BASE_URL, secret); |
||||
ChannelContext ctx = ChannelContext.of(account, conn, "probe-" + System.currentTimeMillis(), clock); |
||||
AccessToken token = new LingxingTokenClient(transport, JSON, clock) |
||||
.fetch(BASE_URL, APP_KEY, APP_SECRET); |
||||
return new LingxingCall(ctx, token, new LingxingRequestFactory(JSON, clock), |
||||
new LingxingRateLimiter(), JSON, transport); |
||||
} |
||||
} |
||||
@ -0,0 +1,217 @@
@@ -0,0 +1,217 @@
|
||||
package common.ecommerce.lingxing.probe; |
||||
|
||||
import common.ecommerce.capability.AggregatorCapable.SubChannel; |
||||
import common.ecommerce.http.HttpRequest; |
||||
import common.ecommerce.http.HttpResponse; |
||||
import common.ecommerce.http.HttpTransport; |
||||
import common.ecommerce.http.JacksonJsonCodec; |
||||
import common.ecommerce.http.OkHttpTransport; |
||||
import common.ecommerce.lingxing.LingxingConstants; |
||||
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.product.ProductSearchField; |
||||
import common.ecommerce.model.support.Page; |
||||
import common.ecommerce.sdk.ChannelConnection; |
||||
import common.ecommerce.sdk.ChannelContext; |
||||
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.Assumptions; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.condition.EnabledIfSystemProperty; |
||||
|
||||
import java.time.Clock; |
||||
import java.time.Instant; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 只读冒烟:从 {@link ChannelSdk} 门面逐个平台拉一页 listing,核对出入参。 |
||||
* |
||||
* <p>默认跳过;用 {@code -Dlingxing.probe=true} 显式开启。</p> |
||||
* |
||||
* <p><b>只做查询</b>。会打印请求体(截断)与统一模型的关键字段, |
||||
* 便于对照接口文档核对分页、日期、SKU 搜索三项参数。</p> |
||||
*/ |
||||
@EnabledIfSystemProperty(named = "lingxing.probe", matches = "true") |
||||
class ListingSmokeProbeTest { |
||||
|
||||
// 真实凭据不入库:本地跑探针时用 -Dlingxing.probe.appKey=... -Dlingxing.probe.appSecret=...
|
||||
private static final String APP_KEY = System.getProperty("lingxing.probe.appKey", ""); |
||||
private static final String APP_SECRET = System.getProperty("lingxing.probe.appSecret", ""); |
||||
private static final String BASE_URL = LingxingConstants.PRODUCTION_BASE_URL; |
||||
|
||||
private static final JacksonJsonCodec JSON = new JacksonJsonCodec(); |
||||
private final LoggingTransport transport = new LoggingTransport(new OkHttpTransport("common-ecommerce-listing-probe")); |
||||
private final InMemoryTokenStore tokens = new InMemoryTokenStore(); |
||||
|
||||
/** 平台 → 实测店铺 id,取自 ShopListProbeTest 的输出。 */ |
||||
private static final Object[][] TARGETS = { |
||||
{Platform.AMAZON_SC, 10001, "127524991355697152", "AMZ-TOY-US"}, |
||||
{Platform.AMAZON_VC, 10035, "134543850419473408", "AMZ-CL-US"}, |
||||
{Platform.WALMART_SC, 10008, "110528861382225920", "WMT-CYB-US"}, |
||||
{Platform.EBAY, 10003, "110627593325390848", "EBA-STE-US"}, |
||||
{Platform.TIKTOK, 10011, "110541661459314688", "TTK-STE"}, |
||||
{Platform.TEMU, 10024, "110544498784182784", "TEMU OFFICE DIRECT"}, |
||||
{Platform.SHEIN, 10028, "110541676737748480", "SHE-JCK-US"}, |
||||
{Platform.ALIEXPRESS, 0, "", "全部阿里店铺(接口不需要 store)"}, |
||||
// shopify/variantList 下真正有商品的是 SPF-BM(10002),Target DVS 是空店
|
||||
{Platform.TARGET, 0, "110593321260223488", "SPF-BM"}, |
||||
}; |
||||
|
||||
/** |
||||
* 列表页不传时间窗:部分平台的数据很久没更新(Temu 最近更新时间早于窗口), |
||||
* 传窄窗口会得到 0 条,看不出字段映射。时间窗参数另行核对,见 {@link ListingParamProbeTest}。 |
||||
*/ |
||||
@Test |
||||
void pullOnePagePerPlatform() { |
||||
Assumptions.assumeTrue(!APP_KEY.trim().isEmpty() && !APP_SECRET.trim().isEmpty(), |
||||
"未提供 -Dlingxing.probe.appKey / -Dlingxing.probe.appSecret,跳过探针"); |
||||
ChannelSdk sdk = sdk(); |
||||
|
||||
for (Object[] row : TARGETS) { |
||||
Platform platform = (Platform) row[0]; |
||||
int code = (Integer) row[1]; |
||||
String storeId = (String) row[2]; |
||||
String label = (String) row[3]; |
||||
System.out.println("\n================ " + platform + " / " + label + " ================"); |
||||
|
||||
ChannelAccount account = account(platform, code, storeId); |
||||
AccountKey key = account.key(); |
||||
ChannelConnection conn = connection(); |
||||
|
||||
try { |
||||
sdk.capabilities(key); |
||||
Page<ProductListing> page = sdk.products(key, conn).pull(ProductQuery.builder() |
||||
.pageSize(3) |
||||
.build()); |
||||
System.out.println("[result] items=" + page.size() + " nextCursor=" + page.nextCursor()); |
||||
for (ProductListing p : page.items()) { |
||||
System.out.println(" platformSku=" + p.platformSku() |
||||
+ " localSku=" + localSkuOf(p) |
||||
+ " listingId=" + p.listingId() |
||||
+ " status=" + p.status() |
||||
+ " title=" + shorten(p.title()) |
||||
+ " listedAt=" + p.listedAt() |
||||
+ " updatedAt=" + p.updatedAt()); |
||||
} |
||||
|
||||
// 用样本回查平台 SKU + 本地 SKU,核对 search 条件
|
||||
if (!page.items().isEmpty()) { |
||||
ProductListing sample = page.items().get(0); |
||||
probeSearch(sdk, key, conn, platform, ProductSearchField.PLATFORM_SKU, sample.platformSku()); |
||||
String localSku = localSkuOf(sample); |
||||
if (localSku != null && !localSku.isEmpty()) { |
||||
probeSearch(sdk, key, conn, platform, ProductSearchField.LOCAL_SKU, localSku); |
||||
} else { |
||||
System.out.println(" [search] 本地 SKU 为空,跳过 LOCAL_SKU 回查"); |
||||
} |
||||
} |
||||
} catch (RuntimeException e) { |
||||
System.out.println("[ERROR] " + e.getClass().getSimpleName() + ": " + e.getMessage()); |
||||
} |
||||
} |
||||
} |
||||
|
||||
private void probeSearch(ChannelSdk sdk, AccountKey key, ChannelConnection conn, |
||||
Platform platform, ProductSearchField field, String value) { |
||||
try { |
||||
Page<ProductListing> got = sdk.products(key, conn).pull(ProductQuery.builder() |
||||
.searchField(field) |
||||
.searchValue(value) |
||||
.pageSize(3) |
||||
.build()); |
||||
System.out.println(" [search] " + field + "=" + value + " → items=" + got.size()); |
||||
} catch (RuntimeException e) { |
||||
String msg = e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage(); |
||||
System.out.println(" [search] " + field + "=" + value + " → 失败:" + shorten(msg)); |
||||
} |
||||
} |
||||
|
||||
/** 统一模型没有 localSku 字段,从 attributes 里取原始值。 */ |
||||
private static String localSkuOf(ProductListing p) { |
||||
Object v = p.attributes().get("local_sku"); |
||||
return v == null ? null : String.valueOf(v); |
||||
} |
||||
|
||||
private ChannelSdk sdk() { |
||||
ChannelAccount[] accounts = new ChannelAccount[TARGETS.length]; |
||||
InMemoryAccounts repo = new InMemoryAccounts(); |
||||
for (int i = 0; i < TARGETS.length; i++) { |
||||
Object[] row = TARGETS[i]; |
||||
accounts[i] = account((Platform) row[0], (Integer) row[1], (String) row[2]); |
||||
repo.put(accounts[i]); |
||||
} |
||||
LingxingAdapter adapter = new LingxingAdapter(transport, JSON, tokens, |
||||
new InMemoryLockProvider(), Clock.systemUTC()); |
||||
return ChannelSdk.builder() |
||||
.accountRepository(repo) |
||||
.clock(Clock.systemUTC()) |
||||
.register(adapter) |
||||
.build(); |
||||
} |
||||
|
||||
private static ChannelAccount account(Platform platform, int code, String storeId) { |
||||
return ChannelAccount.builder() |
||||
.key(AccountKey.of(ChannelProvider.LINGXING, platform, |
||||
"oms-" + platform.name().toLowerCase())) |
||||
.externalStoreId(storeId == null || storeId.trim().isEmpty() ? "probe" : storeId) |
||||
.platformCode(code == 0 ? null : code) |
||||
.credentialRef("probe") |
||||
.marketplaceZone("America/Los_Angeles") |
||||
.currency("USD") |
||||
.build(); |
||||
} |
||||
|
||||
private static ChannelConnection connection() { |
||||
return ChannelConnection.of(BASE_URL, ChannelSecret.builder() |
||||
.appKey(APP_KEY).appSecret(APP_SECRET).build()); |
||||
} |
||||
|
||||
private static String shorten(String s) { |
||||
if (s == null) { |
||||
return "null"; |
||||
} |
||||
return s.length() <= 40 ? s : s.substring(0, 40) + "…"; |
||||
} |
||||
|
||||
/** 打印请求/响应(截断),只读排查用。 */ |
||||
static final class LoggingTransport implements HttpTransport { |
||||
|
||||
private static final int MAX_BODY = 1200; |
||||
|
||||
private final HttpTransport delegate; |
||||
|
||||
LoggingTransport(HttpTransport delegate) { |
||||
this.delegate = delegate; |
||||
} |
||||
|
||||
@Override |
||||
public HttpResponse execute(HttpRequest request) { |
||||
System.out.println(" --> " + request.method() + " " + pathOf(request.url())); |
||||
System.out.println(" body " + truncate(request.body())); |
||||
HttpResponse response = delegate.execute(request); |
||||
System.out.println(" <-- " + response.statusCode() + " " + truncate(response.body())); |
||||
return response; |
||||
} |
||||
|
||||
private static String pathOf(String url) { |
||||
int i = url.indexOf('/', url.indexOf("://") + 3); |
||||
return i < 0 ? url : url.substring(i); |
||||
} |
||||
|
||||
private static String truncate(String body) { |
||||
if (body == null) { |
||||
return "null"; |
||||
} |
||||
return body.length() <= MAX_BODY ? body : body.substring(0, MAX_BODY) + "...(" + body.length() + " chars)"; |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue