Browse Source
两处按文档对齐: 1. lineName(项目名称,非必填)之前没有标准字段,直接丢弃。 ReturnCommand 增加 lineName(),YqnReturnMapper 下发。仓方按项目分账 / 路由时缺这个字段会走默认项目,属于静默偏差。 2. API 81 的响应 code 是 **string**("200"),多数接口是数字 200。 现有 YqnEnvelope 用 stringAt 读再转整数,两种形态本来就能识别—— 但此前没有测试锁定这个行为,改严格数字解析会把退货创建打挂。 新增 YqnEnvelopeTest(11 个)覆盖: 数字码 / 字符串码 / 带空白字符串码 / 数字失败码 / 字符串失败码 / 缺码 / 非数字码 / 空 body / 401 / 429 / 503 全量 406 个测试通过。 Co-Authored-By: Claude <noreply@anthropic.com>feature/supplier-lingxing
4 changed files with 138 additions and 0 deletions
@ -0,0 +1,119 @@
@@ -0,0 +1,119 @@
|
||||
package common.ecommerce.yqn.http; |
||||
|
||||
import common.ecommerce.http.HttpResponse; |
||||
import common.ecommerce.http.JacksonJsonCodec; |
||||
import common.ecommerce.shared.error.PlatformBusinessException; |
||||
import common.ecommerce.shared.error.ValidationException; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import java.util.LinkedHashMap; |
||||
import java.util.Map; |
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||
import static org.junit.jupiter.api.Assertions.assertThrows; |
||||
import static org.junit.jupiter.api.Assertions.assertTrue; |
||||
|
||||
/** |
||||
* 运去哪响应信封校验。 |
||||
* |
||||
* <p>各接口的业务码类型不统一:多数是数字 {@code 200},但 API 81(创建销售退货) |
||||
* 的文档把 {@code code} 标成 <b>string</b>。这里把两种形态都固化下来, |
||||
* 避免以后有人把 envelope 改成严格数字解析而把退货创建打挂。</p> |
||||
*/ |
||||
class YqnEnvelopeTest { |
||||
|
||||
private static final JacksonJsonCodec JSON = new JacksonJsonCodec(); |
||||
|
||||
private static HttpResponse ok(String body) { |
||||
return HttpResponse.of(200, headers(), body, 0L); |
||||
} |
||||
|
||||
private static Map<String, String> headers() { |
||||
Map<String, String> h = new LinkedHashMap<String, String>(); |
||||
h.put("Content-Type", "application/json"); |
||||
return h; |
||||
} |
||||
|
||||
private static YqnEnvelope parse(String body) { |
||||
return YqnEnvelope.of(ok(body), JSON, "warehouse.test", "WH-1", "req-1"); |
||||
} |
||||
|
||||
// ---------------------------------------------------------------- 成功码
|
||||
|
||||
@Test |
||||
void acceptsNumericCode() { |
||||
assertEquals("{\"code\":200,\"message\":\"ok\"}", |
||||
parse("{\"code\":200,\"message\":\"ok\"}").body()); |
||||
} |
||||
|
||||
/** API 81 的 code 是字符串,必须同样按成功处理。 */ |
||||
@Test |
||||
void acceptsStringCode() { |
||||
String body = "{\"code\":\"200\",\"message\":\"ok\",\"statusCode\":1,\"status\":1}"; |
||||
assertEquals(body, parse(body).body()); |
||||
} |
||||
|
||||
@Test |
||||
void stringCodeWithWhitespaceIsAccepted() { |
||||
parse("{\"code\":\" 200 \",\"message\":\"ok\"}"); |
||||
} |
||||
|
||||
// ---------------------------------------------------------------- 失败码
|
||||
|
||||
@Test |
||||
void numericFailureIsBusinessError() { |
||||
PlatformBusinessException ex = assertThrows(PlatformBusinessException.class, |
||||
() -> parse("{\"code\":500,\"message\":\"boom\"}")); |
||||
assertTrue(ex.getMessage().contains("500"), ex.getMessage()); |
||||
} |
||||
|
||||
@Test |
||||
void stringFailureIsBusinessError() { |
||||
PlatformBusinessException ex = assertThrows(PlatformBusinessException.class, |
||||
() -> parse("{\"code\":\"500\",\"message\":\"boom\"}")); |
||||
assertTrue(ex.getMessage().contains("500"), ex.getMessage()); |
||||
} |
||||
|
||||
/** 缺 code 时才报「没有业务码」,不能把字符串码也当缺失。 */ |
||||
@Test |
||||
void missingCodeIsValidationError() { |
||||
ValidationException ex = assertThrows(ValidationException.class, |
||||
() -> parse("{\"message\":\"ok\"}")); |
||||
assertTrue(ex.getMessage().contains("no business code"), ex.getMessage()); |
||||
} |
||||
|
||||
@Test |
||||
void nonNumericCodeIsValidationError() { |
||||
assertThrows(ValidationException.class, () -> parse("{\"code\":\"abc\"}")); |
||||
} |
||||
|
||||
@Test |
||||
void emptyBodyIsValidationError() { |
||||
assertThrows(ValidationException.class, () -> parse("")); |
||||
} |
||||
|
||||
// ---------------------------------------------------------------- HTTP 层
|
||||
|
||||
@Test |
||||
void httpUnauthorizedIsAuthError() { |
||||
HttpResponse resp = HttpResponse.of(401, headers(), "{}", 0L); |
||||
assertThrows(common.ecommerce.shared.error.AuthException.class, |
||||
() -> YqnEnvelope.of(resp, JSON, "op", "WH-1", "req-1")); |
||||
} |
||||
|
||||
@Test |
||||
void httpTooManyRequestsIsRateLimitError() { |
||||
Map<String, String> h = headers(); |
||||
h.put("Retry-After", "2"); |
||||
HttpResponse resp = HttpResponse.of(429, h, "{}", 0L); |
||||
assertThrows(common.ecommerce.shared.error.RateLimitException.class, |
||||
() -> YqnEnvelope.of(resp, JSON, "op", "WH-1", "req-1")); |
||||
} |
||||
|
||||
@Test |
||||
void httpServerErrorIsTransient() { |
||||
HttpResponse resp = HttpResponse.of(503, headers(), "{}", 0L); |
||||
assertThrows(common.ecommerce.shared.error.TransientException.class, |
||||
() -> YqnEnvelope.of(resp, JSON, "op", "WH-1", "req-1")); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue