110 changed files with 2992 additions and 682 deletions
@ -1,58 +0,0 @@
@@ -1,58 +0,0 @@
|
||||
package common.ecommerce.lingxing.platform.shopify; |
||||
|
||||
import common.ecommerce.http.HttpTransport; |
||||
import common.ecommerce.http.JacksonJsonCodec; |
||||
import common.ecommerce.lingxing.platform.LingxingSubPlatform; |
||||
import common.ecommerce.lingxing.platform.LingxingOrderSource; |
||||
import common.ecommerce.lingxing.platform.LingxingProductSource; |
||||
import common.ecommerce.lingxing.platform.LingxingShopSource; |
||||
import common.ecommerce.lingxing.platform.multiplatform.order.MultiPlatformOrderSource; |
||||
import common.ecommerce.lingxing.platform.multiplatform.outbound.MultiPlatformOutboundSource; |
||||
import common.ecommerce.lingxing.platform.multiplatform.shop.MultiPlatformShopSource; |
||||
import common.ecommerce.lingxing.platform.LingxingOutboundSource; |
||||
import common.ecommerce.lingxing.platform.shopify.product.ShopifyProductSource; |
||||
import common.ecommerce.model.Platform; |
||||
|
||||
/** |
||||
* 领星 · Shopify 子平台:只做能力组合。 |
||||
*/ |
||||
public final class ShopifySubPlatform implements LingxingSubPlatform { |
||||
|
||||
/** 快速出库是多平台通用接口,各子平台共用。 */ |
||||
private static final MultiPlatformOutboundSource OUTBOUND = new MultiPlatformOutboundSource(); |
||||
|
||||
private final LingxingOrderSource orders; |
||||
private final LingxingProductSource products; |
||||
private final LingxingShopSource shops; |
||||
|
||||
public ShopifySubPlatform(HttpTransport transport, JacksonJsonCodec json) { |
||||
this.orders = new MultiPlatformOrderSource(transport, json); |
||||
this.products = new ShopifyProductSource(); |
||||
this.shops = new MultiPlatformShopSource(Platform.SHOPIFY); |
||||
} |
||||
|
||||
@Override |
||||
public Platform platform() { |
||||
return Platform.SHOPIFY; |
||||
} |
||||
|
||||
@Override |
||||
public LingxingOrderSource orderSource() { |
||||
return orders; |
||||
} |
||||
|
||||
@Override |
||||
public LingxingProductSource productSource() { |
||||
return products; |
||||
} |
||||
|
||||
@Override |
||||
public LingxingShopSource shopSource() { |
||||
return shops; |
||||
} |
||||
|
||||
@Override |
||||
public LingxingOutboundSource outboundSource() { |
||||
return OUTBOUND; |
||||
} |
||||
} |
||||
@ -1,166 +0,0 @@
@@ -1,166 +0,0 @@
|
||||
package common.ecommerce.lingxing.platform.shopify.dto; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
import lombok.Getter; |
||||
import lombok.Setter; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 领星 · Shopify 在线商品({@code /basicOpen/multiplatform/shopify/variantList} 的 {@code data.list[]})。 |
||||
* |
||||
* <p>文档字段是 snake_case;这里用 camelCase 的 Java 字段 + {@code @JsonProperty} 映射。</p> |
||||
* |
||||
* <p>Shopify 按<b>变体(variant)</b>返回,{@code msku} 是平台侧 SKU。</p> |
||||
*/ |
||||
@Getter |
||||
@Setter |
||||
@JsonIgnoreProperties(ignoreUnknown = true) |
||||
public class ShopifyListingDto { |
||||
|
||||
/** 商品 ID。 */ |
||||
@JsonProperty("item_id") |
||||
private String itemId; |
||||
|
||||
/** 商品链接。 */ |
||||
@JsonProperty("item_url") |
||||
private String itemUrl; |
||||
|
||||
/** 产品 uid。 */ |
||||
@JsonProperty("product_unique_id") |
||||
private String productUniqueId; |
||||
|
||||
/** 子体 uid。 */ |
||||
@JsonProperty("variant_unique_id") |
||||
private String variantUniqueId; |
||||
|
||||
/** 企业 ID。 */ |
||||
@JsonProperty("company_id") |
||||
private String companyId; |
||||
|
||||
/** 父图链接。 */ |
||||
@JsonProperty("parent_picture_url") |
||||
private String parentPictureUrl; |
||||
|
||||
/** 图片链接。 */ |
||||
@JsonProperty("picture_url") |
||||
private String pictureUrl; |
||||
|
||||
/** 父 MSKU。 */ |
||||
@JsonProperty("parent_msku") |
||||
private String parentMsku; |
||||
|
||||
/** 平台侧 MSKU,参与 canonicalKey。 */ |
||||
@JsonProperty("msku") |
||||
private String msku; |
||||
|
||||
/** 母体属性,如 Size/Color。 */ |
||||
@JsonProperty("parent_attribute") |
||||
private String parentAttribute; |
||||
|
||||
/** 变体属性,如 42 / red。 */ |
||||
@JsonProperty("attribute") |
||||
private String attribute; |
||||
|
||||
/** 标题。 */ |
||||
@JsonProperty("title") |
||||
private String title; |
||||
|
||||
/** 本地 SKU。 */ |
||||
@JsonProperty("local_sku") |
||||
private String localSku; |
||||
|
||||
/** 本地商品 id。 */ |
||||
@JsonProperty("pid") |
||||
private String pid; |
||||
|
||||
/** 本地品名。 */ |
||||
@JsonProperty("local_name") |
||||
private String localName; |
||||
|
||||
/** 店铺 id。 */ |
||||
@JsonProperty("store_id") |
||||
private String storeId; |
||||
|
||||
/** 店铺名称。 */ |
||||
@JsonProperty("store_name") |
||||
private String storeName; |
||||
|
||||
/** 售价。 */ |
||||
@JsonProperty("price") |
||||
private String price; |
||||
|
||||
/** 库存。 */ |
||||
@JsonProperty("quantity") |
||||
private String quantity; |
||||
|
||||
/** 货币符号,如 ¥。 */ |
||||
@JsonProperty("currency_icon") |
||||
private String currencyIcon; |
||||
|
||||
/** 是否有子体。 */ |
||||
@JsonProperty("has_variant") |
||||
private Boolean hasVariant; |
||||
|
||||
/** 开始时间,Y-m-d H:i:s。 */ |
||||
@JsonProperty("listing_start_time") |
||||
private String listingStartTime; |
||||
|
||||
/** 结束时间,Y-m-d H:i:s。 */ |
||||
@JsonProperty("listing_end_time") |
||||
private String listingEndTime; |
||||
|
||||
/** 商品状态:1 active,2 draft,3 archived,4 deleted。 */ |
||||
@JsonProperty("status") |
||||
private Integer status; |
||||
|
||||
/** 商品状态名。 */ |
||||
@JsonProperty("status_name") |
||||
private String statusName; |
||||
|
||||
/** 库存策略:1 不跟踪,2 缺货停止销售,3 缺货继续销售。 */ |
||||
@JsonProperty("inventory_policy") |
||||
private Integer inventoryPolicy; |
||||
|
||||
/** 库存策略名。 */ |
||||
@JsonProperty("inventory_policy_name") |
||||
private String inventoryPolicyName; |
||||
|
||||
/** 商品分类 ID。 */ |
||||
@JsonProperty("type_id") |
||||
private String typeId; |
||||
|
||||
/** 商品分类名。 */ |
||||
@JsonProperty("type_name") |
||||
private String typeName; |
||||
|
||||
/** 是否需要运输。 */ |
||||
@JsonProperty("requires_shipping") |
||||
private Boolean requiresShipping; |
||||
|
||||
/** 是否要征税。 */ |
||||
@JsonProperty("taxable") |
||||
private Boolean taxable; |
||||
|
||||
/** 最小重量。 */ |
||||
@JsonProperty("min_weight") |
||||
private Integer minWeight; |
||||
|
||||
/** 最大重量。 */ |
||||
@JsonProperty("max_weight") |
||||
private Integer maxWeight; |
||||
|
||||
/** 重量单位。 */ |
||||
@JsonProperty("weight_unit") |
||||
private String weightUnit; |
||||
|
||||
/** 重量。 */ |
||||
@JsonProperty("weight") |
||||
private String weight; |
||||
|
||||
/** 各仓位信息。 */ |
||||
@JsonProperty("location_infos") |
||||
private List<ShopifyLocationDto> locationInfos = new ArrayList<ShopifyLocationDto>(); |
||||
} |
||||
@ -1,49 +0,0 @@
@@ -1,49 +0,0 @@
|
||||
package common.ecommerce.lingxing.platform.shopify.dto; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
import lombok.Getter; |
||||
import lombok.Setter; |
||||
|
||||
/** Shopify 商品仓位信息({@code location_infos[]})。 */ |
||||
@Getter |
||||
@Setter |
||||
@JsonIgnoreProperties(ignoreUnknown = true) |
||||
public class ShopifyLocationDto { |
||||
|
||||
/** 仓位 id。 */ |
||||
@JsonProperty("location_id") |
||||
private String locationId; |
||||
|
||||
/** 仓位名称。 */ |
||||
@JsonProperty("location_name") |
||||
private String locationName; |
||||
|
||||
/** 该仓位库存。 */ |
||||
@JsonProperty("location_quantity") |
||||
private String locationQuantity; |
||||
|
||||
/** 库存调整后的值。 */ |
||||
@JsonProperty("quantity_adjust_after") |
||||
private String quantityAdjustAfter; |
||||
|
||||
/** 库存调整前的值。 */ |
||||
@JsonProperty("quantity_adjust_before") |
||||
private String quantityAdjustBefore; |
||||
|
||||
/** 库存调整操作人 id。 */ |
||||
@JsonProperty("quantity_user_id") |
||||
private String quantityUserId; |
||||
|
||||
/** 库存调整操作人名字。 */ |
||||
@JsonProperty("quantity_user_id_name") |
||||
private String quantityUserIdName; |
||||
|
||||
/** 库存队列类型。 */ |
||||
@JsonProperty("quantity_queue_type") |
||||
private Integer quantityQueueType; |
||||
|
||||
/** 库存队列状态。 */ |
||||
@JsonProperty("quantity_queue_status") |
||||
private String quantityQueueStatus; |
||||
} |
||||
@ -1,272 +0,0 @@
@@ -1,272 +0,0 @@
|
||||
package common.ecommerce.lingxing.platform.shopify.product; |
||||
|
||||
import common.ecommerce.http.HttpRequest; |
||||
import common.ecommerce.http.HttpResponse; |
||||
import common.ecommerce.lingxing.LingxingApi; |
||||
import common.ecommerce.lingxing.http.LingxingEnvelope; |
||||
import common.ecommerce.lingxing.platform.LingxingCall; |
||||
import common.ecommerce.lingxing.platform.LingxingProductSource; |
||||
import common.ecommerce.lingxing.platform.shopify.dto.ShopifyListingDto; |
||||
import common.ecommerce.model.ChannelProvider; |
||||
import common.ecommerce.model.product.ProductListing; |
||||
import common.ecommerce.model.product.ProductQuery; |
||||
import common.ecommerce.model.product.ProductStatus; |
||||
import common.ecommerce.model.support.Money; |
||||
import common.ecommerce.model.support.Page; |
||||
import common.ecommerce.model.support.PlatformTime; |
||||
|
||||
import java.time.Instant; |
||||
import java.time.LocalDateTime; |
||||
import java.time.ZoneId; |
||||
import java.time.format.DateTimeFormatter; |
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.LinkedHashMap; |
||||
import java.util.List; |
||||
import java.util.Locale; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* 领星 · Shopify 在线商品(变体维度)。 |
||||
* |
||||
* <p>接口 {@code /basicOpen/multiplatform/shopify/variantList}:POST,{@code offset}/{@code length} |
||||
* (上限 1000),返回 {@code data.list};令牌桶容量 1。</p> |
||||
*/ |
||||
public final class ShopifyProductSource implements LingxingProductSource { |
||||
|
||||
private static final int MAX_PAGE_SIZE = 1000; |
||||
|
||||
/** 时间字段格式,文档未给出示例值格式,按 Y-m-d H:i:s 发送。 */ |
||||
private static final DateTimeFormatter DATE_TIME = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
||||
@Override |
||||
public Page<ProductListing> products(LingxingCall call, ProductQuery query) { |
||||
int length = Math.min(query.pageSize() <= 0 ? 100 : query.pageSize(), MAX_PAGE_SIZE); |
||||
int offset = query.cursor() == null ? 0 : Integer.parseInt(query.cursor()); |
||||
|
||||
Map<String, Object> body = new LinkedHashMap<String, Object>(); |
||||
body.put("offset", offset); |
||||
body.put("length", length); |
||||
body.put("store_ids", Collections.singletonList(call.ctx().account().externalStoreId())); |
||||
applyTimeWindow(body, query); |
||||
|
||||
HttpRequest req = call.signed(LingxingApi.LISTING_SHOPIFY, body, "products.pull"); |
||||
HttpResponse resp = call.execute(req); |
||||
LingxingEnvelope envelope = call.envelope(resp, "products.pull"); |
||||
|
||||
List<ShopifyListingDto> dtos = call.json().listAt(envelope.body(), "data.list", ShopifyListingDto.class); |
||||
List<ProductListing> out = new ArrayList<ProductListing>(dtos.size()); |
||||
for (ShopifyListingDto dto : dtos) { |
||||
out.add(toProductListing(dto, call)); |
||||
} |
||||
|
||||
int total = totalOf(call, envelope.body()); |
||||
int nextOffset = offset + dtos.size(); |
||||
boolean hasMore = dtos.size() >= length && (total < 0 || nextOffset < total); |
||||
return Page.of(out, hasMore ? String.valueOf(nextOffset) : null); |
||||
} |
||||
|
||||
/** 时间窗:按 UTC 发送,未给终点时不发终点。 */ |
||||
private static void applyTimeWindow(Map<String, Object> body, ProductQuery query) { |
||||
Instant from = query.updatedAfter(); |
||||
Instant to = query.updatedBefore(); |
||||
if (from == null && to == null) { |
||||
return; |
||||
} |
||||
ZoneId utc = ZoneId.of("UTC"); |
||||
if (from != null) { |
||||
body.put("listing_start_time", DATE_TIME.format(from.atZone(utc))); |
||||
} |
||||
if (to != null) { |
||||
body.put("listing_end_time", DATE_TIME.format(to.atZone(utc))); |
||||
} |
||||
} |
||||
|
||||
/** 总数取 data.count;没有时用顶层 total;都没有返回 -1。 */ |
||||
private static int totalOf(LingxingCall call, String body) { |
||||
String raw = call.json().stringAt(body, "data.count"); |
||||
if (raw == null) { |
||||
raw = call.json().stringAt(body, "total"); |
||||
} |
||||
if (raw == null) { |
||||
return -1; |
||||
} |
||||
try { |
||||
return Integer.parseInt(raw.trim()); |
||||
} catch (NumberFormatException e) { |
||||
return -1; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 领星 Shopify 商品 → 统一模型。 |
||||
* |
||||
* <p>统一字段来源:</p> |
||||
* <ul> |
||||
* <li>platformSku ← {@code msku}</li> |
||||
* <li>listingId ← {@code product_unique_id}</li> |
||||
* <li>gtin ← 无</li> |
||||
* <li>title ← {@code title}</li> |
||||
* <li>imageUrls ← {@code picture_url},为空时用 {@code parent_picture_url}</li> |
||||
* <li>status ← {@code status}(1 active / 2 draft / 3 archived / 4 deleted)</li> |
||||
* <li>rawStatus ← {@code status_name} 原值</li> |
||||
* <li>price ← {@code price};currency 由 {@code currency_icon} 反推</li> |
||||
* <li>listedAt ← {@code listing_start_time}</li> |
||||
* <li>updatedAt ← 无(接口没有内容更新时间)</li> |
||||
* <li>attributes ← 其余全部原始字段</li> |
||||
* </ul> |
||||
*/ |
||||
private ProductListing toProductListing(ShopifyListingDto dto, LingxingCall call) { |
||||
ZoneId zone = ZoneId.of(call.ctx().account().marketplaceZone()); |
||||
String currency = currencyOf(dto.getCurrencyIcon()); |
||||
String image = firstNonBlank(dto.getPictureUrl(), dto.getParentPictureUrl()); |
||||
|
||||
return ProductListing.builder() |
||||
.platform(call.ctx().platform()) |
||||
.omsStoreId(call.ctx().omsStoreId()) |
||||
.platformSku(dto.getMsku()) |
||||
.listingId(trimToNull(dto.getProductUniqueId())) |
||||
.title(dto.getTitle()) |
||||
.imageUrls(image == null |
||||
? Collections.<String>emptyList() |
||||
: Collections.singletonList(image)) |
||||
.status(statusOf(dto)) |
||||
.rawStatus(dto.getStatusName()) |
||||
.price(moneyOrNull(dto.getPrice(), currency)) |
||||
.currency(currency) |
||||
.listedAt(dateTime(dto.getListingStartTime(), zone)) |
||||
.attributes(attributesOf(dto)) |
||||
.sourceProvider(ChannelProvider.LINGXING) |
||||
.build(); |
||||
} |
||||
|
||||
/** Shopify:1 active / 2 draft / 3 archived / 4 deleted。 */ |
||||
private static ProductStatus statusOf(ShopifyListingDto dto) { |
||||
if (dto.getStatus() == null) { |
||||
String name = dto.getStatusName(); |
||||
if (name != null) { |
||||
String s = name.trim().toUpperCase(Locale.ROOT); |
||||
if ("ACTIVE".equals(s)) { |
||||
return ProductStatus.ACTIVE; |
||||
} |
||||
if ("DELETED".equals(s)) { |
||||
return ProductStatus.DELETED; |
||||
} |
||||
if ("DRAFT".equals(s) || "ARCHIVED".equals(s)) { |
||||
return ProductStatus.INACTIVE; |
||||
} |
||||
} |
||||
return ProductStatus.UNKNOWN; |
||||
} |
||||
switch (dto.getStatus()) { |
||||
case 1: |
||||
return ProductStatus.ACTIVE; |
||||
case 2: |
||||
case 3: |
||||
return ProductStatus.INACTIVE; |
||||
case 4: |
||||
return ProductStatus.DELETED; |
||||
default: |
||||
return ProductStatus.UNKNOWN; |
||||
} |
||||
} |
||||
|
||||
private static Map<String, Object> attributesOf(ShopifyListingDto dto) { |
||||
Map<String, Object> attrs = new LinkedHashMap<String, Object>(); |
||||
attrs.put("item_id", dto.getItemId()); |
||||
attrs.put("item_url", dto.getItemUrl()); |
||||
attrs.put("variant_unique_id", dto.getVariantUniqueId()); |
||||
attrs.put("company_id", dto.getCompanyId()); |
||||
attrs.put("parent_picture_url", dto.getParentPictureUrl()); |
||||
attrs.put("parent_msku", dto.getParentMsku()); |
||||
attrs.put("parent_attribute", dto.getParentAttribute()); |
||||
attrs.put("attribute", dto.getAttribute()); |
||||
attrs.put("local_sku", dto.getLocalSku()); |
||||
attrs.put("pid", dto.getPid()); |
||||
attrs.put("local_name", dto.getLocalName()); |
||||
attrs.put("store_id", dto.getStoreId()); |
||||
attrs.put("store_name", dto.getStoreName()); |
||||
attrs.put("quantity", dto.getQuantity()); |
||||
attrs.put("currency_icon", dto.getCurrencyIcon()); |
||||
attrs.put("has_variant", dto.getHasVariant()); |
||||
attrs.put("listing_end_time", dto.getListingEndTime()); |
||||
attrs.put("inventory_policy", dto.getInventoryPolicy()); |
||||
attrs.put("inventory_policy_name", dto.getInventoryPolicyName()); |
||||
attrs.put("type_id", dto.getTypeId()); |
||||
attrs.put("type_name", dto.getTypeName()); |
||||
attrs.put("requires_shipping", dto.getRequiresShipping()); |
||||
attrs.put("taxable", dto.getTaxable()); |
||||
attrs.put("min_weight", dto.getMinWeight()); |
||||
attrs.put("max_weight", dto.getMaxWeight()); |
||||
attrs.put("weight", dto.getWeight()); |
||||
attrs.put("weight_unit", dto.getWeightUnit()); |
||||
attrs.put("location_infos", dto.getLocationInfos()); |
||||
attrs.values().removeAll(Collections.singleton(null)); |
||||
return attrs; |
||||
} |
||||
|
||||
/** 货币符号 → ISO;约定 ¥ 符号视为日元,人民币以 ISO 代码 CNY 返回。 */ |
||||
private static String currencyOf(String icon) { |
||||
if (icon == null) { |
||||
return null; |
||||
} |
||||
String s = icon.trim(); |
||||
if ("$".equals(s) || "US$".equals(s)) { |
||||
return "USD"; |
||||
} |
||||
if ("€".equals(s)) { |
||||
return "EUR"; |
||||
} |
||||
if ("£".equals(s)) { |
||||
return "GBP"; |
||||
} |
||||
if ("¥".equals(s) || "¥".equals(s)) { |
||||
// 约定:¥ 符号按日元处理;人民币由平台以 ISO 代码 CNY 返回
|
||||
return "JPY"; |
||||
} |
||||
if ("CNY".equalsIgnoreCase(s) || "RMB".equalsIgnoreCase(s)) { |
||||
return "CNY"; |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
private static PlatformTime dateTime(String raw, ZoneId zone) { |
||||
if (raw == null || raw.trim().isEmpty()) { |
||||
return null; |
||||
} |
||||
try { |
||||
Instant utc = LocalDateTime.parse(raw.trim(), DATE_TIME).atZone(zone).toInstant(); |
||||
return PlatformTime.of(utc, zone); |
||||
} catch (RuntimeException e) { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
private static Money moneyOrNull(String value, String currency) { |
||||
if (value == null || value.trim().isEmpty() || currency == null) { |
||||
return null; |
||||
} |
||||
try { |
||||
return Money.of(value.trim(), currency); |
||||
} catch (RuntimeException e) { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
private static String firstNonBlank(String... values) { |
||||
for (String v : values) { |
||||
String trimmed = trimToNull(v); |
||||
if (trimmed != null) { |
||||
return trimmed; |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
private static String trimToNull(String v) { |
||||
if (v == null) { |
||||
return null; |
||||
} |
||||
String trimmed = v.trim(); |
||||
return trimmed.isEmpty() ? null : trimmed; |
||||
} |
||||
} |
||||
@ -0,0 +1,164 @@
@@ -0,0 +1,164 @@
|
||||
package common.ecommerce.lingxing.adapter; |
||||
|
||||
import common.ecommerce.audit.CallObserver; |
||||
import common.ecommerce.audit.CallOutcome; |
||||
import common.ecommerce.audit.CallRecord; |
||||
import common.ecommerce.http.JacksonJsonCodec; |
||||
import common.ecommerce.lingxing.LingxingApi; |
||||
import common.ecommerce.lingxing.support.LingxingTestSupport; |
||||
import common.ecommerce.model.Platform; |
||||
import common.ecommerce.model.account.ChannelAccount; |
||||
import common.ecommerce.model.product.ProductListing; |
||||
import common.ecommerce.model.product.ProductQuery; |
||||
import common.ecommerce.model.product.ProductSearchField; |
||||
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.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||
import static org.junit.jupiter.api.Assertions.assertFalse; |
||||
import static org.junit.jupiter.api.Assertions.assertTrue; |
||||
|
||||
/** |
||||
* 从 {@link ChannelSdk} 门面调用领星 listing 的端到端测试。 |
||||
* |
||||
* <p>用 {@link RecordingTransport} 替掉网络,走真实的 {@link LingxingAdapter}, |
||||
* 验证「门面 → 子平台 → Source」这条链路:能力检查、请求路径、SKU 搜索参数、 |
||||
* 统一模型映射和调用日志。</p> |
||||
*/ |
||||
class LingxingSdkListingCallTest { |
||||
|
||||
/** 沃尔玛的商品页;响应里 total 在顶层,data 是数组。 */ |
||||
private static final String WALMART_JSON = |
||||
"{\"code\":0,\"message\":\"success\",\"total\":1,\"data\":[{" |
||||
+ "\"item_id\":\"wal-1\",\"msku\":\"WM-MSKU-1\",\"title\":\"Walmart Item\"," |
||||
+ "\"local_sku\":\"WM-LOCAL-1\",\"status_name\":\"PUBLISHED\"," |
||||
+ "\"store_id\":\"store-1\",\"currency_icon\":\"$\",\"price\":\"19.99\"}]}"; |
||||
|
||||
/** Target 的商品页;响应结构是 data.list。 */ |
||||
private static final String TARGET_JSON = |
||||
"{\"code\":0,\"message\":\"success\",\"data\":{\"count\":1,\"list\":[{" |
||||
+ "\"product_unique_id\":\"prod-1\",\"msku\":\"TGT-MSKU-1\",\"title\":\"Target Item\"," |
||||
+ "\"local_sku\":\"\",\"store_id\":\"store-1\",\"price\":\"9.99\"," |
||||
+ "\"currency_icon\":\"$\",\"status\":1,\"status_name\":\"active\"}]}}"; |
||||
|
||||
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 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 ChannelConnection connection() { |
||||
return ChannelConnection.of(LingxingTestSupport.ENDPOINT, |
||||
ChannelSecret.builder() |
||||
.appKey(LingxingTestSupport.APP_KEY) |
||||
.appSecret(LingxingTestSupport.APP_SECRET) |
||||
.build()); |
||||
} |
||||
|
||||
@Test |
||||
void pullsWalmartListingThroughFacade() { |
||||
RecordingTransport transport = RecordingTransport.create() |
||||
.route(LingxingApi.LISTING_WALMART.path(), r -> Responses.ok(WALMART_JSON)); |
||||
ChannelAccount account = LingxingTestSupport.account(Platform.WALMART_SC, 10008, "store-1"); |
||||
CollectingObserver observer = new CollectingObserver(); |
||||
|
||||
Page<ProductListing> page = sdk(transport, account, observer) |
||||
.products(account.key(), connection()) |
||||
.pull(ProductQuery.builder() |
||||
.pageSize(10) |
||||
.searchField(ProductSearchField.PLATFORM_SKU) |
||||
.searchValue("WM-MSKU-1") |
||||
.build()); |
||||
|
||||
assertEquals(1, page.size()); |
||||
ProductListing p = page.items().get(0); |
||||
assertEquals(Platform.WALMART_SC, p.platform()); |
||||
assertEquals("WM-MSKU-1", p.platformSku()); |
||||
// omsStoreId 是宿主的店铺标识(这里取 AccountKey 的 storeId),不是领星 store_id
|
||||
assertEquals(account.omsStoreId(), p.omsStoreId()); |
||||
assertEquals("PUBLISHED", p.rawStatus()); |
||||
|
||||
// 实测:搜索参数必须是 snake_case,否则平台静默忽略
|
||||
String body = transport.lastDataRequest().body(); |
||||
assertTrue(body.contains("\"search_field\":1"), body); |
||||
assertTrue(body.contains("\"search_single_value\":\"WM-MSKU-1\""), body); |
||||
assertFalse(body.contains("searchField"), body); |
||||
|
||||
// 门面审计记录:operation / 平台 / store / 条数
|
||||
assertEquals(1, observer.records.size()); |
||||
CallRecord record = observer.records.get(0); |
||||
assertEquals("products.pull", record.operation()); |
||||
assertEquals(CallOutcome.SUCCESS, record.outcome()); |
||||
assertEquals(Platform.WALMART_SC, record.platform()); |
||||
assertEquals("store-1", record.externalStoreId()); |
||||
assertEquals(1, record.resultCount()); |
||||
} |
||||
|
||||
@Test |
||||
void pullsTargetListingThroughFacade() { |
||||
RecordingTransport transport = RecordingTransport.create() |
||||
.route(LingxingApi.LISTING_SHOPIFY.path(), r -> Responses.ok(TARGET_JSON)); |
||||
ChannelAccount account = LingxingTestSupport.account(Platform.TARGET, 0, "store-1"); |
||||
|
||||
Page<ProductListing> page = sdk(transport, account, CallObserver.NOOP) |
||||
.products(account.key(), connection()) |
||||
.pull(ProductQuery.builder() |
||||
.pageSize(5) |
||||
.updatedAfter(java.time.Instant.parse("2026-10-01T00:00:00Z")) |
||||
.updatedBefore(java.time.Instant.parse("2026-10-08T00:00:00Z")) |
||||
.build()); |
||||
|
||||
assertEquals(1, page.size()); |
||||
assertEquals("TGT-MSKU-1", page.items().get(0).platformSku()); |
||||
assertEquals("prod-1", page.items().get(0).listingId()); |
||||
|
||||
String body = transport.lastDataRequest().body(); |
||||
assertTrue(body.contains("\"store_ids\":[\"store-1\"]"), body); |
||||
assertTrue(body.contains("\"listing_time_field\":2"), body); |
||||
assertTrue(body.contains("\"listing_start_time\":\"2026-10-01\""), body); |
||||
assertTrue(body.contains("\"listing_end_time\":\"2026-10-08\""), body); |
||||
} |
||||
|
||||
@Test |
||||
void capabilityReportIncludesProductPerSubPlatform() { |
||||
RecordingTransport transport = RecordingTransport.create(); |
||||
ChannelAccount account = LingxingTestSupport.account(Platform.TARGET, 0, "store-1"); |
||||
|
||||
// Target 有商品能力;没有商品接口的长尾平台(如 Mirakl)没有
|
||||
assertTrue(sdk(transport, account, CallObserver.NOOP) |
||||
.capabilities(account.key()).supports(common.ecommerce.model.Capability.PRODUCT)); |
||||
|
||||
ChannelAccount mirakl = LingxingTestSupport.account(Platform.MIRAKL, 10034, "store-1"); |
||||
assertFalse(sdk(transport, mirakl, CallObserver.NOOP) |
||||
.capabilities(mirakl.key()).supports(common.ecommerce.model.Capability.PRODUCT)); |
||||
} |
||||
} |
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue