GovernanceSchemaContextsTest.java 11.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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.
 */

18
package org.apache.shardingsphere.governance.context.schema;
19

20
import org.apache.shardingsphere.governance.core.config.ConfigCenter;
L
Liang Zhang 已提交
21 22
import org.apache.shardingsphere.governance.core.event.model.auth.AuthenticationChangedEvent;
import org.apache.shardingsphere.governance.core.event.model.datasource.DataSourceChangedEvent;
L
Liang Zhang 已提交
23
import org.apache.shardingsphere.governance.core.event.model.metadata.SchemaChangedEvent;
L
Liang Zhang 已提交
24 25 26 27
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;
28 29 30 31 32
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;
33
import org.apache.shardingsphere.infra.auth.Authentication;
34
import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
35
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
K
kimmking 已提交
36
import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
37
import org.apache.shardingsphere.infra.context.schema.impl.StandardSchemaContexts;
38 39
import org.apache.shardingsphere.infra.database.metadata.DataSourceMetaData;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
L
Liang Zhang 已提交
40
import org.apache.shardingsphere.infra.executor.kernel.ExecutorKernel;
L
Liang Zhang 已提交
41 42
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
43
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
44
import org.apache.shardingsphere.infra.rule.event.RuleChangedEvent;
45
import org.apache.shardingsphere.jdbc.test.MockedDataSource;
46
import org.apache.shardingsphere.replicaquery.rule.ReplicaQueryRule;
47 48 49
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
50
import org.mockito.Answers;
51 52 53
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

L
Liang Zhang 已提交
54
import java.sql.SQLException;
55
import java.util.Collections;
56
import java.util.HashMap;
57
import java.util.LinkedHashMap;
58
import java.util.LinkedList;
59 60 61 62 63 64 65
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;
66
import static org.junit.Assert.assertNull;
67
import static org.junit.Assert.assertThat;
68
import static org.junit.Assert.assertTrue;
69 70 71 72 73 74 75
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)
76
public final class GovernanceSchemaContextsTest {
77
    
78 79 80 81
    private final Authentication authentication = new Authentication();
    
    private final ConfigurationProperties props = new ConfigurationProperties(new Properties());
    
82 83 84
    @Mock
    private DatabaseType databaseType;
    
85
    @Mock
86
    private GovernanceFacade governanceFacade;
87 88 89 90 91
    
    @Mock
    private RegistryCenter registryCenter;
    
    @Mock
92
    private ConfigCenter configCenter;
93
    
94
    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
95
    private ShardingSphereMetaData metaData;
96 97
    
    @Mock
98
    private ReplicaQueryRule replicaQueryRule;
99
    
100
    private GovernanceSchemaContexts governanceSchemaContexts;
101 102 103
    
    @Before
    public void setUp() {
104 105
        when(databaseType.getName()).thenReturn("H2");
        when(databaseType.getDataSourceMetaData(any(), any())).thenReturn(mock(DataSourceMetaData.class));
106
        when(governanceFacade.getRegistryCenter()).thenReturn(registryCenter);
107
        when(governanceFacade.getConfigCenter()).thenReturn(configCenter);
108
        when(registryCenter.loadDisabledDataSources("schema")).thenReturn(Collections.singletonList("schema.ds_1"));
L
Liang Zhang 已提交
109
        governanceSchemaContexts = new GovernanceSchemaContexts(new StandardSchemaContexts(createMetaDataMap(), mock(ExecutorKernel.class), authentication, props, databaseType), governanceFacade);
110 111
    }
    
L
Liang Zhang 已提交
112
    private Map<String, ShardingSphereMetaData> createMetaDataMap() {
113
        when(metaData.getName()).thenReturn("schema");
L
Liang Zhang 已提交
114
        when(metaData.getResource()).thenReturn(mock(ShardingSphereResource.class));
115
        when(metaData.getSchema()).thenReturn(mock(ShardingSphereSchema.class));
116
        when(metaData.getRuleMetaData().getRules()).thenReturn(Collections.singletonList(replicaQueryRule));
117
        return Collections.singletonMap("schema", metaData);
118 119 120 121
    }
    
    @Test
    public void assertGetDatabaseType() {
122
        assertThat(governanceSchemaContexts.getDatabaseType().getName(), is("H2"));
123 124 125
    }
    
    @Test
L
Liang Zhang 已提交
126
    public void assertGetSchemas() {
127
        assertThat(governanceSchemaContexts.getMetaDataMap().get("schema"), is(metaData));
128 129 130
    }
    
    @Test
L
Liang Zhang 已提交
131
    public void assertGetDefaultSchema() {
132
        assertNull(governanceSchemaContexts.getDefaultMetaData());
133 134 135 136
    }
    
    @Test
    public void assertGetAuthentication() {
137
        assertThat(governanceSchemaContexts.getAuthentication(), is(authentication));
138 139 140 141
    }
    
    @Test
    public void assertGetProps() {
142
        assertThat(governanceSchemaContexts.getProps(), is(props));
143 144 145 146
    }
    
    @Test
    public void assertIsCircuitBreak() {
147
        assertFalse(governanceSchemaContexts.isCircuitBreak());
148 149 150
    }
    
