Browse Source
- FakeDirectAdapter 增加入库货件/接收明细能力与 Builder 数据,SdkFacadeCallTest 覆盖门面分页、查询校验与能力缺失 - LingxingInboundCapabilityTest 增加 CallRecord 断言(operation/outcome/resultCount/externalStoreId) - 新增 InboundProbeTest,只读探针核对 FBT/WFS/AWD/FBA 的待核对参数,默认跳过 - LingxingLiveSmokeTest 增加只读入库冒烟,亚马逊 SC 额外拉 FBA 接收明细feature/supplier-lingxing
5 changed files with 557 additions and 6 deletions
@ -0,0 +1,275 @@ |
|||||||
|
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.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.inbound.InboundReceipt; |
||||||
|
import common.ecommerce.model.inbound.InboundReceiptQuery; |
||||||
|
import common.ecommerce.model.inbound.InboundShipment; |
||||||
|
import common.ecommerce.model.inbound.InboundShipmentQuery; |
||||||
|
import common.ecommerce.model.inbound.InboundShipmentTimeType; |
||||||
|
import common.ecommerce.model.support.Page; |
||||||
|
import common.ecommerce.model.support.PlatformTime; |
||||||
|
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 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.Duration; |
||||||
|
import java.time.Instant; |
||||||
|
import java.time.LocalDate; |
||||||
|
import java.time.ZoneId; |
||||||
|
|
||||||
|
/** |
||||||
|
* 入库查询的只读探针:FBT / WFS / AWD 货件与 FBA 接收明细各拉一页,核对出入参。 |
||||||
|
* |
||||||
|
* <p>默认跳过;显式开启:</p> |
||||||
|
* |
||||||
|
* <pre> |
||||||
|
* mvn -q -pl lingxing -am -DfailIfNoTests=false \ |
||||||
|
* -Dlingxing.probe=true \ |
||||||
|
* -Dlingxing.probe.appKey=... -Dlingxing.probe.appSecret=... \ |
||||||
|
* -Dlingxing.probe.tiktokStoreId=... -Dlingxing.probe.walmartStoreId=... \ |
||||||
|
* -Dlingxing.probe.amazonSid=... \ |
||||||
|
* -Dtest=InboundProbeTest test |
||||||
|
* </pre> |
||||||
|
* |
||||||
|
* <p><b>只做查询</b>,不调用任何写接口。异常只打印不抛出,一次跑完四个接口, |
||||||
|
* 方便对照 {@code 待核对} 字段({@code queryType/queryValue}、{@code cargo_status_list}、 |
||||||
|
* {@code sidList}、{@code event_date})的真实报文。</p> |
||||||
|
*/ |
||||||
|
@EnabledIfSystemProperty(named = "lingxing.probe", matches = "true") |
||||||
|
class InboundProbeTest { |
||||||
|
|
||||||
|
// 真实凭据不入库:本地跑探针时用 -D 传入。
|
||||||
|
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 TIKTOK_STORE_ID = System.getProperty("lingxing.probe.tiktokStoreId", ""); |
||||||
|
private static final String WALMART_STORE_ID = System.getProperty("lingxing.probe.walmartStoreId", ""); |
||||||
|
private static final String AMAZON_SID = System.getProperty("lingxing.probe.amazonSid", ""); |
||||||
|
|
||||||
|
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-inbound-probe")); |
||||||
|
private final InMemoryTokenStore tokens = new InMemoryTokenStore(); |
||||||
|
|
||||||
|
/** FBT 货件必须传 TikTok 店铺;没有该店铺 id 时跳过。 */ |
||||||
|
@Test |
||||||
|
void probeFbtCargo() { |
||||||
|
requireCredentials(); |
||||||
|
Assumptions.assumeTrue(!TIKTOK_STORE_ID.trim().isEmpty(), |
||||||
|
"缺少 -Dlingxing.probe.tiktokStoreId,跳过 FBT 探针"); |
||||||
|
|
||||||
|
System.out.println("\n================ FBT cargo / TikTok ================"); |
||||||
|
try { |
||||||
|
ChannelAccount account = account(Platform.TIKTOK, 10011, TIKTOK_STORE_ID); |
||||||
|
InboundShipmentQuery query = InboundShipmentQuery.builder() |
||||||
|
.storeIds(TIKTOK_STORE_ID) |
||||||
|
.timeType(InboundShipmentTimeType.CREATED) |
||||||
|
.after(Instant.now().minus(Duration.ofDays(7))) |
||||||
|
.before(Instant.now()) |
||||||
|
.pageSize(3) |
||||||
|
.build(); |
||||||
|
Page<InboundShipment> page = sdk(account).inbounds(account.key(), connection()).shipments(query); |
||||||
|
printShipments("FBT", page); |
||||||
|
} catch (RuntimeException e) { |
||||||
|
printError(e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** WFS 货件必须传沃尔玛店铺。 */ |
||||||
|
@Test |
||||||
|
void probeWfsCargo() { |
||||||
|
requireCredentials(); |
||||||
|
Assumptions.assumeTrue(!WALMART_STORE_ID.trim().isEmpty(), |
||||||
|
"缺少 -Dlingxing.probe.walmartStoreId,跳过 WFS 探针"); |
||||||
|
|
||||||
|
System.out.println("\n================ WFS cargo / Walmart ================"); |
||||||
|
try { |
||||||
|
ChannelAccount account = account(Platform.WALMART_SC, 10008, WALMART_STORE_ID); |
||||||
|
InboundShipmentQuery query = InboundShipmentQuery.builder() |
||||||
|
.storeIds(WALMART_STORE_ID) |
||||||
|
.timeType(InboundShipmentTimeType.CREATED) |
||||||
|
.after(Instant.now().minus(Duration.ofDays(7))) |
||||||
|
.before(Instant.now()) |
||||||
|
.pageSize(3) |
||||||
|
.build(); |
||||||
|
Page<InboundShipment> page = sdk(account).inbounds(account.key(), connection()).shipments(query); |
||||||
|
printShipments("WFS", page); |
||||||
|
} catch (RuntimeException e) { |
||||||
|
printError(e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** AWD 货件走亚马逊 SC;sid 必须是数值。 */ |
||||||
|
@Test |
||||||
|
void probeAwdCargo() { |
||||||
|
requireCredentials(); |
||||||
|
Assumptions.assumeTrue(!AMAZON_SID.trim().isEmpty(), |
||||||
|
"缺少 -Dlingxing.probe.amazonSid,跳过 AWD 探针"); |
||||||
|
|
||||||
|
System.out.println("\n================ AWD cargo / Amazon SC ================"); |
||||||
|
try { |
||||||
|
ChannelAccount account = account(Platform.AMAZON_SC, 10001, AMAZON_SID); |
||||||
|
InboundShipmentQuery query = InboundShipmentQuery.builder() |
||||||
|
.storeIds(AMAZON_SID) |
||||||
|
.timeType(InboundShipmentTimeType.CREATED) |
||||||
|
.after(Instant.now().minus(Duration.ofDays(30))) |
||||||
|
.before(Instant.now()) |
||||||
|
.pageSize(3) |
||||||
|
.build(); |
||||||
|
Page<InboundShipment> page = sdk(account).inbounds(account.key(), connection()).shipments(query); |
||||||
|
printShipments("AWD", page); |
||||||
|
} catch (RuntimeException e) { |
||||||
|
printError(e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** FBA 接收明细按签收日期取一页;日期使用平台时间(北京时间)。 */ |
||||||
|
@Test |
||||||
|
void probeFbaReceivedInventory() { |
||||||
|
requireCredentials(); |
||||||
|
Assumptions.assumeTrue(!AMAZON_SID.trim().isEmpty(), |
||||||
|
"缺少 -Dlingxing.probe.amazonSid,跳过 FBA 接收明细探针"); |
||||||
|
|
||||||
|
System.out.println("\n================ FBA received inventory / Amazon SC ================"); |
||||||
|
try { |
||||||
|
ChannelAccount account = account(Platform.AMAZON_SC, 10001, AMAZON_SID); |
||||||
|
InboundReceiptQuery query = InboundReceiptQuery.builder() |
||||||
|
.sid(AMAZON_SID) |
||||||
|
.eventDate(yesterdayInBeijing()) |
||||||
|
.pageSize(3) |
||||||
|
.build(); |
||||||
|
Page<InboundReceipt> page = sdk(account).inbounds(account.key(), connection()).receipts(query); |
||||||
|
System.out.println("[result] items=" + page.size() + " nextCursor=" + page.nextCursor() |
||||||
|
+ " eventDate=" + query.eventDate()); |
||||||
|
for (InboundReceipt r : page.items()) { |
||||||
|
System.out.println(" shipmentId=" + r.shipmentId() |
||||||
|
+ " fnsku=" + r.fnsku() |
||||||
|
+ " sku=" + r.sku() |
||||||
|
+ " qty=" + r.quantity() |
||||||
|
+ " fc=" + r.fulfillmentCenterId() |
||||||
|
+ " receivedAt=" + (r.receivedAt() == null ? null : r.receivedAt().utc()) |
||||||
|
+ " uniqueIndex=" + r.uniqueIndex()); |
||||||
|
} |
||||||
|
} catch (RuntimeException e) { |
||||||
|
printError(e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static void printShipments(String label, Page<InboundShipment> page) { |
||||||
|
System.out.println("[result] " + label + " items=" + page.size() + " nextCursor=" + page.nextCursor()); |
||||||
|
for (InboundShipment s : page.items()) { |
||||||
|
System.out.println(" shipmentNo=" + s.shipmentNo() |
||||||
|
+ " shipmentId=" + s.shipmentId() |
||||||
|
+ " inboundOrderNo=" + s.inboundOrderNo() |
||||||
|
+ " status=" + s.status() |
||||||
|
+ " statusName=" + s.statusName() |
||||||
|
+ " storeIds=" + s.storeIds() |
||||||
|
+ " warehouse=" + s.warehouseName() |
||||||
|
+ " createdAt=" + (s.createdAt() == null ? null : s.createdAt().utc()) |
||||||
|
+ " lines=" + s.lines().size() |
||||||
|
+ " shippingRefs=" + s.shippingRefs().size()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static void printError(RuntimeException e) { |
||||||
|
String msg = e.getMessage() == null ? "" : e.getMessage(); |
||||||
|
System.out.println("[ERROR] " + e.getClass().getSimpleName() + ": " + shorten(msg)); |
||||||
|
} |
||||||
|
|
||||||
|
private ChannelSdk sdk(ChannelAccount account) { |
||||||
|
LingxingAdapter adapter = new LingxingAdapter(transport, JSON, tokens, |
||||||
|
new InMemoryLockProvider(), Clock.systemUTC()); |
||||||
|
return ChannelSdk.builder() |
||||||
|
.accountRepository(new InMemoryAccounts().put(account)) |
||||||
|
.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 void requireCredentials() { |
||||||
|
Assumptions.assumeTrue(!APP_KEY.trim().isEmpty() && !APP_SECRET.trim().isEmpty(), |
||||||
|
"未提供 -Dlingxing.probe.appKey / -Dlingxing.probe.appSecret,跳过探针"); |
||||||
|
} |
||||||
|
|
||||||
|
/** 签收日期按北京时间算「昨天」,与领星报表口径一致。 */ |
||||||
|
private static LocalDate yesterdayInBeijing() { |
||||||
|
ZoneId zone = PlatformTime.BEIJING_ZONE; |
||||||
|
return LocalDate.now(zone).minusDays(1); |
||||||
|
} |
||||||
|
|
||||||
|
private static String shorten(String s) { |
||||||
|
if (s == null) { |
||||||
|
return "null"; |
||||||
|
} |
||||||
|
return s.length() <= 200 ? s : s.substring(0, 200) + "…"; |
||||||
|
} |
||||||
|
|
||||||
|
/** 打印请求/响应(截断),只读排查用。 */ |
||||||
|
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() + " " + 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