Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add unit test #30587

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,79 @@

package org.apache.shardingsphere.metadata.persist;

import com.google.common.collect.Maps;
import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
import org.apache.shardingsphere.infra.config.database.impl.DataSourceProvidedDatabaseConfiguration;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.metadata.persist.fixture.FixtureRule;
import org.apache.shardingsphere.metadata.persist.fixture.YamlDataNodeGlobalRuleConfigurationFixture;
import org.apache.shardingsphere.metadata.persist.fixture.YamlDataNodeRuleConfigurationFixture;
import org.apache.shardingsphere.metadata.persist.node.GlobalNode;
import org.apache.shardingsphere.metadata.persist.service.config.database.datasource.DataSourceUnitPersistService;
import org.apache.shardingsphere.metadata.persist.service.config.database.rule.DatabaseRulePersistService;
import org.apache.shardingsphere.metadata.persist.service.config.global.GlobalRulePersistService;
import org.apache.shardingsphere.metadata.persist.service.config.global.PropertiesPersistService;
import org.apache.shardingsphere.metadata.persist.service.database.DatabaseMetaDataPersistService;
import org.apache.shardingsphere.mode.spi.PersistRepository;
import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.junit.jupiter.MockitoExtension;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class MetaDataPersistServiceTest {

private static final String DEFAULT_VERSION = "0";

@Mock
private PersistRepository repository;

@Mock
private DataSourceUnitPersistService dataSourceService;

@Mock
private DatabaseRulePersistService databaseRulePersistService;

@Mock
private GlobalRulePersistService globalRuleService;
private DatabaseMetaDataPersistService databaseMetaDataService;

@Mock
private PropertiesPersistService propsService;
private DataSourceUnitPersistService dataSourceUnitService;

private MetaDataPersistService metaDataPersistService;

@BeforeEach
void setUp() throws ReflectiveOperationException {
metaDataPersistService = new MetaDataPersistService(mock(PersistRepository.class));
final GlobalRulePersistService globalRuleService = new GlobalRulePersistService(repository);
final PropertiesPersistService propsService = new PropertiesPersistService(repository);
metaDataPersistService = new MetaDataPersistService(repository);
setField("dataSourceUnitService", dataSourceService);
setField("databaseRulePersistService", databaseRulePersistService);
setField("globalRuleService", globalRuleService);
setField("propsService", propsService);
setField("databaseMetaDataService", databaseMetaDataService);
setField("dataSourceUnitService", dataSourceUnitService);
}

private void setField(final String name, final Object value) throws ReflectiveOperationException {
Expand All @@ -66,4 +100,45 @@ private void setField(final String name, final Object value) throws ReflectiveOp
void assertLoadDataSourceConfigurations() {
assertTrue(metaDataPersistService.loadDataSourceConfigurations("foo_db").isEmpty());
}

@Test
void testPersistGlobalRuleConfiguration() {
Aias00 marked this conversation as resolved.
Show resolved Hide resolved
Collection<RuleConfiguration> expectRuleConfigs = buildRuleConfigs();
when(repository.getDirectly(anyString())).thenReturn("0")
.thenReturn("");
Properties props = new Properties();
when(repository.getChildrenKeys(any())).thenReturn(Collections.singletonList("0"))
.thenReturn(Collections.emptyList());

metaDataPersistService.persistGlobalRuleConfiguration(expectRuleConfigs, props);

// Assert
verify(repository, times(3)).persist(anyString(), anyString());
verify(repository).persist(GlobalNode.getPropsActiveVersionNode(), DEFAULT_VERSION);
verify(repository).persist(GlobalNode.getPropsVersionNode(DEFAULT_VERSION), YamlEngine.marshal(props));
}

@Test
void testPersistConfigurations() throws SQLException {
String databaseName = "test_database";
DataSource datasource = mockDataSource();
DatabaseConfiguration databaseConfig = new DataSourceProvidedDatabaseConfiguration(Collections.singletonMap("foo_db", datasource),
Collections.singletonList(new YamlDataNodeRuleConfigurationFixture("foo", "foo_val")));
Map<String, DataSource> dataSourceMap = Maps.newHashMap();
dataSourceMap.put("mysql", datasource);
metaDataPersistService.persistConfigurations(databaseName, databaseConfig, dataSourceMap, Collections.singletonList(new FixtureRule()));
}

private Collection<RuleConfiguration> buildRuleConfigs() {
YamlDataNodeGlobalRuleConfigurationFixture ruleConfigurationFixture = new YamlDataNodeGlobalRuleConfigurationFixture();
ruleConfigurationFixture.setKey("foo");
ruleConfigurationFixture.setValue("foo_value");
return Collections.singletonList(ruleConfigurationFixture);
}

private DataSource mockDataSource() throws SQLException {
Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS);
return new MockedDataSource(connection);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.metadata.persist.fixture;

import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
import org.apache.shardingsphere.infra.rule.attribute.datasource.DataSourceMapperRuleAttribute;
import org.apache.shardingsphere.infra.rule.scope.DatabaseRule;

import static org.mockito.Mockito.mock;

public final class FixtureRule implements DatabaseRule {

@Override
public RuleConfiguration getConfiguration() {
return mock(RuleConfiguration.class);
}

@Override
public RuleAttributes getAttributes() {
return new RuleAttributes(mock(DataSourceMapperRuleAttribute.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.metadata.persist.fixture;

import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.config.rule.function.EnhancedRuleConfiguration;

@Getter
@Setter
public final class YamlDataNodeGlobalRuleConfigurationFixture implements RuleConfiguration, EnhancedRuleConfiguration {

private String key;

private String value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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.metadata.persist.fixture;

import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.infra.util.yaml.datanode.YamlDataNode;
import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlDataNodeGlobalRuleConfigurationSwapper;

import java.util.Collection;
import java.util.Collections;
import java.util.Optional;

public class YamlDataNodeGlobalRuleConfigurationSwapperFixture implements YamlDataNodeGlobalRuleConfigurationSwapper<YamlDataNodeGlobalRuleConfigurationFixture> {

@Override
public String getRuleTagName() {
return "SINGLE";
}

@Override
public int getOrder() {
return 30;
}

@Override
public Class<YamlDataNodeGlobalRuleConfigurationFixture> getTypeClass() {
return YamlDataNodeGlobalRuleConfigurationFixture.class;
}

@Override
public Collection<YamlDataNode> swapToDataNodes(final YamlDataNodeGlobalRuleConfigurationFixture data) {
return Collections.singletonList(new YamlDataNode(data.getKey(), YamlEngine.marshal(swapToYamlConfiguration(data))));
}

private YamlDataNodeGlobalRuleConfigurationFixture swapToYamlConfiguration(final YamlDataNodeGlobalRuleConfigurationFixture data) {
return data;
}

@Override
public Optional<YamlDataNodeGlobalRuleConfigurationFixture> swapToObject(final Collection<YamlDataNode> dataNodes) {
if (null == dataNodes || dataNodes.isEmpty()) {
return Optional.empty();
}
YamlDataNode dataNode = dataNodes.iterator().next();
YamlDataNodeGlobalRuleConfigurationFixture configurationFixture = new YamlDataNodeGlobalRuleConfigurationFixture();
configurationFixture.setKey(dataNode.getKey());
configurationFixture.setValue(dataNode.getValue());
return Optional.of(configurationFixture);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.metadata.persist.fixture;

import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.config.rule.function.EnhancedRuleConfiguration;

@Getter
@Setter
public final class YamlDataNodeRuleConfigurationFixture implements RuleConfiguration, EnhancedRuleConfiguration {

private String key;

private String value;

public YamlDataNodeRuleConfigurationFixture() {
}

public YamlDataNodeRuleConfigurationFixture(final String key, final String value) {
this.key = key;
this.value = value;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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.metadata.persist.fixture;

import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.infra.util.yaml.datanode.YamlDataNode;
import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlDataNodeRuleConfigurationSwapper;

import java.util.Collection;
import java.util.Collections;
import java.util.Optional;

public class YamlDataNodeRuleConfigurationSwapperFixture implements YamlDataNodeRuleConfigurationSwapper<YamlDataNodeRuleConfigurationFixture> {

@Override
public String getRuleTagName() {
return "SINGLE";
}

@Override
public int getOrder() {
return 30;
}

@Override
public Class<YamlDataNodeRuleConfigurationFixture> getTypeClass() {
return YamlDataNodeRuleConfigurationFixture.class;
}

@Override
public Collection<YamlDataNode> swapToDataNodes(final YamlDataNodeRuleConfigurationFixture data) {
return Collections.singletonList(new YamlDataNode(data.getKey(), YamlEngine.marshal(swapToYamlConfiguration(data))));
}

private YamlDataNodeRuleConfigurationFixture swapToYamlConfiguration(final YamlDataNodeRuleConfigurationFixture data) {
return data;
}

@Override
public Optional<YamlDataNodeRuleConfigurationFixture> swapToObject(final Collection<YamlDataNode> dataNodes) {
if (null == dataNodes || dataNodes.isEmpty()) {
return Optional.empty();
}
YamlDataNode dataNode = dataNodes.iterator().next();
YamlDataNodeRuleConfigurationFixture configurationFixture = new YamlDataNodeRuleConfigurationFixture();
configurationFixture.setKey(dataNode.getKey());
configurationFixture.setValue(dataNode.getValue());
return Optional.of(configurationFixture);
}
}
Loading