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.
118 lines
5.5 KiB
118 lines
5.5 KiB
|
6 days ago
|
package common.ecommerce.edayun.probe;
|
||
|
|
|
||
|
|
import common.ecommerce.edayun.EdayunConstants;
|
||
|
|
import common.ecommerce.edayun.adapter.EdayunAdapter;
|
||
|
|
import common.ecommerce.edayun.adapter.EdayunCarrierMapper;
|
||
|
|
import common.ecommerce.edayun.dto.EdayunCarrierDtos;
|
||
|
|
import common.ecommerce.http.JacksonJsonCodec;
|
||
|
|
import common.ecommerce.http.OkHttpTransport;
|
||
|
|
import common.ecommerce.shared.error.ChannelException;
|
||
|
|
import common.ecommerce.shared.spi.ChannelSecret;
|
||
|
|
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.Assumptions;
|
||
|
|
import org.junit.jupiter.api.Test;
|
||
|
|
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
|
||
|
|
|
||
|
|
import java.time.Clock;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.UUID;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 承运商清单联调探针:<b>只读</b>。
|
||
|
|
*
|
||
|
|
* <pre>
|
||
|
|
* # 只看报文(不联网)
|
||
|
|
* mvn -pl warehouse-sdk-edayun -DfailIfNoTests=false \
|
||
|
|
* -Dedayun.probe.carrier=true -Dtest=EdayunCarrierProbeTest test
|
||
|
|
*
|
||
|
|
* # 真查(只读)
|
||
|
|
* ... -Dedayun.probe.carrier=true -Dedayun.probe.call=true \
|
||
|
|
* -Dedayun.probe.clientId=... -Dedayun.probe.clientSecret=... \
|
||
|
|
* -Dedayun.probe.appKey=... -Dedayun.probe.appSecret=... \
|
||
|
|
* [-Dedayun.probe.warehouseId=2] [-Dedayun.probe.country=US]
|
||
|
|
* </pre>
|
||
|
|
*/
|
||
|
|
@EnabledIfSystemProperty(named = "edayun.probe.carrier", matches = "true")
|
||
|
|
class EdayunCarrierProbeTest {
|
||
|
|
|
||
|
|
private static final String BASE_URL = opt("edayun.probe.baseUrl", EdayunConstants.PR_BASE_URL);
|
||
|
|
private static final String CLIENT_ID = System.getProperty("edayun.probe.clientId", "");
|
||
|
|
private static final String CLIENT_SECRET = System.getProperty("edayun.probe.clientSecret", "");
|
||
|
|
private static final String APP_KEY = System.getProperty("edayun.probe.appKey", "");
|
||
|
|
private static final String APP_SECRET = System.getProperty("edayun.probe.appSecret", "");
|
||
|
|
private static final String WAREHOUSE_ID = System.getProperty("edayun.probe.warehouseId", "");
|
||
|
|
private static final String COUNTRY = System.getProperty("edayun.probe.country", "");
|
||
|
|
private static final boolean CALL = Boolean.getBoolean("edayun.probe.call");
|
||
|
|
|
||
|
|
@Test
|
||
|
|
void probeCarriers() {
|
||
|
|
EdayunCarrierDtos.ListCarrierRequest req = EdayunCarrierMapper.toRequest(
|
||
|
|
emptyToNull(WAREHOUSE_ID), emptyToNull(COUNTRY));
|
||
|
|
|
||
|
|
System.out.println("\n================ edayun / POST "
|
||
|
|
+ EdayunConstants.PATH_CARRIERS + " ================");
|
||
|
|
System.out.println("[probe] baseUrl=" + BASE_URL
|
||
|
|
+ " warehouseId=" + mask(WAREHOUSE_ID) + " country=" + COUNTRY);
|
||
|
|
System.out.println("[request] " + new JacksonJsonCodec().toJson(req));
|
||
|
|
|
||
|
|
Assumptions.assumeTrue(CALL, "dry-run:未加 -Dedayun.probe.call=true,不实际调用");
|
||
|
|
Assumptions.assumeTrue(!CLIENT_ID.trim().isEmpty() && !CLIENT_SECRET.trim().isEmpty()
|
||
|
|
&& !APP_KEY.trim().isEmpty() && !APP_SECRET.trim().isEmpty(),
|
||
|
|
"真查需要 -Dedayun.probe.clientId / clientSecret / appKey / appSecret");
|
||
|
|
|
||
|
|
try {
|
||
|
|
WarehouseContext ctx = WarehouseContext.of("EDY-PROBE", WarehouseProvider.EDAYUN,
|
||
|
|
emptyToNull(WAREHOUSE_ID),
|
||
|
|
WarehouseConnection.of(BASE_URL,
|
||
|
|
ChannelSecret.builder()
|
||
|
|
.appKey(APP_KEY).appSecret(APP_SECRET)
|
||
|
|
.clientId(CLIENT_ID).clientSecret(CLIENT_SECRET)
|
||
|
|
.build()),
|
||
|
|
"probe-" + UUID.randomUUID(), Clock.systemUTC());
|
||
|
|
EdayunAdapter adapter = new EdayunAdapter(
|
||
|
|
new OkHttpTransport("common-ecommerce/1.0.0 (edayun-probe)", 10_000, 60_000),
|
||
|
|
new JacksonJsonCodec());
|
||
|
|
|
||
|
|
List<WarehouseCarrier> carriers = adapter.carrierDirectory(ctx);
|
||
|
|
System.out.println("[OK] carriers=" + carriers.size());
|
||
|
|
for (WarehouseCarrier c : carriers) {
|
||
|
|
System.out.println(" " + c.carrierId() + " " + c.displayName()
|
||
|
|
+ " logistics=" + c.logists().size());
|
||
|
|
for (WarehouseCarrier.OwnLogistic l : c.logists()) {
|
||
|
|
System.out.println(" - " + l.logisticId() + " " + l.name());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} catch (RuntimeException e) {
|
||
|
|
String msg = e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage();
|
||
|
|
System.out.println("[ERROR] " + e.getClass().getSimpleName() + ": "
|
||
|
|
+ (msg.length() <= 800 ? msg : msg.substring(0, 800) + "…"));
|
||
|
|
if (e instanceof ChannelException) {
|
||
|
|
ChannelException ce = (ChannelException) e;
|
||
|
|
System.out.println(" platformCode=" + ce.platformCode()
|
||
|
|
+ " retryable=" + ce.retryable());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private static String opt(String name, String fallback) {
|
||
|
|
String value = System.getProperty(name, "");
|
||
|
|
String trimmed = value == null ? "" : value.trim();
|
||
|
|
return trimmed.isEmpty() ? fallback : trimmed;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static String emptyToNull(String v) {
|
||
|
|
return v == null || v.trim().isEmpty() ? null : v.trim();
|
||
|
|
}
|
||
|
|
|
||
|
|
private static String mask(String v) {
|
||
|
|
if (v == null || v.trim().isEmpty()) {
|
||
|
|
return "(empty)";
|
||
|
|
}
|
||
|
|
String t = v.trim();
|
||
|
|
return t.length() <= 6 ? "***" : t.substring(0, 3) + "***" + t.substring(t.length() - 2);
|
||
|
|
}
|
||
|
|
}
|