Browse Source
- GET api.get_customer_warehouses → WarehouseDirectoryCapable.warehouses() - GET api.get_customer_logistics → WarehouseDirectoryCapable.carriers() core 只新增: - WarehouseDirectoryCapable(warehouses / carriers) - WarehouseFacility 模型:仓代码 + 国家 / 省 / 市 / 街道 / 邮编 - WarehouseCapability 追加 DIRECTORY(210) - WarehouseOperations 增加 warehouses() / carriers(),只读带埋点 未并入 WarehouseRepository:那是宿主维护的 OMS 仓编码 → 账户映射, 这两个接口是仓方返回的可选仓库清单,数据源不同,混用会让宿主分不清信谁。 空 data 返回空列表;只有 rst 缺失(报文结构变化)才抛 ValidationException。 Co-Authored-By: Claude Code <noreply@anthropic.com>feature/supplier-lingxing
10 changed files with 490 additions and 4 deletions
@ -0,0 +1,34 @@
@@ -0,0 +1,34 @@
|
||||
package common.ecommerce.warehouse.capability; |
||||
|
||||
import common.ecommerce.warehouse.model.WarehouseFacility; |
||||
import common.ecommerce.warehouse.sdk.WarehouseAdapter; |
||||
import common.ecommerce.warehouse.sdk.WarehouseContext; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 仓方基础资料能力:列出账户下可用的仓库与承运商。 |
||||
* |
||||
* <p>用途是让宿主在下单前拿到仓代码 / 物流方式,而不是把取值写死在配置里。 |
||||
* 只读,无幂等要求。</p> |
||||
*/ |
||||
public interface WarehouseDirectoryCapable extends WarehouseAdapter { |
||||
|
||||
/** |
||||
* 客户名下所有仓库。 |
||||
* |
||||
* @return 仓库列表;仓方返回空时为 empty list |
||||
* @throws common.ecommerce.warehouse.error.WarehouseUnsupportedCapabilityException |
||||
* 该仓不声明 {@code DIRECTORY} |
||||
*/ |
||||
List<WarehouseFacility> warehouses(WarehouseContext ctx); |
||||
|
||||
/** |
||||
* 客户可用的承运商 / 物流方式代码。 |
||||
* |
||||
* @return 物流方式码列表,如 {@code FEDEX-G};未配置时为空列表 |
||||
* @throws common.ecommerce.warehouse.error.WarehouseUnsupportedCapabilityException |
||||
* 该仓不声明 {@code DIRECTORY} |
||||
*/ |
||||
List<String> carriers(WarehouseContext ctx); |
||||
} |
||||
@ -0,0 +1,120 @@
@@ -0,0 +1,120 @@
|
||||
package common.ecommerce.warehouse.model; |
||||
|
||||
/** |
||||
* 仓方的基础资料:一个可发货的仓库及其地址。 |
||||
* |
||||
* <p>用于下单前让宿主拿到「这家仓有哪些仓、仓代码是什么」,而不是把仓代码写死在配置里。 |
||||
* 只读数据,不含库存与能力。</p> |
||||
*/ |
||||
public final class WarehouseFacility { |
||||
|
||||
/** 仓方内部 id(信仓 {@code id});可能为 null。 */ |
||||
private final String providerWarehouseId; |
||||
/** 仓代码(下单时用的 {@code warehouse_code})。 */ |
||||
private final String warehouseCode; |
||||
private final String country; |
||||
private final String province; |
||||
private final String city; |
||||
private final String street; |
||||
private final String postcode; |
||||
|
||||
private WarehouseFacility(Builder b) { |
||||
this.providerWarehouseId = b.providerWarehouseId; |
||||
this.warehouseCode = require(b.warehouseCode, "warehouseCode"); |
||||
this.country = b.country; |
||||
this.province = b.province; |
||||
this.city = b.city; |
||||
this.street = b.street; |
||||
this.postcode = b.postcode; |
||||
} |
||||
|
||||
public static Builder builder() { |
||||
return new Builder(); |
||||
} |
||||
|
||||
/** 仓方内部 id;可能为 null。 */ |
||||
public String providerWarehouseId() { |
||||
return providerWarehouseId; |
||||
} |
||||
|
||||
/** 仓代码。 */ |
||||
public String warehouseCode() { |
||||
return warehouseCode; |
||||
} |
||||
|
||||
public String country() { |
||||
return country; |
||||
} |
||||
|
||||
public String province() { |
||||
return province; |
||||
} |
||||
|
||||
public String city() { |
||||
return city; |
||||
} |
||||
|
||||
public String street() { |
||||
return street; |
||||
} |
||||
|
||||
public String postcode() { |
||||
return postcode; |
||||
} |
||||
|
||||
private static String require(String v, String n) { |
||||
if (v == null || v.trim().isEmpty()) { |
||||
throw new IllegalArgumentException(n + " must not be blank"); |
||||
} |
||||
return v.trim(); |
||||
} |
||||
|
||||
public static final class Builder { |
||||
private String providerWarehouseId; |
||||
private String warehouseCode; |
||||
private String country; |
||||
private String province; |
||||
private String city; |
||||
private String street; |
||||
private String postcode; |
||||
|
||||
public Builder providerWarehouseId(String v) { |
||||
this.providerWarehouseId = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder warehouseCode(String v) { |
||||
this.warehouseCode = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder country(String v) { |
||||
this.country = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder province(String v) { |
||||
this.province = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder city(String v) { |
||||
this.city = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder street(String v) { |
||||
this.street = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder postcode(String v) { |
||||
this.postcode = v; |
||||
return this; |
||||
} |
||||
|
||||
public WarehouseFacility build() { |
||||
return new WarehouseFacility(this); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,144 @@
@@ -0,0 +1,144 @@
|
||||
package common.ecommerce.xincang.adapter; |
||||
|
||||
import common.ecommerce.shared.error.ValidationException; |
||||
import common.ecommerce.shared.spi.ChannelSecret; |
||||
import common.ecommerce.shared.testkit.InMemoryLockProvider; |
||||
import common.ecommerce.shared.testkit.InMemoryTokenStore; |
||||
import common.ecommerce.shared.testkit.MutableClock; |
||||
import common.ecommerce.shared.testkit.RecordingTransport; |
||||
import common.ecommerce.shared.testkit.Responses; |
||||
import common.ecommerce.warehouse.enums.WarehouseCapability; |
||||
import common.ecommerce.warehouse.enums.WarehouseProvider; |
||||
import common.ecommerce.warehouse.model.WarehouseFacility; |
||||
import common.ecommerce.warehouse.sdk.WarehouseConnection; |
||||
import common.ecommerce.warehouse.sdk.WarehouseContext; |
||||
import common.ecommerce.xincang.XincangConstants; |
||||
import org.junit.jupiter.api.BeforeEach; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import java.time.Clock; |
||||
import java.time.Duration; |
||||
import java.util.List; |
||||
|
||||
import static org.junit.jupiter.api.Assertions.*; |
||||
|
||||
/** |
||||
* 基础资料(客户仓库 / 承运商)契约测试。 |
||||
*/ |
||||
class XincangDirectoryTest { |
||||
|
||||
private static final String BASE = XincangConstants.TEST_BASE_URL; |
||||
private static final String APP_KEY = "ak-1"; |
||||
|
||||
private static final String WAREHOUSES = "{\"rst\":{\"code\":\"0\",\"msg\":\"\",\"data\":[" |
||||
+ "{\"id\":\"84047962214173\",\"warehouse_code\":\"CA-3790\",\"country\":\"US\"," |
||||
+ "\"province\":\"CA\",\"city\":\"JURUPA VALLEY\"," |
||||
+ "\"street\":\"3790 DE FOREST CIRCLE, UNIT B\",\"postcode\":\"91752\"}," |
||||
+ "{\"id\":\"84047962479824\",\"warehouse_code\":\"NJ-2605\",\"country\":\"US\"," |
||||
+ "\"province\":\"NJ\",\"city\":\"Burlington Township\"," |
||||
+ "\"street\":\"2605 Rancocas Road\",\"postcode\":\"08016\"}" |
||||
+ "],\"count\":\"2\"},\"errno\":0,\"err\":\"\",\"trace_id\":\"t-wh\"}"; |
||||
|
||||
private static final String LOGISTICS = "{\"rst\":{\"code\":\"0\",\"msg\":\"\",\"data\":[" |
||||
+ "{\"logistics_type\":\"FEDEX-HD\"},{\"logistics_type\":\"FEDEX-G\"}" |
||||
+ "],\"count\":\"2\"},\"errno\":0,\"err\":\"\",\"trace_id\":\"t-lg\"}"; |
||||
|
||||
private RecordingTransport transport; |
||||
private XincangAdapter adapter; |
||||
|
||||
@BeforeEach |
||||
void setUp() { |
||||
transport = RecordingTransport.create(); |
||||
MutableClock clock = MutableClock.atUtc("2026-11-11T00:00:00Z"); |
||||
transport.route(XincangConstants.PATH_LOGIN, req -> Responses.ok( |
||||
"{\"rst\":{\"id\":\"c-1\",\"token\":\"{T-1}\",\"token_expire_time\":\"" |
||||
+ java.time.LocalDateTime.ofInstant(clock.instant().plus(Duration.ofHours(2)), |
||||
XincangConstants.SYSTEM_ZONE) |
||||
.format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) |
||||
+ "\"},\"errno\":0,\"err\":\"\",\"trace_id\":\"t-login\"}")); |
||||
adapter = new XincangAdapter(transport, new common.ecommerce.http.JacksonJsonCodec(), |
||||
new InMemoryTokenStore(), new InMemoryLockProvider(), clock); |
||||
} |
||||
|
||||
private static WarehouseContext ctx() { |
||||
return WarehouseContext.of("XC-NJ-2605", WarehouseProvider.XINCANG, "NJ-2605", |
||||
WarehouseConnection.of(BASE, ChannelSecret.builder() |
||||
.appKey(APP_KEY).appSecret("sk").build()), |
||||
"req-1", Clock.systemUTC()); |
||||
} |
||||
|
||||
@Test |
||||
void listsWarehousesWithAddress() { |
||||
transport.route(XincangConstants.PATH_CUSTOMER_WAREHOUSES, req -> Responses.ok(WAREHOUSES)); |
||||
|
||||
List<WarehouseFacility> warehouses = adapter.warehouses(ctx()); |
||||
|
||||
String url = transport.lastDataRequest().url(); |
||||
assertTrue(url.contains("/api.get_customer_warehouses?token=%7BT-1%7D"), url); |
||||
|
||||
assertEquals(2, warehouses.size()); |
||||
WarehouseFacility first = warehouses.get(0); |
||||
assertEquals("CA-3790", first.warehouseCode()); |
||||
assertEquals("84047962214173", first.providerWarehouseId()); |
||||
assertEquals("US", first.country()); |
||||
assertEquals("CA", first.province()); |
||||
assertEquals("JURUPA VALLEY", first.city()); |
||||
assertEquals("3790 DE FOREST CIRCLE, UNIT B", first.street()); |
||||
assertEquals("91752", first.postcode()); |
||||
assertEquals("NJ-2605", warehouses.get(1).warehouseCode()); |
||||
} |
||||
|
||||
@Test |
||||
void listsCarriers() { |
||||
transport.route(XincangConstants.PATH_CUSTOMER_LOGISTICS, req -> Responses.ok(LOGISTICS)); |
||||
|
||||
List<String> carriers = adapter.carriers(ctx()); |
||||
|
||||
assertTrue(transport.lastDataRequest().url() |
||||
.contains("/api.get_customer_logistics?"), transport.lastDataRequest().url()); |
||||
assertEquals(2, carriers.size()); |
||||
assertEquals("FEDEX-HD", carriers.get(0)); |
||||
assertEquals("FEDEX-G", carriers.get(1)); |
||||
} |
||||
|
||||
@Test |
||||
void emptyDataYieldsEmptyListNotError() { |
||||
transport.route(XincangConstants.PATH_CUSTOMER_WAREHOUSES, req -> Responses.ok( |
||||
"{\"rst\":{\"code\":\"0\",\"msg\":\"\",\"data\":[],\"count\":\"0\"}," |
||||
+ "\"errno\":0,\"err\":\"\"}")); |
||||
transport.route(XincangConstants.PATH_CUSTOMER_LOGISTICS, req -> Responses.ok( |
||||
"{\"rst\":{\"code\":\"0\",\"msg\":\"\",\"data\":[],\"count\":\"0\"}," |
||||
+ "\"errno\":0,\"err\":\"\"}")); |
||||
|
||||
assertTrue(adapter.warehouses(ctx()).isEmpty()); |
||||
assertTrue(adapter.carriers(ctx()).isEmpty()); |
||||
} |
||||
|
||||
@Test |
||||
void rowsWithoutWarehouseCodeAreSkipped() { |
||||
transport.route(XincangConstants.PATH_CUSTOMER_WAREHOUSES, req -> Responses.ok( |
||||
"{\"rst\":{\"code\":\"0\",\"msg\":\"\",\"data\":[" |
||||
+ "{\"id\":\"1\",\"warehouse_code\":\"\",\"country\":\"US\"}," |
||||
+ "{\"id\":\"2\",\"warehouse_code\":\"CA-3790\",\"country\":\"US\"}" |
||||
+ "],\"count\":\"2\"},\"errno\":0,\"err\":\"\"}")); |
||||
|
||||
List<WarehouseFacility> warehouses = adapter.warehouses(ctx()); |
||||
|
||||
assertEquals(1, warehouses.size()); |
||||
assertEquals("CA-3790", warehouses.get(0).warehouseCode()); |
||||
} |
||||
|
||||
@Test |
||||
void missingRstIsNotSilentEmpty() { |
||||
transport.route(XincangConstants.PATH_CUSTOMER_LOGISTICS, req -> Responses.ok( |
||||
"{\"rst\":null,\"errno\":0,\"err\":\"\"}")); |
||||
|
||||
assertThrows(ValidationException.class, () -> adapter.carriers(ctx())); |
||||
} |
||||
|
||||
@Test |
||||
void directoryCapabilityDeclared() { |
||||
assertTrue(adapter.capabilities().contains(WarehouseCapability.DIRECTORY)); |
||||
assertTrue(adapter.supports("XC-NJ-2605", WarehouseCapability.DIRECTORY)); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue