Browse Source
- AmazonScProductLiveTest:带时间窗与不带时间窗两种拉法 - 顶部常量填 AppID / AppSecret / SID,未填时 Assumptions 跳过 - 打印脱敏请求与响应,便于核对字段映射 Co-Authored-By: Claude <noreply@anthropic.com>feature/supplier-lingxing
1 changed files with 173 additions and 0 deletions
@ -0,0 +1,173 @@ |
|||||||
|
package common.ecommerce.lingxing.platform.amazon; |
||||||
|
|
||||||
|
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.support.Page; |
||||||
|
import common.ecommerce.sdk.ChannelConnection; |
||||||
|
import common.ecommerce.sdk.ChannelContext; |
||||||
|
import common.ecommerce.spi.ChannelSecret; |
||||||
|
import common.ecommerce.testkit.InMemoryLockProvider; |
||||||
|
import common.ecommerce.testkit.InMemoryTokenStore; |
||||||
|
import org.junit.jupiter.api.Test; |
||||||
|
|
||||||
|
import java.time.Clock; |
||||||
|
import java.time.Duration; |
||||||
|
import java.time.Instant; |
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse; |
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull; |
||||||
|
|
||||||
|
/** |
||||||
|
* 领星 · 亚马逊 SC 商品接口真实环境测试。 |
||||||
|
* |
||||||
|
* <p><b>跑之前把下面三个常量填上,然后 IDE 里右键 Run 或执行:</b></p> |
||||||
|
* <pre> |
||||||
|
* mvn -pl lingxing -am test -Dtest=AmazonScProductLiveTest |
||||||
|
* </pre> |
||||||
|
* |
||||||
|
* <p>调用前确认出口 IP 已加入领星白名单。本类只用于联调,不要把真实凭证提交到仓库。</p> |
||||||
|
* |
||||||
|
* <p>亚马逊 Listing 接口令牌桶容量为 1,且按「账户 + path」共享,必须串行调用。</p> |
||||||
|
*/ |
||||||
|
class AmazonScProductLiveTest { |
||||||
|
|
||||||
|
// ==================== 填写这里 ====================
|
||||||
|
/** 领星 ERP 开放接口的 AppID。 */ |
||||||
|
private static final String APP_KEY = "在这里填 AppID"; |
||||||
|
/** 领星 ERP 开放接口的 AppSecret。 */ |
||||||
|
private static final String APP_SECRET = "在这里填 AppSecret"; |
||||||
|
/** 亚马逊店铺的领星 sid(来自店铺列表接口的 sid 字段)。 */ |
||||||
|
private static final String SID = "在这里填 sid"; |
||||||
|
// ================================================
|
||||||
|
|
||||||
|
private static final String BASE_URL = LingxingConstants.PRODUCTION_BASE_URL; |
||||||
|
|
||||||
|
private final JacksonJsonCodec json = new JacksonJsonCodec(); |
||||||
|
private final LoggingTransport transport = |
||||||
|
new LoggingTransport(new OkHttpTransport("common-ecommerce-smoke")); |
||||||
|
|
||||||
|
/** 拉一页亚马逊 Listing,打印请求与解析结果,人工核对字段映射。 */ |
||||||
|
@Test |
||||||
|
void pullsOnePageOfListings() { |
||||||
|
requireConfigured(); |
||||||
|
|
||||||
|
ChannelContext ctx = ChannelContext.of(amazonAccount(), connection(), |
||||||
|
"smoke-" + System.currentTimeMillis(), Clock.systemUTC()); |
||||||
|
|
||||||
|
LingxingAdapter adapter = new LingxingAdapter(transport, json, |
||||||
|
new InMemoryTokenStore(), new InMemoryLockProvider(), Clock.systemUTC()); |
||||||
|
|
||||||
|
Instant to = Instant.now(); |
||||||
|
Instant from = to.minus(Duration.ofDays(7)); |
||||||
|
|
||||||
|
Page<ProductListing> page = adapter.pull(ctx, ProductQuery.builder() |
||||||
|
.updatedAfter(from) |
||||||
|
.updatedBefore(to) |
||||||
|
.pageSize(20) |
||||||
|
.build()); |
||||||
|
|
||||||
|
System.out.println("[listings] window=" + from + " .. " + to |
||||||
|
+ " items=" + page.size() + " nextCursor=" + page.nextCursor()); |
||||||
|
for (ProductListing p : page.items()) { |
||||||
|
System.out.println(" sku=" + p.platformSku() |
||||||
|
+ " title=" + p.title() |
||||||
|
+ " status=" + p.status() |
||||||
|
+ " images=" + p.imageUrls().size() |
||||||
|
+ " updatedAt=" + (p.updatedAt() == null ? null : p.updatedAt().utc())); |
||||||
|
} |
||||||
|
assertFalse(page.size() > 20, "pageSize 未被平台遵守"); |
||||||
|
} |
||||||
|
|
||||||
|
/** 不带时间窗,确认默认分页与解析也能走通。 */ |
||||||
|
@Test |
||||||
|
void pullsFirstPageWithoutTimeWindow() { |
||||||
|
requireConfigured(); |
||||||
|
|
||||||
|
ChannelContext ctx = ChannelContext.of(amazonAccount(), connection(), |
||||||
|
"smoke-" + System.currentTimeMillis(), Clock.systemUTC()); |
||||||
|
|
||||||
|
LingxingAdapter adapter = new LingxingAdapter(transport, json, |
||||||
|
new InMemoryTokenStore(), new InMemoryLockProvider(), Clock.systemUTC()); |
||||||
|
|
||||||
|
Page<ProductListing> page = adapter.pull(ctx, ProductQuery.builder().pageSize(10).build()); |
||||||
|
|
||||||
|
System.out.println("[listings/no-window] items=" + page.size() |
||||||
|
+ " nextCursor=" + page.nextCursor()); |
||||||
|
for (ProductListing p : page.items()) { |
||||||
|
System.out.println(" sku=" + p.platformSku() + " status=" + p.status()); |
||||||
|
} |
||||||
|
// Listing 接口可能返回空列表(店铺没有商品),这里只验证调用与解析没抛异常
|
||||||
|
assertNotNull(page); |
||||||
|
} |
||||||
|
|
||||||
|
// ---------------------------------------------------------------- helpers
|
||||||
|
|
||||||
|
private static void requireConfigured() { |
||||||
|
org.junit.jupiter.api.Assumptions.assumeTrue( |
||||||
|
!APP_KEY.startsWith("在这里填") |
||||||
|
&& !APP_SECRET.startsWith("在这里填") |
||||||
|
&& !SID.startsWith("在这里填"), |
||||||
|
"请先填写 AmazonScProductLiveTest 顶部的 APP_KEY / APP_SECRET / SID"); |
||||||
|
} |
||||||
|
|
||||||
|
private ChannelConnection connection() { |
||||||
|
return ChannelConnection.of(BASE_URL, ChannelSecret.builder() |
||||||
|
.appKey(APP_KEY).appSecret(APP_SECRET).build()); |
||||||
|
} |
||||||
|
|
||||||
|
private ChannelAccount amazonAccount() { |
||||||
|
return ChannelAccount.builder() |
||||||
|
.key(AccountKey.of(ChannelProvider.LINGXING, Platform.AMAZON_SC, "oms-amz-smoke")) |
||||||
|
.externalStoreId(SID) |
||||||
|
.platformCode(10001) |
||||||
|
.credentialRef("smoke") |
||||||
|
.marketplaceZone("America/Los_Angeles") |
||||||
|
.currency("USD") |
||||||
|
.build(); |
||||||
|
} |
||||||
|
|
||||||
|
/** 打印真实请求与响应,签名/字段名不对时靠这个定位。Authorization 只打指纹。 */ |
||||||
|
static final class LoggingTransport implements HttpTransport { |
||||||
|
|
||||||
|
private static final int MAX_BODY = 2000; |
||||||
|
|
||||||
|
private final HttpTransport delegate; |
||||||
|
|
||||||
|
LoggingTransport(HttpTransport delegate) { |
||||||
|
this.delegate = delegate; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public HttpResponse execute(HttpRequest request) { |
||||||
|
System.out.println("--> " + request.method() + " " + request.url()); |
||||||
|
System.out.println(" headers " + mask(request.headers().toString())); |
||||||
|
System.out.println(" body " + truncate(request.body())); |
||||||
|
HttpResponse response = delegate.execute(request); |
||||||
|
System.out.println("<-- " + response.statusCode() + " (" + response.latencyMillis() + " ms) " |
||||||
|
+ truncate(response.body())); |
||||||
|
return response; |
||||||
|
} |
||||||
|
|
||||||
|
private static String mask(String headers) { |
||||||
|
return headers.replaceAll("(?i)(bearer\\s+)[^,\\]]+", "$1***"); |
||||||
|
} |
||||||
|
|
||||||
|
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