    @Test
L
Liang Zhang 已提交
151
    public void assertSchemaAdd() throws SQLException {
152 153
        SchemaAddedEvent event = new SchemaAddedEvent("schema_add", new HashMap<>(), new LinkedList<>());
        when(configCenter.loadDataSourceConfigurations("schema_add")).thenReturn(getDataSourceConfigurations());
154
        governanceSchemaContexts.renew(event);
155
        assertNotNull(governanceSchemaContexts.getMetaDataMap().get("schema_add"));
L
Liang Zhang 已提交
156
        assertNotNull(governanceSchemaContexts.getMetaDataMap().get("schema_add").getResource().getDataSources());
157 158 159
    }
    
    private Map<String, DataSourceConfiguration> getDataSourceConfigurations() {
160
        MockedDataSource dataSource = new MockedDataSource();
161
        Map<String, DataSourceConfiguration> result = new LinkedHashMap<>(3, 1);
162
        result.put("primary_ds", DataSourceConfiguration.getDataSourceConfiguration(dataSource));
163 164 165 166 167 168 169 170
        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");
171
        governanceSchemaContexts.renew(event);
172
        assertNull(governanceSchemaContexts.getMetaDataMap().get("schema"));
173 174 175 176 177
    }
    
    @Test
    public void assertPropertiesChanged() {
        Properties properties = new Properties();
K
kimmking 已提交
178
        properties.setProperty(ConfigurationPropertyKey.SQL_SHOW.getKey(), "true");
179
        PropertiesChangedEvent event = new PropertiesChangedEvent(properties);
180
        governanceSchemaContexts.renew(event);
K
kimmking 已提交
181
        assertThat(governanceSchemaContexts.getProps().getProps().getProperty(ConfigurationPropertyKey.SQL_SHOW.getKey()), is("true"));
182 183 184 185 186 187
    }
    
    @Test
    public void assertAuthenticationChanged() {
        Authentication authentication = new Authentication();
        AuthenticationChangedEvent event = new AuthenticationChangedEvent(authentication);
188 189
        governanceSchemaContexts.renew(event);
        assertThat(governanceSchemaContexts.getAuthentication(), is(authentication));
190 191 192
    }
    
    @Test
L
Liang Zhang 已提交
193 194
    public void assertSchemaChanged() {
        SchemaChangedEvent event = new SchemaChangedEvent("schema_changed", mock(ShardingSphereSchema.class));
195
        governanceSchemaContexts.renew(event);
196 197
        assertTrue(governanceSchemaContexts.getMetaDataMap().containsKey("schema"));
        assertFalse(governanceSchemaContexts.getMetaDataMap().containsKey("schema_changed"));
198 199 200
    }
    
    @Test
L
Liang Zhang 已提交
201 202
    public void assertSchemaChangedWithExistSchema() {
        SchemaChangedEvent event = new SchemaChangedEvent("schema", mock(ShardingSphereSchema.class));
203
        governanceSchemaContexts.renew(event);
204
        assertThat(governanceSchemaContexts.getMetaDataMap().get("schema"), not(metaData));
205 206 207
    }
    
    @Test
L
Liang Zhang 已提交
208
    public void assertRuleConfigurationsChanged() throws SQLException {
209
        assertThat(governanceSchemaContexts.getMetaDataMap().get("schema"), is(metaData));
210
        RuleConfigurationsChangedEvent event = new RuleConfigurationsChangedEvent("schema", new LinkedList<>());
211
        governanceSchemaContexts.renew(event);
212
        assertThat(governanceSchemaContexts.getMetaDataMap().get("schema"), not(metaData));
213 214 215 216
    }
    
    @Test
    public void assertDisableStateChanged() {
217 218
        DisabledStateChangedEvent event = new DisabledStateChangedEvent(new GovernanceSchema("schema.ds_0"), true);
        governanceSchemaContexts.renew(event);
219
        verify(replicaQueryRule, times(2)).updateRuleStatus(any(RuleChangedEvent.class));
220 221 222
    }
    
    @Test
L
Liang Zhang 已提交
223
    public void assertDataSourceChanged() throws SQLException {
224
        DataSourceChangedEvent event = new DataSourceChangedEvent("schema", getChangedDataSourceConfigurations());
225
        governanceSchemaContexts.renew(event);
L
Liang Zhang 已提交
226
        assertTrue(governanceSchemaContexts.getMetaDataMap().get("schema").getResource().getDataSources().containsKey("ds_2"));
227 228 229
    }
    
    private Map<String, DataSourceConfiguration> getChangedDataSourceConfigurations() {
230
        MockedDataSource dataSource = new MockedDataSource();
231
        Map<String, DataSourceConfiguration> result = new LinkedHashMap<>(3, 1);
232
        result.put("primary_ds", DataSourceConfiguration.getDataSourceConfiguration(dataSource));
233 234 235 236 237 238 239 240
        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);
241 242
        governanceSchemaContexts.renew(event);
        assertTrue(governanceSchemaContexts.isCircuitBreak());
243 244
    }
}