Browse Source
按要求只测真实接口,删除依赖录制报文的部分: - LingxingAdapterTest / LingxingTokenClientTest / LingxingOrderMapperTest - fixtures/lingxing 下全部夹具 - testkit 的 FakeTransport / Fixtures / Responses 保留 LingxingLiveSmokeTest(未配环境变量整类跳过), 以及不涉及网络用例(Signer / Envelope / RateLimiter / RequestFactory)。 Co-Authored-By: Claude <noreply@anthropic.com>feature/supplier-lingxing
9 changed files with 0 additions and 702 deletions
@ -1,197 +0,0 @@
@@ -1,197 +0,0 @@
|
||||
package common.ecommerce.lingxing; |
||||
|
||||
import common.ecommerce.model.Capability; |
||||
import common.ecommerce.http.JacksonJsonCodec; |
||||
import common.ecommerce.model.account.AccountKey; |
||||
import common.ecommerce.model.account.ChannelAccount; |
||||
import common.ecommerce.model.ChannelProvider; |
||||
import common.ecommerce.model.support.Page; |
||||
import common.ecommerce.model.Platform; |
||||
import common.ecommerce.model.order.SalesOrder; |
||||
import common.ecommerce.model.order.SalesOrderQuery; |
||||
import common.ecommerce.sdk.ChannelConnection; |
||||
import common.ecommerce.sdk.ChannelContext; |
||||
import common.ecommerce.spi.ChannelSecret; |
||||
import common.ecommerce.sync.SyncEngine; |
||||
import common.ecommerce.sync.SyncResult; |
||||
import common.ecommerce.sync.SyncSpec; |
||||
import common.ecommerce.testkit.FakeTransport; |
||||
import common.ecommerce.testkit.Fixtures; |
||||
import common.ecommerce.testkit.InMemoryAccounts; |
||||
import common.ecommerce.testkit.InMemoryCursorStore; |
||||
import common.ecommerce.testkit.InMemoryLockProvider; |
||||
import common.ecommerce.testkit.InMemorySink; |
||||
import common.ecommerce.testkit.InMemoryTokenStore; |
||||
import common.ecommerce.testkit.Responses; |
||||
import org.junit.jupiter.api.BeforeEach; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import java.time.Clock; |
||||
import java.time.Duration; |
||||
import java.time.Instant; |
||||
import java.time.ZoneOffset; |
||||
import java.util.Collections; |
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||
import static org.junit.jupiter.api.Assertions.assertNotNull; |
||||
import static org.junit.jupiter.api.Assertions.assertNull; |
||||
import static org.junit.jupiter.api.Assertions.assertTrue; |
||||
|
||||
/** |
||||
* 领星订单拉取的契约测试:不依赖网络、数据库、Spring。 |
||||
*/ |
||||
class LingxingAdapterTest { |
||||
|
||||
private static final String CREDENTIAL_REF = "kms://test/lingxing/001"; |
||||
|
||||
/** 领星 appId 同时是 AES 密钥,必须 16 字节;测试用假值。 */ |
||||
private static final String APP_KEY = "ak_testappkey123"; |
||||
|
||||
private FakeTransport transport; |
||||
private InMemoryTokenStore tokenStore; |
||||
private InMemoryAccounts accounts; |
||||
private InMemoryLockProvider locks; |
||||
private LingxingAdapter adapter; |
||||
private ChannelAccount account; |
||||
private ChannelContext ctx; |
||||
private ChannelSecret secret; |
||||
|
||||
@BeforeEach |
||||
void setUp() { |
||||
transport = FakeTransport.create(); |
||||
tokenStore = new InMemoryTokenStore(); |
||||
accounts = new InMemoryAccounts(); |
||||
locks = new InMemoryLockProvider(); |
||||
|
||||
account = ChannelAccount.builder() |
||||
.key(AccountKey.of(ChannelProvider.LINGXING, Platform.WALMART_SC, "oms-wm-001")) |
||||
.externalStoreId("1108413237731") |
||||
.platformCode(10008) |
||||
.credentialRef(CREDENTIAL_REF) |
||||
.marketplaceZone("America/Los_Angeles") |
||||
.currency("USD") |
||||
.build(); |
||||
|
||||
secret = ChannelSecret.builder() |
||||
.appKey(APP_KEY) |
||||
.appSecret("test-app-secret") |
||||
.build(); |
||||
|
||||
Clock clock = Clock.fixed(Instant.parse("2026-10-29T00:00:00Z"), ZoneOffset.UTC); |
||||
adapter = new LingxingAdapter(transport, new JacksonJsonCodec(), tokenStore, locks, clock); |
||||
|
||||
ctx = ChannelContext.of(account, ChannelConnection.of("https://openapi.lingxing.com", secret), |
||||
"req-1", clock); |
||||
|
||||
String tokenJson = Fixtures.load(LingxingAdapterTest.class, "lingxing", "token", "success"); |
||||
String orderJson = Fixtures.load(LingxingAdapterTest.class, "lingxing", "orderList", "success"); |
||||
transport.route(LingxingConstants.PATH_ACCESS_TOKEN, "POST", r -> Responses.ok(tokenJson)); |
||||
transport.route(LingxingConstants.PATH_ORDER_LIST, "POST", r -> Responses.ok(orderJson)); |
||||
} |
||||
|
||||
@Test |
||||
void pullsOrdersAndMapsToUnifiedModel() { |
||||
Page<SalesOrder> page = adapter.pull(ctx, SalesOrderQuery.builder() |
||||
.updatedAfter(Instant.parse("2026-10-25T00:00:00Z")) |
||||
.updatedBefore(Instant.parse("2026-10-26T00:00:00Z")) |
||||
.pageSize(100) |
||||
.build()); |
||||
|
||||
assertEquals(3, page.size()); |
||||
assertEquals("PO-123456789", page.items().get(0).channelOrderId()); |
||||
assertEquals(ChannelProvider.LINGXING, page.items().get(0).sourceProvider()); |
||||
// total=3, offset 0 + 3 = 3 → 结束
|
||||
assertNull(page.nextCursor()); |
||||
} |
||||
|
||||
@Test |
||||
void tokenFetchedOnceAndReused() { |
||||
SalesOrderQuery q = SalesOrderQuery.builder() |
||||
.updatedAfter(Instant.parse("2026-10-25T00:00:00Z")) |
||||
.updatedBefore(Instant.parse("2026-10-26T00:00:00Z")) |
||||
.build(); |
||||
adapter.pull(ctx, q); |
||||
adapter.pull(ctx, q); |
||||
|
||||
assertEquals(1, tokenStore.saveCount(), "token should be refreshed only once"); |
||||
assertEquals(1, locks.acquireCount("token-refresh:lingxing:" + APP_KEY)); |
||||
assertNotNull(tokenStore.get("lingxing:" + APP_KEY).orElse(null)); |
||||
} |
||||
|
||||
@Test |
||||
void requestCarriesSignAndBearerToken() { |
||||
adapter.pull(ctx, SalesOrderQuery.builder() |
||||
.updatedAfter(Instant.parse("2026-10-25T00:00:00Z")) |
||||
.updatedBefore(Instant.parse("2026-10-26T00:00:00Z")) |
||||
.build()); |
||||
|
||||
common.ecommerce.http.HttpRequest last = transport.lastRequest(); |
||||
assertTrue(last.url().contains("app_key=" + APP_KEY), last.url()); |
||||
assertTrue(last.url().contains("sign="), last.url()); |
||||
assertEquals("Bearer lx-access-token-abc", last.headers().get("Authorization")); |
||||
assertEquals("req-1", last.headers().get("X-Request-Id")); |
||||
assertTrue(last.body().contains("\"platform_code\":[10008]"), last.body()); |
||||
assertTrue(last.body().contains("\"store_id\":[\"1108413237731\"]"), last.body()); |
||||
} |
||||
|
||||
@Test |
||||
void queryWindowClampedTo31Days() { |
||||
adapter.pull(ctx, SalesOrderQuery.builder() |
||||
.updatedAfter(Instant.parse("2026-01-01T00:00:00Z")) |
||||
.updatedBefore(Instant.parse("2026-10-26T00:00:00Z")) |
||||
.build()); |
||||
|
||||
String body = transport.lastRequest().body(); |
||||
long end = Instant.parse("2026-10-26T00:00:00Z").getEpochSecond(); |
||||
long expectedStart = Instant.parse("2026-10-26T00:00:00Z").minus(Duration.ofDays(31)).getEpochSecond(); |
||||
assertTrue(body.contains("\"end_time\":" + end), body); |
||||
assertTrue(body.contains("\"start_time\":" + expectedStart), body); |
||||
assertTrue(body.contains("\"date_type\":\"update_time\""), body); |
||||
} |
||||
|
||||
@Test |
||||
void syncEngineAdvancesWatermarkOnlyAfterWholeBatch() { |
||||
InMemoryCursorStore cursors = new InMemoryCursorStore(); |
||||
InMemorySink sink = new InMemorySink(); |
||||
SyncSpec spec = SyncSpec.of(ChannelProvider.LINGXING, "oms-wm-001", |
||||
SyncSpec.STREAM_SALES_ORDERS, Duration.ofMinutes(10)); |
||||
cursors.seed(ChannelProvider.LINGXING, "oms-wm-001", SyncSpec.STREAM_SALES_ORDERS, |
||||
Instant.parse("2026-10-25T00:00:00Z")); |
||||
|
||||
Instant to = Instant.parse("2026-10-26T00:00:00Z"); |
||||
SyncResult result = new SyncEngine(cursors, sink).syncSalesOrders(spec, to, |
||||
(from, cursor) -> adapter.pull(ctx, SalesOrderQuery.builder() |
||||
.updatedAfter(from).updatedBefore(to).build())); |
||||
|
||||
assertEquals(1, result.pages()); |
||||
assertEquals(3, result.items()); |
||||
assertTrue(result.watermarkAdvanced()); |
||||
assertEquals(to, cursors.watermark(ChannelProvider.LINGXING, "oms-wm-001", |
||||
SyncSpec.STREAM_SALES_ORDERS)); |
||||
assertEquals(3, sink.salesOrders().size()); |
||||
} |
||||
|
||||
@Test |
||||
void capabilityIsAccountScoped() { |
||||
ChannelAccount ebay = ChannelAccount.builder() |
||||
.key(AccountKey.of(ChannelProvider.LINGXING, Platform.EBAY, "oms-ebay-001")) |
||||
.externalStoreId("1108413237731") |
||||
.platformCode(10003) |
||||
.credentialRef(CREDENTIAL_REF) |
||||
.marketplaceZone("America/New_York") |
||||
.currency("USD") |
||||
.build(); |
||||
|
||||
assertTrue(adapter.supports(account, Capability.SALES_ORDER)); |
||||
assertTrue(adapter.supports(ebay, Capability.SALES_ORDER)); |
||||
// eBay 在领星侧未见账单与退货接口
|
||||
assertEquals(false, adapter.supports(ebay, Capability.BILLING)); |
||||
assertEquals(false, adapter.supports(ebay, Capability.REFUND)); |
||||
} |
||||
|
||||
@Test |
||||
void subChannelsReflectAccount() { |
||||
assertEquals(1, adapter.listSubChannels(account).size()); |
||||
assertEquals(10008, adapter.listSubChannels(account).get(0).platformCode()); |
||||
} |
||||
} |
||||
@ -1,100 +0,0 @@
@@ -1,100 +0,0 @@
|
||||
package common.ecommerce.lingxing; |
||||
|
||||
import common.ecommerce.http.JacksonJsonCodec; |
||||
import common.ecommerce.lingxing.dto.LingxingOrderDto; |
||||
import common.ecommerce.model.ChannelProvider; |
||||
import common.ecommerce.model.Platform; |
||||
import common.ecommerce.model.order.SalesOrder; |
||||
import common.ecommerce.testkit.Fixtures; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import java.time.ZoneId; |
||||
import java.util.List; |
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||
import static org.junit.jupiter.api.Assertions.assertNull; |
||||
import static org.junit.jupiter.api.Assertions.assertThrows; |
||||
|
||||
class LingxingOrderMapperTest { |
||||
|
||||
private final JacksonJsonCodec json = new JacksonJsonCodec(); |
||||
private final LingxingOrderMapper mapper = new LingxingOrderMapper(json); |
||||
|
||||
private List<LingxingOrderDto> fixture() { |
||||
String body = Fixtures.load(LingxingOrderMapperTest.class, "lingxing", "orderList", "success"); |
||||
return json.listAt(body, "data.list", LingxingOrderDto.class); |
||||
} |
||||
|
||||
@Test |
||||
void mapsWalmartOrder() { |
||||
SalesOrder o = mapper.toSalesOrder(fixture().get(0), "oms-wm-001", ZoneId.of("America/Los_Angeles")); |
||||
|
||||
assertEquals("oms-wm-001", o.omsStoreId()); |
||||
assertEquals("PO-123456789", o.channelOrderId()); |
||||
// 领星系统状态原值:6 已发货
|
||||
assertEquals("6", o.status()); |
||||
assertEquals("US", o.shipToCountryCode()); |
||||
assertEquals(2, o.lines().size()); |
||||
assertEquals("SKU-RED-M", o.lines().get(0).platformSku()); |
||||
assertEquals(2, o.lines().get(0).quantity()); |
||||
assertEquals("59.97 USD", o.totalAmount().toString()); |
||||
assertEquals(ChannelProvider.LINGXING, o.sourceProvider()); |
||||
assertEquals("1108413237731", o.sourceAccountId()); |
||||
// epoch 秒 1793001600 → UTC 2026-10-26T08:00:00Z
|
||||
assertEquals(1793001600L, o.occurredAt().utc().getEpochSecond()); |
||||
} |
||||
|
||||
@Test |
||||
void mergedOrderPicksSortedFirstPlatformOrderNo() { |
||||
// 返回顺序是 EBAY-2222, EBAY-1111,取排序后第一个,保证多次同步 key 稳定
|
||||
SalesOrder o = mapper.toSalesOrder(fixture().get(1), "oms-ebay-001", ZoneId.of("America/New_York")); |
||||
assertEquals("EBAY-1111", o.channelOrderId()); |
||||
} |
||||
|
||||
@Test |
||||
void fallsBackToLingxingGlobalOrderNoWhenNoPlatformInfo() { |
||||
SalesOrder o = mapper.toSalesOrder(fixture().get(2), "oms-tts-001", ZoneId.of("Europe/London")); |
||||
assertEquals("LX-1003", o.channelOrderId()); |
||||
assertEquals(0, o.lines().size()); |
||||
assertNull(o.shipToCountryCode()); |
||||
} |
||||
|
||||
@Test |
||||
void stripsCurrencySymbolFromAmounts() { |
||||
// 真实响应里 order_total_amount 可能是 "¥15.00"
|
||||
SalesOrder o = mapper.toSalesOrder(fixture().get(1), "oms-ebay-001", ZoneId.of("America/New_York")); |
||||
assertEquals("15.00 USD", o.totalAmount().toString()); |
||||
} |
||||
|
||||
@Test |
||||
void blankAmountBecomesNull() { |
||||
SalesOrder o = mapper.toSalesOrder(fixture().get(1), "oms-ebay-001", ZoneId.of("America/New_York")); |
||||
// tax_amount 是空串,不能变成 0 或抛异常
|
||||
assertNull(o.lines().get(0).tax()); |
||||
} |
||||
|
||||
@Test |
||||
void cleanAmountHandlesSymbolsCommasAndSigns() { |
||||
assertEquals("49.16", LingxingOrderMapper.cleanAmount("¥49.16")); |
||||
assertEquals("-0.98", LingxingOrderMapper.cleanAmount("¥-0.98")); |
||||
assertEquals("1234.56", LingxingOrderMapper.cleanAmount("1,234.56")); |
||||
assertEquals("14.00", LingxingOrderMapper.cleanAmount("JP¥14.00")); |
||||
assertNull(LingxingOrderMapper.cleanAmount("")); |
||||
assertNull(LingxingOrderMapper.cleanAmount("¥")); |
||||
assertNull(LingxingOrderMapper.cleanAmount(null)); |
||||
} |
||||
@Test |
||||
void missingBothOrderNumbersFailsLoudly() { |
||||
LingxingOrderDto dto = new LingxingOrderDto(); |
||||
assertThrows(IllegalArgumentException.class, () -> LingxingOrderMapper.resolveChannelOrderId(dto)); |
||||
} |
||||
|
||||
@Test |
||||
void platformCodeMapsToUnifiedPlatform() { |
||||
assertEquals(Platform.WALMART_SC, LingxingConstants.toPlatform(10008)); |
||||
assertEquals(Platform.EBAY, LingxingConstants.toPlatform(10003)); |
||||
assertEquals(Platform.SHEIN, LingxingConstants.toPlatform(10027)); |
||||
assertEquals(Platform.CUSTOM, LingxingConstants.toPlatform(99999)); |
||||
assertNull(LingxingConstants.toPlatform(null)); |
||||
} |
||||
} |
||||
@ -1,95 +0,0 @@
@@ -1,95 +0,0 @@
|
||||
package common.ecommerce.lingxing; |
||||
|
||||
import common.ecommerce.error.AuthException; |
||||
import common.ecommerce.http.HttpRequest; |
||||
import common.ecommerce.http.JacksonJsonCodec; |
||||
import common.ecommerce.spi.AccessToken; |
||||
import common.ecommerce.testkit.FakeTransport; |
||||
import common.ecommerce.testkit.Fixtures; |
||||
import common.ecommerce.testkit.Responses; |
||||
import org.junit.jupiter.api.BeforeEach; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import java.time.Clock; |
||||
import java.time.Instant; |
||||
import java.time.ZoneOffset; |
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||
import static org.junit.jupiter.api.Assertions.assertThrows; |
||||
import static org.junit.jupiter.api.Assertions.assertTrue; |
||||
|
||||
/** |
||||
* 领星令牌接口契约测试:按官方文档核对出入参。 |
||||
*/ |
||||
class LingxingTokenClientTest { |
||||
|
||||
private static final String BASE_URL = LingxingConstants.PRODUCTION_BASE_URL; |
||||
private static final Clock CLOCK = Clock.fixed(Instant.parse("2026-10-30T00:00:00Z"), ZoneOffset.UTC); |
||||
|
||||
private FakeTransport transport; |
||||
private LingxingTokenClient client; |
||||
|
||||
@BeforeEach |
||||
void setUp() { |
||||
transport = FakeTransport.create(); |
||||
client = new LingxingTokenClient(transport, new JacksonJsonCodec(), CLOCK); |
||||
} |
||||
|
||||
@Test |
||||
void requestUsesMultipartFormWithAppIdAndSecret() { |
||||
transport.route(LingxingConstants.PATH_ACCESS_TOKEN, "POST", |
||||
r -> Responses.ok(Fixtures.load(getClass(), "lingxing", "token", "success"))); |
||||
|
||||
client.fetch(BASE_URL, "my-app-id", "my-secret"); |
||||
|
||||
HttpRequest req = transport.lastRequest(); |
||||
assertEquals("POST", req.method()); |
||||
assertTrue(req.url().endsWith(LingxingConstants.PATH_ACCESS_TOKEN), req.url()); |
||||
assertTrue(req.contentType().startsWith("multipart/form-data"), req.contentType()); |
||||
assertTrue(req.body().contains("name=\"appId\"\r\n\r\nmy-app-id"), req.body()); |
||||
assertTrue(req.body().contains("name=\"appSecret\"\r\n\r\nmy-secret"), req.body()); |
||||
} |
||||
|
||||
@Test |
||||
void parsesTokenFromDataObject() { |
||||
transport.route(LingxingConstants.PATH_ACCESS_TOKEN, "POST", |
||||
r -> Responses.ok(Fixtures.load(getClass(), "lingxing", "token", "success"))); |
||||
|
||||
AccessToken token = client.fetch(BASE_URL, "id", "secret"); |
||||
|
||||
assertEquals("lx-access-token-abc", token.value()); |
||||
assertEquals("lx-refresh-token-xyz", token.refreshToken()); |
||||
assertEquals("Bearer", token.tokenType()); |
||||
// expires_in=7199 秒,基于注入时钟计算
|
||||
assertEquals(Instant.parse("2026-10-30T01:59:59Z"), token.expiresAt()); |
||||
} |
||||
|
||||
@Test |
||||
void businessErrorCodeThrowsAuthException() { |
||||
transport.route(LingxingConstants.PATH_ACCESS_TOKEN, "POST", |
||||
r -> Responses.ok(Fixtures.load(getClass(), "lingxing", "token", "auth_failed"))); |
||||
|
||||
AuthException ex = assertThrows(AuthException.class, () -> client.fetch(BASE_URL, "id", "bad")); |
||||
assertTrue(ex.getMessage().contains("2001001"), ex.getMessage()); |
||||
assertTrue(ex.getMessage().contains("app not exist"), ex.getMessage()); |
||||
assertEquals("2001001", ex.platformCode()); |
||||
} |
||||
|
||||
@Test |
||||
void missingTokenThrowsInsteadOfNpe() { |
||||
transport.route(LingxingConstants.PATH_ACCESS_TOKEN, "POST", |
||||
r -> Responses.ok("{\"code\":\"200\",\"msg\":\"OK\",\"data\":{}}")); |
||||
|
||||
AuthException ex = assertThrows(AuthException.class, () -> client.fetch(BASE_URL, "id", "secret")); |
||||
assertTrue(ex.getMessage().contains("data.access_token"), ex.getMessage()); |
||||
} |
||||
|
||||
@Test |
||||
void httpErrorMapsToException() { |
||||
transport.route(LingxingConstants.PATH_ACCESS_TOKEN, "POST", |
||||
r -> Responses.status(500, "boom")); |
||||
|
||||
assertThrows(common.ecommerce.error.TransientException.class, |
||||
() -> client.fetch(BASE_URL, "id", "secret")); |
||||
} |
||||
} |
||||
@ -1,150 +0,0 @@
@@ -1,150 +0,0 @@
|
||||
{ |
||||
"code": 0, |
||||
"message": "操作成功", |
||||
"request_id": "351a5dcef3da43ebaca27bfeec606a9b.1793586299739", |
||||
"response_time": "2026-11-02 10:24:59", |
||||
"data": { |
||||
"total": "3", |
||||
"list": [ |
||||
{ |
||||
"global_order_no": "103351413752115712", |
||||
"original_global_order_no": null, |
||||
"reference_no": "1", |
||||
"store_id": "1108413237731", |
||||
"amount_currency": "USD", |
||||
"status": 6, |
||||
"delivery_type": 2, |
||||
"update_time": "1793088000", |
||||
"global_purchase_time": 1793001600, |
||||
"global_delivery_time": 0, |
||||
"item_info": [ |
||||
{ |
||||
"id": "8745387", |
||||
"global_item_no": "ITEM-1", |
||||
"order_item_no": "ITEM-1", |
||||
"msku": "SKU-RED-M", |
||||
"local_sku": "", |
||||
"product_no": "", |
||||
"local_product_name": "Red Shirt", |
||||
"quantity": 2, |
||||
"unit_price_amount": "19.99", |
||||
"item_price_amount": "39.98", |
||||
"discount_amount": "0.00", |
||||
"tax_amount": "1.60", |
||||
"platform_order_no": "PO-123456789" |
||||
}, |
||||
{ |
||||
"id": "8745388", |
||||
"global_item_no": "ITEM-2", |
||||
"order_item_no": "ITEM-2", |
||||
"msku": "SKU-BLUE-L", |
||||
"local_sku": "", |
||||
"product_no": "", |
||||
"local_product_name": "Blue Shirt", |
||||
"quantity": 1, |
||||
"unit_price_amount": "19.99", |
||||
"item_price_amount": "19.99", |
||||
"discount_amount": "0.00", |
||||
"tax_amount": "0.80", |
||||
"platform_order_no": "PO-123456789" |
||||
} |
||||
], |
||||
"platform_info": [ |
||||
{ |
||||
"order_from": "线上订单", |
||||
"platform_order_no": "PO-123456789", |
||||
"platform_order_name": "PO-123456789", |
||||
"platform_code": "10008", |
||||
"store_Country_code": "US", |
||||
"status": "Shipped", |
||||
"payment_status": "paid", |
||||
"shipping_status": "fulfilled", |
||||
"purchase_time": 1793001600, |
||||
"delivery_time": 0 |
||||
} |
||||
], |
||||
"transaction_info": [ |
||||
{ |
||||
"order_item_amount": "59.97", |
||||
"discount_amount": "0.00", |
||||
"order_total_amount": "59.97" |
||||
} |
||||
] |
||||
}, |
||||
{ |
||||
"global_order_no": "103351413752115713", |
||||
"store_id": "1108413237731", |
||||
"amount_currency": "USD", |
||||
"status": 4, |
||||
"delivery_type": 2, |
||||
"update_time": "1793088000", |
||||
"global_purchase_time": 1793002000, |
||||
"item_info": [ |
||||
{ |
||||
"id": "8745390", |
||||
"global_item_no": "ITEM-3", |
||||
"order_item_no": "ITEM-3", |
||||
"msku": "MSKU-GREEN-S", |
||||
"local_sku": "", |
||||
"product_no": "", |
||||
"quantity": 1, |
||||
"unit_price_amount": "15.00", |
||||
"item_price_amount": "15.00", |
||||
"discount_amount": "0.00", |
||||
"tax_amount": "", |
||||
"platform_order_no": "EBAY-2222" |
||||
} |
||||
], |
||||
"platform_info": [ |
||||
{ |
||||
"platform_order_no": "EBAY-2222", |
||||
"platform_order_name": "EBAY-2222", |
||||
"platform_code": "10003", |
||||
"store_Country_code": "US", |
||||
"status": "Paid", |
||||
"payment_status": "paid", |
||||
"shipping_status": "", |
||||
"purchase_time": 1793002000, |
||||
"delivery_time": 0 |
||||
}, |
||||
{ |
||||
"platform_order_no": "EBAY-1111", |
||||
"platform_order_name": "EBAY-1111", |
||||
"platform_code": "10003", |
||||
"store_Country_code": "US", |
||||
"status": "Paid", |
||||
"payment_status": "paid", |
||||
"shipping_status": "", |
||||
"purchase_time": 1793002000, |
||||
"delivery_time": 0 |
||||
} |
||||
], |
||||
"transaction_info": [ |
||||
{ |
||||
"order_item_amount": "¥15.00", |
||||
"discount_amount": "¥0.00", |
||||
"order_total_amount": "¥15.00" |
||||
} |
||||
] |
||||
}, |
||||
{ |
||||
"global_order_no": "LX-1003", |
||||
"store_id": "1108413237731", |
||||
"amount_currency": "GBP", |
||||
"status": 3, |
||||
"delivery_type": 2, |
||||
"update_time": "1793088000", |
||||
"global_purchase_time": 1793003000, |
||||
"item_info": [], |
||||
"platform_info": [], |
||||
"transaction_info": [ |
||||
{ |
||||
"order_item_amount": "10.00", |
||||
"discount_amount": "0.00", |
||||
"order_total_amount": "10.00" |
||||
} |
||||
] |
||||
} |
||||
] |
||||
} |
||||
} |
||||
@ -1,5 +0,0 @@
@@ -1,5 +0,0 @@
|
||||
{ |
||||
"code": "2001001", |
||||
"msg": "app not exist", |
||||
"data": null |
||||
} |
||||
@ -1,9 +0,0 @@
@@ -1,9 +0,0 @@
|
||||
{ |
||||
"code": "200", |
||||
"msg": "OK", |
||||
"data": { |
||||
"access_token": "lx-access-token-abc", |
||||
"refresh_token": "lx-refresh-token-xyz", |
||||
"expires_in": 7199 |
||||
} |
||||
} |
||||
@ -1,85 +0,0 @@
@@ -1,85 +0,0 @@
|
||||
package common.ecommerce.testkit; |
||||
|
||||
import common.ecommerce.http.HttpRequest; |
||||
import common.ecommerce.http.HttpResponse; |
||||
import common.ecommerce.http.HttpTransport; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
import java.util.function.Function; |
||||
|
||||
/** |
||||
* 可编程传输。适配器单测直接返回录制的响应,不需要网络或 mock server。 |
||||
* |
||||
* <pre> |
||||
* FakeTransport t = FakeTransport.create() |
||||
* .route("lingxing", "order/v2/list", body -> HttpResponse.of(200, null, json, 5)); |
||||
* </pre> |
||||
*/ |
||||
public final class FakeTransport implements HttpTransport { |
||||
|
||||
private final List<HttpRequest> recorded = new ArrayList<HttpRequest>(); |
||||
private final List<Route> routes = new ArrayList<Route>(); |
||||
private Function<HttpRequest, HttpResponse> fallback; |
||||
|
||||
public static FakeTransport create() { |
||||
return new FakeTransport(); |
||||
} |
||||
|
||||
/** 按 URL 子串 + 方法匹配。 */ |
||||
public FakeTransport route(String urlContains, String method, Function<HttpRequest, HttpResponse> responder) { |
||||
routes.add(new Route(urlContains, method, responder)); |
||||
return this; |
||||
} |
||||
|
||||
public FakeTransport route(String urlContains, Function<HttpRequest, HttpResponse> responder) { |
||||
return route(urlContains, null, responder); |
||||
} |
||||
|
||||
public FakeTransport fallback(Function<HttpRequest, HttpResponse> responder) { |
||||
this.fallback = responder; |
||||
return this; |
||||
} |
||||
|
||||
@Override |
||||
public HttpResponse execute(HttpRequest request) { |
||||
recorded.add(request); |
||||
for (Route r : routes) { |
||||
if (request.url().contains(r.urlContains) |
||||
&& (r.method == null || r.method.equalsIgnoreCase(request.method()))) { |
||||
return r.responder.apply(request); |
||||
} |
||||
} |
||||
if (fallback != null) { |
||||
return fallback.apply(request); |
||||
} |
||||
throw new IllegalStateException("no fake route for " + request.method() + " " + request.url()); |
||||
} |
||||
|
||||
public List<HttpRequest> recorded() { |
||||
return Collections.unmodifiableList(recorded); |
||||
} |
||||
|
||||
/** 最后一次请求;无请求时返回 null。 */ |
||||
public HttpRequest lastRequest() { |
||||
return recorded.isEmpty() ? null : recorded.get(recorded.size() - 1); |
||||
} |
||||
|
||||
public void clear() { |
||||
recorded.clear(); |
||||
} |
||||
|
||||
private static final class Route { |
||||
|
||||
private final String urlContains; |
||||
private final String method; |
||||
private final Function<HttpRequest, HttpResponse> responder; |
||||
|
||||
private Route(String urlContains, String method, Function<HttpRequest, HttpResponse> responder) { |
||||
this.urlContains = urlContains; |
||||
this.method = method; |
||||
this.responder = responder; |
||||
} |
||||
} |
||||
} |
||||
@ -1,36 +0,0 @@
@@ -1,36 +0,0 @@
|
||||
package common.ecommerce.testkit; |
||||
|
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.nio.charset.StandardCharsets; |
||||
import java.util.Scanner; |
||||
|
||||
/** |
||||
* 夹具加载器。 |
||||
* |
||||
* <p>约定目录:{@code src/test/resources/fixtures/{platform}/{operation}/{case}.json}。 |
||||
* 适配器测试只依赖录制报文,不依赖网络。</p> |
||||
*/ |
||||
public final class Fixtures { |
||||
|
||||
private Fixtures() { |
||||
} |
||||
|
||||
public static String load(Class<?> anchor, String path) { |
||||
String resource = path.startsWith("/") ? path : "/" + path; |
||||
try (InputStream in = anchor.getResourceAsStream(resource)) { |
||||
if (in == null) { |
||||
throw new IllegalStateException("fixture not found: " + resource); |
||||
} |
||||
Scanner scanner = new Scanner(in, StandardCharsets.UTF_8.name()).useDelimiter("\\A"); |
||||
return scanner.hasNext() ? scanner.next() : ""; |
||||
} catch (IOException e) { |
||||
throw new IllegalStateException("read fixture failed: " + resource, e); |
||||
} |
||||
} |
||||
|
||||
/** 按约定拼路径。 */ |
||||
public static String load(Class<?> anchor, String platform, String operation, String caseName) { |
||||
return load(anchor, "fixtures/" + platform + "/" + operation + "/" + caseName + ".json"); |
||||
} |
||||
} |
||||
@ -1,25 +0,0 @@
@@ -1,25 +0,0 @@
|
||||
package common.ecommerce.testkit; |
||||
|
||||
import common.ecommerce.http.HttpResponse; |
||||
|
||||
import java.util.Collections; |
||||
|
||||
/** 快捷构造响应。 */ |
||||
public final class Responses { |
||||
|
||||
private Responses() { |
||||
} |
||||
|
||||
public static HttpResponse ok(String body) { |
||||
return HttpResponse.of(200, Collections.<String, String>emptyMap(), body, 1); |
||||
} |
||||
|
||||
public static HttpResponse status(int code, String body) { |
||||
return HttpResponse.of(code, Collections.<String, String>emptyMap(), body, 1); |
||||
} |
||||
|
||||
public static HttpResponse rateLimited(String retryAfterSeconds) { |
||||
return HttpResponse.of(429, |
||||
Collections.singletonMap("Retry-After", retryAfterSeconds), "", 1); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue