|
|
|
@ -17,16 +17,19 @@ import static common.ecommerce.walmart.support.WalmartTestSupport.assertUrlConta |
|
|
|
import static common.ecommerce.walmart.support.WalmartTestSupport.context; |
|
|
|
import static common.ecommerce.walmart.support.WalmartTestSupport.context; |
|
|
|
import static common.ecommerce.walmart.support.WalmartTestSupport.lastUrl; |
|
|
|
import static common.ecommerce.walmart.support.WalmartTestSupport.lastUrl; |
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals; |
|
|
|
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.assertThrows; |
|
|
|
import static org.junit.jupiter.api.Assertions.assertTrue; |
|
|
|
import static org.junit.jupiter.api.Assertions.assertTrue; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 退款:报文严格对齐文档 §5.3 的请求示例。 |
|
|
|
* 退款:报文对齐官方 {@code returns-management.json}(2026-05 版)。 |
|
|
|
* |
|
|
|
* |
|
|
|
* <p>文档示例: |
|
|
|
* <p>官方请求体: |
|
|
|
* {@code {"returnOrderId":"...","refundLines":[{"returnOrderLineNumber":1, |
|
|
|
* {@code {"customerOrderId":"1535274411287","refundLines":[ |
|
|
|
* "refundCharges":[{"chargeName":"ItemPrice","chargeType":"PRODUCT", |
|
|
|
* {"returnOrderLineNumber":1,"quantity":{"unitOfMeasure":"EA","measurementValue":2}}]}}</p> |
|
|
|
* "refundAmount":{"currencyUnit":"USD","currencyAmount":119.95}}]}]}}</p> |
|
|
|
* |
|
|
|
|
|
|
|
* <p>注意:当前 schema 里<b>没有</b>金额字段,也没有 {@code refundCharges}; |
|
|
|
|
|
|
|
* 旧资料(文档 §5.3)那种「按费用项退金额」的报文已不适用。</p> |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
class WalmartRefundTest { |
|
|
|
class WalmartRefundTest { |
|
|
|
|
|
|
|
|
|
|
|
@ -38,17 +41,14 @@ class WalmartRefundTest { |
|
|
|
.route("/refund", r -> Responses.ok(body)); |
|
|
|
.route("/refund", r -> Responses.ok(body)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** 与文档 §5.3 示例完全同构的命令。 */ |
|
|
|
/** 与官方示例同构的命令:客户订单号 + 行号 + 数量。 */ |
|
|
|
private static RefundCommand docExampleCommand() { |
|
|
|
private static RefundCommand officialExampleCommand() { |
|
|
|
return RefundCommand.builder() |
|
|
|
return RefundCommand.builder() |
|
|
|
.afterSaleId("103738048909818825") |
|
|
|
.afterSaleId("106152711271982880") |
|
|
|
|
|
|
|
.customerOrderId("1535274411287") |
|
|
|
.line(RefundCommand.Line.builder() |
|
|
|
.line(RefundCommand.Line.builder() |
|
|
|
.lineId("1") |
|
|
|
.lineId("1") |
|
|
|
.charge(RefundCommand.Charge.builder() |
|
|
|
.quantity(2) |
|
|
|
.chargeName("ItemPrice") |
|
|
|
|
|
|
|
.chargeType("PRODUCT") |
|
|
|
|
|
|
|
.amount(Money.of("119.95", "USD")) |
|
|
|
|
|
|
|
.build()) |
|
|
|
|
|
|
|
.build()) |
|
|
|
.build()) |
|
|
|
.build(); |
|
|
|
.build(); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -58,60 +58,88 @@ class WalmartRefundTest { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void postsToRefundPath() { |
|
|
|
void postsToRefundPath() { |
|
|
|
RecordingTransport transport = transport("{}"); |
|
|
|
RecordingTransport transport = transport("{}"); |
|
|
|
adapter(transport).refund(context(), docExampleCommand()); |
|
|
|
adapter(transport).refund(context(), officialExampleCommand()); |
|
|
|
assertUrlContains(lastUrl(transport), ENDPOINT + "/v3/returns/103738048909818825/refund"); |
|
|
|
assertUrlContains(lastUrl(transport), ENDPOINT + "/v3/returns/106152711271982880/refund"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** 统一模型的 afterSaleId 是「单号:行号」,路径只能取前半段。 */ |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void bodyMatchesDocumentExample() { |
|
|
|
void compositeAfterSaleIdIsTrimmedForPath() { |
|
|
|
RecordingTransport transport = transport("{}"); |
|
|
|
RecordingTransport transport = transport("{}"); |
|
|
|
adapter(transport).refund(context(), docExampleCommand()); |
|
|
|
adapter(transport).refund(context(), RefundCommand.builder() |
|
|
|
|
|
|
|
.afterSaleId("103738048909818825:1") |
|
|
|
|
|
|
|
.customerOrderId("1535274411287") |
|
|
|
|
|
|
|
.line(RefundCommand.Line.builder().lineId("1").build()) |
|
|
|
|
|
|
|
.build()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String url = lastUrl(transport); |
|
|
|
|
|
|
|
assertUrlContains(url, ENDPOINT + "/v3/returns/103738048909818825/refund"); |
|
|
|
|
|
|
|
assertTrue(!url.contains(":1/refund"), url); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
void bodyMatchesOfficialSchema() { |
|
|
|
|
|
|
|
RecordingTransport transport = transport("{}"); |
|
|
|
|
|
|
|
adapter(transport).refund(context(), officialExampleCommand()); |
|
|
|
|
|
|
|
|
|
|
|
String body = transport.recorded().get(transport.recorded().size() - 1).body(); |
|
|
|
String body = transport.recorded().get(transport.recorded().size() - 1).body(); |
|
|
|
// 逐字段对照文档 §5.3
|
|
|
|
assertTrue(body.contains("\"customerOrderId\":\"1535274411287\""), body); |
|
|
|
assertTrue(body.contains("\"returnOrderId\":\"103738048909818825\""), body); |
|
|
|
|
|
|
|
assertTrue(body.contains("\"refundLines\""), body); |
|
|
|
assertTrue(body.contains("\"refundLines\""), body); |
|
|
|
assertTrue(body.contains("\"returnOrderLineNumber\":1"), body); |
|
|
|
assertTrue(body.contains("\"returnOrderLineNumber\":1"), body); |
|
|
|
assertTrue(body.contains("\"refundCharges\""), body); |
|
|
|
assertTrue(body.contains("\"unitOfMeasure\":\"EACH\""), body); |
|
|
|
assertTrue(body.contains("\"chargeName\":\"ItemPrice\""), body); |
|
|
|
assertTrue(body.contains("\"measurementValue\":2"), body); |
|
|
|
assertTrue(body.contains("\"chargeType\":\"PRODUCT\""), body); |
|
|
|
// 当前 schema 里不存在这些字段
|
|
|
|
assertTrue(body.contains("\"refundAmount\""), body); |
|
|
|
assertTrue(!body.contains("refundCharges"), body); |
|
|
|
assertTrue(body.contains("\"currencyUnit\":\"USD\""), body); |
|
|
|
assertTrue(!body.contains("refundAmount"), body); |
|
|
|
assertTrue(body.contains("\"currencyAmount\":119.95"), body); |
|
|
|
assertTrue(!body.contains("returnOrderId"), body); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** 不传数量 = 整行全退。 */ |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void multiLineMultiChargeIsPreserved() { |
|
|
|
void quantityIsOptional() { |
|
|
|
RecordingTransport transport = transport("{}"); |
|
|
|
RecordingTransport transport = transport("{}"); |
|
|
|
adapter(transport).refund(context(), RefundCommand.builder() |
|
|
|
adapter(transport).refund(context(), RefundCommand.builder() |
|
|
|
.afterSaleId("R-1") |
|
|
|
.afterSaleId("R-1") |
|
|
|
.line(RefundCommand.Line.builder() |
|
|
|
.customerOrderId("C-1") |
|
|
|
.lineId("1") |
|
|
|
.line(RefundCommand.Line.builder().lineId("3").build()) |
|
|
|
.charge(RefundCommand.Charge.builder() |
|
|
|
.build()); |
|
|
|
.chargeName("ItemPrice").chargeType("PRODUCT") |
|
|
|
|
|
|
|
.amount(Money.of("10.00", "USD")).build()) |
|
|
|
String body = transport.recorded().get(transport.recorded().size() - 1).body(); |
|
|
|
.charge(RefundCommand.Charge.builder() |
|
|
|
assertTrue(body.contains("\"returnOrderLineNumber\":3"), body); |
|
|
|
.chargeName("Shipping").chargeType("SHIPPING") |
|
|
|
assertTrue(!body.contains("quantity"), body); |
|
|
|
.amount(Money.of("5.00", "USD")).build()) |
|
|
|
} |
|
|
|
.build()) |
|
|
|
|
|
|
|
.line(RefundCommand.Line.builder() |
|
|
|
@Test |
|
|
|
.lineId("2") |
|
|
|
void multiLineIsPreserved() { |
|
|
|
.charge(RefundCommand.Charge.builder() |
|
|
|
RecordingTransport transport = transport("{}"); |
|
|
|
.chargeName("ItemPrice").chargeType("PRODUCT") |
|
|
|
adapter(transport).refund(context(), RefundCommand.builder() |
|
|
|
.amount(Money.of("7.50", "USD")).build()) |
|
|
|
.afterSaleId("R-1") |
|
|
|
.build()) |
|
|
|
.customerOrderId("C-1") |
|
|
|
|
|
|
|
.line(RefundCommand.Line.builder().lineId("1").quantity(1).build()) |
|
|
|
|
|
|
|
.line(RefundCommand.Line.builder().lineId("2").build()) |
|
|
|
.build()); |
|
|
|
.build()); |
|
|
|
|
|
|
|
|
|
|
|
String body = transport.recorded().get(transport.recorded().size() - 1).body(); |
|
|
|
String body = transport.recorded().get(transport.recorded().size() - 1).body(); |
|
|
|
assertTrue(body.contains("\"returnOrderLineNumber\":1"), body); |
|
|
|
assertTrue(body.contains("\"returnOrderLineNumber\":1"), body); |
|
|
|
assertTrue(body.contains("\"returnOrderLineNumber\":2"), body); |
|
|
|
assertTrue(body.contains("\"returnOrderLineNumber\":2"), body); |
|
|
|
assertTrue(body.contains("\"chargeName\":\"Shipping\""), body); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
void missingCustomerOrderIdIsRejectedLocally() { |
|
|
|
|
|
|
|
RecordingTransport transport = transport("{}"); |
|
|
|
|
|
|
|
ValidationException ex = assertThrows(ValidationException.class, |
|
|
|
|
|
|
|
() -> adapter(transport).refund(context(), RefundCommand.builder() |
|
|
|
|
|
|
|
.afterSaleId("R-1") |
|
|
|
|
|
|
|
.line(RefundCommand.Line.builder().lineId("1").build()) |
|
|
|
|
|
|
|
.build())); |
|
|
|
|
|
|
|
assertTrue(ex.getMessage().contains("customerOrderId"), ex.getMessage()); |
|
|
|
|
|
|
|
assertEquals(0, transport.recorded().size()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void nullFieldsAreOmitted() { |
|
|
|
void nullFieldsAreOmitted() { |
|
|
|
RecordingTransport transport = transport("{}"); |
|
|
|
RecordingTransport transport = transport("{}"); |
|
|
|
adapter(transport).refund(context(), docExampleCommand()); |
|
|
|
adapter(transport).refund(context(), officialExampleCommand()); |
|
|
|
String body = transport.recorded().get(transport.recorded().size() - 1).body(); |
|
|
|
String body = transport.recorded().get(transport.recorded().size() - 1).body(); |
|
|
|
// @JsonInclude(NON_NULL):沃尔玛对 null 字段会报格式错误
|
|
|
|
// @JsonInclude(NON_NULL):沃尔玛对 null 字段会报格式错误
|
|
|
|
assertTrue(!body.contains(":null"), body); |
|
|
|
assertTrue(!body.contains(":null"), body); |
|
|
|
@ -123,18 +151,27 @@ class WalmartRefundTest { |
|
|
|
ValidationException ex = assertThrows(ValidationException.class, |
|
|
|
ValidationException ex = assertThrows(ValidationException.class, |
|
|
|
() -> adapter(transport).refund(context(), RefundCommand.builder() |
|
|
|
() -> adapter(transport).refund(context(), RefundCommand.builder() |
|
|
|
.afterSaleId("R-1") |
|
|
|
.afterSaleId("R-1") |
|
|
|
.line(RefundCommand.Line.builder() |
|
|
|
.customerOrderId("C-1") |
|
|
|
.lineId("1a") |
|
|
|
.line(RefundCommand.Line.builder().lineId("1a").build()) |
|
|
|
.charge(RefundCommand.Charge.builder() |
|
|
|
|
|
|
|
.chargeName("ItemPrice").chargeType("PRODUCT") |
|
|
|
|
|
|
|
.amount(Money.of("1.00", "USD")).build()) |
|
|
|
|
|
|
|
.build()) |
|
|
|
|
|
|
|
.build())); |
|
|
|
.build())); |
|
|
|
assertTrue(ex.getMessage().contains("numeric"), ex.getMessage()); |
|
|
|
assertTrue(ex.getMessage().contains("numeric"), ex.getMessage()); |
|
|
|
assertEquals(0, transport.recorded().size()); |
|
|
|
assertEquals(0, transport.recorded().size()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
|
|
|
|
void nonPositiveQuantityIsRejectedLocally() { |
|
|
|
|
|
|
|
RecordingTransport transport = transport("{}"); |
|
|
|
|
|
|
|
ValidationException ex = assertThrows(ValidationException.class, |
|
|
|
|
|
|
|
() -> adapter(transport).refund(context(), RefundCommand.builder() |
|
|
|
|
|
|
|
.afterSaleId("R-1") |
|
|
|
|
|
|
|
.customerOrderId("C-1") |
|
|
|
|
|
|
|
.line(RefundCommand.Line.builder().lineId("1").quantity(0).build()) |
|
|
|
|
|
|
|
.build())); |
|
|
|
|
|
|
|
assertTrue(ex.getMessage().contains("positive"), ex.getMessage()); |
|
|
|
|
|
|
|
assertEquals(0, transport.recorded().size()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
void nullCommandIsRejected() { |
|
|
|
void nullCommandIsRejected() { |
|
|
|
RecordingTransport transport = transport("{}"); |
|
|
|
RecordingTransport transport = transport("{}"); |
|
|
|
assertThrows(ValidationException.class, () -> adapter(transport).refund(context(), null)); |
|
|
|
assertThrows(ValidationException.class, () -> adapter(transport).refund(context(), null)); |
|
|
|
@ -144,22 +181,24 @@ class WalmartRefundTest { |
|
|
|
// ---------------------------------------------------------------- 响应
|
|
|
|
// ---------------------------------------------------------------- 响应
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void mapsReturnOrderResponse() { |
|
|
|
void mapsRefundResponse() { |
|
|
|
RecordingTransport transport = transport( |
|
|
|
RecordingTransport transport = transport( |
|
|
|
"{\"returnOrderId\":\"103738048909818825\",\"returnOrderLines\":[" |
|
|
|
"{\"returnOrderId\":\"106152711271982880\",\"customerOrderId\":\"1535274411287\"," |
|
|
|
+ "{\"returnOrderLineNumber\":1,\"currentRefundStatus\":\"REFUND_PROCESSED\"}]}"); |
|
|
|
+ "\"refundLines\":[{\"returnOrderLineNumber\":1," |
|
|
|
|
|
|
|
+ "\"quantity\":{\"unitOfMeasure\":\"EA\",\"measurementValue\":2}}]}"); |
|
|
|
|
|
|
|
|
|
|
|
RefundResult result = adapter(transport).refund(context(), docExampleCommand()); |
|
|
|
RefundResult result = adapter(transport).refund(context(), officialExampleCommand()); |
|
|
|
assertEquals("103738048909818825", result.afterSaleId()); |
|
|
|
assertEquals("106152711271982880", result.afterSaleId()); |
|
|
|
assertEquals("REFUND_PROCESSED", result.rawStatus()); |
|
|
|
|
|
|
|
assertEquals(1, result.lines().size()); |
|
|
|
assertEquals(1, result.lines().size()); |
|
|
|
assertEquals("1", result.lines().get(0).lineId()); |
|
|
|
assertEquals("1", result.lines().get(0).lineId()); |
|
|
|
|
|
|
|
// 官方响应不含行级退款状态
|
|
|
|
|
|
|
|
assertNull(result.lines().get(0).status()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void emptyResponseStillReturnsAfterSaleId() { |
|
|
|
void emptyResponseStillReturnsAfterSaleId() { |
|
|
|
RefundResult result = adapter(transport("")).refund(context(), docExampleCommand()); |
|
|
|
RefundResult result = adapter(transport("")).refund(context(), officialExampleCommand()); |
|
|
|
assertEquals("103738048909818825", result.afterSaleId()); |
|
|
|
assertEquals("106152711271982880", result.afterSaleId()); |
|
|
|
assertTrue(result.lines().isEmpty()); |
|
|
|
assertTrue(result.lines().isEmpty()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -176,13 +215,21 @@ class WalmartRefundTest { |
|
|
|
assertThrows(IllegalArgumentException.class, |
|
|
|
assertThrows(IllegalArgumentException.class, |
|
|
|
() -> RefundCommand.builder().afterSaleId("R").build()); |
|
|
|
() -> RefundCommand.builder().afterSaleId("R").build()); |
|
|
|
assertThrows(IllegalArgumentException.class, |
|
|
|
assertThrows(IllegalArgumentException.class, |
|
|
|
() -> RefundCommand.Line.builder().lineId("1").build()); |
|
|
|
() -> RefundCommand.Line.builder().lineId(" ").build()); |
|
|
|
assertThrows(IllegalArgumentException.class, |
|
|
|
assertThrows(IllegalArgumentException.class, |
|
|
|
() -> RefundCommand.Charge.builder() |
|
|
|
() -> RefundCommand.Charge.builder() |
|
|
|
.chargeName("ItemPrice").chargeType("PRODUCT") |
|
|
|
.chargeName("ItemPrice").chargeType("PRODUCT") |
|
|
|
.amount(Money.of("-1.00", "USD")).build()); |
|
|
|
.amount(Money.of("-1.00", "USD")).build()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** 按数量退款不需要费用明细,Line 允许只有行号。 */ |
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
void lineWithoutChargesIsValid() { |
|
|
|
|
|
|
|
RefundCommand.Line line = RefundCommand.Line.builder().lineId("1").build(); |
|
|
|
|
|
|
|
assertEquals("1", line.lineId()); |
|
|
|
|
|
|
|
assertTrue(line.charges().isEmpty()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void totalAmountSumsCharges() { |
|
|
|
void totalAmountSumsCharges() { |
|
|
|
RefundCommand cmd = RefundCommand.builder() |
|
|
|
RefundCommand cmd = RefundCommand.builder() |
|
|
|
@ -203,7 +250,6 @@ class WalmartRefundTest { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
void constantPathMatchesDocument() { |
|
|
|
void constantPathMatchesDocument() { |
|
|
|
assertEquals("/refund", WalmartConstants.PATH_RETURN_REFUND); |
|
|
|
assertEquals("/refund", WalmartConstants.PATH_RETURN_REFUND); |
|
|
|
// 与文档 §5.3 的 POST /v3/returns/{returnOrderId}/refund 一致
|
|
|
|
|
|
|
|
assertEquals("/v3/returns/", WalmartConstants.PATH_RETURN_BY_ID); |
|
|
|
assertEquals("/v3/returns/", WalmartConstants.PATH_RETURN_BY_ID); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|