Browse Source
- 新增 CallObserverTest:成功/失败记录、观察者异常隔离、默认 NOOP、UTC 与本地时间转换 - 新增 LingxingCallObserverTest:用 RecordingTransport 走真实 LingxingAdapter, 覆盖成功、限流失败、无平台码三种场景feature/supplier-lingxing
2 changed files with 340 additions and 0 deletions
@ -0,0 +1,157 @@
@@ -0,0 +1,157 @@
|
||||
package common.ecommerce.sdk; |
||||
|
||||
import common.ecommerce.audit.CallObserver; |
||||
import common.ecommerce.audit.CallOutcome; |
||||
import common.ecommerce.audit.CallRecord; |
||||
import common.ecommerce.error.ChannelException; |
||||
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.order.SalesOrder; |
||||
import common.ecommerce.model.order.SalesOrderQuery; |
||||
import common.ecommerce.model.product.ProductQuery; |
||||
import common.ecommerce.model.support.Page; |
||||
import common.ecommerce.spi.ChannelSecret; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import java.time.Instant; |
||||
import java.time.ZoneOffset; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import static java.time.Clock.fixed; |
||||
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||
import static org.junit.jupiter.api.Assertions.assertFalse; |
||||
import static org.junit.jupiter.api.Assertions.assertNotNull; |
||||
import static org.junit.jupiter.api.Assertions.assertThrows; |
||||
import static org.junit.jupiter.api.Assertions.assertTrue; |
||||
|
||||
/** |
||||
* 门面调用日志({@link CallObserver})的契约测试。 |
||||
* |
||||
* <p>不碰网络:用 {@link ChannelSdkTest.FakeDirectAdapter} 与内存账户仓库, |
||||
* 验证「一次调用产生一条记录」「失败也记录」「观察者报错不影响业务」。</p> |
||||
*/ |
||||
class CallObserverTest { |
||||
|
||||
/** 账户的 provider 必须与注册的适配器一致,否则解析不到适配器。 */ |
||||
private static final AccountKey KEY = |
||||
AccountKey.of(ChannelProvider.DIRECT, Platform.WALMART_SC, "oms-wm-001"); |
||||
|
||||
private static final ChannelConnection CONNECTION = ChannelConnection.of( |
||||
"https://api.example.com", |
||||
ChannelSecret.builder().appKey("ak").appSecret("sk").build()); |
||||
|
||||
/** 把回调记录收集到内存里,便于断言。 */ |
||||
private static final class CollectingObserver implements CallObserver { |
||||
|
||||
private final List<CallRecord> records = new ArrayList<>(); |
||||
|
||||
@Override |
||||
public void onCall(CallRecord record) { |
||||
records.add(record); |
||||
} |
||||
} |
||||
|
||||
private static ChannelAccount account() { |
||||
return ChannelAccount.builder() |
||||
.key(KEY) |
||||
.externalStoreId("wm-seller-01") |
||||
.credentialRef("kms://test/wm") |
||||
.marketplaceZone("America/Chicago") |
||||
.currency("USD") |
||||
.build(); |
||||
} |
||||
|
||||
private static ChannelSdk sdk(CallObserver observer) { |
||||
return ChannelSdk.builder() |
||||
.accountRepository(new ChannelSdkTest.FixedAccounts(account())) |
||||
.clock(fixed(Instant.parse("2026-11-05T00:00:00Z"), ZoneOffset.UTC)) |
||||
.register(new ChannelSdkTest.FakeDirectAdapter()) |
||||
.callObserver(observer) |
||||
.build(); |
||||
} |
||||
|
||||
@Test |
||||
void recordsSuccessfulCall() { |
||||
CollectingObserver observer = new CollectingObserver(); |
||||
ChannelSdk sdk = sdk(observer); |
||||
|
||||
Page<SalesOrder> page = sdk.salesOrders(KEY, CONNECTION).pull( |
||||
SalesOrderQuery.builder() |
||||
.updatedAfter(Instant.parse("2026-10-01T00:00:00Z")) |
||||
.pageSize(20) |
||||
.build()); |
||||
|
||||
assertNotNull(page); |
||||
assertEquals(1, observer.records.size(), "一次调用只产生一条记录"); |
||||
|
||||
CallRecord record = observer.records.get(0); |
||||
assertEquals("salesOrders.pull", record.operation()); |
||||
assertEquals(CallOutcome.SUCCESS, record.outcome()); |
||||
assertEquals(ChannelProvider.DIRECT, record.provider()); |
||||
assertEquals(Platform.WALMART_SC, record.platform()); |
||||
assertEquals("oms-wm-001", record.omsStoreId()); |
||||
assertEquals("wm-seller-01", record.externalStoreId()); |
||||
assertEquals(0, record.resultCount(), "FakeDirectAdapter 返回空页"); |
||||
assertNotNull(record.requestId(), "requestId 用于串起 HTTP 日志"); |
||||
assertEquals(20, record.params().get("pageSize")); |
||||
assertFalse(record.failed()); |
||||
assertEquals(0L, record.durationMillis(), "固定时钟下耗时为 0"); |
||||
|
||||
// Instant 本身永远是 UTC(toString 带 Z);本地时间要用带 ZoneId 的重载
|
||||
assertEquals("2026-11-05T00:00:00Z", record.occurredAt().toString()); |
||||
assertEquals("2026-11-05T08:00+08:00", |
||||
record.occurredAt(java.time.ZoneId.of("Asia/Shanghai")) |
||||
.toOffsetDateTime().toString()); |
||||
} |
||||
|
||||
@Test |
||||
void recordsFailedCallAndRethrows() { |
||||
CollectingObserver observer = new CollectingObserver(); |
||||
ChannelSdk sdk = sdk(observer); |
||||
|
||||
// FakeDirectAdapter 没有商品能力:能力检查在门面里失败
|
||||
assertThrows(ChannelException.UnsupportedCapabilityException.class, |
||||
() -> sdk.products(KEY, CONNECTION).pull(ProductQuery.builder().build())); |
||||
|
||||
assertEquals(1, observer.records.size()); |
||||
CallRecord record = observer.records.get(0); |
||||
assertEquals("products.pull", record.operation()); |
||||
assertEquals(CallOutcome.FAILURE, record.outcome()); |
||||
assertTrue(record.failed()); |
||||
assertTrue(record.errorType().contains("UnsupportedCapabilityException"), |
||||
record.errorType()); |
||||
assertNotNull(record.errorMessage()); |
||||
} |
||||
|
||||
@Test |
||||
void observerFailureDoesNotBreakBusinessCall() { |
||||
CallObserver broken = new CallObserver() { |
||||
@Override |
||||
public void onCall(CallRecord record) { |
||||
throw new IllegalStateException("observer down"); |
||||
} |
||||
}; |
||||
ChannelSdk sdk = sdk(broken); |
||||
|
||||
// 观察者抛异常被吞掉,业务结果照常返回
|
||||
Page<SalesOrder> page = sdk.salesOrders(KEY, CONNECTION).pull( |
||||
SalesOrderQuery.builder().updatedAfter(Instant.EPOCH).build()); |
||||
assertNotNull(page); |
||||
} |
||||
|
||||
@Test |
||||
void defaultObserverIsNoop() { |
||||
// 不注入 callObserver 时不应该有任何副作用,也不应该 NPE
|
||||
ChannelSdk sdk = ChannelSdk.builder() |
||||
.accountRepository(new ChannelSdkTest.FixedAccounts(account())) |
||||
.register(new ChannelSdkTest.FakeDirectAdapter()) |
||||
.build(); |
||||
|
||||
Page<SalesOrder> page = sdk.salesOrders(KEY, CONNECTION).pull( |
||||
SalesOrderQuery.builder().updatedAfter(Instant.EPOCH).build()); |
||||
assertEquals(0, page.size()); |
||||
} |
||||
} |
||||
@ -0,0 +1,183 @@
@@ -0,0 +1,183 @@
|
||||
package common.ecommerce.lingxing.adapter; |
||||
|
||||
import common.ecommerce.audit.CallObserver; |
||||
import common.ecommerce.audit.CallOutcome; |
||||
import common.ecommerce.audit.CallRecord; |
||||
import common.ecommerce.error.ChannelException; |
||||
import common.ecommerce.http.JacksonJsonCodec; |
||||
import common.ecommerce.lingxing.LingxingApi; |
||||
import common.ecommerce.lingxing.support.LingxingTestSupport; |
||||
import common.ecommerce.model.ChannelProvider; |
||||
import common.ecommerce.model.Platform; |
||||
import common.ecommerce.model.account.ChannelAccount; |
||||
import common.ecommerce.model.order.SalesOrder; |
||||
import common.ecommerce.model.order.SalesOrderQuery; |
||||
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.time.Instant; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Objects; |
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||
import static org.junit.jupiter.api.Assertions.assertFalse; |
||||
import static org.junit.jupiter.api.Assertions.assertNotNull; |
||||
import static org.junit.jupiter.api.Assertions.assertThrows; |
||||
import static org.junit.jupiter.api.Assertions.assertTrue; |
||||
|
||||
/** |
||||
* 领星 + ChannelSdk 的调用日志测试。 |
||||
* |
||||
* <p>用 {@link RecordingTransport} 替掉网络,走真实的 {@link LingxingAdapter}, |
||||
* 验证操作日志记录的是领星账户的真实链路:provider=LINGXING、platform=领取子平台、 |
||||
* store_id=领星 store_id。</p> |
||||
*/ |
||||
class LingxingCallObserverTest { |
||||
|
||||
private static final String ORDER_JSON = |
||||
"{" |
||||
+ "\"code\":0,\"message\":\"操作成功\"," |
||||
+ "\"data\":{\"total\":1,\"list\":[{" |
||||
+ "\"global_order_no\":\"GO-1\"," |
||||
+ "\"store_id\":\"store-1\"," |
||||
+ "\"amount_currency\":\"USD\"," |
||||
+ "\"status\":6," |
||||
+ "\"global_purchase_time\":1759363200," |
||||
+ "\"item_info\":[{" |
||||
+ "\"global_item_no\":\"GI-1\",\"msku\":\"SKU-1\",\"quantity\":2," |
||||
+ "\"unit_price_amount\":\"9.99\",\"tax_amount\":\"0.00\"," |
||||
+ "\"discount_amount\":\"0.00\"}]," |
||||
+ "\"platform_info\":[{" |
||||
+ "\"platform_code\":\"10008\",\"platform_order_no\":\"PO-1\"," |
||||
+ "\"platform_order_name\":\"PO-1\"," |
||||
+ "\"store_Country_code\":\"US\",\"purchase_time\":1759363200}]," |
||||
+ "\"transaction_info\":[{\"order_total_amount\":\"19.98\"}]" |
||||
+ "}]}" |
||||
+ "}"; |
||||
|
||||
private static final String RATE_LIMITED_JSON = |
||||
"{\"code\":3001008,\"message\":\"请求过于频繁\"}"; |
||||
|
||||
/** 收集回调记录,避免依赖 SLF4J 绑定。 */ |
||||
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 ChannelConnection connection() { |
||||
return ChannelConnection.of( |
||||
LingxingTestSupport.ENDPOINT, |
||||
ChannelSecret.builder() |
||||
.appKey(LingxingTestSupport.APP_KEY) |
||||
.appSecret(LingxingTestSupport.APP_SECRET) |
||||
.build()); |
||||
} |
||||
|
||||
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 SalesOrderQuery query() { |
||||
return SalesOrderQuery.builder() |
||||
.updatedAfter(Instant.parse("2026-10-01T00:00:00Z")) |
||||
.updatedBefore(Instant.parse("2026-10-08T00:00:00Z")) |
||||
.pageSize(20) |
||||
.build(); |
||||
} |
||||
|
||||
@Test |
||||
void recordsSuccessfulLingxingCall() { |
||||
RecordingTransport transport = RecordingTransport.create() |
||||
.route(LingxingApi.ORDER_LIST.path(), r -> Responses.ok(ORDER_JSON)); |
||||
ChannelAccount account = LingxingTestSupport.account(Platform.WALMART_SC, 10008, "store-1"); |
||||
CollectingObserver observer = new CollectingObserver(); |
||||
|
||||
Page<SalesOrder> page = sdk(transport, account, observer) |
||||
.salesOrders(account.key(), connection()).pull(query()); |
||||
|
||||
assertEquals(1, page.size()); |
||||
assertEquals("PO-1", page.items().get(0).channelOrderId()); |
||||
|
||||
assertEquals(1, observer.records.size()); |
||||
CallRecord record = observer.records.get(0); |
||||
assertEquals("salesOrders.pull", record.operation()); |
||||
assertEquals(CallOutcome.SUCCESS, record.outcome()); |
||||
assertEquals(ChannelProvider.LINGXING, record.provider()); |
||||
assertEquals(Platform.WALMART_SC, record.platform()); |
||||
assertEquals(account.omsStoreId(), record.omsStoreId()); |
||||
assertEquals("store-1", record.externalStoreId()); |
||||
assertEquals(1, record.resultCount()); |
||||
assertNotNull(record.requestId()); |
||||
assertEquals(20, record.params().get("pageSize")); |
||||
assertFalse(record.failed()); |
||||
|
||||
// 领星请求确实带了账户的 platform_code + store_id
|
||||
String body = transport.lastDataRequest().body(); |
||||
assertTrue(body.contains("\"platform_code\":[10008]"), body); |
||||
assertTrue(body.contains("\"store_id\":[\"store-1\"]"), body); |
||||
} |
||||
|
||||
@Test |
||||
void recordsFailureWhenLingxingReturnsBusinessError() { |
||||
RecordingTransport transport = RecordingTransport.create() |
||||
.route(LingxingApi.ORDER_LIST.path(), r -> Responses.ok(RATE_LIMITED_JSON)); |
||||
ChannelAccount account = LingxingTestSupport.account(Platform.WALMART_SC, 10008, "store-1"); |
||||
CollectingObserver observer = new CollectingObserver(); |
||||
|
||||
assertThrows(ChannelException.class, |
||||
() -> sdk(transport, account, observer) |
||||
.salesOrders(account.key(), connection()).pull(query())); |
||||
|
||||
assertEquals(1, observer.records.size()); |
||||
CallRecord record = observer.records.get(0); |
||||
assertEquals("salesOrders.pull", record.operation()); |
||||
assertEquals(CallOutcome.FAILURE, record.outcome()); |
||||
assertTrue(record.errorType().contains("RateLimitException"), record.errorType()); |
||||
assertTrue(record.errorMessage().contains("3001008"), record.errorMessage()); |
||||
} |
||||
|
||||
@Test |
||||
void recordsCallWhenAccountHasNoPlatformCode() { |
||||
RecordingTransport transport = RecordingTransport.create() |
||||
.route(LingxingApi.ORDER_LIST.path(), r -> Responses.ok(ORDER_JSON)); |
||||
// platformCode = 0 表示不设置,对应没有平台码的长尾平台
|
||||
ChannelAccount account = LingxingTestSupport.account(Platform.WALMART_SC, 0, "store-1"); |
||||
CollectingObserver observer = new CollectingObserver(); |
||||
|
||||
Page<SalesOrder> page = sdk(transport, account, observer) |
||||
.salesOrders(account.key(), connection()).pull(query()); |
||||
|
||||
assertEquals(1, page.size()); |
||||
assertEquals(1, observer.records.size()); |
||||
assertEquals(CallOutcome.SUCCESS, observer.records.get(0).outcome()); |
||||
|
||||
String body = Objects.requireNonNull(transport.lastDataRequest()).body(); |
||||
assertFalse(body.contains("platform_code"), "没有平台码时不应发送该字段"); |
||||
assertTrue(body.contains("\"store_id\":[\"store-1\"]"), body); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue