You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
319 lines
14 KiB
319 lines
14 KiB
package common.ecommerce.walmart.probe; |
|
|
|
import common.ecommerce.channel.model.aftersale.AfterSale; |
|
import common.ecommerce.channel.model.aftersale.AfterSaleQuery; |
|
import common.ecommerce.channel.model.order.SalesOrder; |
|
import common.ecommerce.channel.model.order.SalesOrderQuery; |
|
import common.ecommerce.channel.model.product.ProductListing; |
|
import common.ecommerce.channel.model.product.ProductQuery; |
|
import common.ecommerce.channel.model.product.ProductSearchField; |
|
import common.ecommerce.channel.sdk.ChannelConnection; |
|
import common.ecommerce.channel.sdk.ChannelContext; |
|
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.shared.error.ChannelException; |
|
import common.ecommerce.shared.spi.ChannelSecret; |
|
import common.ecommerce.shared.support.Page; |
|
import common.ecommerce.shared.testkit.InMemoryLockProvider; |
|
import common.ecommerce.shared.testkit.InMemoryTokenStore; |
|
import common.ecommerce.walmart.adapter.WalmartAdapter; |
|
import common.ecommerce.walmart.support.WalmartTestSupport; |
|
import org.junit.jupiter.api.MethodOrderer; |
|
import org.junit.jupiter.api.Order; |
|
import org.junit.jupiter.api.Test; |
|
import org.junit.jupiter.api.TestInstance; |
|
import org.junit.jupiter.api.TestMethodOrder; |
|
import org.junit.jupiter.api.condition.EnabledIfSystemProperty; |
|
|
|
import java.time.Clock; |
|
import java.time.Instant; |
|
import java.time.LocalDate; |
|
import java.time.ZoneOffset; |
|
import java.util.ArrayList; |
|
import java.util.List; |
|
|
|
import static org.junit.jupiter.api.Assertions.assertTrue; |
|
|
|
/** |
|
* 沃尔玛沙箱探针(<b>只读</b>)。 |
|
* |
|
* <p>默认跳过,用 {@code -Dwalmart.probe=true} 显式开启;凭据走系统属性, |
|
* <b>真实凭据不入库</b>:</p> |
|
* |
|
* <pre> |
|
* mvn -pl channel-sdk-walmart -am -DfailIfNoTests=false \ |
|
* -Dwalmart.probe=true \ |
|
* -Dwalmart.probe.clientId=你的clientId \ |
|
* -Dwalmart.probe.clientSecret=你的clientSecret \ |
|
* -Dtest=WalmartProbeTest test |
|
* </pre> |
|
* |
|
* <p>可选:{@code -Dwalmart.probe.baseUrl=https://marketplace.walmartapis.com} 打生产环境 |
|
* (<b>只跑读接口</b>),{@code -Dwalmart.probe.sku=SKU} 单独查一个商品。</p> |
|
* |
|
* <h2>沙箱已知限制(2026-11-16 实测)</h2> |
|
* <table> |
|
* <tr><th>接口</th><th>沙箱行为</th></tr> |
|
* <tr><td>GET /v3/items</td><td>固定 20 条,limit / sku / nextCursor 过滤不生效</td></tr> |
|
* <tr><td>GET /v3/items/{sku}</td><td>404,mock 未实现</td></tr> |
|
* <tr><td>GET /v3/orders</td><td>固定 12 条,过滤参数是否生效以生产为准</td></tr> |
|
* <tr><td>GET /v3/returns</td><td>固定 10 条 totalCount=30,过滤参数不生效</td></tr> |
|
* </table> |
|
* |
|
* <p>因此探针<b>不把 404 / 固定条数当失败</b>,只打印原始报文,方便逐条核对字段; |
|
* 唯一硬断言是 token 能换到。</p> |
|
*/ |
|
@EnabledIfSystemProperty(named = "walmart.probe", matches = "true") |
|
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
|
class WalmartProbeTest { |
|
|
|
// 真实凭据不入库:本地跑探针时用 -D 传入 |
|
private static final String CLIENT_ID = System.getProperty("walmart.probe.clientId", ""); |
|
private static final String CLIENT_SECRET = System.getProperty("walmart.probe.clientSecret", ""); |
|
private static final String BASE_URL = System.getProperty("walmart.probe.baseUrl", |
|
common.ecommerce.walmart.WalmartConstants.SANDBOX_BASE_URL); |
|
private static final String SKU = System.getProperty("walmart.probe.sku", ""); |
|
|
|
private static final JacksonJsonCodec JSON = new JacksonJsonCodec(); |
|
|
|
/** 打印用;真实 token 不打印,只打印前 12 位指纹。 */ |
|
private final LoggingTransport transport = new LoggingTransport( |
|
new OkHttpTransport("common-ecommerce-walmart-probe")); |
|
|
|
private final WalmartAdapter adapter = new WalmartAdapter(transport, JSON, |
|
new InMemoryTokenStore(), new InMemoryLockProvider(), Clock.systemUTC()); |
|
|
|
/** 最近一次拉到的售后行;详情探针按它取一个 returnOrderId。 */ |
|
private List<AfterSale> lastAfterSales = new ArrayList<AfterSale>(); |
|
|
|
private final ChannelContext ctx = ChannelContext.of( |
|
WalmartTestSupport.account("probe-store"), |
|
ChannelConnection.of(BASE_URL, ChannelSecret.builder() |
|
.clientId(CLIENT_ID).clientSecret(CLIENT_SECRET).build()), |
|
"probe-1", Clock.systemUTC()); |
|
|
|
@Test |
|
@Order(1) |
|
void probeOrderList() { |
|
requireCredentials(); |
|
section("GET /v3/orders createdStartDate=[now-7d, now]"); |
|
Instant to = Instant.now(); |
|
Instant from = to.minusSeconds(7 * 24 * 3600L); |
|
probe(() -> { |
|
Page<SalesOrder> page = adapter.pull(ctx, SalesOrderQuery.builder() |
|
.updatedAfter(from).updatedBefore(to).pageSize(5).build()); |
|
System.out.println("orders=" + page.size() + " nextCursor=" + page.nextCursor()); |
|
for (SalesOrder order : page.items()) { |
|
System.out.println(" " + order.channelOrderId() |
|
+ " status=" + order.status() |
|
+ " buyer=" + order.buyerName() |
|
+ " addr=" + order.shipToAddress() |
|
+ " lines=" + order.lines().size() |
|
+ " total=" + order.totalAmount() |
|
+ " at=" + order.occurredAt()); |
|
order.lines().forEach(line -> System.out.println(" line " + line.lineId() |
|
+ " sku=" + line.platformSku() |
|
+ " qty=" + line.quantity() |
|
+ " status=" + line.status() |
|
+ " unitPrice=" + line.unitPrice() |
|
+ " tax=" + line.tax())); |
|
} |
|
}); |
|
} |
|
|
|
@Test |
|
@Order(2) |
|
void probeOrderDetail() { |
|
requireCredentials(); |
|
List<SalesOrder> orders = ordersOfLastWindow(); |
|
if (orders.isEmpty()) { |
|
System.out.println("skip detail: no order in last 7 days"); |
|
return; |
|
} |
|
String id = orders.get(0).channelOrderId(); |
|
section("GET /v3/orders/" + id); |
|
probe(() -> { |
|
SalesOrder order = adapter.get(ctx, |
|
common.ecommerce.channel.model.order.SalesOrderRef.of(id)); |
|
System.out.println("status=" + order.status() + " addr=" + order.shipToAddress()); |
|
}); |
|
} |
|
|
|
@Test |
|
@Order(3) |
|
void probeItemList() { |
|
requireCredentials(); |
|
section("GET /v3/items?limit=20"); |
|
probe(() -> { |
|
Page<ProductListing> page = adapter.pull(ctx, ProductQuery.builder().pageSize(20).build()); |
|
System.out.println("items=" + page.size() + " nextCursor=" + page.nextCursor()); |
|
for (ProductListing item : page.items()) { |
|
System.out.println(" " + item.platformSku() |
|
+ " status=" + item.status() + "/" + item.rawStatus() |
|
+ " price=" + item.price() |
|
+ " shelf=" + item.attributes().get("shelf") |
|
+ " reasons=" + item.attributes().get("unpublishedReasons")); |
|
} |
|
}); |
|
} |
|
|
|
@Test |
|
@Order(4) |
|
void probeItemSearchAndDetail() { |
|
requireCredentials(); |
|
String sku = SKU.isEmpty() ? "5212572" : SKU; |
|
section("GET /v3/items?sku=" + sku + "(沙箱可能忽略该过滤)"); |
|
probe(() -> { |
|
Page<ProductListing> page = adapter.pull(ctx, ProductQuery.builder() |
|
.searchField(ProductSearchField.PLATFORM_SKU).searchValue(sku).build()); |
|
System.out.println("items=" + page.size() + " (沙箱固定 20 条属正常)"); |
|
}); |
|
|
|
section("GET /v3/items/" + sku + "(沙箱 404 属正常)"); |
|
probe(() -> System.out.println("detail=" + adapter.get(ctx, |
|
common.ecommerce.channel.model.product.ProductRef.of(sku)))); |
|
} |
|
|
|
@Test |
|
@Order(5) |
|
void probeReturns() { |
|
requireCredentials(); |
|
LocalDate today = LocalDate.now(ZoneOffset.UTC); |
|
section("GET /v3/returns createdStartDate=[" + today.minusDays(7) + ", " + today + "]"); |
|
probe(() -> { |
|
Page<AfterSale> page = adapter.pull(ctx, AfterSaleQuery.builder() |
|
.startDate(today.minusDays(7)).endDate(today).pageSize(10).build()); |
|
lastAfterSales = page.items(); |
|
System.out.println("afterSales=" + page.size() + " nextCursor=" + page.nextCursor()); |
|
for (AfterSale a : page.items()) { |
|
System.out.println(" " + a.afterSaleId() |
|
+ " type=" + a.type() + "/" + a.rawType() |
|
+ " status=" + a.status() |
|
+ " qty=" + a.quantity() |
|
+ " refund=" + a.refundAmount() |
|
+ " carrier=" + a.carrier() |
|
+ " tracking=" + a.trackingNumber() |
|
+ " refundStatus=" + a.attributes().get("currentRefundStatus")); |
|
} |
|
}); |
|
} |
|
|
|
@Test |
|
@Order(6) |
|
void probeReturnDetail() { |
|
requireCredentials(); |
|
List<AfterSale> page = lastAfterSales; |
|
if (page.isEmpty()) { |
|
System.out.println("skip return detail: no return in last 7 days (sandbox mock is fixed data)"); |
|
return; |
|
} |
|
String id = String.valueOf(page.get(0).attributes().get("returnOrderId")); |
|
section("GET /v3/returns/" + id + "(沙箱 404 属正常)"); |
|
probe(() -> { |
|
List<AfterSale> lines = adapter.getReturn(ctx, id); |
|
System.out.println("detail afterSales=" + lines.size()); |
|
for (AfterSale a : lines) { |
|
System.out.println(" " + a.afterSaleId() + " status=" + a.status() |
|
+ " refund=" + a.refundAmount()); |
|
} |
|
}); |
|
} |
|
|
|
@Test |
|
@Order(7) |
|
void probeOrderShipping() { |
|
requireCredentials(); |
|
List<SalesOrder> orders = ordersOfLastWindow(); |
|
if (orders.isEmpty()) { |
|
System.out.println("skip shipping: no order in last 7 days"); |
|
return; |
|
} |
|
String id = orders.get(0).channelOrderId(); |
|
section("GET /v3/orders/" + id + "/shipping(沙箱 404 属正常)"); |
|
probe(() -> { |
|
List<common.ecommerce.walmart.mapper.WalmartShipmentMapper.ShipmentLine> lines = |
|
adapter.getShipping(ctx, id); |
|
System.out.println("shipped lines=" + lines.size()); |
|
for (common.ecommerce.walmart.mapper.WalmartShipmentMapper.ShipmentLine line : lines) { |
|
System.out.println(" " + line); |
|
} |
|
}); |
|
} |
|
|
|
// ---------------------------------------------------------------- internals |
|
|
|
/** 探针不因平台 404 / 固定条数失败:打印异常后继续跑下一段。 */ |
|
private static void probe(Runnable action) { |
|
try { |
|
action.run(); |
|
} catch (ChannelException e) { |
|
System.out.println(" !! " + e.getClass().getSimpleName() |
|
+ " code=" + e.platformCode() + " : " + e.getMessage()); |
|
} catch (RuntimeException e) { |
|
System.out.println(" !! " + e.getClass().getSimpleName() + " : " + e.getMessage()); |
|
} |
|
} |
|
|
|
private List<SalesOrder> ordersOfLastWindow() { |
|
List<SalesOrder> out = new ArrayList<SalesOrder>(); |
|
try { |
|
Instant to = Instant.now(); |
|
out.addAll(adapter.pull(ctx, SalesOrderQuery.builder() |
|
.updatedAfter(to.minusSeconds(7 * 24 * 3600L)).updatedBefore(to).build()).items()); |
|
} catch (RuntimeException e) { |
|
System.out.println(" order window unavailable: " + e.getMessage()); |
|
} |
|
return out; |
|
} |
|
|
|
private static void requireCredentials() { |
|
assertTrue(!CLIENT_ID.isEmpty() && !CLIENT_SECRET.isEmpty(), |
|
"set -Dwalmart.probe.clientId=... -Dwalmart.probe.clientSecret=... " |
|
+ "(或用环境变量注入后转成系统属性)"); |
|
} |
|
|
|
private static void section(String title) { |
|
System.out.println("\n========== " + title + " =========="); |
|
} |
|
|
|
/** 打印请求与响应摘要;token 头只打印前缀。 */ |
|
private static final class LoggingTransport implements HttpTransport { |
|
|
|
private final HttpTransport delegate; |
|
|
|
private LoggingTransport(HttpTransport delegate) { |
|
this.delegate = delegate; |
|
} |
|
|
|
@Override |
|
public HttpResponse execute(HttpRequest request) { |
|
System.out.println("-> " + request.method() + " " + request.url()); |
|
System.out.println(" corr=" + request.headers().get("WM_QOS.CORRELATION_ID") |
|
+ " token=" + mask(request.headers().get("WM_SEC.ACCESS_TOKEN")) |
|
+ " count=" + request.headers().get("x-current-token-count")); |
|
HttpResponse response = delegate.execute(request); |
|
System.out.println("<- " + response.statusCode() |
|
+ " tokenCount=" + response.header("x-current-token-count") |
|
+ " body=" + shorten(response.body())); |
|
return response; |
|
} |
|
|
|
private static String mask(String token) { |
|
if (token == null) { |
|
return "null"; |
|
} |
|
return token.length() <= 12 ? "***" : token.substring(0, 12) + "..."; |
|
} |
|
|
|
private static String shorten(String body) { |
|
if (body == null) { |
|
return "null"; |
|
} |
|
return body.length() <= 1_200 ? body : body.substring(0, 1_200) + "...(" + body.length() + " chars)"; |
|
} |
|
} |
|
}
|
|
|