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.
161 lines
6.2 KiB
161 lines
6.2 KiB
|
5 days ago
|
package common.ecommerce.edayun.adapter;
|
||
|
|
|
||
|
|
import common.ecommerce.edayun.dto.EdayunCarrierDtos;
|
||
|
|
import common.ecommerce.http.JacksonJsonCodec;
|
||
|
|
import common.ecommerce.shared.spi.ChannelSecret;
|
||
|
|
import common.ecommerce.shared.testkit.RecordingTransport;
|
||
|
|
import common.ecommerce.shared.testkit.Responses;
|
||
|
|
import common.ecommerce.warehouse.enums.WarehouseProvider;
|
||
|
|
import common.ecommerce.warehouse.model.WarehouseCarrier;
|
||
|
|
import common.ecommerce.warehouse.sdk.WarehouseConnection;
|
||
|
|
import common.ecommerce.warehouse.sdk.WarehouseContext;
|
||
|
|
import org.junit.jupiter.api.Test;
|
||
|
|
|
||
|
|
import java.time.Clock;
|
||
|
|
import java.time.Instant;
|
||
|
|
import java.time.ZoneOffset;
|
||
|
|
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;
|
||
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 承运商清单({@code /webApi/carrier/v1/listCarrierByWarehouse})。
|
||
|
|
*
|
||
|
|
* <p>重点:ownLogisticList 文档写成对象、真机是数组;carrierId 缺失的行跳过。</p>
|
||
|
|
*/
|
||
|
|
class EdayunCarrierTest {
|
||
|
|
|
||
|
|
private static final Clock CLOCK = Clock.fixed(Instant.parse("2026-11-16T00:00:00Z"), ZoneOffset.UTC);
|
||
|
|
|
||
|
|
private static WarehouseContext ctx(String providerWarehouseId) {
|
||
|
|
return WarehouseContext.of("EDY-WH", WarehouseProvider.EDAYUN, providerWarehouseId,
|
||
|
|
WarehouseConnection.of("https://test.example/openapi",
|
||
|
|
ChannelSecret.builder().appKey("k").appSecret("s")
|
||
|
|
.clientId("c").clientSecret("cs").build()),
|
||
|
|
"req-carrier", CLOCK);
|
||
|
|
}
|
||
|
|
|
||
|
|
private static RecordingTransport stub(String body) {
|
||
|
|
return RecordingTransport.create()
|
||
|
|
.route(common.ecommerce.edayun.EdayunConstants.PATH_TOKEN, req -> Responses.ok(
|
||
|
|
"{\"access_token\":\"T1\",\"token_type\":\"bearer\"}"))
|
||
|
|
.route(common.ecommerce.edayun.EdayunConstants.PATH_CARRIERS,
|
||
|
|
req -> Responses.ok(body));
|
||
|
|
}
|
||
|
|
|
||
|
|
// ------------------------------------------------------------ 请求
|
||
|
|
|
||
|
|
@Test
|
||
|
|
void numericWarehouseIdIsSent() {
|
||
|
|
EdayunCarrierDtos.ListCarrierRequest req =
|
||
|
|
EdayunCarrierMapper.toRequest("2", null);
|
||
|
|
|
||
|
|
assertEquals(Long.valueOf(2L), req.warehouseId);
|
||
|
|
assertNull(req.countryCode);
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 非数字仓 id 不下发——仓方会返回所有服务商。 */
|
||
|
|
@Test
|
||
|
|
void nonNumericWarehouseIdIsOmitted() {
|
||
|
|
assertNull(EdayunCarrierMapper.toRequest("USLA", null).warehouseId);
|
||
|
|
assertNull(EdayunCarrierMapper.toRequest(null, null).warehouseId);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test
|
||
|
|
void countryCodeIsTrimmed() {
|
||
|
|
assertEquals("US", EdayunCarrierMapper.toRequest("2", " US ").countryCode);
|
||
|
|
assertNull(EdayunCarrierMapper.toRequest("2", " ").countryCode);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ------------------------------------------------------------ 响应
|
||
|
|
|
||
|
|
private static EdayunCarrierDtos.Carrier carrier() {
|
||
|
|
EdayunCarrierDtos.OwnLogistic l1 = new EdayunCarrierDtos.OwnLogistic();
|
||
|
|
l1.logisticId = "583";
|
||
|
|
l1.logisticName = "Collections by Tiktok - FedEx";
|
||
|
|
|
||
|
|
EdayunCarrierDtos.OwnLogistic l2 = new EdayunCarrierDtos.OwnLogistic();
|
||
|
|
l2.logisticId = "250";
|
||
|
|
l2.logisticName = "FedEx-express";
|
||
|
|
|
||
|
|
EdayunCarrierDtos.Carrier c = new EdayunCarrierDtos.Carrier();
|
||
|
|
c.carrierId = "1";
|
||
|
|
c.carrierNameEn = "FedEx";
|
||
|
|
c.carrierNameCn = "联邦快递";
|
||
|
|
c.ownLogisticList = java.util.Arrays.asList(l1, l2);
|
||
|
|
return c;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test
|
||
|
|
void mapsCarrierWithLogistics() {
|
||
|
|
WarehouseCarrier c = EdayunCarrierMapper.toCarrier(carrier());
|
||
|
|
|
||
|
|
assertEquals("1", c.carrierId());
|
||
|
|
assertEquals("FedEx", c.nameEn());
|
||
|
|
assertEquals("联邦快递", c.nameCn());
|
||
|
|
assertEquals("联邦快递", c.displayName());
|
||
|
|
assertEquals(2, c.logists().size());
|
||
|
|
assertEquals("583", c.logists().get(0).logisticId());
|
||
|
|
assertEquals("FedEx-express", c.logists().get(1).name());
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 没有中文名时 displayName 退回英文名。 */
|
||
|
|
@Test
|
||
|
|
void displayNameFallsBackToEnglish() {
|
||
|
|
EdayunCarrierDtos.Carrier c = carrier();
|
||
|
|
c.carrierNameCn = null;
|
||
|
|
|
||
|
|
assertEquals("FedEx", EdayunCarrierMapper.toCarrier(c).displayName());
|
||
|
|
}
|
||
|
|
|
||
|
|
/** carrierId 缺失的行跳过——模型要求非空。 */
|
||
|
|
@Test
|
||
|
|
void carrierWithoutIdIsSkipped() {
|
||
|
|
EdayunCarrierDtos.Carrier bad = carrier();
|
||
|
|
bad.carrierId = " ";
|
||
|
|
|
||
|
|
List<WarehouseCarrier> list = EdayunCarrierMapper.toCarriers(
|
||
|
|
java.util.Arrays.asList(bad, carrier()));
|
||
|
|
|
||
|
|
assertEquals(1, list.size());
|
||
|
|
}
|
||
|
|
|
||
|
|
/** ownLogisticList 缺失或为空不抛异常。 */
|
||
|
|
@Test
|
||
|
|
void missingLogisticListYieldsEmpty() {
|
||
|
|
EdayunCarrierDtos.Carrier c = carrier();
|
||
|
|
c.ownLogisticList = null;
|
||
|
|
|
||
|
|
assertTrue(EdayunCarrierMapper.toCarrier(c).logists().isEmpty());
|
||
|
|
assertTrue(EdayunCarrierMapper.toCarriers(null).isEmpty());
|
||
|
|
assertTrue(EdayunCarrierMapper.toCarriers(
|
||
|
|
java.util.Collections.<EdayunCarrierDtos.Carrier>emptyList()).isEmpty());
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test
|
||
|
|
void adapterQueriesCarriers() {
|
||
|
|
RecordingTransport transport = stub("{\"code\":200,\"msg\":\"操作成功\",\"data\":[{"
|
||
|
|
+ "\"carrierId\":\"1\",\"carrierNameEn\":\"FedEx\",\"carrierNameCn\":\"FedEx\","
|
||
|
|
+ "\"ownLogisticList\":[{\"logisticId\":\"250\",\"logisticName\":\"FedEx-express\"}]}]}");
|
||
|
|
|
||
|
|
List<WarehouseCarrier> list = new EdayunAdapter(transport, new JacksonJsonCodec())
|
||
|
|
.carrierDirectory(ctx("2"));
|
||
|
|
|
||
|
|
assertEquals(1, list.size());
|
||
|
|
assertEquals("FedEx", list.get(0).nameEn());
|
||
|
|
assertEquals(1, list.get(0).logists().size());
|
||
|
|
assertTrue(transport.lastDataRequest().body().contains("\"warehouseId\":2"));
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 仓库列表接口易达云没有,显式拒绝而不是空列表。 */
|
||
|
|
@Test
|
||
|
|
void warehousesAreUnsupported() {
|
||
|
|
assertThrows(RuntimeException.class,
|
||
|
|
() -> new EdayunAdapter(stub("{}"), new JacksonJsonCodec())
|
||
|
|
.warehouses(ctx("2")));
|
||
|
|
}
|
||
|
|
}
|