feat(brand-check): 品牌检测按任务归属用户取代理并计用量——三处调用接入

- BrandCheckClient 契约扩展:构造器收 UserApiSecretService,check/checkAll/
  checkTitleText(s) 全系带 userId,请求体新增 user_id + proxy_url(proxy 取自
  用户密钥库 module=proxy);响应新增 reason/retryable,retryable=false(供应商
  拒付、用户没配代理)当场停重试阶梯并短路整批
- 三处调用方接入:CollectDataService 品牌过滤、CollectDataBrandRecheckScheduler
  补测(markAttempted 带真实原因)、AppearancePatentLlmClient 标题查标(取
  SecretUsageContext 归属用户)
- 用户未配置代理:整批本地定论「用户未配置代理」,零 HTTP,不再有平台钥匙兜底
- 测试迁移:15 支测试文件按新契约迁移(3 参构造、userId 实参、BrandCheckBatchResult
  第 4 位 reason、Mockito 匹配器加宽);HTTP 类测试 stub findPlainValue 非空代理
  以绕开零 HTTP 短路。13 类 119 例全绿(1 skip 为既有快照写盘门),test-compile 零错误
This commit is contained in:
2026-10-20 18:14:52 +08:00
parent 492a27270a
commit 6c14f29a87
20 changed files with 395 additions and 220 deletions
@@ -96,8 +96,8 @@ class HttpClientConnectionReuseTest {
void test_task_077_brand_normal_default_path() throws Exception {
// 默认路径:品牌检查客户端通过共享池创建单例 RestClient,
// 工厂为带 keep-alive 连接池的 JdkClientHttpRequestFactory。
BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null);
BrandCheckBatchResult result = client.checkTitleText(" ");
BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null, null);
BrandCheckBatchResult result = client.checkTitleText(" ", null, null);
assertTrue(result.brands().isEmpty(), "空标题安全跳过,不创建无效资源");
assertPooled(factoryOf(restClientOf(client)));
@@ -108,7 +108,7 @@ class HttpClientConnectionReuseTest {
// 批量场景:LLM/品牌/紫鸟三个客户端各自持有独立 RestClient,
// 但底层连接池共用同一 HttpClient 实例,不重复创建。
SimilarAsinLlmClient llm = new SimilarAsinLlmClient(new SimilarAsinProperties(), new ObjectMapper(), null, null);
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null);
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null);
ZiniaoClientImpl ziniao = new ZiniaoClientImpl(new ZiniaoProperties(), new ObjectMapper());
HttpClient llmClient = clientOf(factoryOf(restClientOf(llm)));
@@ -124,7 +124,7 @@ class HttpClientConnectionReuseTest {
void test_task_077_brand_normal_repeated_operation_is_idempotent() throws Exception {
// 幂等:同一客户端重复触发请求创建逻辑只产生一个 RestClient,
// 重复调用返回同一实例,不重复创建客户端对象。
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null);
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null);
assertSame(restClientOf(brand), restClientOf(brand), "品牌客户端复用同一 RestClient");
ZiniaoClientImpl ziniao = new ZiniaoClientImpl(new ZiniaoProperties(), new ObjectMapper());
@@ -134,8 +134,8 @@ class HttpClientConnectionReuseTest {
@Test
void test_task_077_brand_boundary_empty_input() throws Exception {
// 空输入:空品牌列表不发起任何 HTTP 请求、不创建客户端资源。
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null);
BrandCheckBatchResult result = brand.checkAll(List.of(), "Terms");
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null);
BrandCheckBatchResult result = brand.checkAll(List.of(), "Terms", null);
assertTrue(result.brands().isEmpty());
assertTrue(result.faildData().isEmpty());
@@ -145,7 +145,7 @@ class HttpClientConnectionReuseTest {
@Test
void test_task_077_brand_boundary_single_item() throws Exception {
// 单元素:单客户端单请求走共享池,工厂带连接池,行为与批量一致。
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null);
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null);
assertPooled(factoryOf(restClientOf(brand)));
}
@@ -155,7 +155,7 @@ class HttpClientConnectionReuseTest {
// 不随实例数量线性增长连接资源。
int instances = 8;
for (int i = 0; i < instances; i++) {
restClientOf(new BrandCheckClient(new BrandCheckProperties(), null));
restClientOf(new BrandCheckClient(new BrandCheckProperties(), null, null));
restClientOf(new ZiniaoClientImpl(new ZiniaoProperties(), new ObjectMapper()));
}
assertSame(HttpClientPool.sharedHttpClient(), HttpClientPool.sharedHttpClient(),
@@ -7,6 +7,7 @@ import com.nanri.aiimage.config.ZiniaoProperties;
import com.nanri.aiimage.metrics.ExternalCallMetricsRecorder;
import com.nanri.aiimage.modules.brand.client.BrandCheckClient;
import com.nanri.aiimage.modules.similarasin.client.SimilarAsinLlmClient;
import com.nanri.aiimage.modules.usersecret.service.UserApiSecretService;
import com.nanri.aiimage.modules.ziniao.client.ZiniaoClientImpl;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpServer;
@@ -34,6 +35,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.nanri.aiimage.modules.usersecret.service.UserSecretUsageService;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Task 78:为所有外部调用(LLM / 品牌检查 / 紫鸟)统一增加耗时、重试、
@@ -190,8 +192,8 @@ class ExternalCallMetricsRecorderTest {
"client", "llm", "result", "success"), "三次成功全部记录");
// 全部外部客户端(LLM / 品牌检查 / 紫鸟)在同一次批量中各自记录指标
BrandCheckClient brand = new BrandCheckClient(brandProps(), new ExternalCallMetricsRecorder(registry));
brand.checkAll(List.of("Nintendo", "LEGO", "Sony"), "Terms");
BrandCheckClient brand = new BrandCheckClient(brandProps(), new ExternalCallMetricsRecorder(registry), stubbedUserSecrets());
brand.checkAll(List.of("Nintendo", "LEGO", "Sony"), "Terms", 7L);
ZiniaoClientImpl ziniao = new ZiniaoClientImpl(ziniaoProps(), objectMapper, new ExternalCallMetricsRecorder(registry));
assertEquals(1001L, ziniao.getCompanyIdByApiKey("test-api-key"));
@@ -208,10 +210,10 @@ class ExternalCallMetricsRecorderTest {
@Test
void test_task_078_payload_metrics_normal_repeated_operation_is_idempotent() throws Exception {
BrandCheckProperties props = brandProps();
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry));
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets());
client.check("Nintendo");
client.check("Nintendo");
client.check("Nintendo", "Terms", 7L);
client.check("Nintendo", "Terms", 7L);
assertEquals(2, brandCount.get(), "两次请求真实发出");
assertEquals(2.0, counterCount("aiimage.external-call.total",
@@ -225,9 +227,9 @@ class ExternalCallMetricsRecorderTest {
@Test
void test_task_078_payload_metrics_boundary_empty_input() throws Exception {
BrandCheckProperties props = brandProps();
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry));
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets());
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(List.of(), "Terms");
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(List.of(), "Terms", 7L);
assertEquals(0, brandCount.get(), "空输入不发起外部调用");
assertTrue(result.brands().isEmpty());
@@ -240,9 +242,9 @@ class ExternalCallMetricsRecorderTest {
@Test
void test_task_078_payload_metrics_boundary_single_item() throws Exception {
BrandCheckProperties props = brandProps();
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry));
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets());
BrandCheckClient.BrandCheckResponse response = client.check("LEGO");
BrandCheckClient.BrandCheckResponse response = client.check("LEGO", "Terms", 7L);
assertNotNull(response);
awaitMetric("aiimage.external-call.duration", "client", "brand");
@@ -295,9 +297,9 @@ class ExternalCallMetricsRecorderTest {
@Test
void test_task_078_payload_metrics_invalid_input_rejected() throws Exception {
BrandCheckProperties props = brandProps();
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry));
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets());
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(null, "Terms");
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(null, "Terms", 7L);
assertTrue(result.brands().isEmpty(), "null 列表安全跳过");
assertEquals(0, brandCount.get(), "非法输入不发起外部调用");
@@ -345,6 +347,14 @@ class ExternalCallMetricsRecorderTest {
return props;
}
/** 品牌检查按 userId 取用户自己的代理:不桩一个非空代理就一次上游都不会打。 */
private UserApiSecretService stubbedUserSecrets() {
UserApiSecretService secrets = mock(UserApiSecretService.class);
when(secrets.findPlainValue(org.mockito.ArgumentMatchers.anyLong(),
org.mockito.ArgumentMatchers.anyString())).thenReturn("http://user:pass@127.0.0.1:9");
return secrets;
}
private ZiniaoProperties ziniaoProps() {
ZiniaoProperties props = new ZiniaoProperties();
props.setBaseUrl("http://127.0.0.1:" + port);
@@ -17,6 +17,7 @@ import java.util.ArrayList;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
@@ -50,12 +51,12 @@ class AppearancePatentBrandBatchTest {
@Test
void mergedBatchAsksBrandCheckOnceAndAttributesPerRow() throws Exception {
// 桩按「本行的词」返回:含阿凡达的行 failedData 非空,另一行为空。
when(brandCheckClient.checkTitleTexts(anyList(), eq("Terms"))).thenAnswer(invocation -> {
when(brandCheckClient.checkTitleTexts(anyList(), eq("Terms"), any())).thenAnswer(invocation -> {
List<String> titles = invocation.getArgument(0);
List<BrandCheckClient.BrandCheckBatchResult> results = new ArrayList<>();
for (String title : titles) {
List<Object> failed = title.contains("阿凡达") ? List.of("阿凡达") : List.of();
results.add(new BrandCheckClient.BrandCheckBatchResult(List.of(title), failed, List.of()));
results.add(new BrandCheckClient.BrandCheckBatchResult(List.of(title), failed, List.of(), ""));
}
return results;
});
@@ -69,8 +70,8 @@ class AppearancePatentBrandBatchTest {
List<AppearancePatentResultRowDto> merged = client.mergeRowsFromDataText(rows, dataText);
verify(brandCheckClient, times(1)).checkTitleTexts(anyList(), eq("Terms"));
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString());
verify(brandCheckClient, times(1)).checkTitleTexts(anyList(), eq("Terms"), any());
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString(), any());
assertThat(merged).hasSize(2);
assertThat(merged.get(0).getTitleRisk()).isEqualTo("侵权");
assertThat(merged.get(1).getTitleRisk()).as("干净行不得继承别行的侵权词").isEqualTo("无侵权");
@@ -85,8 +86,8 @@ class AppearancePatentBrandBatchTest {
client.mergeRowsFromDataText(rows, dataText);
verify(brandCheckClient, never()).checkTitleTexts(anyList(), anyString());
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString());
verify(brandCheckClient, never()).checkTitleTexts(anyList(), anyString(), any());
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString(), any());
}
/**
@@ -98,12 +99,12 @@ class AppearancePatentBrandBatchTest {
*/
@Test
void fallbackRowsAlsoAskBrandCheckOnceForTheWholeBatch() throws Exception {
when(brandCheckClient.checkTitleTexts(anyList(), eq("Terms"))).thenAnswer(invocation -> {
when(brandCheckClient.checkTitleTexts(anyList(), eq("Terms"), any())).thenAnswer(invocation -> {
List<String> titles = invocation.getArgument(0);
List<BrandCheckClient.BrandCheckBatchResult> results = new ArrayList<>();
for (String title : titles) {
List<Object> failed = title.contains("阿凡达") ? List.of("阿凡达") : List.of();
results.add(new BrandCheckClient.BrandCheckBatchResult(List.of(title), failed, List.of()));
results.add(new BrandCheckClient.BrandCheckBatchResult(List.of(title), failed, List.of(), ""));
}
return results;
});
@@ -126,8 +127,8 @@ class AppearancePatentBrandBatchTest {
@SuppressWarnings("unchecked")
ArgumentCaptor<List<String>> titlesCaptor = ArgumentCaptor.forClass(List.class);
verify(brandCheckClient, times(1)).checkTitleTexts(titlesCaptor.capture(), eq("Terms"));
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString());
verify(brandCheckClient, times(1)).checkTitleTexts(titlesCaptor.capture(), eq("Terms"), any());
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString(), any());
assertThat(titlesCaptor.getValue())
.containsExactly("阿凡达 玩具 手办 正版", "小米 充电宝 20000mAh 快充");
assertThat(rows).hasSize(2);
@@ -85,7 +85,7 @@ class AppearancePatentLlmClientHttpTest {
brand.setRetryTimes(1);
brand.setRetryIntervalMillis(0);
brand.setTotalTimeoutMillis(5000);
return new BrandCheckClient(brand, null);
return new BrandCheckClient(brand, null, null);
}
private void handleBrandCheck(HttpExchange exchange) throws IOException {
@@ -77,7 +77,7 @@ class AppearancePatentLlmClientImageDedupeTest {
brand.setRetryTimes(1);
brand.setRetryIntervalMillis(0);
brand.setTotalTimeoutMillis(5000);
return new BrandCheckClient(brand, null);
return new BrandCheckClient(brand, null, null);
}
@AfterEach
@@ -10,6 +10,7 @@ import org.junit.jupiter.api.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
@@ -25,7 +26,7 @@ class AppearancePatentLlmClientTest {
new AppearancePatentProperties(),
new ObjectMapper(),
null,
new BrandCheckClient(new BrandCheckProperties(), null),
new BrandCheckClient(new BrandCheckProperties(), null, null),
mock(UserSecretUsageService.class)
);
@@ -50,7 +51,7 @@ class AppearancePatentLlmClientTest {
@Test
void titleRiskIsInfringementWhenBrandCheckHasFailedData() {
BrandCheckClient.BrandCheckBatchResult result =
new BrandCheckClient.BrandCheckBatchResult(List.of("阿凡达"), List.of("阿凡达"), List.of());
new BrandCheckClient.BrandCheckBatchResult(List.of("阿凡达"), List.of("阿凡达"), List.of(), "");
assertThat(client.buildTitleRisk("阿凡达", result)).isEqualTo("侵权");
}
@@ -88,7 +89,7 @@ class AppearancePatentLlmClientTest {
@Test
void titleRiskNamesUpstreamOutageOnlyWhenWholeBatchFailed() {
BrandCheckClient.BrandCheckBatchResult failed =
new BrandCheckClient.BrandCheckBatchResult(List.of("nike"), List.of(), List.of("nike"));
new BrandCheckClient.BrandCheckBatchResult(List.of("nike"), List.of(), List.of("nike"), "");
assertThat(client.buildTitleRisk("nike air", failed, true))
.isEqualTo(AppearancePatentLlmClient.BRAND_UPSTREAM_UNAVAILABLE);
@@ -100,7 +101,7 @@ class AppearancePatentLlmClientTest {
@Test
void infringementStillWinsOverWholeBatchOutageMessage() {
BrandCheckClient.BrandCheckBatchResult hit =
new BrandCheckClient.BrandCheckBatchResult(List.of("阿凡达"), List.of("阿凡达"), List.of());
new BrandCheckClient.BrandCheckBatchResult(List.of("阿凡达"), List.of("阿凡达"), List.of(), "");
assertThat(client.buildTitleRisk("阿凡达 联名", hit, true)).isEqualTo("侵权");
}
@@ -134,7 +135,7 @@ class AppearancePatentLlmClientTest {
assertThat(merged).hasSize(1);
assertThat(merged.get(0).getTitleRisk()).isEqualTo("无侵权");
assertThat(merged.get(0).getConclusion()).isEqualTo("无侵权");
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString());
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString(), any());
}
private static AppearancePatentLlmClient clientWith(BrandCheckClient brandCheckClient) {
@@ -152,9 +153,9 @@ class AppearancePatentLlmClientTest {
@Test
void wholeBatchQueryFailureEscalatesEveryRow() throws Exception {
BrandCheckClient brandCheckClient = mock(BrandCheckClient.class);
when(brandCheckClient.checkTitleTexts(anyList(), eq("Terms"))).thenReturn(List.of(
new BrandCheckClient.BrandCheckBatchResult(List.of("nike"), List.of(), List.of("nike")),
new BrandCheckClient.BrandCheckBatchResult(List.of("puma"), List.of(), List.of("puma"))));
when(brandCheckClient.checkTitleTexts(anyList(), eq("Terms"), any())).thenReturn(List.of(
new BrandCheckClient.BrandCheckBatchResult(List.of("nike"), List.of(), List.of("nike"), ""),
new BrandCheckClient.BrandCheckBatchResult(List.of("puma"), List.of(), List.of("puma"), "")));
String dataText = """
{"data":[{"row_id":"1","title":"nike","title_reason":"nike","appearance":"外观识别异常","result":""},
{"row_id":"2","title":"puma","title_reason":"puma","appearance":"外观识别异常","result":""}]}
@@ -175,9 +176,9 @@ class AppearancePatentLlmClientTest {
@Test
void oneSuccessfulRowKeepsOthersOnPerRowFailureText() throws Exception {
BrandCheckClient brandCheckClient = mock(BrandCheckClient.class);
when(brandCheckClient.checkTitleTexts(anyList(), eq("Terms"))).thenReturn(List.of(
new BrandCheckClient.BrandCheckBatchResult(List.of("nike"), List.of(), List.of("nike")),
new BrandCheckClient.BrandCheckBatchResult(List.of("puma"), List.of(), List.of())));
when(brandCheckClient.checkTitleTexts(anyList(), eq("Terms"), any())).thenReturn(List.of(
new BrandCheckClient.BrandCheckBatchResult(List.of("nike"), List.of(), List.of("nike"), ""),
new BrandCheckClient.BrandCheckBatchResult(List.of("puma"), List.of(), List.of(), "")));
String dataText = """
{"data":[{"row_id":"1","title":"nike","title_reason":"nike","appearance":"外观识别异常","result":""},
{"row_id":"2","title":"puma","title_reason":"puma","appearance":"外观识别异常","result":""}]}
@@ -194,9 +195,9 @@ class AppearancePatentLlmClientTest {
void callBrandCheckOnlyWhenTitleAndAppearanceArePresent() throws Exception {
BrandCheckClient brandCheckClient = mock(BrandCheckClient.class);
// 一行也走批接口:逐行调用等于把 90 秒耗时预算按行复制
when(brandCheckClient.checkTitleTexts(anyList(), eq("Terms")))
when(brandCheckClient.checkTitleTexts(anyList(), eq("Terms"), any()))
.thenReturn(List.of(new BrandCheckClient.BrandCheckBatchResult(
List.of("阿凡达"), List.of(), List.of())));
List.of("阿凡达"), List.of(), List.of(), "")));
AppearancePatentLlmClient llmClient = new AppearancePatentLlmClient(
new AppearancePatentProperties(),
new ObjectMapper(),
@@ -214,8 +215,8 @@ class AppearancePatentLlmClientTest {
assertThat(merged).hasSize(1);
assertThat(merged.get(0).getTitleRisk()).isEqualTo("无侵权");
verify(brandCheckClient).checkTitleTexts(List.of("阿凡达"), "Terms");
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString());
verify(brandCheckClient).checkTitleTexts(eq(List.of("阿凡达")), eq("Terms"), any());
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString(), any());
}
@Test
@@ -238,7 +239,7 @@ class AppearancePatentLlmClientTest {
assertThat(merged).hasSize(1);
assertThat(merged.get(0).getTitleRisk()).isEmpty();
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString());
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString(), any());
}
@Test
@@ -262,7 +263,7 @@ class AppearancePatentLlmClientTest {
assertThat(merged).hasSize(1);
assertThat(merged.get(0).getTitleRisk()).isEmpty();
assertThat(merged.get(0).getAppearanceRisk()).isNullOrEmpty();
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString());
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString(), any());
}
@Test
@@ -68,7 +68,7 @@ class AppearancePatentLlmClientTokenUsageTest {
brand.setRetryTimes(1);
brand.setRetryIntervalMillis(0);
brand.setTotalTimeoutMillis(5000);
return new BrandCheckClient(brand, null);
return new BrandCheckClient(brand, null, null);
}
@AfterEach
@@ -1,6 +1,7 @@
package com.nanri.aiimage.modules.brand.client;
import com.nanri.aiimage.config.BrandCheckProperties;
import com.nanri.aiimage.modules.usersecret.service.UserApiSecretService;
import com.sun.net.httpserver.HttpServer;
import org.junit.jupiter.api.Test;
@@ -12,12 +13,16 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class BrandCheckClientTest {
@Test
void splitTitleTextSupportsCommonSeparatorsAndQuotes() {
BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null);
BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null, null);
List<String> brands = client.splitTitleText("'阿凡达,任天堂'; Disney、LEGO\nSony");
@@ -26,7 +31,7 @@ class BrandCheckClientTest {
@Test
void splitTitleTextDeduplicatesBlankValues() {
BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null);
BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null, null);
List<String> brands = client.splitTitleText(" 任天堂, ,任天堂,Sony ");
@@ -46,7 +51,7 @@ class BrandCheckClientTest {
try {
BrandCheckClient client = newClient(server, 3);
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(List.of("Apple"), "Terms");
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(List.of("Apple"), "Terms", 7L);
assertThat(attempts.get()).as("前两次失败后应重试到成功").isEqualTo(3);
assertThat(result.hasFailedData()).isFalse();
@@ -66,7 +71,7 @@ class BrandCheckClientTest {
try {
BrandCheckClient client = newClient(server, 3);
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(List.of("Apple"), "Terms");
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(List.of("Apple"), "Terms", 7L);
assertThat(attempts.get()).as("持续失败应耗尽重试次数").isEqualTo(3);
assertThat(result.hasFailedData()).isFalse();
@@ -87,7 +92,7 @@ class BrandCheckClientTest {
try {
BrandCheckClient client = newClient(server, 3);
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(List.of("Apple"), "Terms");
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(List.of("Apple"), "Terms", 7L);
assertThat(attempts.get()).as("命中侵权属有效结论,不应重试").isEqualTo(1);
assertThat(result.hasFailedData()).isTrue();
@@ -115,7 +120,7 @@ class BrandCheckClientTest {
BrandCheckClient client = newClient(server, 3);
List<String> brands = List.of("b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8");
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(brands, "Terms");
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(brands, "Terms", 7L);
assertThat(requests.get()).as("首轮始终执行:短路不许让任何品牌一次都没被查过")
.isGreaterThanOrEqualTo(brands.size());
@@ -149,7 +154,7 @@ class BrandCheckClientTest {
BrandCheckClient client = newClient(server, 3);
BrandCheckClient.BrandCheckBatchResult result =
client.checkAll(List.of("ok1", "bad", "ok2", "ok3"), "Terms");
client.checkAll(List.of("ok1", "bad", "ok2", "ok3"), "Terms", 7L);
assertThat(badAttempts.get()).as("同批有成功品牌时,失败品牌仍跑满整条阶梯").isEqualTo(3);
assertThat(result.queryFaildData()).containsExactly("bad");
@@ -164,7 +169,7 @@ class BrandCheckClientTest {
props.setPath("/brand_check");
props.setRetryTimes(retryTimes);
props.setRetryIntervalMillis(1);
return new BrandCheckClient(props, null);
return new BrandCheckClient(props, null, stubbedUserSecrets());
}
/** 重试阶梯可调的构造:用于验证「耗时预算掐的是重试,不是首轮」。 */
@@ -175,7 +180,14 @@ class BrandCheckClientTest {
props.setRetryTimes(retryTimes);
props.setRetryIntervalMillis(retryIntervalMillis);
props.setTotalTimeoutMillis(totalTimeoutMillis);
return new BrandCheckClient(props, null);
return new BrandCheckClient(props, null, stubbedUserSecrets());
}
/** 有真实 HTTP 断言的用例必须桩出非空代理,否则客户端零请求短路、阶梯与指标全测不到。 */
private UserApiSecretService stubbedUserSecrets() {
UserApiSecretService secrets = mock(UserApiSecretService.class);
when(secrets.findPlainValue(anyLong(), anyString())).thenReturn("http://user:pass@127.0.0.1:9");
return secrets;
}
@Test
@@ -188,12 +200,12 @@ class BrandCheckClientTest {
try {
// 阶梯 20/40/60…:跑满 10 次要 0.9 秒,60ms 预算只够跑到第 2~3 次。
BrandCheckClient unlimited = newClient(server, 10, 20, 0);
unlimited.checkAll(List.of("Apple"), "Terms");
unlimited.checkAll(List.of("Apple"), "Terms", 7L);
int attemptsWithoutBudget = attempts.get();
attempts.set(0);
BrandCheckClient client = newClient(server, 10, 20, 0);
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(List.of("Apple"), "Terms", 60L);
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(List.of("Apple"), "Terms", 60L, 7L);
assertThat(attemptsWithoutBudget).as("不限制时跑满整条重试阶梯").isEqualTo(10);
assertThat(attempts.get()).as("预算用尽即停重试,但首轮始终执行")
@@ -215,7 +227,7 @@ class BrandCheckClientTest {
try {
BrandCheckClient client = newClient(server, 10, 20, 60);
client.checkAll(List.of("Apple"), "Terms");
client.checkAll(List.of("Apple"), "Terms", 7L);
assertThat(attempts.get()).as("两参重载按 total-timeout-millis 兜底,未退化也不越界")
.isBetween(1, 9);
@@ -238,7 +250,7 @@ class BrandCheckClientTest {
BrandCheckClient client = newClient(server, 3, 200, 0);
List<String> brands = List.of("b1", "b2", "b3", "b4");
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(brands, "Terms", 1L);
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(brands, "Terms", 1L, 7L);
assertThat(requests.get()).as("预算过期后每个品牌仍各发一次真实请求").isEqualTo(brands.size());
assertThat(result.brands()).as("降级不丢品牌,入参集合原样带回").containsExactlyElementsOf(brands);
@@ -1,6 +1,7 @@
package com.nanri.aiimage.modules.brand.client;
import com.nanri.aiimage.config.BrandCheckProperties;
import com.nanri.aiimage.modules.usersecret.service.UserApiSecretService;
import com.sun.net.httpserver.HttpServer;
import org.junit.jupiter.api.Test;
@@ -14,6 +15,10 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* 一批标题的品牌校验(外观专利热路径:原先每行各调一次 checkTitleText,
@@ -34,7 +39,7 @@ class BrandCheckClientTitleBatchTest {
HttpServer server = startBrandCheckServer(brandResponses(hits));
try {
List<BrandCheckClient.BrandCheckBatchResult> results =
newClient(server, 3).checkTitleTexts(List.of("阿凡达,玩具", "小米,充电宝"), "Terms");
newClient(server, 3).checkTitleTexts(List.of("阿凡达,玩具", "小米,充电宝"), "Terms", 7L);
assertThat(results).hasSize(2);
assertThat(results.get(0).hasFailedData()).as("第 1 行含侵权词").isTrue();
@@ -59,7 +64,7 @@ class BrandCheckClientTitleBatchTest {
});
try {
List<BrandCheckClient.BrandCheckBatchResult> results =
newClient(server, 3).checkTitleTexts(List.of("阿凡达,玩具", "阿凡达,手机", "阿凡达,周边"), "Terms");
newClient(server, 3).checkTitleTexts(List.of("阿凡达,玩具", "阿凡达,手机", "阿凡达,周边"), "Terms", 7L);
assertThat(counters.get("阿凡达").get()).as("跨行同一个词只查一次").isEqualTo(1);
assertThat(counters).hasSize(4);
@@ -80,7 +85,7 @@ class BrandCheckClientTitleBatchTest {
Map.of("阿凡达", "{\"faild_data\":[\"阿凡达\"],\"query_faild_data\":[]}")));
try {
List<BrandCheckClient.BrandCheckBatchResult> results =
newClient(server, 3).checkTitleTexts(List.of("", "阿凡达"), "Terms");
newClient(server, 3).checkTitleTexts(List.of("", "阿凡达"), "Terms", 7L);
assertThat(results.get(0).brands()).as("空标题行的词集合必须为空,否则判定从标题原文变成无侵权").isEmpty();
assertThat(results.get(0).hasFailedData()).isFalse();
@@ -100,7 +105,7 @@ class BrandCheckClientTitleBatchTest {
HttpServer server = startBrandCheckServer(brandResponses(hits));
try {
List<BrandCheckClient.BrandCheckBatchResult> results =
newClient(server, 2).checkTitleTexts(List.of("任天堂,主机", "小米,充电宝"), "Terms");
newClient(server, 2).checkTitleTexts(List.of("任天堂,主机", "小米,充电宝"), "Terms", 7L);
assertThat(results.get(0).hasQueryFailedData()).isTrue();
assertThat(results.get(1).hasQueryFailedData()).as("别行的查询失败不得扩散").isFalse();
@@ -130,7 +135,9 @@ class BrandCheckClientTitleBatchTest {
props.setPath("/brand_check");
props.setRetryTimes(retryTimes);
props.setRetryIntervalMillis(1);
return new BrandCheckClient(props, null);
UserApiSecretService secrets = mock(UserApiSecretService.class);
when(secrets.findPlainValue(anyLong(), anyString())).thenReturn("http://user:pass@127.0.0.1:9");
return new BrandCheckClient(props, null, secrets);
}
private HttpServer startBrandCheckServer(com.sun.net.httpserver.HttpHandler handler) throws IOException {
@@ -117,7 +117,7 @@ class CollectDataBrandRecheckSchedulerTest {
scheduler.runRound();
verify(recheckService).postponeWithoutAttempt(List.of(e1), 30);
verify(brandCheckClient, never()).checkAll(anyList(), anyString());
verify(brandCheckClient, never()).checkAll(anyList(), anyString(), any());
verify(notificationDispatchService, never()).pushRecheckOutcome(
any(), any(), anyString(), anyString(), anyInt(), anyInt(), anyInt(), anyInt());
}
@@ -131,7 +131,7 @@ class CollectDataBrandRecheckSchedulerTest {
scheduler.runRound();
verify(recheckService).markExhausted(e1, "任务不存在(可能已被清理)");
verify(brandCheckClient, never()).checkAll(anyList(), anyString());
verify(brandCheckClient, never()).checkAll(anyList(), anyString(), any());
}
@Test
@@ -144,7 +144,7 @@ class CollectDataBrandRecheckSchedulerTest {
scheduler.runRound();
verify(recheckService).postponeWithoutAttempt(List.of(e1), 30);
verify(brandCheckClient, never()).checkAll(anyList(), anyString());
verify(brandCheckClient, never()).checkAll(anyList(), anyString(), any());
}
@Test
@@ -155,8 +155,8 @@ class CollectDataBrandRecheckSchedulerTest {
when(fileTaskMapper.selectById(100L)).thenReturn(task(100L, "SUCCESS"));
when(taskDistributedLockService.acquire("COLLECT_DATA", 100L, 2000L)).thenReturn(taskLock);
// "Nike " 带尾随空格:验证取品牌时按归一化口径去查(入队时已归一化,防御性兜底)
when(brandCheckClient.checkAll(anyList(), eq("Terms"))).thenReturn(
new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of("Zara"), List.of()));
when(brandCheckClient.checkAll(anyList(), eq("Terms"), any())).thenReturn(
new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of("Zara"), List.of(), ""));
when(injector.apply(any(FileTaskEntity.class), any())).thenReturn(
new GroupOutcome(9L, 3, 1));
@@ -176,8 +176,8 @@ class CollectDataBrandRecheckSchedulerTest {
when(recheckService.listDue(any(), anyInt())).thenReturn(List.of(e1));
when(fileTaskMapper.selectById(100L)).thenReturn(task(100L, "SUCCESS"));
when(taskDistributedLockService.acquire("COLLECT_DATA", 100L, 2000L)).thenReturn(taskLock);
when(brandCheckClient.checkAll(anyList(), eq("Terms"))).thenReturn(
new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of("zara")));
when(brandCheckClient.checkAll(anyList(), eq("Terms"), any())).thenReturn(
new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of("zara"), ""));
scheduler.runRound();
@@ -194,7 +194,7 @@ class CollectDataBrandRecheckSchedulerTest {
when(recheckService.listDue(any(), anyInt())).thenReturn(List.of(e1));
when(fileTaskMapper.selectById(100L)).thenReturn(task(100L, "FAILED"));
when(taskDistributedLockService.acquire("COLLECT_DATA", 100L, 2000L)).thenReturn(taskLock);
when(brandCheckClient.checkAll(anyList(), eq("Terms")))
when(brandCheckClient.checkAll(anyList(), eq("Terms"), any()))
.thenThrow(new RuntimeException("connection reset"));
scheduler.runRound();
@@ -209,8 +209,8 @@ class CollectDataBrandRecheckSchedulerTest {
when(recheckService.listDue(any(), anyInt())).thenReturn(List.of(e1));
when(fileTaskMapper.selectById(100L)).thenReturn(task(100L, "SUCCESS"));
when(taskDistributedLockService.acquire("COLLECT_DATA", 100L, 2000L)).thenReturn(taskLock);
when(brandCheckClient.checkAll(anyList(), eq("Terms"))).thenReturn(
new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of()));
when(brandCheckClient.checkAll(anyList(), eq("Terms"), any())).thenReturn(
new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of(), ""));
when(injector.apply(any(FileTaskEntity.class), any()))
.thenThrow(new RuntimeException("结果明细写入失败"));
@@ -232,9 +232,9 @@ class CollectDataBrandRecheckSchedulerTest {
when(recheckService.listDue(any(), anyInt())).thenReturn(List.of(f1, f2, f3, later));
when(fileTaskMapper.selectById(100L)).thenReturn(task(100L, "SUCCESS"));
when(taskDistributedLockService.acquire("COLLECT_DATA", 100L, 2000L)).thenReturn(taskLock);
when(brandCheckClient.checkAll(anyList(), eq("Terms"))).thenReturn(
when(brandCheckClient.checkAll(anyList(), eq("Terms"), any())).thenReturn(
new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(),
List.of("bad-one", "bad-two", "bad-three")));
List.of("bad-one", "bad-two", "bad-three"), ""));
scheduler.runRound();
@@ -243,7 +243,7 @@ class CollectDataBrandRecheckSchedulerTest {
verify(recheckService).markAttempted(f2, "query_faild_data 持续非空");
verify(recheckService).markAttempted(f3, "query_faild_data 持续非空");
// 第二组整组顺延:不实测、不计数
verify(brandCheckClient, org.mockito.Mockito.times(1)).checkAll(anyList(), anyString());
verify(brandCheckClient, org.mockito.Mockito.times(1)).checkAll(anyList(), anyString(), any());
verify(recheckService).postponeWithoutAttempt(List.of(later), 30);
verify(recheckService, never()).markInjected(any());
verify(recheckService, never()).markRejected(any());
@@ -143,8 +143,8 @@ class CollectDataServiceTxBoundaryTest {
row.setAsin("B0COLLECT1");
lenient().when(collectDataBatchQuery.filter(any(), any())).thenReturn(
new CollectDataBatchQuery.FilterResult(List.of(row), 0, 0));
lenient().when(brandBatchFilter.filter(any())).thenReturn(
new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(), List.of(row)));
lenient().when(brandBatchFilter.filter(any(), any())).thenReturn(
new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(), List.of(row), ""));
lenient().when(resultItemBatchWriter.upsertAccepted(anyLong(), anyLong(), anyString(), anyInt(), any(), anyString()))
.thenReturn(new CollectDataResultItemBatchWriter.UpsertCounts(1, 0, 1));
lenient().when(resultDetailCodec.encodeChunk(any())).thenReturn("{\"chunk\":1}");
@@ -199,8 +199,8 @@ class CollectDataServiceTxBoundaryTest {
void brandRejectedRowsWrittenToInvalidWriter() {
CollectDataResultRowVo bad = new CollectDataResultRowVo();
bad.setAsin("B0INVALID");
when(brandBatchFilter.filter(any())).thenReturn(
new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(bad), List.of(), List.of()));
when(brandBatchFilter.filter(any(), any())).thenReturn(
new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(bad), List.of(), List.of(), ""));
service.submitResult(TASK_ID, submitRequest());
@@ -211,8 +211,8 @@ class CollectDataServiceTxBoundaryTest {
void brandQueryFailedRowsNotWrittenToInvalidWriter() {
CollectDataResultRowVo queryFailed = new CollectDataResultRowVo();
queryFailed.setAsin("B0QUERYFAIL");
when(brandBatchFilter.filter(any())).thenReturn(
new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(queryFailed), List.of()));
when(brandBatchFilter.filter(any(), any())).thenReturn(
new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(queryFailed), List.of(), ""));
service.submitResult(TASK_ID, submitRequest());
@@ -223,8 +223,8 @@ class CollectDataServiceTxBoundaryTest {
void emptyRejectedOutcomeSkipsInvalidWriter() {
CollectDataResultRowVo accepted = new CollectDataResultRowVo();
accepted.setAsin("B0OK");
when(brandBatchFilter.filter(any())).thenReturn(
new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(), List.of(accepted)));
when(brandBatchFilter.filter(any(), any())).thenReturn(
new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(), List.of(accepted), ""));
service.submitResult(TASK_ID, submitRequest());
@@ -337,11 +337,11 @@ class CollectDataSubmitResultSnapshotTest {
+ " dedupe=0 invalid=0");
return new CollectDataBatchQuery.FilterResult(kept, 0, 0);
});
when(brandFilter.filter(anyList())).thenAnswer(invocation -> {
when(brandFilter.filter(anyList(), any())).thenAnswer(invocation -> {
@SuppressWarnings("unchecked")
List<CollectDataResultRowVo> rows = invocation.getArgument(0);
f.events.add("outsideLock brandFilter accepted=" + rows.size() + " rejected=0 queryFailed=0");
return new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(), rows);
return new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(), rows, "");
});
doAnswer(invocation -> {
@SuppressWarnings("unchecked")
@@ -42,22 +42,22 @@ class CollectDataBrandBatchFilterTest {
@Test
void test_task_046_brand_normal_default_path() {
// 正常输入:非空品牌批次发起检查,失败品牌行 rejected、其余 accepted。
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(
new BrandCheckClient.BrandCheckBatchResult(
List.of(), List.of("Zara"), List.of()));
List.of(), List.of("Zara"), List.of(), ""));
CollectDataBrandBatchFilter.BrandBatchOutcome outcome = filter.filter(List.of(
row("B000000001", "Zara"),
row("B000000002", "Nike"),
row("B000000003", "H&M")
));
), 7L);
assertEquals(1, outcome.rejected().size(), "失败品牌行进入 rejected");
assertEquals("B000000001", outcome.rejected().get(0).getAsin());
assertEquals(2, outcome.accepted().size(), "其余行 accepted");
assertEquals("B000000002", outcome.accepted().get(0).getAsin(), "accepted 顺序稳定");
assertEquals(0, outcome.queryFailed().size(), "无查询失败行");
verify(brandCheckClient).checkAll(anyList(), any(), anyLong());
verify(brandCheckClient).checkAll(anyList(), any(), anyLong(), any());
}
@Test
@@ -67,74 +67,74 @@ class CollectDataBrandBatchFilterTest {
for (int i = 0; i < 120; i++) {
rows.add(row("B" + String.format("%09d", i + 1), i % 10 == 0 ? "bad-brand" : "good-" + (i / 10)));
}
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(
new BrandCheckClient.BrandCheckBatchResult(
List.of(), List.of("bad-brand"), List.of()));
List.of(), List.of("bad-brand"), List.of(), ""));
CollectDataBrandBatchFilter.BrandBatchOutcome outcome = filter.filter(rows);
CollectDataBrandBatchFilter.BrandBatchOutcome outcome = filter.filter(rows, 7L);
assertEquals(12, outcome.rejected().size(), "每批次 1 个失败品牌行");
assertEquals(108, outcome.accepted().size(), "其余行 accepted");
assertEquals("B000000001", outcome.rejected().get(0).getAsin(), "rejected 顺序稳定");
assertEquals("B000000002", outcome.accepted().get(0).getAsin(), "accepted 顺序稳定");
verify(brandCheckClient, times(12)).checkAll(anyList(), any(), anyLong());
verify(brandCheckClient, times(12)).checkAll(anyList(), any(), anyLong(), any());
}
@Test
void test_task_046_brand_normal_repeated_operation_is_idempotent() {
// 幂等:同一输入重复执行结果一致,不产生重复对象。
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(
new BrandCheckClient.BrandCheckBatchResult(
List.of(), List.of("Nike"), List.of()));
List.of(), List.of("Nike"), List.of(), ""));
List<CollectDataResultRowVo> rows = List.of(
row("B000000001", "Nike"),
row("B000000002", "Adidas")
);
CollectDataBrandBatchFilter.BrandBatchOutcome first = filter.filter(rows);
CollectDataBrandBatchFilter.BrandBatchOutcome second = filter.filter(rows);
CollectDataBrandBatchFilter.BrandBatchOutcome first = filter.filter(rows, 7L);
CollectDataBrandBatchFilter.BrandBatchOutcome second = filter.filter(rows, 7L);
assertEquals(first.rejected(), second.rejected(), "重复执行 rejected 一致");
assertEquals(first.accepted(), second.accepted(), "重复执行 accepted 一致");
// 缓存生效:第二次执行全部命中缓存,不再发起远程调用。
verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong());
verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong(), any());
}
@Test
void test_task_046_brand_boundary_empty_input() {
// 空输入:空列表返回空结果;全空品牌批次不发起远程检查,行全部 rejected。
CollectDataBrandBatchFilter.BrandBatchOutcome empty = filter.filter(List.of());
CollectDataBrandBatchFilter.BrandBatchOutcome empty = filter.filter(List.of(), 7L);
assertEquals(0, empty.rejected().size() + empty.accepted().size() + empty.queryFailed().size(),
"空列表返回空结果");
verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong());
verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong(), any());
CollectDataBrandBatchFilter.BrandBatchOutcome blankBatch = filter.filter(List.of(
row("B000000001", ""), row("B000000002", null), row("B000000003", " ")
));
), 7L);
assertEquals(3, blankBatch.rejected().size(), "空品牌批次行全部 rejected");
assertEquals(0, blankBatch.accepted().size(), "空品牌批次无 accepted");
verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong());
verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong(), any());
}
@Test
void test_task_046_brand_boundary_single_item() {
// 单元素:单行单品牌批次正常检查;单行空品牌批次不发远程调用。
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(
new BrandCheckClient.BrandCheckBatchResult(
List.of(), List.of(), List.of()));
List.of(), List.of(), List.of(), ""));
CollectDataBrandBatchFilter.BrandBatchOutcome single = filter.filter(List.of(
row("B000000001", "solo")
));
), 7L);
assertEquals(1, single.accepted().size(), "单行未命中失败归 accepted");
verify(brandCheckClient).checkAll(anyList(), any(), anyLong());
verify(brandCheckClient).checkAll(anyList(), any(), anyLong(), any());
CollectDataBrandBatchFilter.BrandBatchOutcome blank = filter.filter(List.of(
row("B000000002", "")
));
), 7L);
assertEquals(1, blank.rejected().size(), "单行空品牌归 rejected");
// 同实例缓存生效:solo 已查过,空品牌批次无远程调用,总调用保持 1 次。
verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong());
verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong(), any());
}
@Test
@@ -145,50 +145,50 @@ class CollectDataBrandBatchFilterTest {
rows.add(row("B" + String.format("%09d", i + 1), "brand-" + (i % 100)));
}
rows.add(row("B999999999", " "));
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(
new BrandCheckClient.BrandCheckBatchResult(
List.of(), List.of(), List.of()));
List.of(), List.of(), List.of(), ""));
CollectDataBrandBatchFilter.BrandBatchOutcome outcome = filter.filter(rows);
CollectDataBrandBatchFilter.BrandBatchOutcome outcome = filter.filter(rows, 7L);
assertEquals(1, outcome.rejected().size(), "空品牌行归 rejected");
assertEquals(5000, outcome.accepted().size(), "非空品牌行全 accepted");
// 每批次 10 行恰好引入 10 个新品牌,10 批次后 100 品牌全部缓存,后续批次零调用。
verify(brandCheckClient, times(10)).checkAll(anyList(), any(), anyLong());
verify(brandCheckClient, times(10)).checkAll(anyList(), any(), anyLong(), any());
}
@Test
void test_task_046_brand_invalid_input_rejected() {
// 非法参数:checkAll 返回 null 按无失败处理;null 行安全跳过不计数。
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(null);
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(null);
List<CollectDataResultRowVo> rows = new ArrayList<>();
rows.add(null);
rows.add(row("B000000001", "Nike"));
rows.add(row("B000000002", "Zara"));
CollectDataBrandBatchFilter.BrandBatchOutcome outcome = filter.filter(rows);
CollectDataBrandBatchFilter.BrandBatchOutcome outcome = filter.filter(rows, 7L);
assertEquals(2, outcome.accepted().size(), "null 结果按无失败处理,行全 accepted");
assertEquals(0, outcome.rejected().size(), "null 行不计数");
verify(brandCheckClient).checkAll(anyList(), any(), anyLong());
verify(brandCheckClient).checkAll(anyList(), any(), anyLong(), any());
}
@Test
void test_task_046_brand_dependency_failure_releases_resources() {
// 依赖失败:远程检查抛错时批次整组降级 queryFailed,可恢复;恢复后重新检查成功。
when(brandCheckClient.checkAll(anyList(), any(), anyLong()))
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any()))
.thenThrow(new RuntimeException("brand service down"))
.thenReturn(new BrandCheckClient.BrandCheckBatchResult(
List.of(), List.of("Zara"), List.of()));
List.of(), List.of("Zara"), List.of(), ""));
List<CollectDataResultRowVo> rows = List.of(
row("B000000001", "Zara"), row("B000000002", "Nike"));
CollectDataBrandBatchFilter.BrandBatchOutcome failed = filter.filter(rows);
CollectDataBrandBatchFilter.BrandBatchOutcome failed = filter.filter(rows, 7L);
assertEquals(2, failed.queryFailed().size(), "依赖失败批次整组降级 queryFailed");
assertEquals(0, failed.accepted().size() + failed.rejected().size(), "降级后无其他分类");
CollectDataBrandBatchFilter.BrandBatchOutcome recovered = filter.filter(rows);
CollectDataBrandBatchFilter.BrandBatchOutcome recovered = filter.filter(rows, 7L);
assertEquals(1, recovered.rejected().size(), "恢复后失败品牌重新分类");
assertEquals(1, recovered.accepted().size(), "恢复后其余行 accepted");
assertTrue(recovered.queryFailed().isEmpty(), "恢复后无残留 queryFailed");
@@ -202,17 +202,17 @@ class CollectDataBrandBatchFilterTest {
@Test
void test_chunk_budget_exhausted_stops_calling_upstream() {
AtomicInteger calls = new AtomicInteger();
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenAnswer(invocation -> {
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenAnswer(invocation -> {
calls.incrementAndGet();
Thread.sleep(30);
return new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of());
return new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of(), "");
});
CollectDataBrandBatchFilter tight = new CollectDataBrandBatchFilter(brandCheckClient, 2, 512, 1L);
CollectDataBrandBatchFilter.BrandBatchOutcome outcome = tight.filter(List.of(
row("B000000001", "brand-1"), row("B000000002", "brand-2"),
row("B000000003", "brand-3"), row("B000000004", "brand-4"),
row("B000000005", "brand-5"), row("B000000006", "brand-6")));
row("B000000005", "brand-5"), row("B000000006", "brand-6")), 7L);
assertEquals(1, calls.get(), "预算用尽后不再为剩余批次发起远程检查");
assertEquals(2, outcome.accepted().size(), "首个已检查批次照常分类");
@@ -224,18 +224,18 @@ class CollectDataBrandBatchFilterTest {
void test_cached_verdict_still_applies_and_blank_brand_rejects_after_budget_exhausted() {
// 预算用尽只做「不再花代价」,不改分类口径:缓存里有结论的仍按结论走,
// 空品牌行仍归 rejected(补测队列按品牌入队,空品牌进去就永远回不来)。
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenAnswer(invocation -> {
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenAnswer(invocation -> {
Thread.sleep(30);
List<String> brands = invocation.getArgument(0);
List<Object> hits = new ArrayList<>(brands.stream().filter("bad-brand"::equals).toList());
return new BrandCheckClient.BrandCheckBatchResult(List.of(), hits, List.of());
return new BrandCheckClient.BrandCheckBatchResult(List.of(), hits, List.of(), "");
});
CollectDataBrandBatchFilter tight = new CollectDataBrandBatchFilter(brandCheckClient, 2, 512, 1L);
CollectDataBrandBatchFilter.BrandBatchOutcome outcome = tight.filter(List.of(
row("B000000001", "bad-brand"), row("B000000002", "seen-ok"),
row("B000000003", "bad-brand"), row("B000000004", "brand-4"),
row("B000000005", "brand-5"), row("B000000006", " ")));
row("B000000005", "brand-5"), row("B000000006", " ")), 7L);
assertEquals(List.of("B000000001", "B000000003", "B000000006"), asins(outcome.rejected()),
"侵权品牌与空品牌行归 rejected");
@@ -247,19 +247,19 @@ class CollectDataBrandBatchFilterTest {
@Test
void test_each_batch_gets_only_the_remaining_chunk_budget() {
// 共用一个截止时刻,而不是每批各领一份预算。
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenAnswer(invocation -> {
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenAnswer(invocation -> {
Thread.sleep(40);
return new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of());
return new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of(), "");
});
CollectDataBrandBatchFilter bounded = new CollectDataBrandBatchFilter(brandCheckClient, 2, 512, 5_000L);
bounded.filter(List.of(
row("B000000001", "brand-1"), row("B000000002", "brand-2"),
row("B000000003", "brand-3"), row("B000000004", "brand-4"),
row("B000000005", "brand-5"), row("B000000006", "brand-6")));
row("B000000005", "brand-5"), row("B000000006", "brand-6")), 7L);
ArgumentCaptor<Long> budgets = ArgumentCaptor.forClass(Long.class);
verify(brandCheckClient, times(3)).checkAll(anyList(), any(), budgets.capture());
verify(brandCheckClient, times(3)).checkAll(anyList(), any(), budgets.capture(), any());
List<Long> values = budgets.getAllValues();
assertTrue(values.get(0) <= 5_000L, "首批拿到的不超过 chunk 预算");
assertTrue(values.get(1) < values.get(0), "第二批只能用剩余的");
@@ -269,15 +269,15 @@ class CollectDataBrandBatchFilterTest {
@Test
void test_zero_budget_passes_through_as_unlimited() {
// 逃生阀:预算配 0 表示不掐批间,把每批的预算交给 checkAll 按配置走(旧口径)。
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of()));
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(
new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of(), ""));
CollectDataBrandBatchFilter unbounded = new CollectDataBrandBatchFilter(brandCheckClient, 2, 512, 0L);
CollectDataBrandBatchFilter.BrandBatchOutcome outcome = unbounded.filter(List.of(
row("B000000001", "brand-1"), row("B000000002", "brand-2"), row("B000000003", "brand-3")));
row("B000000001", "brand-1"), row("B000000002", "brand-2"), row("B000000003", "brand-3")), 7L);
ArgumentCaptor<Long> budgets = ArgumentCaptor.forClass(Long.class);
verify(brandCheckClient, times(2)).checkAll(anyList(), any(), budgets.capture());
verify(brandCheckClient, times(2)).checkAll(anyList(), any(), budgets.capture(), any());
assertEquals(List.of(0L, 0L), budgets.getAllValues(), "预算 0 逐批透传 0(不限制)");
assertEquals(3, outcome.accepted().size(), "不限制时没有行被延后");
}
@@ -41,32 +41,32 @@ class CollectDataBrandCacheTest {
@Test
void test_task_047_cache_brand_normal_default_path() {
// 正常输入:首查品牌发起远程检查并缓存判定,分类正确。
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(
new BrandCheckClient.BrandCheckBatchResult(
List.of(), List.of("Zara"), List.of()));
List.of(), List.of("Zara"), List.of(), ""));
CollectDataBrandBatchFilter.BrandBatchOutcome outcome = filter.filter(List.of(
row("B000000001", "Zara"),
row("B000000002", "Nike")
));
), 7L);
assertEquals(1, outcome.rejected().size(), "失败品牌行 rejected");
assertEquals("B000000001", outcome.rejected().get(0).getAsin());
assertEquals(1, outcome.accepted().size(), "其余行 accepted");
verify(brandCheckClient).checkAll(anyList(), any(), anyLong());
verify(brandCheckClient).checkAll(anyList(), any(), anyLong(), any());
}
@Test
void test_task_047_cache_brand_normal_multiple_items() {
// 批量场景:多 chunk 提交时同品牌只远程检查一次,每 chunk 只查未缓存品牌。
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(
new BrandCheckClient.BrandCheckBatchResult(
List.of(), List.of("Zara"), List.of()));
filter.filter(List.of(row("B000000001", "Zara"), row("B000000002", "Nike")));
List.of(), List.of("Zara"), List.of(), ""));
filter.filter(List.of(row("B000000001", "Zara"), row("B000000002", "Nike")), 7L);
CollectDataBrandBatchFilter.BrandBatchOutcome second = filter.filter(List.of(
row("B000000001", "Zara"),
row("B000000003", "Adidas")
));
), 7L);
assertEquals(1, second.rejected().size(), "chunk2 Zara 命中缓存仍 rejected");
assertEquals(1, second.accepted().size(), "chunk2 新品牌 Adidas accepted");
@@ -78,63 +78,63 @@ class CollectDataBrandCacheTest {
@Test
void test_task_047_cache_brand_normal_repeated_operation_is_idempotent() {
// 幂等:同一输入重复执行结果一致,第二次全部命中缓存不发远程调用。
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(
new BrandCheckClient.BrandCheckBatchResult(
List.of(), List.of("Nike"), List.of()));
List.of(), List.of("Nike"), List.of(), ""));
List<CollectDataResultRowVo> rows = List.of(
row("B000000001", "Nike"),
row("B000000002", "Adidas")
);
CollectDataBrandBatchFilter.BrandBatchOutcome first = filter.filter(rows);
CollectDataBrandBatchFilter.BrandBatchOutcome second = filter.filter(rows);
CollectDataBrandBatchFilter.BrandBatchOutcome first = filter.filter(rows, 7L);
CollectDataBrandBatchFilter.BrandBatchOutcome second = filter.filter(rows, 7L);
assertEquals(first.rejected(), second.rejected(), "重复执行 rejected 一致");
assertEquals(first.accepted(), second.accepted(), "重复执行 accepted 一致");
verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong());
verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong(), any());
}
@Test
void test_task_047_cache_brand_boundary_empty_input() {
// 空输入:空列表与全空品牌批次均不发起远程调用,也不写缓存。
CollectDataBrandBatchFilter.BrandBatchOutcome empty = filter.filter(List.of());
CollectDataBrandBatchFilter.BrandBatchOutcome empty = filter.filter(List.of(), 7L);
assertEquals(0, empty.rejected().size() + empty.accepted().size() + empty.queryFailed().size(),
"空列表返回空结果");
verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong());
verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong(), any());
CollectDataBrandBatchFilter.BrandBatchOutcome blank = filter.filter(List.of(
row("B000000001", "")
));
), 7L);
assertEquals(1, blank.rejected().size(), "空品牌行 rejected");
verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong());
verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong(), any());
}
@Test
void test_task_047_cache_brand_boundary_single_item() {
// 单元素:单品牌首查一次;同品牌再出现时命中缓存零调用。
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(
new BrandCheckClient.BrandCheckBatchResult(
List.of(), List.of(), List.of()));
List.of(), List.of(), List.of(), ""));
CollectDataBrandBatchFilter.BrandBatchOutcome single = filter.filter(List.of(
row("B000000001", "solo")
));
), 7L);
assertEquals(1, single.accepted().size(), "单品牌未命中失败 accepted");
CollectDataBrandBatchFilter.BrandBatchOutcome again = filter.filter(List.of(
row("B000000002", "solo")
));
), 7L);
assertEquals(1, again.accepted().size(), "同品牌再次出现结果一致");
verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong());
verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong(), any());
}
@Test
void test_task_047_cache_brand_boundary_limit_and_overflow() {
// 上限/超限:缓存容量 8,首批 10 个品牌淘汰最旧 2 个;第二批同品牌
// 只重查被淘汰的 2 个,其余 8 个命中缓存;无无界增长。
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(
new BrandCheckClient.BrandCheckBatchResult(
List.of(), List.of(), List.of()));
List.of(), List.of(), List.of(), ""));
CollectDataBrandBatchFilter smallCache = new CollectDataBrandBatchFilter(brandCheckClient, 10, 8);
List<CollectDataResultRowVo> first = new ArrayList<>();
List<CollectDataResultRowVo> second = new ArrayList<>();
@@ -143,8 +143,8 @@ class CollectDataBrandCacheTest {
second.add(row("B" + (100 + i), "brand-" + i));
}
smallCache.filter(first);
CollectDataBrandBatchFilter.BrandBatchOutcome out = smallCache.filter(second);
smallCache.filter(first, 7L);
CollectDataBrandBatchFilter.BrandBatchOutcome out = smallCache.filter(second, 7L);
assertEquals(10, out.accepted().size(), "第二批 10 行全部 accepted");
ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
@@ -157,39 +157,39 @@ class CollectDataBrandCacheTest {
@Test
void test_task_047_cache_brand_invalid_input_rejected() {
// 非法参数:checkAll 返回 null 按无失败处理并缓存;null 行安全跳过。
when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(null);
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(null);
List<CollectDataResultRowVo> rows = new ArrayList<>();
rows.add(null);
rows.add(row("B000000001", "Nike"));
CollectDataBrandBatchFilter.BrandBatchOutcome first = filter.filter(rows);
CollectDataBrandBatchFilter.BrandBatchOutcome first = filter.filter(rows, 7L);
assertEquals(1, first.accepted().size(), "null 结果按无失败处理");
assertEquals(0, first.rejected().size(), "null 行不计数");
CollectDataBrandBatchFilter.BrandBatchOutcome second = filter.filter(rows);
CollectDataBrandBatchFilter.BrandBatchOutcome second = filter.filter(rows, 7L);
assertEquals(1, second.accepted().size(), "重复执行结果一致");
verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong());
verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong(), any());
}
@Test
void test_task_047_cache_brand_dependency_failure_releases_resources() {
// 依赖失败:远程抛错整批 queryFailed 且不缓存;恢复后重查成功,无残留状态。
when(brandCheckClient.checkAll(anyList(), any(), anyLong()))
when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any()))
.thenThrow(new RuntimeException("brand service down"))
.thenReturn(new BrandCheckClient.BrandCheckBatchResult(
List.of(), List.of("Zara"), List.of()));
List.of(), List.of("Zara"), List.of(), ""));
List<CollectDataResultRowVo> rows = List.of(
row("B000000001", "Zara"), row("B000000002", "Nike"));
CollectDataBrandBatchFilter.BrandBatchOutcome failed = filter.filter(rows);
CollectDataBrandBatchFilter.BrandBatchOutcome failed = filter.filter(rows, 7L);
assertEquals(2, failed.queryFailed().size(), "失败批次整组降级 queryFailed");
CollectDataBrandBatchFilter.BrandBatchOutcome recovered = filter.filter(rows);
CollectDataBrandBatchFilter.BrandBatchOutcome recovered = filter.filter(rows, 7L);
assertEquals(1, recovered.rejected().size(), "恢复后重新检查并分类");
assertEquals(1, recovered.accepted().size(), "恢复后 accepted 正确");
assertTrue(recovered.queryFailed().isEmpty(), "恢复后无残留 queryFailed");
verify(brandCheckClient, times(2)).checkAll(anyList(), any(), anyLong());
verify(brandCheckClient, times(2)).checkAll(anyList(), any(), anyLong(), any());
}
private static CollectDataResultRowVo row(String asin, String brand) {
@@ -140,8 +140,8 @@ class DuplicateSubmissionContractTest {
row.setAsin("B0DUP1234");
lenient().when(collectDataBatchQuery.filter(any(), any())).thenReturn(
new CollectDataBatchQuery.FilterResult(List.of(row), 0, 0));
lenient().when(brandBatchFilter.filter(any())).thenReturn(
new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(), List.of(row)));
lenient().when(brandBatchFilter.filter(any(), any())).thenReturn(
new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(), List.of(row), ""));
lenient().when(resultItemBatchWriter.upsertAccepted(anyLong(), anyLong(), anyString(), anyInt(), any(), anyString()))
.thenReturn(new CollectDataResultItemBatchWriter.UpsertCounts(1, 0, 1));
lenient().when(resultDetailCodec.encodeChunk(any())).thenReturn("{\"chunk\":1}");