feat(fill): 更新机位数量匹配策略为大于等于匹配

- 修改策略注释说明匹配方式由精确匹配改为大于等于匹配
- 更新全局数量匹配逻辑,从 == 改为 >= 判断
- 更新列表数量匹配逻辑,从 == 改为 >= 判断
- 在列表匹配成功时,只取前 N 个机位存入 context.extra
- 调整日志描述,明确显示最小数量与实际数量的比较
- 更新单元测试用例以验证大于等于匹配逻辑
- 增加测试用例验证匹配成功时只取前 N 个机位的行为
- 调整测试用例名称和断言逻辑以适应新的匹配规则
This commit is contained in:
2025-11-20 16:38:08 +08:00
parent c8054c60ab
commit 3b93e07a66
2 changed files with 79 additions and 48 deletions

View File

@@ -13,9 +13,9 @@ import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
/**
* 机位数量精确匹配策略测试
* 机位数量匹配策略测试(大于等于匹配)
*/
@DisplayName("机位数量精确匹配策略测试")
@DisplayName("机位数量匹配策略测试(大于等于匹配)")
class DeviceCountConditionStrategyTest {
private DeviceCountConditionStrategy strategy;
@@ -34,7 +34,7 @@ class DeviceCountConditionStrategyTest {
}
@Test
@DisplayName("当机位数量完全匹配时应该返回true")
@DisplayName("当机位数量大于等于期望时应该返回true")
void shouldReturnTrueWhenDeviceCountMatches() throws Exception {
// Given
String conditionValueJson = "{\"deviceCount\": 4}";
@@ -52,7 +52,25 @@ class DeviceCountConditionStrategyTest {
}
@Test
@DisplayName("当机位数量不匹配时应该返回false")
@DisplayName("模式1:当机位数量大于期望时应该返回true")
void shouldReturnTrueWhenDeviceCountGreaterThanExpected() throws Exception {
// Given
String conditionValueJson = "{\"deviceCount\": 4}";
JsonNode conditionValue = objectMapper.readTree(conditionValueJson);
ConditionContext context = ConditionContext.builder()
.deviceCount(5)
.build();
// When
boolean result = strategy.evaluate(conditionValue, context);
// Then
assertTrue(result);
}
@Test
@DisplayName("当机位数量小于期望时应该返回false")
void shouldReturnFalseWhenDeviceCountDoesNotMatch() throws Exception {
// Given
String conditionValueJson = "{\"deviceCount\": 4}";
@@ -138,7 +156,7 @@ class DeviceCountConditionStrategyTest {
assertFalse(result); // 修改:deviceCount必须大于0
}
// ==================== 模式2:指定列表数量匹配 ====================
// ==================== 模式2:指定列表数量匹配(大于等于) ====================
@Test
@DisplayName("模式2:当指定列表中恰好有期望数量的机位时应该返回true")
@@ -167,6 +185,32 @@ class DeviceCountConditionStrategyTest {
assertEquals(Arrays.asList(200L, 300L), filteredIds); // 保持配置顺序
}
@Test
@DisplayName("模式2:当指定列表中机位数量大于期望时应该返回true,且只取前N个")
void shouldReturnTrueWhenDeviceIdListCountExceedsExpected() throws Exception {
// Given - 配置需要2个,实际过滤出3个
String conditionValueJson = "{\"deviceCount\": 2, \"deviceIds\": [200, 300, 400]}";
JsonNode conditionValue = objectMapper.readTree(conditionValueJson);
List<Long> contextDeviceIds = Arrays.asList(200L, 300L, 400L);
ConditionContext context = ConditionContext.builder()
.deviceCount(3)
.deviceIds(contextDeviceIds)
.build();
// When
boolean result = strategy.evaluate(conditionValue, context);
// Then
assertTrue(result);
assertNotNull(context.getExtra());
@SuppressWarnings("unchecked")
List<Long> filteredIds = (List<Long>) context.getExtra().get("filteredDeviceIds");
assertEquals(2, filteredIds.size()); // 只取前2个
assertEquals(Arrays.asList(200L, 300L), filteredIds); // 按配置顺序取前2个,不包含400
}
@Test
@DisplayName("模式2:当指定列表中的机位数量不足时应该返回false")
void shouldReturnFalseWhenDeviceIdListCountInsufficient() throws Exception {
@@ -188,30 +232,10 @@ class DeviceCountConditionStrategyTest {
}
@Test
@DisplayName("模式2:当指定列表中的机位数量超过期望时应该返回false")
void shouldReturnFalseWhenDeviceIdListCountExcessive() throws Exception {
// Given
String conditionValueJson = "{\"deviceCount\": 1, \"deviceIds\": [200, 300, 400]}";
JsonNode conditionValue = objectMapper.readTree(conditionValueJson);
List<Long> contextDeviceIds = Arrays.asList(200L, 300L, 500L);
ConditionContext context = ConditionContext.builder()
.deviceCount(3)
.deviceIds(contextDeviceIds)
.build();
// When
boolean result = strategy.evaluate(conditionValue, context);
// Then
assertFalse(result);
}
@Test
@DisplayName("模式2:应该保持配置的机位顺序,不排序")
@DisplayName("模式2:应该保持配置的机位顺序,不排序,且只取前N个")
void shouldPreserveDeviceIdOrderInMode2() throws Exception {
// Given - 配置顺序为 400, 200, 300
String conditionValueJson = "{\"deviceCount\": 3, \"deviceIds\": [400, 200, 300]}";
// Given - 配置顺序为 400, 200, 300,需要2个
String conditionValueJson = "{\"deviceCount\": 2, \"deviceIds\": [400, 200, 300]}";
JsonNode conditionValue = objectMapper.readTree(conditionValueJson);
List<Long> contextDeviceIds = Arrays.asList(200L, 300L, 400L);
@@ -228,7 +252,8 @@ class DeviceCountConditionStrategyTest {
@SuppressWarnings("unchecked")
List<Long> filteredIds = (List<Long>) context.getExtra().get("filteredDeviceIds");
assertEquals(Arrays.asList(400L, 200L, 300L), filteredIds); // 保持配置顺序,不是[200, 300, 400]
assertEquals(2, filteredIds.size());
assertEquals(Arrays.asList(400L, 200L), filteredIds); // 保持配置顺序,只取前2个
}
@Test