Browse Source
- 新增 HengjianOrderAttachmentDtos,在 OutboundCapable 上加 uploadAttachments()(default 拒绝),恒健覆盖 - 门面新增 outbound().attachment(ref, list):写操作走幂等守卫, 业务键是「单号 + 附件名 + attachment」——同附件重传幂等、 不同附件各自一次(只用单号做键会让第二次附件被判成重复) - 请求平铺;type 在这个接口是 INTEGER 0~6(创建订单那边标 STRING, 两页不一致),认不出即拒绝,不猜 - 给了 contentBase64 时忽略 url 并剥掉 data: 前缀(仓方语义) - 真机无 token 时直接 403、不做参数校验,必填项按文档实现: orderNo + 非空列表,每行 name/type 必填、url 与 file 二选一 - 响应无 data,只有 code/message 验证:mvn test 全量通过,恒健 305 个用例(+12)通过。 Co-Authored-By: Claude Code <noreply@anthropic.com>feature/Walmart
8 changed files with 454 additions and 6 deletions
@ -0,0 +1,58 @@
@@ -0,0 +1,58 @@
|
||||
package common.ecommerce.hengjian.dto; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 恒健报文结构:订单上传附件({@code POST /v1/saleOrder/createAttachment})。 |
||||
* |
||||
* <p><b>请求平铺</b>(无 {@code params} 包裹),与创建订单一致。</p> |
||||
* |
||||
* <p><b>真机情况</b>:无 token 直接 403、<b>不做参数校验</b>,必填项按文档字段表: |
||||
* {@code orderNo}、{@code attachInfoItemRequestList}(非空),每行 {@code name} / {@code type}, |
||||
* 以及 {@code url} 与 {@code file} <b>二选一</b>。</p> |
||||
* |
||||
* <p><b>响应无 data</b>:只有 {@code code / message}。</p> |
||||
*/ |
||||
public final class HengjianOrderAttachmentDtos { |
||||
|
||||
private HengjianOrderAttachmentDtos() { |
||||
} |
||||
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL) |
||||
public static final class CreateAttachmentRequest { |
||||
|
||||
/** 订单号(来源单号);必填。 */ |
||||
public String orderNo; |
||||
|
||||
/** 附件列表;必填。 */ |
||||
public List<AttachInfoItem> attachInfoItemRequestList; |
||||
} |
||||
|
||||
/** 一条附件。 */ |
||||
@JsonInclude(JsonInclude.Include.NON_NULL) |
||||
public static final class AttachInfoItem { |
||||
|
||||
/** 附件名称;必填。文档建议用 trackingNo 且不带后缀。 */ |
||||
public String name; |
||||
|
||||
/** 附件 URL;与 {@link #file} 二选一。 */ |
||||
public String url; |
||||
|
||||
/** |
||||
* 附件类型;必填。 |
||||
* |
||||
* <p>0 Shippinglabel / 1 Packinglist / 2 BOL / 3 Other / 4 CartonLabel / |
||||
* 5 PalletLabel / 6 itemlabel。<b>文档标 INTEGER</b>(创建订单那边是 STRING, |
||||
* 两页不一致)——这里按 integer 发。</p> |
||||
*/ |
||||
public Integer type; |
||||
|
||||
/** 备注。 */ |
||||
public String remark; |
||||
|
||||
/** base64 内容;给了就忽略 {@link #url}。 */ |
||||
public String file; |
||||
} |
||||
} |
||||
@ -0,0 +1,208 @@
@@ -0,0 +1,208 @@
|
||||
package common.ecommerce.hengjian.adapter; |
||||
|
||||
import common.ecommerce.hengjian.HengjianConstants; |
||||
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.OutboundRef; |
||||
import common.ecommerce.warehouse.model.WarehouseAttachment; |
||||
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.Arrays; |
||||
import java.util.Collections; |
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||
import static org.junit.jupiter.api.Assertions.assertFalse; |
||||
import static org.junit.jupiter.api.Assertions.assertThrows; |
||||
import static org.junit.jupiter.api.Assertions.assertTrue; |
||||
|
||||
/** |
||||
* 订单上传附件({@code /v1/saleOrder/createAttachment})契约测试。 |
||||
*/ |
||||
class HengjianOrderAttachmentTest { |
||||
|
||||
private static final WarehouseContext CTX = WarehouseContext.of( |
||||
"HJ-EFCA", WarehouseProvider.HENGJIAN, "EFCA", |
||||
WarehouseConnection.of(HengjianConstants.TEST_BASE_URL, |
||||
ChannelSecret.builder().appSecret("token-1").build()), |
||||
"req-1", Clock.fixed(Instant.parse("2026-11-17T00:00:00Z"), ZoneOffset.UTC)); |
||||
|
||||
private static final String OK_BODY = |
||||
"{\"statusCode\":200,\"status\":0,\"code\":200,\"message\":\"上传成功\"}"; |
||||
|
||||
private static HengjianAdapter adapter(RecordingTransport transport) { |
||||
return new HengjianAdapter(transport, new JacksonJsonCodec()); |
||||
} |
||||
|
||||
private static RecordingTransport uploading() { |
||||
return RecordingTransport.create() |
||||
.route(HengjianConstants.PATH_ORDER_ATTACHMENT, req -> Responses.ok(OK_BODY)); |
||||
} |
||||
|
||||
private static WarehouseAttachment urlAttachment() { |
||||
return WarehouseAttachment.builder() |
||||
.name("1ZXH1234567890").type("0") |
||||
.url("https://cdn/label.pdf").build(); |
||||
} |
||||
|
||||
// ---------------------------------------------------------------- 请求形状
|
||||
|
||||
@Test |
||||
void postsToCreateAttachment() { |
||||
RecordingTransport transport = uploading(); |
||||
adapter(transport).uploadAttachments(CTX, OutboundRef.ofRequestNo("OB-1"), |
||||
Collections.singletonList(urlAttachment())); |
||||
|
||||
assertEquals("POST", transport.lastDataRequest().method()); |
||||
assertEquals(HengjianConstants.TEST_BASE_URL + "/v1/saleOrder/createAttachment", |
||||
transport.lastDataRequest().url()); |
||||
} |
||||
|
||||
/** 请求平铺(无 params 包裹),字段名与文档一致。 */ |
||||
@Test |
||||
void sendsFlatBody() { |
||||
RecordingTransport transport = uploading(); |
||||
adapter(transport).uploadAttachments(CTX, OutboundRef.ofRequestNo("OB-1"), |
||||
Collections.singletonList(urlAttachment())); |
||||
String body = transport.lastDataRequest().body(); |
||||
|
||||
assertTrue(body.contains("\"orderNo\":\"OB-1\""), body); |
||||
assertTrue(body.contains("\"attachInfoItemRequestList\":["), body); |
||||
assertTrue(body.contains("\"name\":\"1ZXH1234567890\""), body); |
||||
assertTrue(body.contains("\"url\":\"https://cdn/label.pdf\""), body); |
||||
// 类型按 INTEGER 下发
|
||||
assertTrue(body.contains("\"type\":0"), body); |
||||
assertFalse(body.contains("\"params\""), "这个接口是平铺的,body=" + body); |
||||
} |
||||
|
||||
/** 给了 base64 就忽略 url(仓方语义)。 */ |
||||
@Test |
||||
void contentBase64TakesPrecedence() { |
||||
RecordingTransport transport = uploading(); |
||||
adapter(transport).uploadAttachments(CTX, OutboundRef.ofRequestNo("OB-1"), |
||||
Collections.singletonList(WarehouseAttachment.builder() |
||||
.name("label").type("0") |
||||
.url("https://cdn/label.pdf") |
||||
.contentBase64("data:application/pdf;base64,JVBERi0xLjQK") |
||||
.build())); |
||||
String body = transport.lastDataRequest().body(); |
||||
|
||||
assertTrue(body.contains("\"file\":\"JVBERi0xLjQK\""), body); |
||||
assertFalse(body.contains("\"url\""), "有 file 时不发 url"); |
||||
assertFalse(body.contains("data:application/pdf"), "data: 前缀要剥掉"); |
||||
} |
||||
|
||||
/** 支持一次传多个附件(7 种类型)。 */ |
||||
@Test |
||||
void sendsMultipleAttachments() { |
||||
RecordingTransport transport = uploading(); |
||||
adapter(transport).uploadAttachments(CTX, OutboundRef.ofRequestNo("OB-1"), Arrays.asList( |
||||
WarehouseAttachment.builder().name("label").type("0").url("https://cdn/l.pdf").build(), |
||||
WarehouseAttachment.builder().name("packing").type("1").url("https://cdn/p.pdf").build(), |
||||
WarehouseAttachment.builder().name("bol").type("2").url("https://cdn/b.pdf").build())); |
||||
String body = transport.lastDataRequest().body(); |
||||
|
||||
assertTrue(body.contains("\"type\":0"), body); |
||||
assertTrue(body.contains("\"type\":1"), body); |
||||
assertTrue(body.contains("\"type\":2"), body); |
||||
} |
||||
|
||||
/** 只给单号(无 outboundRequestNo)时用 providerOrderId。 */ |
||||
@Test |
||||
void acceptsProviderOrderId() { |
||||
RecordingTransport transport = uploading(); |
||||
adapter(transport).uploadAttachments(CTX, OutboundRef.ofProviderOrderId("193252852"), |
||||
Collections.singletonList(urlAttachment())); |
||||
|
||||
assertTrue(transport.lastDataRequest().body().contains("\"orderNo\":\"193252852\"")); |
||||
} |
||||
|
||||
// ---------------------------------------------------------------- 本地校验
|
||||
|
||||
@Test |
||||
void rejectsMissingName() { |
||||
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, |
||||
() -> adapter(uploading()).uploadAttachments(CTX, OutboundRef.ofRequestNo("OB-1"), |
||||
Collections.singletonList(WarehouseAttachment.builder() |
||||
.type("0").url("https://cdn/l.pdf").build()))); |
||||
assertTrue(ex.getMessage().contains("name"), ex.getMessage()); |
||||
} |
||||
|
||||
@Test |
||||
void rejectsMissingType() { |
||||
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, |
||||
() -> adapter(uploading()).uploadAttachments(CTX, OutboundRef.ofRequestNo("OB-1"), |
||||
Collections.singletonList(WarehouseAttachment.builder() |
||||
.name("label").url("https://cdn/l.pdf").build()))); |
||||
assertTrue(ex.getMessage().contains("type"), ex.getMessage()); |
||||
} |
||||
|
||||
/** url 与 file 都没有时拒绝。 */ |
||||
@Test |
||||
void rejectsWithoutUrlOrContent() { |
||||
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, |
||||
() -> adapter(uploading()).uploadAttachments(CTX, OutboundRef.ofRequestNo("OB-1"), |
||||
Collections.singletonList(WarehouseAttachment.builder() |
||||
.name("label").type("0").build()))); |
||||
assertTrue(ex.getMessage().contains("url or contentBase64"), ex.getMessage()); |
||||
} |
||||
|
||||
/** 类型必须是 0~6 的整数:认不出的拒绝,不猜。 */ |
||||
@Test |
||||
void rejectsUnknownType() { |
||||
RecordingTransport transport = uploading(); |
||||
HengjianAdapter adapter = adapter(transport); |
||||
|
||||
assertThrows(IllegalArgumentException.class, |
||||
() -> adapter.uploadAttachments(CTX, OutboundRef.ofRequestNo("OB-1"), |
||||
Collections.singletonList(WarehouseAttachment.builder() |
||||
.name("label").type("99").url("https://cdn/l.pdf").build()))); |
||||
assertThrows(IllegalArgumentException.class, |
||||
() -> adapter.uploadAttachments(CTX, OutboundRef.ofRequestNo("OB-1"), |
||||
Collections.singletonList(WarehouseAttachment.builder() |
||||
.name("label").type("Shippinglabel") |
||||
.url("https://cdn/l.pdf").build()))); |
||||
assertTrue(transport.dataRequests().isEmpty()); |
||||
} |
||||
|
||||
@Test |
||||
void rejectsEmptyAttachments() { |
||||
RecordingTransport transport = uploading(); |
||||
HengjianAdapter adapter = adapter(transport); |
||||
|
||||
assertThrows(IllegalArgumentException.class, |
||||
() -> adapter.uploadAttachments(CTX, OutboundRef.ofRequestNo("OB-1"), |
||||
Collections.<WarehouseAttachment>emptyList())); |
||||
assertTrue(transport.dataRequests().isEmpty()); |
||||
} |
||||
|
||||
/** 附件列表为 null 时拒绝。 */ |
||||
@Test |
||||
void rejectsNullAttachments() { |
||||
assertThrows(IllegalArgumentException.class, |
||||
() -> adapter(uploading()).uploadAttachments(CTX, OutboundRef.ofRequestNo("OB-1"), |
||||
null)); |
||||
} |
||||
|
||||
// ---------------------------------------------------------------- 失败路径
|
||||
|
||||
@Test |
||||
void businessErrorIsPropagated() { |
||||
RecordingTransport transport = RecordingTransport.create() |
||||
.route(HengjianConstants.PATH_ORDER_ATTACHMENT, req -> Responses.ok( |
||||
"{\"statusCode\":500,\"status\":1,\"code\":999999," |
||||
+ "\"message\":\"订单不存在\"}")); |
||||
|
||||
RuntimeException ex = assertThrows(RuntimeException.class, |
||||
() -> adapter(transport).uploadAttachments(CTX, OutboundRef.ofRequestNo("OB-1"), |
||||
Collections.singletonList(urlAttachment()))); |
||||
assertTrue(ex.getMessage().contains("订单不存在"), ex.getMessage()); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue