Browse Source
POST /webApi/skuInfo/v2/printBarcode → WarehouseLabel 与入库 SKU 贴标(inbound().printSkuLabel)不是一回事: - 那个绑定入库计划单、14 种小纸尺寸、异步返回任务号 - 这个按任意 SKU 打、4 档纸张、同步返回 PDF 地址 纸张集合完全不重叠,所以新建 BarcodePaperSize 而不是复用 SkuLabelSizeType。 新增 core 层 BarcodePrintCapable / BarcodePrintCommand / BarcodePaperSize, 能力位 BARCODE_PRINT,门面 printBarcodes(cmd)。 本地提前挡两条(仓方对这两条的返回都是「内部服务器错误」,看不出原因): - SKU 数 > 50(仓方提示「最多打印50个SKU」) - 份数 <= 0 isPrintSkuName 只在 true 时下发,false 交给仓方默认。 真机(2026-11-16):ybtest + A4 拿到真实 PDF;四种纸张尺寸都 code=200。 其余实测校验:缺 productInfos →「产品信息不能为空」、缺 pageSize → 「纸张尺寸不能为空」、pageSize 非法 / num=0 →「内部服务器错误」、 SKU 不存在 →「sku=XXX信息不存在」。 Co-Authored-By: Claude <noreply@anthropic.com>feature/Walmart
10 changed files with 650 additions and 3 deletions
@ -0,0 +1,27 @@
@@ -0,0 +1,27 @@
|
||||
package common.ecommerce.warehouse.capability; |
||||
|
||||
import common.ecommerce.warehouse.model.BarcodePrintCommand; |
||||
import common.ecommerce.warehouse.model.WarehouseLabel; |
||||
import common.ecommerce.warehouse.sdk.WarehouseAdapter; |
||||
import common.ecommerce.warehouse.sdk.WarehouseContext; |
||||
|
||||
/** |
||||
* 商品条码打印能力:按 SKU 打条码贴纸,不依赖任何单据。 |
||||
* |
||||
* <p>与 {@link InboundCapable#printSkuLabel} 不是一回事:那个是<b>入库计划单</b>的贴标 |
||||
* (绑定入库单号、有 14 种小纸尺寸、仓方异步生成);这个是<b>按 SKU 打条码</b>, |
||||
* 仓库 / 门店自己贴标用,仓方同步返回文件地址。</p> |
||||
* |
||||
* <p>只读语义(不改业务状态),但会生成文件;重复调用只是再生成一份。</p> |
||||
*/ |
||||
public interface BarcodePrintCapable extends WarehouseAdapter { |
||||
|
||||
/** |
||||
* 打印 SKU 条码。 |
||||
* |
||||
* @return 已生成的文件(地址或 base64);仓方未返回文件时抛出异常,不返回 null |
||||
* @throws common.ecommerce.warehouse.error.WarehouseUnsupportedCapabilityException |
||||
* 该仓不声明 {@code BARCODE_PRINT} |
||||
*/ |
||||
WarehouseLabel printBarcodes(WarehouseContext ctx, BarcodePrintCommand cmd); |
||||
} |
||||
@ -0,0 +1,37 @@
@@ -0,0 +1,37 @@
|
||||
package common.ecommerce.warehouse.enums; |
||||
|
||||
/** |
||||
* SKU 条码打印的纸张尺寸。 |
||||
* |
||||
* <p>易达云 {@code printBarcode} 的 {@code pageSize}:0 7×3cm / 1 10×6cm / |
||||
* 2 6×4cm / 3 A4。<b>只有这 4 档</b>,与入库贴标用的 |
||||
* {@link SkuLabelSizeType}(14 种小尺寸)不是一套,不能互换。</p> |
||||
*/ |
||||
public enum BarcodePaperSize { |
||||
|
||||
/** 0:70mm × 30mm。 */ |
||||
SIZE_70_30(0, "7×3cm"), |
||||
/** 1:100mm × 60mm。 */ |
||||
SIZE_100_60(1, "10×6cm"), |
||||
/** 2:60mm × 40mm。 */ |
||||
SIZE_60_40(2, "6×4cm"), |
||||
/** 3:A4。 */ |
||||
A4(3, "A4"); |
||||
|
||||
private final int code; |
||||
private final String label; |
||||
|
||||
BarcodePaperSize(int code, String label) { |
||||
this.code = code; |
||||
this.label = label; |
||||
} |
||||
|
||||
/** 仓方码值。 */ |
||||
public int code() { |
||||
return code; |
||||
} |
||||
|
||||
public String label() { |
||||
return label; |
||||
} |
||||
} |
||||
@ -0,0 +1,126 @@
@@ -0,0 +1,126 @@
|
||||
package common.ecommerce.warehouse.model; |
||||
|
||||
import common.ecommerce.warehouse.enums.BarcodePaperSize; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* SKU 条码打印命令(打商品条码,不是入库单的 SKU 贴标)。 |
||||
* |
||||
* <p>与 {@link SkuLabelCommand} 的区别:那个是<b>入库计划单</b>的贴标(绑定 |
||||
* 入库单号、14 种小纸尺寸);这个是<b>按 SKU 打条码</b>,不依赖任何单据, |
||||
* 仓库 / 门店自己贴标用。</p> |
||||
* |
||||
* <p>仓方<b>同步</b>返回文件地址(不是异步任务),所以返回的是 |
||||
* {@link WarehouseLabel} 而不是 {@link LabelPrintTask}。</p> |
||||
*/ |
||||
public final class BarcodePrintCommand { |
||||
|
||||
/** 一次最多打多少种 SKU。 */ |
||||
public static final int MAX_SKUS = 50; |
||||
|
||||
private final BarcodePaperSize paperSize; |
||||
/** 备注,如 {@code Made in China}。 */ |
||||
private final String remark; |
||||
/** 是否打印 SKU 名称。 */ |
||||
private final boolean printSkuName; |
||||
private final List<BarcodeItem> items; |
||||
|
||||
private BarcodePrintCommand(Builder b) { |
||||
if (b.paperSize == null) { |
||||
throw new IllegalArgumentException("paperSize is required"); |
||||
} |
||||
this.paperSize = b.paperSize; |
||||
this.remark = b.remark; |
||||
this.printSkuName = b.printSkuName; |
||||
if (b.items.isEmpty()) { |
||||
throw new IllegalArgumentException("items must not be empty"); |
||||
} |
||||
if (b.items.size() > MAX_SKUS) { |
||||
throw new IllegalArgumentException( |
||||
"at most " + MAX_SKUS + " skus per print, got " + b.items.size()); |
||||
} |
||||
this.items = Collections.unmodifiableList(new ArrayList<BarcodeItem>(b.items)); |
||||
} |
||||
|
||||
public static Builder builder() { |
||||
return new Builder(); |
||||
} |
||||
|
||||
public BarcodePaperSize paperSize() { |
||||
return paperSize; |
||||
} |
||||
|
||||
/** 备注;null 表示不打印。 */ |
||||
public String remark() { |
||||
return remark; |
||||
} |
||||
|
||||
/** 是否打印 SKU 名称。 */ |
||||
public boolean printSkuName() { |
||||
return printSkuName; |
||||
} |
||||
|
||||
public List<BarcodeItem> items() { |
||||
return items; |
||||
} |
||||
|
||||
/** 一个 SKU 及其打印份数。 */ |
||||
public static final class BarcodeItem { |
||||
|
||||
private final String providerSku; |
||||
private final int copies; |
||||
|
||||
public BarcodeItem(String providerSku, int copies) { |
||||
if (providerSku == null || providerSku.trim().isEmpty()) { |
||||
throw new IllegalArgumentException("providerSku must not be blank"); |
||||
} |
||||
if (copies <= 0) { |
||||
throw new IllegalArgumentException("copies must be positive"); |
||||
} |
||||
this.providerSku = providerSku.trim(); |
||||
this.copies = copies; |
||||
} |
||||
|
||||
public String providerSku() { |
||||
return providerSku; |
||||
} |
||||
|
||||
public int copies() { |
||||
return copies; |
||||
} |
||||
} |
||||
|
||||
public static final class Builder { |
||||
private BarcodePaperSize paperSize; |
||||
private String remark; |
||||
private boolean printSkuName; |
||||
private final List<BarcodeItem> items = new ArrayList<BarcodeItem>(); |
||||
|
||||
public Builder paperSize(BarcodePaperSize v) { |
||||
this.paperSize = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder remark(String v) { |
||||
this.remark = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder printSkuName(boolean v) { |
||||
this.printSkuName = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder item(String providerSku, int copies) { |
||||
this.items.add(new BarcodeItem(providerSku, copies)); |
||||
return this; |
||||
} |
||||
|
||||
public BarcodePrintCommand build() { |
||||
return new BarcodePrintCommand(this); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,70 @@
@@ -0,0 +1,70 @@
|
||||
package common.ecommerce.edayun.adapter; |
||||
|
||||
import common.ecommerce.edayun.dto.EdayunBarcodeDtos; |
||||
import common.ecommerce.warehouse.model.BarcodePrintCommand; |
||||
import common.ecommerce.warehouse.model.WarehouseLabel; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 易达云条码打印映射({@code POST /webApi/skuInfo/v2/printBarcode})。 |
||||
* |
||||
* <p>响应 {@code data} 是文件地址字符串——同步生成,不像入库箱标那样只有任务号。</p> |
||||
*/ |
||||
public final class EdayunBarcodeMapper { |
||||
|
||||
/** 单次最多打印的 SKU 数(文档:最大 50 个)。 */ |
||||
static final int MAX_SKUS = 50; |
||||
|
||||
private EdayunBarcodeMapper() { |
||||
} |
||||
|
||||
/** |
||||
* 统一命令 → 打印请求。 |
||||
* |
||||
* @throws IllegalArgumentException SKU 数超限(本地先挡,省一次失败请求) |
||||
*/ |
||||
public static EdayunBarcodeDtos.PrintBarcodeRequest toRequest(BarcodePrintCommand cmd) { |
||||
EdayunBarcodeDtos.PrintBarcodeRequest req = new EdayunBarcodeDtos.PrintBarcodeRequest(); |
||||
req.pageSize = cmd.paperSize().code(); |
||||
req.remark = cmd.remark(); |
||||
// 只有 true 才下发:false 是仓方默认,不下发省得干扰它自己的默认值
|
||||
req.isPrintSkuName = cmd.printSkuName() ? Boolean.TRUE : null; |
||||
req.productInfos = toItems(cmd.items()); |
||||
return req; |
||||
} |
||||
|
||||
private static List<EdayunBarcodeDtos.ProductInfo> toItems( |
||||
List<BarcodePrintCommand.BarcodeItem> items) { |
||||
if (items.size() > MAX_SKUS) { |
||||
throw new IllegalArgumentException( |
||||
"edayun prints at most " + MAX_SKUS + " skus at once, got " + items.size() |
||||
+ "; split into multiple calls"); |
||||
} |
||||
List<EdayunBarcodeDtos.ProductInfo> out = |
||||
new ArrayList<EdayunBarcodeDtos.ProductInfo>(items.size()); |
||||
for (BarcodePrintCommand.BarcodeItem item : items) { |
||||
EdayunBarcodeDtos.ProductInfo info = new EdayunBarcodeDtos.ProductInfo(); |
||||
info.skuCode = item.providerSku(); |
||||
info.num = item.copies(); |
||||
out.add(info); |
||||
} |
||||
return out; |
||||
} |
||||
|
||||
/** |
||||
* 响应 → 面单对象。 |
||||
* |
||||
* <p>文件地址可能为空——仓方生成失败时仍可能回 {@code code=200}。调用方应在 |
||||
* 调用前确认 {@code data} 非空,适配器已做该检查。</p> |
||||
*/ |
||||
public static WarehouseLabel toLabel(String fileUrl, BarcodePrintCommand cmd) { |
||||
return WarehouseLabel.builder() |
||||
// 仓库不返回文件 id,用 SKU 数 + 纸张尺寸拼一个可追溯的编号
|
||||
.labelId("barcode-" + cmd.paperSize().name() + "-" + cmd.items().size()) |
||||
.format("PDF") |
||||
.url(fileUrl) |
||||
.build(); |
||||
} |
||||
} |
||||
@ -0,0 +1,52 @@
@@ -0,0 +1,52 @@
|
||||
package common.ecommerce.edayun.dto; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 易达云报文结构:打印条码({@code POST /webApi/skuInfo/v2/printBarcode})。 |
||||
* |
||||
* <p>一个文件对应一个接口。响应 {@code data} 是<b>文件地址字符串</b>, |
||||
* 不是对象——与入库箱标那种「异步任务号」不同,这里同步给文件。</p> |
||||
*/ |
||||
public final class EdayunBarcodeDtos { |
||||
|
||||
private EdayunBarcodeDtos() { |
||||
} |
||||
|
||||
/** 打印请求。 */ |
||||
@JsonInclude(JsonInclude.Include.NON_NULL) |
||||
public static final class PrintBarcodeRequest { |
||||
|
||||
/** 备注,如 {@code Made in China};≤64。 */ |
||||
public String remark; |
||||
/** 是否打印 SKU 名称。 */ |
||||
public Boolean isPrintSkuName; |
||||
|
||||
/** 纸张尺寸:0 7×3cm / 1 10×6cm / 2 6×4cm / 3 A4。<b>必填</b>。 */ |
||||
public Integer pageSize; |
||||
|
||||
/** 产品信息,最大 50 个 SKU。<b>必填</b>。 */ |
||||
public List<ProductInfo> productInfos; |
||||
} |
||||
|
||||
/** 一个 SKU。 */ |
||||
@JsonInclude(JsonInclude.Include.NON_NULL) |
||||
public static final class ProductInfo { |
||||
public String skuCode; |
||||
/** 打印份数;必须 > 0。 */ |
||||
public Integer num; |
||||
} |
||||
|
||||
/** 打印响应。 */ |
||||
@JsonInclude(JsonInclude.Include.NON_NULL) |
||||
public static final class PrintBarcodeResponse { |
||||
public Integer code; |
||||
public String msg; |
||||
/** 条码文件地址(PDF)。 */ |
||||
public String data; |
||||
/** 文档标为 object,实际是字符串 {@code "null"};保留 Object 避免解析失败。 */ |
||||
public Object traceId; |
||||
} |
||||
} |
||||
@ -0,0 +1,166 @@
@@ -0,0 +1,166 @@
|
||||
package common.ecommerce.edayun.adapter; |
||||
|
||||
import common.ecommerce.edayun.dto.EdayunBarcodeDtos; |
||||
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.BarcodePaperSize; |
||||
import common.ecommerce.warehouse.enums.WarehouseProvider; |
||||
import common.ecommerce.warehouse.model.BarcodePrintCommand; |
||||
import common.ecommerce.warehouse.model.WarehouseLabel; |
||||
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 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/skuInfo/v2/printBarcode})映射。 |
||||
* |
||||
* <p>重点:响应 data 是文件地址、同步返回(不是任务号)、50 个 SKU 上限。</p> |
||||
*/ |
||||
class EdayunBarcodeTest { |
||||
|
||||
private static final Clock CLOCK = Clock.fixed(Instant.parse("2026-11-16T00:00:00Z"), ZoneOffset.UTC); |
||||
|
||||
private static WarehouseContext ctx() { |
||||
return WarehouseContext.of("EDY-WH", WarehouseProvider.EDAYUN, "2", |
||||
WarehouseConnection.of("https://test.example/openapi", |
||||
ChannelSecret.builder().appKey("k").appSecret("s") |
||||
.clientId("c").clientSecret("cs").build()), |
||||
"req-barcode", 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_BARCODE_PRINT, |
||||
req -> Responses.ok(body)); |
||||
} |
||||
|
||||
private static BarcodePrintCommand command() { |
||||
return BarcodePrintCommand.builder() |
||||
.paperSize(BarcodePaperSize.SIZE_70_30) |
||||
.remark("Made in China") |
||||
.printSkuName(false) |
||||
.item("ybtest", 2) |
||||
.build(); |
||||
} |
||||
|
||||
// ------------------------------------------------------------ 请求
|
||||
|
||||
@Test |
||||
void mapsRequest() { |
||||
EdayunBarcodeDtos.PrintBarcodeRequest req = EdayunBarcodeMapper.toRequest(command()); |
||||
|
||||
assertEquals(Integer.valueOf(0), req.pageSize); |
||||
assertEquals("Made in China", req.remark); |
||||
assertEquals(1, req.productInfos.size()); |
||||
assertEquals("ybtest", req.productInfos.get(0).skuCode); |
||||
assertEquals(Integer.valueOf(2), req.productInfos.get(0).num); |
||||
} |
||||
|
||||
/** isPrintSkuName 只在 true 时下发,false 交给仓方默认。 */ |
||||
@Test |
||||
void printSkuNameOnlySentWhenTrue() { |
||||
assertNull(EdayunBarcodeMapper.toRequest(command()).isPrintSkuName); |
||||
|
||||
BarcodePrintCommand withName = BarcodePrintCommand.builder() |
||||
.paperSize(BarcodePaperSize.A4) |
||||
.printSkuName(true) |
||||
.item("sku", 1) |
||||
.build(); |
||||
assertEquals(Boolean.TRUE, EdayunBarcodeMapper.toRequest(withName).isPrintSkuName); |
||||
} |
||||
|
||||
@Test |
||||
void paperSizesMap() { |
||||
assertEquals(0, BarcodePaperSize.SIZE_70_30.code()); |
||||
assertEquals(1, BarcodePaperSize.SIZE_100_60.code()); |
||||
assertEquals(2, BarcodePaperSize.SIZE_60_40.code()); |
||||
assertEquals(3, BarcodePaperSize.A4.code()); |
||||
} |
||||
|
||||
/** 超过 50 个 SKU 本地就挡,不发请求(仓方也会报「最多打印50个SKU」)。 */ |
||||
@Test |
||||
void moreThan50SkusRejectedLocally() { |
||||
BarcodePrintCommand.Builder b = BarcodePrintCommand.builder() |
||||
.paperSize(BarcodePaperSize.A4); |
||||
for (int i = 0; i < 51; i++) { |
||||
b.item("SKU-" + i, 1); |
||||
} |
||||
|
||||
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, |
||||
() -> EdayunBarcodeMapper.toRequest(b.build())); |
||||
|
||||
assertTrue(ex.getMessage().contains("at most 50"), ex.getMessage()); |
||||
} |
||||
|
||||
/** 空 SKU 列表与非法份数在命令构造期就被拒。 */ |
||||
@Test |
||||
void invalidCommandRejectedAtConstruction() { |
||||
assertThrows(IllegalArgumentException.class, |
||||
() -> BarcodePrintCommand.builder().paperSize(BarcodePaperSize.A4).build()); |
||||
assertThrows(IllegalArgumentException.class, |
||||
() -> BarcodePrintCommand.builder().paperSize(BarcodePaperSize.A4) |
||||
.item("sku", 0)); |
||||
assertThrows(IllegalArgumentException.class, |
||||
() -> BarcodePrintCommand.builder().paperSize(BarcodePaperSize.A4) |
||||
.item(" ", 1)); |
||||
// 缺纸张尺寸
|
||||
assertThrows(IllegalArgumentException.class, |
||||
() -> BarcodePrintCommand.builder().item("sku", 1).build()); |
||||
} |
||||
|
||||
// ------------------------------------------------------------ 响应
|
||||
|
||||
/** data 是文件地址字符串,直接落 WarehouseLabel.url。 */ |
||||
@Test |
||||
void responseDataIsFileUrl() { |
||||
RecordingTransport transport = stub("{\"code\":200,\"msg\":\"操作成功\"," |
||||
+ "\"data\":\"https://oss.edayun.com/barcode1.pdf\",\"traceId\":null}"); |
||||
|
||||
WarehouseLabel label = new EdayunAdapter(transport, new JacksonJsonCodec()) |
||||
.printBarcodes(ctx(), command()); |
||||
|
||||
assertEquals("https://oss.edayun.com/barcode1.pdf", label.url()); |
||||
assertEquals("PDF", label.format()); |
||||
assertNull(label.contentBase64()); |
||||
assertTrue(transport.lastDataRequest().body().contains("\"skuCode\":\"ybtest\"")); |
||||
} |
||||
|
||||
/** code=200 但 data 为空 → 抛异常,不能返回没有文件的对象。 */ |
||||
@Test |
||||
void blankDataFails() { |
||||
RecordingTransport transport = stub( |
||||
"{\"code\":200,\"msg\":\"操作成功\",\"data\":\" \"}"); |
||||
|
||||
assertThrows(RuntimeException.class, |
||||
() -> new EdayunAdapter(transport, new JacksonJsonCodec()) |
||||
.printBarcodes(ctx(), command())); |
||||
} |
||||
|
||||
/** 仓方业务错误(如 SKU 不存在)要抛异常并带出原因。 */ |
||||
@Test |
||||
void businessErrorFails() { |
||||
RecordingTransport transport = stub( |
||||
"{\"code\":500,\"msg\":\"sku=NO-SUCH信息不存在\",\"data\":null}"); |
||||
|
||||
try { |
||||
new EdayunAdapter(transport, new JacksonJsonCodec()) |
||||
.printBarcodes(ctx(), command()); |
||||
org.junit.jupiter.api.Assertions.fail("should have thrown"); |
||||
} catch (RuntimeException e) { |
||||
assertTrue(e.getMessage().contains("SKU") || e.getMessage().contains("sku"), e.getMessage()); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,104 @@
@@ -0,0 +1,104 @@
|
||||
package common.ecommerce.edayun.probe; |
||||
|
||||
import common.ecommerce.edayun.EdayunConstants; |
||||
import common.ecommerce.edayun.adapter.EdayunAdapter; |
||||
import common.ecommerce.edayun.adapter.EdayunBarcodeMapper; |
||||
import common.ecommerce.edayun.dto.EdayunBarcodeDtos; |
||||
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.BarcodePaperSize; |
||||
import common.ecommerce.warehouse.enums.WarehouseProvider; |
||||
import common.ecommerce.warehouse.model.BarcodePrintCommand; |
||||
import common.ecommerce.warehouse.model.WarehouseLabel; |
||||
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.UUID; |
||||
|
||||
/** |
||||
* 打印条码联调探针:<b>只读</b>(生成 PDF,不改仓侧业务数据)。 |
||||
* |
||||
* <pre> |
||||
* # 只看报文(不联网) |
||||
* mvn -pl warehouse-sdk-edayun -DfailIfNoTests=false \ |
||||
* -Dedayun.probe.barcode=true -Dtest=EdayunBarcodeProbeTest test |
||||
* |
||||
* # 真跑 |
||||
* ... -Dedayun.probe.barcode=true -Dedayun.probe.call=true \ |
||||
* -Dedayun.probe.clientId=... -Dedayun.probe.clientSecret=... \ |
||||
* -Dedayun.probe.appKey=... -Dedayun.probe.appSecret=... \ |
||||
* [-Dedayun.probe.sku=ybtest] |
||||
* </pre> |
||||
*/ |
||||
@EnabledIfSystemProperty(named = "edayun.probe.barcode", matches = "true") |
||||
class EdayunBarcodeProbeTest { |
||||
|
||||
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 SKU = opt("edayun.probe.sku", "ybtest"); |
||||
private static final boolean CALL = Boolean.getBoolean("edayun.probe.call"); |
||||
|
||||
@Test |
||||
void probePrintBarcode() { |
||||
BarcodePrintCommand cmd = BarcodePrintCommand.builder() |
||||
.paperSize(BarcodePaperSize.A4) |
||||
.remark("PROBE") |
||||
.printSkuName(true) |
||||
.item(SKU, 1) |
||||
.build(); |
||||
|
||||
EdayunBarcodeDtos.PrintBarcodeRequest req = EdayunBarcodeMapper.toRequest(cmd); |
||||
String bodyJson = new JacksonJsonCodec().toJson(req); |
||||
|
||||
System.out.println("\n================ edayun / POST " |
||||
+ EdayunConstants.PATH_BARCODE_PRINT + " ================"); |
||||
System.out.println("[probe] baseUrl=" + BASE_URL + " sku=" + SKU); |
||||
System.out.println("[request] " + bodyJson); |
||||
|
||||
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, null, |
||||
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()); |
||||
|
||||
WarehouseLabel label = adapter.printBarcodes(ctx, cmd); |
||||
System.out.println("[OK] labelId=" + label.labelId() + " format=" + label.format() |
||||
+ " url=" + label.url()); |
||||
} 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; |
||||
} |
||||
} |
||||
Loading…
Reference in new issue