Browse Source

feat(edayun): 补测试环境基址 + 免签请求重载 + token 探针

- EdayunConstants.PR_BASE_URL 填成
  https://test.omniselling.com.cn/saas-service/openapi/microService
- HEADER_TOKEN 的 Javadoc 写明「仅业务接口使用」:POST /oauth/token 本身不带
  这个头,文档里那行是全局头定义,不是该接口的参数(已确认)
- EdayunRequestFactory.businessUnsigned(...):测试环境可直接把
  openApiAdminSecret 当 sign 传,不必算 MD5;生产走带 appSecret 的重载

新增 EdayunTokenProbeTest(默认跳过):
- probeToken:真实调用 /oauth/token,打印 token 脱敏、tokenType、过期时刻、
  剩余秒数、是否带 refreshToken;异常只打印不抛出
- probeSignatureShape:不联网,核对 MD5(body + "." + appSecret) 形态

凭据一律走系统属性,不入库:
  -Dedayun.probe.clientId / clientSecret / appSecret

全量测试仍绿(533)。

Co-Authored-By: Claude <noreply@anthropic.com>
master
chenchuchuan 1 week ago
parent
commit
c07d848628
  1. 12
      warehouse-sdk-edayun/src/main/java/common/ecommerce/edayun/EdayunConstants.java
  2. 23
      warehouse-sdk-edayun/src/main/java/common/ecommerce/edayun/http/EdayunRequestFactory.java
  3. 100
      warehouse-sdk-edayun/src/test/java/common/ecommerce/edayun/probe/EdayunTokenProbeTest.java

12
warehouse-sdk-edayun/src/main/java/common/ecommerce/edayun/EdayunConstants.java

