Copilot commented on code in PR #37084:
URL: https://github.com/apache/shardingsphere/pull/37084#discussion_r2519083635
##########
infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/rule/RuleMetaDataTest.java:
##########
@@ -105,6 +106,40 @@ void assertGetInUsedStorageUnitNameAndRulesMap() {
assertTrue(actual.containsKey("foo_db"));
}
+ @Test
+ void
assertGetInUsedStorageUnitNameAndRulesMapWhenRuleClassAddedForExistingStorageUnit()
{
+ ShardingSphereRule firstRule =
mockRuleMetaDataShardingSphereRuleFixture("shared_ds");
+ ShardingSphereRule secondRule = mockShardingSphereRule();
+ Map<String, Collection<Class<? extends ShardingSphereRule>>> actual =
new RuleMetaData(Arrays.asList(firstRule,
secondRule)).getInUsedStorageUnitNameAndRulesMap();
+ Collection<Class<? extends ShardingSphereRule>> ruleClasses =
actual.get("shared_ds");
+ assertThat(ruleClasses.size(), is(2));
+ assertTrue(ruleClasses.contains(firstRule.getClass()));
+ assertTrue(ruleClasses.contains(secondRule.getClass()));
+ }
+
+ @Test
+ void
assertGetInUsedStorageUnitNameAndRulesMapWhenDuplicatedRuleClassSkippedForExistingStorageUnit()
{
+ ShardingSphereRule duplicatedRule =
mockRuleMetaDataShardingSphereRuleFixture("dup_ds");
+ RuleMetaData metaData = new RuleMetaData(Arrays.asList(duplicatedRule,
duplicatedRule));
+
assertThat(metaData.getInUsedStorageUnitNameAndRulesMap().get("dup_ds").size(),
is(1));
+ }
+
+ private RuleMetaDataShardingSphereRuleFixture
mockRuleMetaDataShardingSphereRuleFixture(final String storageUnitName) {
+ RuleMetaDataShardingSphereRuleFixture result =
mock(RuleMetaDataShardingSphereRuleFixture.class, RETURNS_DEEP_STUBS);
+ DataSourceMapperRuleAttribute ruleAttribute =
mock(DataSourceMapperRuleAttribute.class);
+
when(ruleAttribute.getDataSourceMapper()).thenReturn(Collections.singletonMap("logic_db",
Collections.singleton(storageUnitName)));
+ when(result.getAttributes()).thenReturn(new
RuleAttributes(ruleAttribute));
+ return result;
+ }
+
+ private ShardingSphereRule mockShardingSphereRule() {
+ ShardingSphereRule result = mock(ShardingSphereRule.class,
RETURNS_DEEP_STUBS);
+ DataSourceMapperRuleAttribute ruleAttribute =
mock(DataSourceMapperRuleAttribute.class);
+
when(ruleAttribute.getDataSourceMapper()).thenReturn(Collections.singletonMap("logic_db",
Collections.singleton("shared_ds")));
+ when(result.getAttributes()).thenReturn(new
RuleAttributes(ruleAttribute));
+ return result;
Review Comment:
The `mockShardingSphereRule()` method duplicates the mocking logic from
`mockRuleMetaDataShardingSphereRuleFixture()` with only a hardcoded storage
unit name difference. Consider refactoring by having `mockShardingSphereRule()`
delegate to `mockRuleMetaDataShardingSphereRuleFixture(\"shared_ds\")` or by
extracting the common mocking logic into a single parameterized helper method
to reduce code duplication.
```suggestion
private <T extends ShardingSphereRule> T
mockDataSourceMapperRuleAttributeRule(Class<T> type, final String
storageUnitName) {
T result = mock(type, RETURNS_DEEP_STUBS);
DataSourceMapperRuleAttribute ruleAttribute =
mock(DataSourceMapperRuleAttribute.class);
when(ruleAttribute.getDataSourceMapper()).thenReturn(Collections.singletonMap("logic_db",
Collections.singleton(storageUnitName)));
when(result.getAttributes()).thenReturn(new
RuleAttributes(ruleAttribute));
return result;
}
private RuleMetaDataShardingSphereRuleFixture
mockRuleMetaDataShardingSphereRuleFixture(final String storageUnitName) {
return
mockDataSourceMapperRuleAttributeRule(RuleMetaDataShardingSphereRuleFixture.class,
storageUnitName);
}
private ShardingSphereRule mockShardingSphereRule() {
return
mockDataSourceMapperRuleAttributeRule(ShardingSphereRule.class, "shared_ds");
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]