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; /** * 运去哪响应信封校验。 * *

各接口的业务码类型不统一:多数是数字 {@code 200},但 API 81(创建销售退货) * 的文档把 {@code code} 标成 string。这里把两种形态都固化下来, * 避免以后有人把 envelope 改成严格数字解析而把退货创建打挂。

*/ 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 headers() { Map h = new LinkedHashMap(); 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 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")); } }