/
GovernanceSchemaContextsTest.java
252 lines (218 loc) · 11.7 KB
/
GovernanceSchemaContextsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.shardingsphere.governance.context.schema;
import lombok.SneakyThrows;
import org.apache.shardingsphere.governance.core.config.ConfigCenter;
import org.apache.shardingsphere.governance.core.event.model.auth.AuthenticationChangedEvent;
import org.apache.shardingsphere.governance.core.event.model.datasource.DataSourceChangedEvent;
import org.apache.shardingsphere.governance.core.event.model.metadata.MetaDataChangedEvent;
import org.apache.shardingsphere.governance.core.event.model.props.PropertiesChangedEvent;
import org.apache.shardingsphere.governance.core.event.model.rule.RuleConfigurationsChangedEvent;
import org.apache.shardingsphere.governance.core.event.model.schema.SchemaAddedEvent;
import org.apache.shardingsphere.governance.core.event.model.schema.SchemaDeletedEvent;
import org.apache.shardingsphere.governance.core.facade.GovernanceFacade;
import org.apache.shardingsphere.governance.core.registry.RegistryCenter;
import org.apache.shardingsphere.governance.core.registry.event.CircuitStateChangedEvent;
import org.apache.shardingsphere.governance.core.registry.event.DisabledStateChangedEvent;
import org.apache.shardingsphere.governance.core.registry.schema.GovernanceSchema;
import org.apache.shardingsphere.infra.auth.Authentication;
import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.context.schema.SchemaContext;
import org.apache.shardingsphere.infra.context.schema.ShardingSphereSchema;
import org.apache.shardingsphere.infra.context.schema.impl.StandardSchemaContexts;
import org.apache.shardingsphere.infra.context.schema.runtime.RuntimeContext;
import org.apache.shardingsphere.infra.database.metadata.DataSourceMetaData;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.schema.RuleSchemaMetaData;
import org.apache.shardingsphere.infra.rule.event.RuleChangedEvent;
import org.apache.shardingsphere.jdbc.test.MockedDataSource;
import org.apache.shardingsphere.replication.primaryreplica.rule.PrimaryReplicaReplicationRule;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public final class GovernanceSchemaContextsTest {
private final Authentication authentication = new Authentication();
private final ConfigurationProperties props = new ConfigurationProperties(new Properties());
@Mock
private DatabaseType databaseType;
@Mock
private GovernanceFacade governanceFacade;
@Mock
private RegistryCenter registryCenter;
@Mock
private ConfigCenter configCenter;
@Mock
private SchemaContext schemaContext;
@Mock
private PrimaryReplicaReplicationRule primaryReplicaReplicationRule;
private GovernanceSchemaContexts governanceSchemaContexts;
@Before
public void setUp() {
when(databaseType.getName()).thenReturn("H2");
when(databaseType.getDataSourceMetaData(any(), any())).thenReturn(mock(DataSourceMetaData.class));
when(governanceFacade.getRegistryCenter()).thenReturn(registryCenter);
when(governanceFacade.getConfigCenter()).thenReturn(configCenter);
when(registryCenter.loadDisabledDataSources("schema")).thenReturn(Collections.singletonList("schema.ds_1"));
governanceSchemaContexts = new GovernanceSchemaContexts(new StandardSchemaContexts(createSchemaContextMap(), authentication, props, databaseType), governanceFacade);
}
@SneakyThrows
private Map<String, SchemaContext> createSchemaContextMap() {
ShardingSphereSchema shardingSphereSchema = mock(ShardingSphereSchema.class);
ShardingSphereMetaData shardingSphereMetaData = mock(ShardingSphereMetaData.class);
RuntimeContext runtimeContext = mock(RuntimeContext.class);
when(schemaContext.getSchema()).thenReturn(shardingSphereSchema);
when(schemaContext.getSchema().getName()).thenReturn("schema");
when(schemaContext.getRuntimeContext()).thenReturn(runtimeContext);
when(shardingSphereSchema.getMetaData()).thenReturn(shardingSphereMetaData);
when(shardingSphereSchema.getRules()).thenReturn(Collections.singletonList(primaryReplicaReplicationRule));
return Collections.singletonMap("schema", schemaContext);
}
@Test
public void assertGetDatabaseType() {
assertThat(governanceSchemaContexts.getDatabaseType().getName(), is("H2"));
}
@Test
public void assertGetSchemaContexts() {
assertThat(governanceSchemaContexts.getSchemaContextMap().get("schema"), is(schemaContext));
}
@Test
public void assertGetDefaultSchemaContext() {
assertNull(governanceSchemaContexts.getDefaultSchemaContext());
}
@Test
public void assertGetAuthentication() {
assertThat(governanceSchemaContexts.getAuthentication(), is(authentication));
}
@Test
public void assertGetProps() {
assertThat(governanceSchemaContexts.getProps(), is(props));
}
@Test
public void assertIsCircuitBreak() {
assertFalse(governanceSchemaContexts.isCircuitBreak());
}
@Test
@SneakyThrows
public void assertSchemaAdd() {
SchemaAddedEvent event = new SchemaAddedEvent("schema_add", new HashMap<>(), new LinkedList<>());
when(configCenter.loadDataSourceConfigurations("schema_add")).thenReturn(getDataSourceConfigurations());
governanceSchemaContexts.renew(event);
assertNotNull(governanceSchemaContexts.getSchemaContextMap().get("schema_add"));
assertNotNull(governanceSchemaContexts.getSchemaContextMap().get("schema_add").getSchema().getDataSources());
}
private Map<String, DataSourceConfiguration> getDataSourceConfigurations() {
MockedDataSource dataSource = new MockedDataSource();
Map<String, DataSourceConfiguration> result = new LinkedHashMap<>(3, 1);
result.put("primary_ds", DataSourceConfiguration.getDataSourceConfiguration(dataSource));
result.put("ds_0", DataSourceConfiguration.getDataSourceConfiguration(dataSource));
result.put("ds_1", DataSourceConfiguration.getDataSourceConfiguration(dataSource));
return result;
}
@Test
public void assertSchemaDelete() {
SchemaDeletedEvent event = new SchemaDeletedEvent("schema");
governanceSchemaContexts.renew(event);
assertNull(governanceSchemaContexts.getSchemaContextMap().get("schema"));
}
@Test
public void assertPropertiesChanged() {
Properties properties = new Properties();
properties.setProperty(ConfigurationPropertyKey.SQL_SHOW.getKey(), "true");
PropertiesChangedEvent event = new PropertiesChangedEvent(properties);
governanceSchemaContexts.renew(event);
assertThat(governanceSchemaContexts.getProps().getProps().getProperty(ConfigurationPropertyKey.SQL_SHOW.getKey()), is("true"));
}
@Test
public void assertAuthenticationChanged() {
Authentication authentication = new Authentication();
AuthenticationChangedEvent event = new AuthenticationChangedEvent(authentication);
governanceSchemaContexts.renew(event);
assertThat(governanceSchemaContexts.getAuthentication(), is(authentication));
}
@Test
public void assertMetaDataChanged() {
MetaDataChangedEvent event = new MetaDataChangedEvent(Collections.singletonList("schema_changed"), mock(RuleSchemaMetaData.class));
governanceSchemaContexts.renew(event);
assertTrue(governanceSchemaContexts.getSchemaContextMap().containsKey("schema"));
assertFalse(governanceSchemaContexts.getSchemaContextMap().containsKey("schema_changed"));
}
@Test
public void assertMetaDataChangedWithExistSchema() {
MetaDataChangedEvent event = new MetaDataChangedEvent(Collections.singletonList("schema"), mock(RuleSchemaMetaData.class));
governanceSchemaContexts.renew(event);
assertThat(governanceSchemaContexts.getSchemaContextMap().get("schema"), not(schemaContext));
}
@Test
@SneakyThrows
public void assertRuleConfigurationsChanged() {
assertThat(governanceSchemaContexts.getSchemaContextMap().get("schema"), is(schemaContext));
RuleConfigurationsChangedEvent event = new RuleConfigurationsChangedEvent("schema", new LinkedList<>());
governanceSchemaContexts.renew(event);
assertThat(governanceSchemaContexts.getSchemaContextMap().get("schema"), not(schemaContext));
}
@Test
public void assertDisableStateChanged() {
DisabledStateChangedEvent event = new DisabledStateChangedEvent(new GovernanceSchema("schema.ds_0"), true);
governanceSchemaContexts.renew(event);
verify(primaryReplicaReplicationRule, times(2)).updateRuleStatus(any(RuleChangedEvent.class));
}
@Test
@SneakyThrows
public void assertDataSourceChanged() {
DataSourceChangedEvent event = new DataSourceChangedEvent("schema", getChangedDataSourceConfigurations());
governanceSchemaContexts.renew(event);
assertTrue(governanceSchemaContexts.getSchemaContextMap().get("schema").getSchema().getDataSources().containsKey("ds_2"));
}
private Map<String, DataSourceConfiguration> getChangedDataSourceConfigurations() {
MockedDataSource dataSource = new MockedDataSource();
Map<String, DataSourceConfiguration> result = new LinkedHashMap<>(3, 1);
result.put("primary_ds", DataSourceConfiguration.getDataSourceConfiguration(dataSource));
result.put("ds_1", DataSourceConfiguration.getDataSourceConfiguration(dataSource));
result.put("ds_2", DataSourceConfiguration.getDataSourceConfiguration(dataSource));
return result;
}
@Test
public void assertCircuitStateChanged() {
CircuitStateChangedEvent event = new CircuitStateChangedEvent(true);
governanceSchemaContexts.renew(event);
assertTrue(governanceSchemaContexts.isCircuitBreak());
}
}