@ -20,12 +20,13 @@ public final class EdayunConstants {
* *
* <p><b>TODO 待核对</b>:等易达云提供测试域名后替换。</p> * <p><b>TODO 待核对</b>:等易达云提供测试域名后替换。</p>
*/ */
public static final String PR_BASE_URL = "https://TODO.edayun.example"; public static final String PR_BASE_URL = "https://test.omniselling.com.cn/saas-service/openapi/microService";
/** /**
* 生产环境基址。 * 生产环境基址。
* *
* <p><b>TODO 待核对</b>。</p> * <p><b>TODO 待核对</b>:测试环境是 {@code test.omniselling.com.cn},
* 生产域名应以正式文档为准。</p>
*/ */
public static final String PRODUCTION_BASE_URL = "TODO"; public static final String PRODUCTION_BASE_URL = "TODO";
@ -35,7 +36,12 @@ public final class EdayunConstants {
/** Content-Type。 */ /** Content-Type。 */
public static final String CONTENT_TYPE_JSON = "application/json"; public static final String CONTENT_TYPE_JSON = "application/json";
/** 请求头:token(来自 {@code POST /oauth/token})。 */ /**
* 请求头:token(来自 {@code POST /oauth/token})。
*
* <p><b>仅业务接口使用</b>。{@code POST /oauth/token} 本身不带这个头——
* 文档里列出它是全局头定义,不是该接口的参数。</p>
*/
public static final String HEADER_TOKEN = "token"; public static final String HEADER_TOKEN = "token";
/** 请求头:appKey。 */ /** 请求头:appKey。 */

23
warehouse-sdk-edayun/src/main/java/common/ecommerce/edayun/http/EdayunRequestFactory.java

@ -75,12 +75,19 @@ public final class EdayunRequestFactory {
public HttpRequest business(String baseUrl, String path, String method, public HttpRequest business(String baseUrl, String path, String method,
String token, String appKey, String appSecret, String token, String appKey, String appSecret,
long timestamp, String bodyJson, String operation) { long timestamp, String bodyJson, String operation) {
return buildBusiness(baseUrl, path, method, token, appKey,
sign(bodyJson, appSecret), timestamp, bodyJson, operation);
}
private HttpRequest buildBusiness(String baseUrl, String path, String method,
String token, String appKey, String signValue,
long timestamp, String bodyJson, String operation) {
Map<String, String> headers = new LinkedHashMap<String, String>(); Map<String, String> headers = new LinkedHashMap<String, String>();
headers.put(EdayunConstants.HEADER_TOKEN, requireText(token, "edayun requires a token")); headers.put(EdayunConstants.HEADER_TOKEN, requireText(token, "edayun requires a token"));
headers.put(EdayunConstants.HEADER_APP_KEY, requireText(appKey, headers.put(EdayunConstants.HEADER_APP_KEY, requireText(appKey,
"edayun requires ChannelSecret.appKey()")); "edayun requires ChannelSecret.appKey()"));
headers.put(EdayunConstants.HEADER_TIMESTAMP, String.valueOf(timestamp)); headers.put(EdayunConstants.HEADER_TIMESTAMP, String.valueOf(timestamp));
headers.put(EdayunConstants.HEADER_SIGN, sign(bodyJson, appSecret)); headers.put(EdayunConstants.HEADER_SIGN, signValue);
HttpRequest.Builder builder = HttpRequest.builder() HttpRequest.Builder builder = HttpRequest.builder()
.method(method) .method(method)
@ -94,6 +101,20 @@ public final class EdayunRequestFactory {
} }
/** /**
* 业务请求(测试环境免签版本)。
*
* <p>文档说明:测试环境可把 {@code openApiAdminSecret} 直接当 {@code sign} 传,
* 不必计算 MD5。生产环境请用带 {@code appSecret} 的重载。</p>
*/
public HttpRequest businessUnsigned(String baseUrl, String path, String method,
String token, String appKey, String openApiAdminSecret,
long timestamp, String bodyJson, String operation) {
return buildBusiness(baseUrl, path, method, token, appKey,
requireText(openApiAdminSecret, "edayun requires openApiAdminSecret"), timestamp,
bodyJson, operation);
}
/**
* 签名:{@code MD5(body + "." + appSecret)},小写十六进制。 * 签名:{@code MD5(body + "." + appSecret)},小写十六进制。
* *
* <p>注意是 <b>body 原文</b>参与签名——所以业务方法要把「即将发出的那个字符串」 * <p>注意是 <b>body 原文</b>参与签名——所以业务方法要把「即将发出的那个字符串」

100
warehouse-sdk-edayun/src/test/java/common/ecommerce/edayun/probe/EdayunTokenProbeTest.java

@ -0,0 +1,100 @@
package common.ecommerce.edayun.probe;
import common.ecommerce.edayun.EdayunConstants;
import common.ecommerce.edayun.auth.EdayunTokenClient;
import common.ecommerce.edayun.http.EdayunRequestFactory;
import common.ecommerce.http.JacksonJsonCodec;
import common.ecommerce.http.OkHttpTransport;
import common.ecommerce.shared.error.ChannelException;
import common.ecommerce.shared.spi.AccessToken;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import java.time.Clock;
/**
* 易达云获取 token 的联调探针。
*
* <p>默认跳过,用 {@code -Dedayun.probe=true} 开启;凭据走系统属性,
* <b>真实值不入库</b>:</p>
*
* <pre>
* mvn -pl warehouse-sdk-edayun -DfailIfNoTests=false \
* -Dedayun.probe=true \
* -Dedayun.probe.baseUrl=https://test.omniselling.com.cn/saas-service/openapi/microService \
* -Dedayun.probe.clientId=你的OMS授权用户名 \
* -Dedayun.probe.clientSecret=你的OMS授权密码 \
* -Dtest=EdayunTokenProbeTest test
* </pre>
*
* <p>只调只读的 token 接口,不碰任何业务写接口。异常只打印不抛出,
* 便于一次跑完对照真实报文。</p>
*/
@EnabledIfSystemProperty(named = "edayun.probe", matches = "true")
class EdayunTokenProbeTest {
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", "");
@Test
void probeToken() {
Assumptions.assumeTrue(!CLIENT_ID.trim().isEmpty() && !CLIENT_SECRET.trim().isEmpty(),
"未提供 -Dedayun.probe.clientId / clientSecret,跳过");
System.out.println("\n================ edayun / POST /oauth/token ================");
System.out.println("[probe] baseUrl=" + BASE_URL
+ " clientId=" + mask(CLIENT_ID) + " clientSecret=" + mask(CLIENT_SECRET));
EdayunTokenClient client = new EdayunTokenClient(
new OkHttpTransport("common-ecommerce/1.0.0 (edayun-probe)"),
new JacksonJsonCodec(), Clock.systemUTC());
try {
AccessToken token = client.fetch(BASE_URL, CLIENT_ID, CLIENT_SECRET);
System.out.println("[OK] token=" + mask(token.value())
+ " tokenType=" + token.tokenType()
+ " expiresAt=" + token.expiresAt()
+ " refreshToken=" + (token.refreshToken() == null ? "null" : "present"));
System.out.println(" 有效期剩余(秒)="
+ (token.expiresAt().getEpochSecond() - java.time.Instant.now().getEpochSecond()));
} catch (RuntimeException e) {
String msg = e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage();
System.out.println("[ERROR] " + e.getClass().getSimpleName() + ": "
+ (msg.length() <= 600 ? msg : msg.substring(0, 600) + "…"));
if (e instanceof ChannelException) {
ChannelException ce = (ChannelException) e;
System.out.println(" platformCode=" + ce.platformCode()
+ " retryable=" + ce.retryable());
}
}
}
/** 签名算法自检:不联网,确认 MD5(body + "." + appSecret) 的低 8 位形态。 */
@Test
void probeSignatureShape() {
String secret = System.getProperty("edayun.probe.appSecret", "");
Assumptions.assumeTrue(!secret.trim().isEmpty(),
"未提供 -Dedayun.probe.appSecret,跳过签名自检");
String sign = EdayunRequestFactory.sign("{\"a\":1}", secret);
System.out.println("[sign] MD5(body + \".\" + appSecret) = " + sign
+ " (长度应为 32 的小写十六进制)");
}
private static String mask(String v) {
if (v == null || v.length() <= 6) {
return "***";
}
return v.substring(0, 4) + "***" + v.substring(v.length() - 2);
}
private static String opt(String name, String fallback) {
String value = System.getProperty(name, "");
if (value == null) {
return fallback;
}
String trimmed = value.trim();
return trimmed.isEmpty() ? fallback : trimmed;
}
}
Loading…
Cancel
Save