diff --git a/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleStatementUpdater.java b/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleStatementUpdater.java index 30eee07bc4cef..820d305d1c833 100644 --- a/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleStatementUpdater.java +++ b/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleStatementUpdater.java @@ -20,9 +20,10 @@ import org.apache.shardingsphere.broadcast.api.config.BroadcastRuleConfiguration; import org.apache.shardingsphere.broadcast.distsql.statement.CreateBroadcastTableRuleStatement; import org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException; +import org.apache.shardingsphere.distsql.handler.exception.storageunit.EmptyStorageUnitException; import org.apache.shardingsphere.distsql.handler.update.RuleDefinitionCreateUpdater; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import java.util.Collection; import java.util.LinkedHashSet; @@ -34,6 +35,7 @@ public final class CreateBroadcastTableRuleStatementUpdater implements RuleDefin @Override public void checkSQLStatement(final ShardingSphereDatabase database, final CreateBroadcastTableRuleStatement sqlStatement, final BroadcastRuleConfiguration currentRuleConfig) { + ShardingSpherePreconditions.checkState(!database.getResourceMetaData().getStorageUnits().isEmpty(), () -> new EmptyStorageUnitException(database.getName())); if (!sqlStatement.isIfNotExists()) { checkDuplicate(sqlStatement, currentRuleConfig); } diff --git a/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleStatementUpdaterTest.java b/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleStatementUpdaterTest.java index 305fb34fdcf71..b0407516cd8ba 100644 --- a/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleStatementUpdaterTest.java +++ b/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleStatementUpdaterTest.java @@ -20,7 +20,9 @@ import org.apache.shardingsphere.broadcast.api.config.BroadcastRuleConfiguration; import org.apache.shardingsphere.broadcast.distsql.statement.CreateBroadcastTableRuleStatement; import org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException; +import org.apache.shardingsphere.distsql.handler.exception.storageunit.EmptyStorageUnitException; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; import org.junit.jupiter.api.Test; import java.util.Collections; @@ -29,6 +31,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -36,23 +39,39 @@ class CreateBroadcastTableRuleStatementUpdaterTest { private final CreateBroadcastTableRuleStatementUpdater updater = new CreateBroadcastTableRuleStatementUpdater(); + @Test + void assertCheckSQLStatementWithEmptyStorageUnit() { + BroadcastRuleConfiguration currentConfig = mock(BroadcastRuleConfiguration.class); + when(currentConfig.getTables()).thenReturn(Collections.singleton("t_address")); + CreateBroadcastTableRuleStatement statement = new CreateBroadcastTableRuleStatement(false, Collections.singleton("t_address")); + ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getResourceMetaData().getStorageUnits()).thenReturn(Collections.emptyMap()); + assertThrows(EmptyStorageUnitException.class, () -> updater.checkSQLStatement(database, statement, currentConfig)); + } + @Test void assertCheckSQLStatementWithDuplicateBroadcastRule() { BroadcastRuleConfiguration currentConfig = mock(BroadcastRuleConfiguration.class); when(currentConfig.getTables()).thenReturn(Collections.singleton("t_address")); CreateBroadcastTableRuleStatement statement = new CreateBroadcastTableRuleStatement(false, Collections.singleton("t_address")); - assertThrows(DuplicateRuleException.class, () -> updater.checkSQLStatement(mock(ShardingSphereDatabase.class), statement, currentConfig)); + assertThrows(DuplicateRuleException.class, () -> updater.checkSQLStatement(mockShardingSphereDatabase(), statement, currentConfig)); } @Test void assertBuildToBeCreatedRuleConfiguration() { BroadcastRuleConfiguration currentConfig = new BroadcastRuleConfiguration(new LinkedList<>()); CreateBroadcastTableRuleStatement statement = new CreateBroadcastTableRuleStatement(false, Collections.singleton("t_address")); - updater.checkSQLStatement(mock(ShardingSphereDatabase.class), statement, currentConfig); + updater.checkSQLStatement(mockShardingSphereDatabase(), statement, currentConfig); BroadcastRuleConfiguration toBeCreatedRuleConfig = updater.buildToBeCreatedRuleConfiguration(currentConfig, statement); updater.updateCurrentRuleConfiguration(currentConfig, toBeCreatedRuleConfig); assertThat(currentConfig.getTables().size(), is(1)); assertThat(currentConfig.getTables().iterator().next(), is("t_address")); updater.updateCurrentRuleConfiguration(currentConfig, toBeCreatedRuleConfig); } + + private ShardingSphereDatabase mockShardingSphereDatabase() { + ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getResourceMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("ds_0", mock(StorageUnit.class))); + return database; + } }