diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseTest.java index ccc5c9a84a065..fa3de5059d2d4 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseTest.java @@ -23,6 +23,7 @@ import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; +import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule; import org.apache.shardingsphere.infra.rule.identifier.type.MutableDataNodeRule; @@ -31,6 +32,7 @@ import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource; import org.apache.shardingsphere.test.mock.AutoMockExtension; import org.apache.shardingsphere.test.mock.StaticMockSettings; + import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoSettings; @@ -40,12 +42,15 @@ import java.util.Collection; import java.util.Collections; import java.util.LinkedList; +import java.util.HashMap; +import java.util.Map; import java.util.Properties; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; @@ -55,6 +60,57 @@ @MockitoSettings(strictness = Strictness.LENIENT) class ShardingSphereDatabaseTest { + @Test + void assertContainsSchema() { + DatabaseType databaseType = mock(DatabaseType.class); + RuleMetaData ruleMetaData = mock(RuleMetaData.class); + Map schemas = new HashMap<>(); + ShardingSphereSchema schema = mock(ShardingSphereSchema.class); + schemas.put("schema1", schema); + ShardingSphereDatabase database = new ShardingSphereDatabase("foo_db", databaseType, mock(ResourceMetaData.class), ruleMetaData, schemas); + assertTrue(database.containsSchema("schema1")); + assertFalse(database.containsSchema("non_existent_schema")); + } + + @Test + void assertGetSchema() { + DatabaseType databaseType = mock(DatabaseType.class); + RuleMetaData ruleMetaData = mock(RuleMetaData.class); + Map schemas = new HashMap<>(); + ShardingSphereSchema schema = mock(ShardingSphereSchema.class); + schemas.put("schema1", schema); + ShardingSphereDatabase database = new ShardingSphereDatabase("foo_db", databaseType, mock(ResourceMetaData.class), ruleMetaData, schemas); + assertThat(database.getSchema("schema1"), is(schema)); + assertNull(database.getSchema("non_existent_schema")); + } + + @Test + void assertAddSchema() { + DatabaseType databaseType = mock(DatabaseType.class); + RuleMetaData ruleMetaData = mock(RuleMetaData.class); + Map schemas = new HashMap<>(); + ShardingSphereSchema schema = mock(ShardingSphereSchema.class); + ShardingSphereDatabase database = new ShardingSphereDatabase("foo_db", databaseType, mock(ResourceMetaData.class), ruleMetaData, schemas); + assertFalse(database.containsSchema("new_schema")); + + database.addSchema("new_schema", schema); + assertTrue(database.containsSchema("new_schema")); + assertThat(database.getSchema("new_schema"), is(schema)); + } + + @Test + void assertDropSchema() { + DatabaseType databaseType = mock(DatabaseType.class); + RuleMetaData ruleMetaData = mock(RuleMetaData.class); + Map schemas = new HashMap<>(); + ShardingSphereSchema schema = mock(ShardingSphereSchema.class); + schemas.put("schema1", schema); + ShardingSphereDatabase database = new ShardingSphereDatabase("foo_db", databaseType, mock(ResourceMetaData.class), ruleMetaData, schemas); + assertTrue(database.containsSchema("schema1")); + database.dropSchema("schema1"); + assertFalse(database.containsSchema("schema1")); + } + @Test void assertIsComplete() { ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.singletonMap("ds", new MockedDataSource())); @@ -76,6 +132,20 @@ void assertIsNotCompleteWithoutDataSource() { assertFalse(new ShardingSphereDatabase("foo_db", mock(DatabaseType.class), resourceMetaData, ruleMetaData, Collections.emptyMap()).isComplete()); } + @Test + void assertNotContainsDataSource() { + ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.emptyMap()); + RuleMetaData ruleMetaData = new RuleMetaData(Collections.singleton(mock(ShardingSphereRule.class))); + assertFalse(new ShardingSphereDatabase("foo_db", mock(DatabaseType.class), resourceMetaData, ruleMetaData, Collections.emptyMap()).containsDataSource()); + } + + @Test + void assertContainsDataSource() { + ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.singletonMap("ds", new MockedDataSource())); + RuleMetaData ruleMetaData = new RuleMetaData(Collections.singleton(mock(ShardingSphereRule.class))); + assertTrue(new ShardingSphereDatabase("foo_db", mock(DatabaseType.class), resourceMetaData, ruleMetaData, Collections.emptyMap()).containsDataSource()); + } + @Test void assertReloadRules() { ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.singletonMap("ds", new MockedDataSource()));