fix: tolerate duplicate region configs

This commit is contained in:
hy001 2026-05-20 14:11:54 +08:00
parent 663affb734
commit a0cc63d784
4 changed files with 124 additions and 5 deletions

View File

@ -99,7 +99,8 @@ public class SysRegionConfigServiceImpl implements SysRegionConfigService {
SysRegionConfig.class))
.map(region -> region.stream().collect(
Collectors.toMap(SysRegionConfig::getId,
v -> StringUtils.isBlankOrElse(v.getRegionName(), ""))))
v -> StringUtils.isBlankOrElse(v.getRegionName(), ""),
(first, second) -> first)))
.orElseGet(Maps::newHashMap);
}
@ -133,7 +134,8 @@ public class SysRegionConfigServiceImpl implements SysRegionConfigService {
SysRegionConfig.class))
.map(region -> region.stream().collect(
Collectors.toMap(SysRegionConfig::getId,
v -> StringUtils.isBlankOrElse(v.getRegionName(), ""))))
v -> StringUtils.isBlankOrElse(v.getRegionName(), ""),
(first, second) -> first)))
.orElseGet(Maps::newHashMap);
}
@ -177,7 +179,8 @@ public class SysRegionConfigServiceImpl implements SysRegionConfigService {
return CollectionUtils.newHashMap();
}
return regionConfigs.stream().collect(Collectors.toMap(SysRegionConfig::getId, v -> v));
return regionConfigs.stream().collect(
Collectors.toMap(SysRegionConfig::getId, v -> v, (first, second) -> first));
}
@Override

View File

@ -408,7 +408,7 @@ public class UserRegionGatewayImpl implements UserRegionGateway {
}
Map<String, SysRegionConfig> regionMap = sysRegionConfigs.stream()
.collect(Collectors.toMap(SysRegionConfig::getId, v -> v));
.collect(Collectors.toMap(SysRegionConfig::getId, v -> v, (first, second) -> first));
if (CollectionUtils.isEmpty(regionMap)) {
return Boolean.FALSE;
}
@ -775,7 +775,7 @@ public class UserRegionGatewayImpl implements UserRegionGateway {
}
Map<String, SysRegionConfig> sysRegionConfigMap = sysRegionConfigs.stream()
.collect(Collectors.toMap(SysRegionConfig::getId, v -> v));
.collect(Collectors.toMap(SysRegionConfig::getId, v -> v, (first, second) -> first));
return teamProfiles.stream()
.collect(

View File

@ -0,0 +1,64 @@
package com.red.circle.other.infra.database.mongo.service.user.region.impl;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.red.circle.other.infra.database.mongo.entity.user.region.SysRegionConfig;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
class SysRegionConfigServiceImplTest {
private static final String REGION_ID = "2051777551323426818";
private static final String REGION_NAME = "东南亚区";
private MongoTemplate mongoTemplate;
private SysRegionConfigServiceImpl service;
@BeforeEach
void setUp() {
mongoTemplate = mock(MongoTemplate.class);
service = new SysRegionConfigServiceImpl(mongoTemplate);
}
@Test
void mapRegionName_shouldKeepFirstWhenMongoReturnsDuplicateIds() {
when(mongoTemplate.find(any(Query.class), eq(SysRegionConfig.class))).thenReturn(List.of(
region(REGION_ID, "SEA", REGION_NAME),
region(REGION_ID, "SEA", REGION_NAME)
));
Map<String, String> result = service.mapRegionName("LIKEI");
assertEquals(Map.of(REGION_ID, REGION_NAME), result);
}
@Test
void mapAvailable_shouldKeepFirstWhenMongoReturnsDuplicateIds() {
when(mongoTemplate.find(any(Query.class), eq(SysRegionConfig.class))).thenReturn(List.of(
region(REGION_ID, "SEA", REGION_NAME),
region(REGION_ID, "SEA2", REGION_NAME)
));
Map<String, SysRegionConfig> result = service.mapAvailable(Set.of(REGION_ID));
assertEquals(1, result.size());
assertEquals("SEA", result.get(REGION_ID).getRegionCode());
}
private SysRegionConfig region(String id, String regionCode, String regionName) {
return new SysRegionConfig()
.setId(id)
.setRegionCode(regionCode)
.setRegionName(regionName)
.setDel(Boolean.FALSE);
}
}

View File

@ -37,6 +37,7 @@ class UserRegionGatewayImplTest {
private static final String SYS_ORIGIN = "LIKEI";
private static final String TEAM_REGION_ID = "middle-east-region";
private static final String TEAM_REGION_CODE = "中东区";
private static final String SEA_REGION_ID = "2051777551323426818";
private static final String SOUTH_ASIA_REGION_CODE = "南亚";
private static final String PAK_ALEX_REGION_CODE = "Pakistan - rolex";
@ -158,6 +159,57 @@ class UserRegionGatewayImplTest {
verify(countryCodeAliasSupport, never()).containsCode("PK,IN,BD,NP", "P2");
}
@Test
void checkOpenDailyAutoSalary_shouldKeepFirstWhenCacheHasDuplicateRegionIds() {
when(sysRegionCacheService.getCache(SYS_ORIGIN)).thenReturn(List.of(
new SysRegionConfig()
.setId(SEA_REGION_ID)
.setRegionName("东南亚区")
.setMetadata(Map.of("openDailyAutoSalary", "true")),
new SysRegionConfig()
.setId(SEA_REGION_ID)
.setRegionName("东南亚区")
.setMetadata(Map.of("openDailyAutoSalary", "false"))
));
Boolean result = userRegionGateway.checkOpenDailyAutoSalary(SEA_REGION_ID, SYS_ORIGIN);
assertEquals(Boolean.TRUE, result);
}
@Test
void mapUserRegionConfig_shouldKeepFirstWhenTeamRegionConfigHasDuplicateIds() {
SysRegionConfig firstRegion = new SysRegionConfig()
.setId(SEA_REGION_ID)
.setRegionCode("SEA")
.setRegionName("东南亚区");
when(teamMemberService.listByMemberIds(Set.of(TEAM_USER_ID))).thenReturn(List.of(
new TeamMember()
.setMemberId(TEAM_USER_ID)
.setTeamId(TEAM_ID)
.setSysOrigin(SYS_ORIGIN)
.setRole(TeamMemberRole.OWN)
));
when(teamProfileService.listByIds(Set.of(TEAM_ID))).thenReturn(List.of(
new TeamProfile().setId(TEAM_ID).setRegion(SEA_REGION_ID)
));
when(sysRegionConfigService.listAvailable(Set.of(SEA_REGION_ID))).thenReturn(List.of(
firstRegion,
new SysRegionConfig()
.setId(SEA_REGION_ID)
.setRegionCode("SEA2")
.setRegionName("东南亚区")
));
when(userRegionInfraConvertor.toRegionConfig(firstRegion)).thenReturn(
new RegionConfig().setId(SEA_REGION_ID).setRegionCode("SEA")
);
Map<Long, RegionConfig> result = userRegionGateway.mapUserRegionConfig(
SYS_ORIGIN, Set.of(TEAM_USER_ID));
assertEquals("SEA", result.get(TEAM_USER_ID).getRegionCode());
}
private SysRegionConfig region(String regionCode, String countryCodes, String langeCodes) {
return new SysRegionConfig()
.setId(regionCode)