18
18
package org .apache .shardingsphere .readwritesplitting .distsql .handler .update ;
19
19
20
20
import org .apache .shardingsphere .distsql .segment .AlgorithmSegment ;
21
+ import org .apache .shardingsphere .infra .algorithm .core .exception .InvalidAlgorithmConfigurationException ;
21
22
import org .apache .shardingsphere .infra .exception .kernel .metadata .resource .storageunit .MissingRequiredStorageUnitsException ;
22
23
import org .apache .shardingsphere .infra .exception .kernel .metadata .rule .MissingRequiredRuleException ;
23
24
import org .apache .shardingsphere .infra .metadata .database .ShardingSphereDatabase ;
29
30
import org .apache .shardingsphere .readwritesplitting .distsql .statement .AlterReadwriteSplittingRuleStatement ;
30
31
import org .apache .shardingsphere .readwritesplitting .exception .actual .DuplicateReadwriteSplittingActualDataSourceException ;
31
32
import org .apache .shardingsphere .readwritesplitting .rule .ReadwriteSplittingRule ;
33
+ import org .apache .shardingsphere .test .util .PropertiesBuilder ;
34
+ import org .apache .shardingsphere .test .util .PropertiesBuilder .Property ;
32
35
import org .junit .jupiter .api .BeforeEach ;
33
36
import org .junit .jupiter .api .Test ;
34
37
import org .junit .jupiter .api .extension .ExtendWith ;
@@ -74,7 +77,7 @@ void assertCheckSQLStatementWithoutToBeAlteredRules() {
74
77
}
75
78
76
79
@ Test
77
- void assertCheckSQLStatementWithoutExistedResources () {
80
+ void assertCheckSQLStatementWithNotExistedDataSources () {
78
81
when (resourceMetaData .getNotExistedDataSources (any ())).thenReturn (Collections .singleton ("read_ds_0" ));
79
82
ReadwriteSplittingRule rule = mock (ReadwriteSplittingRule .class );
80
83
when (rule .getConfiguration ()).thenReturn (createCurrentRuleConfiguration ());
@@ -93,48 +96,58 @@ void assertCheckSQLStatementWithoutToBeAlteredLoadBalancers() {
93
96
}
94
97
95
98
@ Test
96
- void assertCheckSQLStatementWithDuplicateWriteResourceNamesInStatement () {
99
+ void assertCheckSQLStatementWithDuplicateWriteDataSourcesInStatement () {
97
100
ShardingSphereDatabase database = mock (ShardingSphereDatabase .class , RETURNS_DEEP_STUBS );
98
101
when (database .getResourceMetaData ()).thenReturn (resourceMetaData );
99
102
ReadwriteSplittingRule rule = mock (ReadwriteSplittingRule .class );
100
103
when (rule .getConfiguration ()).thenReturn (createCurrentRuleConfigurationWithMultipleRules ());
101
104
executor .setRule (rule );
102
105
assertThrows (DuplicateReadwriteSplittingActualDataSourceException .class ,
103
- () -> executor .checkBeforeUpdate (createSQLStatementWithDuplicateWriteResourceNames ("readwrite_ds_0" , "readwrite_ds_1" , "TEST" )));
106
+ () -> executor .checkBeforeUpdate (createSQLStatementWithDuplicateWriteDataSources ("readwrite_ds_0" , "readwrite_ds_1" , "TEST" )));
104
107
}
105
108
106
109
@ Test
107
- void assertCheckSQLStatementWithDuplicateWriteResourceNames () {
110
+ void assertCheckSQLStatementWithDuplicateWriteDataSources () {
108
111
ShardingSphereDatabase database = mock (ShardingSphereDatabase .class , RETURNS_DEEP_STUBS );
109
112
when (database .getResourceMetaData ()).thenReturn (resourceMetaData );
110
113
ReadwriteSplittingRule rule = mock (ReadwriteSplittingRule .class );
111
114
when (rule .getConfiguration ()).thenReturn (createCurrentRuleConfigurationWithMultipleRules ());
112
115
executor .setRule (rule );
113
116
assertThrows (DuplicateReadwriteSplittingActualDataSourceException .class ,
114
117
() -> executor .checkBeforeUpdate (
115
- createSQLStatement ("readwrite_ds_0" , "ds_write_1" , Arrays .asList ("read_ds_0" , "read_ds_1" ), "TEST" )));
118
+ createSQLStatement ("readwrite_ds_0" , "ds_write_1" , Arrays .asList ("read_ds_0" , "read_ds_1" ), "TEST" , new Properties () )));
116
119
}
117
120
118
121
@ Test
119
- void assertCheckSQLStatementWithDuplicateReadResourceNamesInStatement () {
122
+ void assertCheckSQLStatementWithDuplicateReadDataSourcesInStatement () {
120
123
ShardingSphereDatabase database = mock (ShardingSphereDatabase .class , RETURNS_DEEP_STUBS );
121
124
when (database .getResourceMetaData ()).thenReturn (resourceMetaData );
122
125
ReadwriteSplittingRule rule = mock (ReadwriteSplittingRule .class );
123
126
when (rule .getConfiguration ()).thenReturn (createCurrentRuleConfigurationWithMultipleRules ());
124
127
executor .setRule (rule );
125
128
assertThrows (DuplicateReadwriteSplittingActualDataSourceException .class ,
126
- () -> executor .checkBeforeUpdate (createSQLStatementWithDuplicateReadResourceNames ("readwrite_ds_0" , "readwrite_ds_1" , "TEST" )));
129
+ () -> executor .checkBeforeUpdate (createSQLStatementWithDuplicateReadDataSources ("readwrite_ds_0" , "readwrite_ds_1" , "TEST" )));
127
130
}
128
131
129
132
@ Test
130
- void assertCheckSQLStatementWithDuplicateReadResourceNames () {
133
+ void assertCheckSQLStatementWithDuplicateReadDataSources () {
131
134
ShardingSphereDatabase database = mock (ShardingSphereDatabase .class , RETURNS_DEEP_STUBS );
132
135
when (database .getResourceMetaData ()).thenReturn (resourceMetaData );
133
136
ReadwriteSplittingRule rule = mock (ReadwriteSplittingRule .class );
134
137
when (rule .getConfiguration ()).thenReturn (createCurrentRuleConfigurationWithMultipleRules ());
135
138
executor .setRule (rule );
136
139
assertThrows (DuplicateReadwriteSplittingActualDataSourceException .class ,
137
- () -> executor .checkBeforeUpdate (createSQLStatement ("readwrite_ds_1" , "write_ds_1" , Arrays .asList ("read_ds_0_0" , "read_ds_0_1" ), "TEST" )));
140
+ () -> executor .checkBeforeUpdate (createSQLStatement ("readwrite_ds_1" , "write_ds_1" , Arrays .asList ("read_ds_0_0" , "read_ds_0_1" ), "TEST" , new Properties ())));
141
+ }
142
+
143
+ @ Test
144
+ void assertCheckSQLStatementWithInvalidLoadBalancerProperties () {
145
+ ReadwriteSplittingRule rule = mock (ReadwriteSplittingRule .class );
146
+ when (rule .getConfiguration ()).thenReturn (createCurrentRuleConfiguration ());
147
+ executor .setRule (rule );
148
+ assertThrows (InvalidAlgorithmConfigurationException .class ,
149
+ () -> executor .checkBeforeUpdate (createSQLStatement ("readwrite_ds" , "write_ds" , Arrays .asList ("read_ds_0" , "read_ds_1" ), "weight" ,
150
+ PropertiesBuilder .build (new Property ("read_ds_0" , "5" ), new Property ("read_ds_2" , "5" )))));
138
151
}
139
152
140
153
private AlterReadwriteSplittingRuleStatement createSQLStatement (final String loadBalancerTypeName ) {
@@ -143,20 +156,21 @@ private AlterReadwriteSplittingRuleStatement createSQLStatement(final String loa
143
156
return new AlterReadwriteSplittingRuleStatement (Collections .singleton (ruleSegment ));
144
157
}
145
158
146
- private AlterReadwriteSplittingRuleStatement createSQLStatement (final String ruleName , final String writeDataSource , final Collection <String > readDataSources , final String loadBalancerName ) {
147
- ReadwriteSplittingRuleSegment ruleSegment = new ReadwriteSplittingRuleSegment (ruleName , writeDataSource , readDataSources , new AlgorithmSegment (loadBalancerName , new Properties ()));
159
+ private AlterReadwriteSplittingRuleStatement createSQLStatement (final String ruleName , final String writeDataSource , final Collection <String > readDataSources ,
160
+ final String loadBalancerName , final Properties props ) {
161
+ ReadwriteSplittingRuleSegment ruleSegment = new ReadwriteSplittingRuleSegment (ruleName , writeDataSource , readDataSources , new AlgorithmSegment (loadBalancerName , props ));
148
162
return new AlterReadwriteSplittingRuleStatement (Collections .singleton (ruleSegment ));
149
163
}
150
164
151
- private AlterReadwriteSplittingRuleStatement createSQLStatementWithDuplicateWriteResourceNames (final String ruleName0 , final String ruleName1 , final String loadBalancerName ) {
165
+ private AlterReadwriteSplittingRuleStatement createSQLStatementWithDuplicateWriteDataSources (final String ruleName0 , final String ruleName1 , final String loadBalancerName ) {
152
166
ReadwriteSplittingRuleSegment ruleSegment0 = new ReadwriteSplittingRuleSegment (ruleName0 , "write_ds" , Arrays .asList ("read_ds_0" , "read_ds_1" ),
153
167
new AlgorithmSegment (loadBalancerName , new Properties ()));
154
168
ReadwriteSplittingRuleSegment ruleSegment1 = new ReadwriteSplittingRuleSegment (ruleName1 , "write_ds" , Arrays .asList ("read_ds_2" , "read_ds_3" ),
155
169
new AlgorithmSegment (loadBalancerName , new Properties ()));
156
170
return new AlterReadwriteSplittingRuleStatement (Arrays .asList (ruleSegment0 , ruleSegment1 ));
157
171
}
158
172
159
- private AlterReadwriteSplittingRuleStatement createSQLStatementWithDuplicateReadResourceNames (final String ruleName0 , final String ruleName1 , final String loadBalancerName ) {
173
+ private AlterReadwriteSplittingRuleStatement createSQLStatementWithDuplicateReadDataSources (final String ruleName0 , final String ruleName1 , final String loadBalancerName ) {
160
174
ReadwriteSplittingRuleSegment ruleSegment0 = new ReadwriteSplittingRuleSegment (ruleName0 , "write_ds_0" , Arrays .asList ("read_ds_0" , "read_ds_1" ),
161
175
new AlgorithmSegment (loadBalancerName , new Properties ()));
162
176
ReadwriteSplittingRuleSegment ruleSegment1 = new ReadwriteSplittingRuleSegment (ruleName1 , "write_ds_1" , Arrays .asList ("read_ds_0" , "read_ds_1" ),
0 commit comments