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.
399 lines
15 KiB
399 lines
15 KiB
|
2 weeks ago
|
package common.ecommerce.shared.testkit;
|
||
|
3 weeks ago
|
|
||
|
2 weeks ago
|
import common.ecommerce.channel.capability.AfterSaleCapable;
|
||
|
2 weeks ago
|
import common.ecommerce.channel.capability.InboundReceiptCapable;
|
||
|
|
import common.ecommerce.channel.capability.InboundShipmentCapable;
|
||
|
2 weeks ago
|
import common.ecommerce.channel.capability.ProductCapable;
|
||
|
|
import common.ecommerce.channel.capability.SalesOrderCapable;
|
||
|
2 weeks ago
|
import common.ecommerce.shared.error.ChannelException;
|
||
|
2 weeks ago
|
import common.ecommerce.channel.model.account.AccountKey;
|
||
|
2 weeks ago
|
import common.ecommerce.shared.model.Capability;
|
||
|
2 weeks ago
|
import common.ecommerce.channel.model.aftersale.AfterSale;
|
||
|
|
import common.ecommerce.channel.model.aftersale.AfterSaleQuery;
|
||
|
|
import common.ecommerce.channel.model.aftersale.AfterSaleType;
|
||
|
|
import common.ecommerce.channel.model.account.ChannelAccount;
|
||
|
2 weeks ago
|
import common.ecommerce.channel.model.inbound.InboundReceipt;
|
||
|
|
import common.ecommerce.channel.model.inbound.InboundReceiptQuery;
|
||
|
|
import common.ecommerce.channel.model.inbound.InboundShipment;
|
||
|
|
import common.ecommerce.channel.model.inbound.InboundShipmentQuery;
|
||
|
2 weeks ago
|
import common.ecommerce.shared.model.ChannelProvider;
|
||
|
|
import common.ecommerce.shared.support.Money;
|
||
|
2 weeks ago
|
import common.ecommerce.channel.model.order.OrderLine;
|
||
|
2 weeks ago
|
import common.ecommerce.shared.support.Page;
|
||
|
|
import common.ecommerce.shared.model.Platform;
|
||
|
|
import common.ecommerce.shared.support.PlatformTime;
|
||
|
2 weeks ago
|
import common.ecommerce.channel.model.product.ProductListing;
|
||
|
|
import common.ecommerce.channel.model.product.ProductQuery;
|
||
|
|
import common.ecommerce.channel.model.product.ProductStatus;
|
||
|
|
import common.ecommerce.channel.model.product.ProductRef;
|
||
|
|
import common.ecommerce.channel.model.product.ProductSyncStrategy;
|
||
|
|
import common.ecommerce.channel.model.order.SalesOrder;
|
||
|
|
import common.ecommerce.channel.model.order.SalesOrderQuery;
|
||
|
|
import common.ecommerce.channel.model.order.SalesOrderRef;
|
||
|
|
import common.ecommerce.channel.sdk.ChannelConnection;
|
||
|
|
import common.ecommerce.channel.sdk.ChannelContext;
|
||
|
2 weeks ago
|
import common.ecommerce.shared.spi.ChannelSecret;
|
||
|
3 weeks ago
|
|
||
|
|
import java.time.Instant;
|
||
|
|
import java.util.ArrayList;
|
||
|
|
import java.util.Arrays;
|
||
|
|
import java.util.Collections;
|
||
|
|
import java.util.EnumSet;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.Set;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 假直连适配器。用来验证 core 抽象没有被聚合器带偏:
|
||
|
|
* 单平台、能力并集即账户能力、分页靠游标、不依赖网络。
|
||
|
|
*
|
||
|
|
* <pre>
|
||
|
|
* FakeDirectAdapter adapter = FakeDirectAdapter.builder()
|
||
|
|
* .platform(Platform.WALMART_SC)
|
||
|
|
* .orders(3)
|
||
|
|
* .pageSize(2)
|
||
|
|
* .build();
|
||
|
|
* </pre>
|
||
|
|
*/
|
||
|
2 weeks ago
|
public final class FakeDirectAdapter implements SalesOrderCapable, ProductCapable, AfterSaleCapable,
|
||
|
|
InboundShipmentCapable, InboundReceiptCapable {
|
||
|
3 weeks ago
|
|
||
|
|
/** 游标格式:{@code item:序号},仅用于测试分页。 */
|
||
|
|
private static final String CURSOR_PREFIX = "item:";
|
||
|
|
|
||
|
|
private final String adapterId;
|
||
|
|
private final Platform platform;
|
||
|
|
private final EnumSet<Capability> capabilities;
|
||
|
|
private final List<SalesOrder> orders;
|
||
|
|
private final List<ProductListing> products;
|
||
|
3 weeks ago
|
private final List<AfterSale> afterSales;
|
||
|
2 weeks ago
|
private final List<InboundShipment> inboundShipments;
|
||
|
|
private final List<InboundReceipt> inboundReceipts;
|
||
|
3 weeks ago
|
private final int pageSize;
|
||
|
|
private final List<SalesOrderQuery> orderQueries = new ArrayList<SalesOrderQuery>();
|
||
|
|
private final List<ProductQuery> productQueries = new ArrayList<ProductQuery>();
|
||
|
|
private final List<String> writeCalls = new ArrayList<String>();
|
||
|
|
|
||
|
|
private FakeDirectAdapter(Builder b) {
|
||
|
|
this.adapterId = b.adapterId;
|
||
|
|
this.platform = b.platform;
|
||
|
|
this.capabilities = b.capabilities;
|
||
|
|
this.orders = b.orders;
|
||
|
|
this.products = b.products;
|
||
|
3 weeks ago
|
this.afterSales = b.afterSales;
|
||
|
2 weeks ago
|
this.inboundShipments = b.inboundShipments;
|
||
|
|
this.inboundReceipts = b.inboundReceipts;
|
||
|
3 weeks ago
|
this.pageSize = b.pageSize;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static Builder builder() {
|
||
|
|
return new Builder();
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 直连测试账户;聚合器账户只是 provider / platformCode 不同,能力检查逻辑共用。 */
|
||
|
|
public static ChannelAccount account(Platform platform, String omsStoreId) {
|
||
|
|
return ChannelAccount.builder()
|
||
|
|
.key(AccountKey.of(ChannelProvider.DIRECT, platform, omsStoreId))
|
||
|
|
.externalStoreId("fake-seller-01")
|
||
|
|
.credentialRef("kms://test/fake-direct")
|
||
|
|
.marketplaceZone("America/Los_Angeles")
|
||
|
|
.currency("USD")
|
||
|
|
.build();
|
||
|
|
}
|
||
|
|
|
||
|
3 weeks ago
|
/** 测试用连接信息:调用方每次传入的那一份。 */
|
||
|
|
public static ChannelConnection connection() {
|
||
|
|
return ChannelConnection.of("https://fake.example.com",
|
||
|
|
ChannelSecret.builder().appKey("fake-key").appSecret("fake-secret").build());
|
||
|
|
}
|
||
|
|
|
||
|
3 weeks ago
|
@Override
|
||
|
|
public String adapterId() {
|
||
|
|
return adapterId;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public ChannelProvider provider() {
|
||
|
|
return ChannelProvider.DIRECT;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public Set<Platform> platforms() {
|
||
|
|
return EnumSet.of(platform);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public EnumSet<Capability> capabilities() {
|
||
|
|
return EnumSet.copyOf(capabilities);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public Page<SalesOrder> pull(ChannelContext ctx, SalesOrderQuery query) {
|
||
|
|
requireCapability(Capability.SALES_ORDER);
|
||
|
|
orderQueries.add(query);
|
||
|
|
return slice(orders, query.cursor());
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public SalesOrder get(ChannelContext ctx, SalesOrderRef ref) {
|
||
|
|
requireCapability(Capability.SALES_ORDER);
|
||
|
|
for (SalesOrder o : orders) {
|
||
|
|
if (o.channelOrderId().equals(ref.channelOrderId())) {
|
||
|
|
return o;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public ProductSyncStrategy syncStrategy() {
|
||
|
|
return ProductSyncStrategy.FULL_PULL_DIFF;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public Page<ProductListing> pull(ChannelContext ctx, ProductQuery query) {
|
||
|
|
requireCapability(Capability.PRODUCT);
|
||
|
|
productQueries.add(query);
|
||
|
|
return slice(products, query.cursor());
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public ProductListing get(ChannelContext ctx, ProductRef ref) {
|
||
|
|
requireCapability(Capability.PRODUCT);
|
||
|
|
for (ProductListing p : products) {
|
||
|
|
if (p.platformSku().equals(ref.platformSku())) {
|
||
|
|
return p;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
3 weeks ago
|
@Override
|
||
|
|
public Page<AfterSale> pull(ChannelContext ctx, AfterSaleQuery query) {
|
||
|
|
requireCapability(Capability.RETURNS);
|
||
|
|
return slice(afterSales, query.cursor());
|
||
|
|
}
|
||
|
|
|
||
|
2 weeks ago
|
@Override
|
||
|
|
public Page<InboundShipment> pull(ChannelContext ctx, InboundShipmentQuery query) {
|
||
|
|
requireCapability(Capability.INBOUND_SHIPMENT);
|
||
|
|
return slice(inboundShipments, query.cursor());
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public Page<InboundReceipt> pull(ChannelContext ctx, InboundReceiptQuery query) {
|
||
|
|
requireCapability(Capability.INBOUND_RECEIPT);
|
||
|
|
return slice(inboundReceipts, query.cursor());
|
||
|
|
}
|
||
|
|
|
||
|
3 weeks ago
|
/** 模拟一次写操作,如标发;带 IdempotencyGuard 的测试用它确认平台只被真正调用一次。 */
|
||
|
|
public String markShipped(String packageId, String trackingNumber) {
|
||
|
|
writeCalls.add(packageId + ':' + trackingNumber);
|
||
|
|
return "SHIP-" + writeCalls.size();
|
||
|
|
}
|
||
|
|
|
||
|
|
public int writeCallCount() {
|
||
|
|
return writeCalls.size();
|
||
|
|
}
|
||
|
|
|
||
|
|
public List<String> writeCalls() {
|
||
|
|
return Collections.unmodifiableList(writeCalls);
|
||
|
|
}
|
||
|
|
|
||
|
|
public List<SalesOrderQuery> orderQueries() {
|
||
|
|
return Collections.unmodifiableList(orderQueries);
|
||
|
|
}
|
||
|
|
|
||
|
|
public List<ProductQuery> productQueries() {
|
||
|
|
return Collections.unmodifiableList(productQueries);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void resetRecorded() {
|
||
|
|
orderQueries.clear();
|
||
|
|
productQueries.clear();
|
||
|
|
writeCalls.clear();
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---------------------------------------------------------------- internals
|
||
|
|
|
||
|
|
private <T> Page<T> slice(List<T> all, String cursor) {
|
||
|
|
int from = cursorIndex(cursor);
|
||
|
|
int to = Math.min(from + pageSize, all.size());
|
||
|
|
String next = to < all.size() ? CURSOR_PREFIX + to : null;
|
||
|
|
return Page.of(all.subList(from, to), next);
|
||
|
|
}
|
||
|
|
|
||
|
|
private void requireCapability(Capability capability) {
|
||
|
|
if (!capabilities.contains(capability)) {
|
||
|
|
throw new ChannelException.UnsupportedCapabilityException(capability);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private static int cursorIndex(String cursor) {
|
||
|
|
if (cursor == null || cursor.isEmpty()) {
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
if (!cursor.startsWith(CURSOR_PREFIX)) {
|
||
|
|
throw new IllegalArgumentException("bad cursor: " + cursor);
|
||
|
|
}
|
||
|
|
return Integer.parseInt(cursor.substring(CURSOR_PREFIX.length()));
|
||
|
|
}
|
||
|
|
|
||
|
|
private static SalesOrder sampleOrder(Platform platform, String channelOrderId) {
|
||
|
|
return SalesOrder.builder()
|
||
|
|
.omsStoreId("oms-test-001")
|
||
|
|
.channelOrderId(channelOrderId)
|
||
|
|
.status("Created")
|
||
|
|
.buyerName("Fake Buyer")
|
||
|
|
.shipToCountryCode("US")
|
||
|
|
.lines(Collections.singletonList(OrderLine.builder()
|
||
|
|
.lineId(channelOrderId + "-1")
|
||
|
|
.platformSku("SKU-1")
|
||
|
|
.quantity(1)
|
||
|
|
.unitPrice(Money.of("9.99", "USD"))
|
||
|
|
.build()))
|
||
|
|
.totalAmount(Money.of("9.99", "USD"))
|
||
|
|
.occurredAt(PlatformTime.of(Instant.parse("2026-10-26T00:00:00Z"), "America/Los_Angeles"))
|
||
|
|
.sourceProvider(ChannelProvider.DIRECT)
|
||
|
|
.sourceAccountId("fake-seller-01")
|
||
|
|
.build();
|
||
|
|
}
|
||
|
|
|
||
|
2 weeks ago
|
/** 生成一条最小可用的入库货件,单号形如 {@code SHIP-1}。 */
|
||
|
|
private static InboundShipment sampleInboundShipment(Platform platform, String shipmentNo) {
|
||
|
|
return InboundShipment.builder()
|
||
|
|
.omsStoreId("oms-test-001")
|
||
|
|
.sourceProvider(ChannelProvider.DIRECT)
|
||
|
|
.sourceAccountId("fake-seller-01")
|
||
|
|
.shipmentId("ID-" + shipmentNo)
|
||
|
|
.shipmentNo(shipmentNo)
|
||
|
|
.storeId("store-1")
|
||
|
|
.status("WORKING")
|
||
|
|
.statusName("处理中")
|
||
|
|
.createdAt(PlatformTime.of(Instant.parse("2026-10-26T00:00:00Z"), "America/Los_Angeles"))
|
||
|
|
.build();
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 生成一条最小可用的 FBA 接收明细,货件号形如 {@code SHIP-1}。 */
|
||
|
|
private static InboundReceipt sampleInboundReceipt(Platform platform, String shipmentNo) {
|
||
|
|
return InboundReceipt.builder()
|
||
|
|
.omsStoreId("oms-test-001")
|
||
|
|
.sourceProvider(ChannelProvider.DIRECT)
|
||
|
|
.sourceAccountId("fake-seller-01")
|
||
|
|
.shipmentId(shipmentNo)
|
||
|
|
.fnsku("FNSKU-1")
|
||
|
|
.sku("SKU-1")
|
||
|
|
.quantity(1)
|
||
|
|
.fulfillmentCenterId("FC1")
|
||
|
|
.build();
|
||
|
|
}
|
||
|
|
|
||
|
3 weeks ago
|
// ---------------------------------------------------------------- builder
|
||
|
|
|
||
|
|
public static final class Builder {
|
||
|
|
|
||
|
|
private String adapterId = "fake-direct";
|
||
|
|
private Platform platform = Platform.WALMART_SC;
|
||
|
3 weeks ago
|
private EnumSet<Capability> capabilities = EnumSet.of(Capability.SALES_ORDER, Capability.PRODUCT,
|
||
|
2 weeks ago
|
Capability.RETURNS, Capability.INBOUND_SHIPMENT, Capability.INBOUND_RECEIPT);
|
||
|
3 weeks ago
|
private List<SalesOrder> orders = new ArrayList<SalesOrder>();
|
||
|
|
private List<ProductListing> products = new ArrayList<ProductListing>();
|
||
|
3 weeks ago
|
private List<AfterSale> afterSales = new ArrayList<AfterSale>();
|
||
|
2 weeks ago
|
private List<InboundShipment> inboundShipments = new ArrayList<InboundShipment>();
|
||
|
|
private List<InboundReceipt> inboundReceipts = new ArrayList<InboundReceipt>();
|
||
|
3 weeks ago
|
private int pageSize = 100;
|
||
|
|
|
||
|
|
public Builder adapterId(String adapterId) {
|
||
|
|
this.adapterId = adapterId;
|
||
|
|
return this;
|
||
|
|
}
|
||
|
|
|
||
|
|
public Builder platform(Platform platform) {
|
||
|
|
this.platform = platform;
|
||
|
|
return this;
|
||
|
|
}
|
||
|
|
|
||
|
2 weeks ago
|
/** 能力并集必须与实现的接口一致;默认订单 + 商品 + 售后 + 两类入库。 */
|
||
|
3 weeks ago
|
public Builder capabilities(Capability... capabilities) {
|
||
|
|
this.capabilities = capabilities.length == 0
|
||
|
|
? EnumSet.noneOf(Capability.class) : EnumSet.copyOf(Arrays.asList(capabilities));
|
||
|
|
return this;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 生成 count 条顺序订单,单号形如 {@code FAKE-1..count}。 */
|
||
|
|
public Builder orders(int count) {
|
||
|
|
this.orders = new ArrayList<SalesOrder>(count);
|
||
|
|
for (int i = 1; i <= count; i++) {
|
||
|
|
this.orders.add(sampleOrder(platform, "FAKE-" + i));
|
||
|
|
}
|
||
|
|
return this;
|
||
|
|
}
|
||
|
|
|
||
|
|
public Builder products(int count) {
|
||
|
|
this.products = new ArrayList<ProductListing>(count);
|
||
|
|
for (int i = 1; i <= count; i++) {
|
||
|
|
this.products.add(ProductListing.builder()
|
||
|
|
.omsStoreId("oms-test-001")
|
||
|
|
.platformSku("SKU-" + i)
|
||
|
|
.title("Fake product " + i)
|
||
|
3 weeks ago
|
.platform(platform)
|
||
|
|
.status(ProductStatus.ACTIVE)
|
||
|
|
.rawStatus("ACTIVE")
|
||
|
3 weeks ago
|
.sourceProvider(ChannelProvider.DIRECT)
|
||
|
|
.build());
|
||
|
|
}
|
||
|
|
return this;
|
||
|
|
}
|
||
|
|
|
||
|
3 weeks ago
|
/** 生成 count 条售后行,类型在退款/退货/换货之间轮转。 */
|
||
|
|
public Builder afterSales(int count) {
|
||
|
|
this.afterSales = new ArrayList<AfterSale>(count);
|
||
|
|
AfterSaleType[] types = AfterSaleType.values();
|
||
|
|
for (int i = 1; i <= count; i++) {
|
||
|
|
AfterSaleType type = types[(i - 1) % 3];
|
||
|
|
this.afterSales.add(AfterSale.builder()
|
||
|
|
.platform(platform)
|
||
|
|
.omsStoreId("oms-test-001")
|
||
|
|
.afterSaleId("AS-" + i)
|
||
|
|
.channelOrderId("FAKE-" + i)
|
||
|
|
.type(type)
|
||
|
|
.rawType(type.name())
|
||
|
|
.quantity(1)
|
||
|
|
.platformSku("SKU-" + i)
|
||
|
|
.sourceProvider(ChannelProvider.DIRECT)
|
||
|
|
.build());
|
||
|
|
}
|
||
|
|
return this;
|
||
|
|
}
|
||
|
|
|
||
|
2 weeks ago
|
/** 生成 count 条入库货件,单号形如 {@code SHIP-1..count}。 */
|
||
|
|
public Builder inboundShipments(int count) {
|
||
|
|
this.inboundShipments = new ArrayList<InboundShipment>(count);
|
||
|
|
for (int i = 1; i <= count; i++) {
|
||
|
|
this.inboundShipments.add(sampleInboundShipment(platform, "SHIP-" + i));
|
||
|
|
}
|
||
|
|
return this;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 生成 count 条 FBA 接收明细,货件号形如 {@code SHIP-1..count}。 */
|
||
|
|
public Builder inboundReceipts(int count) {
|
||
|
|
this.inboundReceipts = new ArrayList<InboundReceipt>(count);
|
||
|
|
for (int i = 1; i <= count; i++) {
|
||
|
|
this.inboundReceipts.add(sampleInboundReceipt(platform, "SHIP-" + i));
|
||
|
|
}
|
||
|
|
return this;
|
||
|
|
}
|
||
|
|
|
||
|
3 weeks ago
|
public Builder pageSize(int pageSize) {
|
||
|
|
if (pageSize <= 0) {
|
||
|
|
throw new IllegalArgumentException("pageSize must be positive");
|
||
|
|
}
|
||
|
|
this.pageSize = pageSize;
|
||
|
|
return this;
|
||
|
|
}
|
||
|
|
|
||
|
|
public FakeDirectAdapter build() {
|
||
|
|
return new FakeDirectAdapter(this);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|