This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 37dc335 Refactor DataSourceConfigurationValidator (#12783)
37dc335 is described below
commit 37dc335cfa76604d4e0adb2f1a14b50e8f0b99c2
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Sep 28 06:54:41 2021 +0800
Refactor DataSourceConfigurationValidator (#12783)
* Refactor DataSourceConfigurationValidator
* Refactor DataSourceConfigurationValidator
---
....java => DataSourceConfigurationValidator.java} | 19 +++++-----
.../config/datasource/DataSourceConverter.java | 6 +--
.../InvalidDataSourceConfigurationException.java} | 14 +++----
...ception.java => InvalidResourcesException.java} | 8 ++--
...a => DataSourceConfigurationValidatorTest.java} | 14 +++----
.../rdl/resource/AddResourceBackendHandler.java | 43 +++++++++++-----------
.../rdl/resource/AlterResourceBackendHandler.java | 37 +++++++++----------
.../resource/AddResourceBackendHandlerTest.java | 17 ++++-----
.../resource/AlterResourceBackendHandlerTest.java | 11 ++----
9 files changed, 78 insertions(+), 91 deletions(-)
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceValidator.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceConfigurationValidator.java
similarity index 70%
rename from
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceValidator.java
rename to
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceConfigurationValidator.java
index c4f64e7..fd9dc49 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceValidator.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceConfigurationValidator.java
@@ -20,26 +20,25 @@ package org.apache.shardingsphere.infra.config.datasource;
import javax.sql.DataSource;
/**
- * Data source validator.
+ * Data source configuration validator.
*/
-public final class DataSourceValidator {
+public final class DataSourceConfigurationValidator {
/**
- * Validate.
+ * Validate data source configuration.
*
- * @param dataSourceConfiguration data source configuration
- * @return is valid or not
- * @throws Exception exception
+ * @param dataSourceConfigName data source configuration name to be valid
+ * @param dataSourceConfig data source configuration to be valid
+ * @throws InvalidDataSourceConfigurationException invalid data source
configuration exception
*/
- public boolean validate(final DataSourceConfiguration
dataSourceConfiguration) throws Exception {
+ public void validate(final String dataSourceConfigName, final
DataSourceConfiguration dataSourceConfig) throws
InvalidDataSourceConfigurationException {
DataSource dataSource = null;
try {
- dataSource =
DataSourceConverter.getDataSource(dataSourceConfiguration);
- return true;
+ dataSource = DataSourceConverter.getDataSource(dataSourceConfig);
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
- throw ex;
+ throw new
InvalidDataSourceConfigurationException(dataSourceConfigName, ex.getMessage());
} finally {
if (null != dataSource) {
close(dataSource);
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceConverter.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceConverter.java
index 12d116f..d0c0a81 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceConverter.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceConverter.java
@@ -45,8 +45,7 @@ public final class DataSourceConverter {
* @return data source
*/
public static DataSource getDataSource(final DataSourceConfiguration
dataSourceConfiguration) {
- return
JDBCParameterDecoratorHelper.decorate(DataSourceCreatorFactory.getDataSourceCreator(dataSourceConfiguration.getDataSourceClassName())
- .createDataSource(dataSourceConfiguration));
+ return
JDBCParameterDecoratorHelper.decorate(DataSourceCreatorFactory.getDataSourceCreator(dataSourceConfiguration.getDataSourceClassName()).createDataSource(dataSourceConfiguration));
}
/**
@@ -66,8 +65,7 @@ public final class DataSourceConverter {
* @return data source map
*/
public static Map<String, DataSource> getDataSourceMap(final Map<String,
DataSourceConfiguration> dataSourceConfigMap) {
- return
dataSourceConfigMap.entrySet().stream().collect(Collectors.toMap(Entry::getKey,
- entry -> getDataSource(entry.getValue()), (oldValue, currentValue)
-> oldValue, LinkedHashMap::new));
+ return
dataSourceConfigMap.entrySet().stream().collect(Collectors.toMap(Entry::getKey,
entry -> getDataSource(entry.getValue()), (oldValue, currentValue) -> oldValue,
LinkedHashMap::new));
}
/**
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/exception/resource/InvalidResourceException.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/InvalidDataSourceConfigurationException.java
similarity index 62%
copy from
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/exception/resource/InvalidResourceException.java
copy to
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/InvalidDataSourceConfigurationException.java
index 600dc14..8d9a382 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/exception/resource/InvalidResourceException.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/InvalidDataSourceConfigurationException.java
@@ -15,18 +15,16 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.distsql.exception.resource;
-
-import java.util.Collection;
+package org.apache.shardingsphere.infra.config.datasource;
/**
- * Invalid resource exception.
+ * Invalid data source configuration exception.
*/
-public final class InvalidResourceException extends
ResourceDefinitionViolationException {
+public final class InvalidDataSourceConfigurationException extends Exception {
- private static final long serialVersionUID = 7029641448948791509L;
+ private static final long serialVersionUID = -7221138369057943935L;
- public InvalidResourceException(final Collection<String> resourceNames) {
- super(1104, String.format("Can not add invalid resources %s.",
resourceNames));
+ public InvalidDataSourceConfigurationException(final String
dataSourceConfigName, final String errorMessage) {
+ super(String.format("Invalid data source configuration name `%s`,
error message is: %s", dataSourceConfigName, errorMessage));
}
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/exception/resource/InvalidResourceException.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/exception/resource/InvalidResourcesException.java
similarity index 75%
rename from
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/exception/resource/InvalidResourceException.java
rename to
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/exception/resource/InvalidResourcesException.java
index 600dc14..1511dc4 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/exception/resource/InvalidResourceException.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/exception/resource/InvalidResourcesException.java
@@ -20,13 +20,13 @@ package
org.apache.shardingsphere.infra.distsql.exception.resource;
import java.util.Collection;
/**
- * Invalid resource exception.
+ * Invalid resources exception.
*/
-public final class InvalidResourceException extends
ResourceDefinitionViolationException {
+public final class InvalidResourcesException extends
ResourceDefinitionViolationException {
private static final long serialVersionUID = 7029641448948791509L;
- public InvalidResourceException(final Collection<String> resourceNames) {
- super(1104, String.format("Can not add invalid resources %s.",
resourceNames));
+ public InvalidResourcesException(final Collection<String> errorMessages) {
+ super(1104, String.format("Can not process invalid resources, error
messages are: %s.", errorMessages));
}
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/DataSourceValidatorTest.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/DataSourceConfigurationValidatorTest.java
similarity index 78%
rename from
shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/DataSourceValidatorTest.java
rename to
shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/DataSourceConfigurationValidatorTest.java
index 59ee291..dcfc468 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/DataSourceValidatorTest.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/DataSourceConfigurationValidatorTest.java
@@ -19,21 +19,19 @@ package org.apache.shardingsphere.infra.datasource;
import com.zaxxer.hikari.HikariDataSource;
import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
-import org.apache.shardingsphere.infra.config.datasource.DataSourceValidator;
+import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfigurationValidator;
+import
org.apache.shardingsphere.infra.config.datasource.InvalidDataSourceConfigurationException;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-
-public final class DataSourceValidatorTest {
+public final class DataSourceConfigurationValidatorTest {
@Test
- public void assertValidate() throws Exception {
- DataSourceValidator dataSourceValidator = new DataSourceValidator();
-
assertThat(dataSourceValidator.validate(createDataSourceConfiguration()),
is(Boolean.TRUE));
+ public void assertValidate() throws
InvalidDataSourceConfigurationException {
+ DataSourceConfigurationValidator dataSourceConfigurationValidator =
new DataSourceConfigurationValidator();
+ dataSourceConfigurationValidator.validate("name",
createDataSourceConfiguration());
}
private DataSourceConfiguration createDataSourceConfiguration() {
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AddResourceBackendHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AddResourceBackendHandler.java
index c6b8854..db9e1fb 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AddResourceBackendHandler.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AddResourceBackendHandler.java
@@ -20,11 +20,12 @@ package
org.apache.shardingsphere.proxy.backend.text.distsql.rdl.resource;
import org.apache.shardingsphere.distsql.parser.segment.DataSourceSegment;
import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.AddResourceStatement;
import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
-import org.apache.shardingsphere.infra.config.datasource.DataSourceValidator;
+import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfigurationValidator;
+import
org.apache.shardingsphere.infra.config.datasource.InvalidDataSourceConfigurationException;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
import
org.apache.shardingsphere.infra.distsql.exception.resource.DuplicateResourceException;
-import
org.apache.shardingsphere.infra.distsql.exception.resource.InvalidResourceException;
+import
org.apache.shardingsphere.infra.distsql.exception.resource.InvalidResourcesException;
import
org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
@@ -36,12 +37,11 @@ import
org.apache.shardingsphere.proxy.converter.ResourceSegmentsConverter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
+import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
-import java.util.Objects;
import java.util.Set;
-import java.util.stream.Collectors;
/**
* Add resource backend handler.
@@ -50,12 +50,12 @@ public final class AddResourceBackendHandler extends
SchemaRequiredBackendHandle
private final DatabaseType databaseType;
- private final DataSourceValidator dataSourceValidator;
+ private final DataSourceConfigurationValidator dataSourceConfigValidator;
public AddResourceBackendHandler(final DatabaseType databaseType, final
AddResourceStatement sqlStatement, final BackendConnection backendConnection) {
super(sqlStatement, backendConnection);
this.databaseType = databaseType;
- dataSourceValidator = new DataSourceValidator();
+ dataSourceConfigValidator = new DataSourceConfigurationValidator();
}
@Override
@@ -63,13 +63,25 @@ public final class AddResourceBackendHandler extends
SchemaRequiredBackendHandle
check(schemaName, sqlStatement);
Map<String, DataSourceConfiguration> dataSourceConfigs =
DataSourceParameterConverter.getDataSourceConfigurationMap(
DataSourceParameterConverter.getDataSourceParameterMapFromYamlConfiguration(ResourceSegmentsConverter.convert(databaseType,
sqlStatement.getDataSources())));
- Collection<String> invalidResources =
dataSourceConfigs.entrySet().stream().map(entry ->
validateDataSource(entry)).filter(Objects::nonNull).collect(Collectors.toList());
- DistSQLException.predictionThrow(invalidResources.isEmpty(), new
InvalidResourceException(invalidResources));
+ validateDataSourceConfigurations(dataSourceConfigs);
// TODO update meta data context in memory
- ProxyContext.getInstance().getContextManager()
-
.getMetaDataContexts().getMetaDataPersistService().ifPresent(optional ->
optional.getDataSourceService().append(schemaName, dataSourceConfigs));
+
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaDataPersistService().ifPresent(optional
-> optional.getDataSourceService().append(schemaName, dataSourceConfigs));
return new UpdateResponseHeader(sqlStatement);
}
+
+ private void validateDataSourceConfigurations(final Map<String,
DataSourceConfiguration> dataSourceConfigs) throws InvalidResourcesException {
+ Collection<String> errorMessages = new LinkedList<>();
+ for (Entry<String, DataSourceConfiguration> entry :
dataSourceConfigs.entrySet()) {
+ try {
+ dataSourceConfigValidator.validate(entry.getKey(),
entry.getValue());
+ } catch (final InvalidDataSourceConfigurationException ex) {
+ errorMessages.add(ex.getMessage());
+ }
+ }
+ if (!errorMessages.isEmpty()) {
+ throw new InvalidResourcesException(errorMessages);
+ }
+ }
private void check(final String schemaName, final AddResourceStatement
sqlStatement) throws DuplicateResourceException {
List<String> dataSourceNames = new
ArrayList<>(sqlStatement.getDataSources().size());
@@ -84,15 +96,4 @@ public final class AddResourceBackendHandler extends
SchemaRequiredBackendHandle
throw new DuplicateResourceException(duplicateDataSourceNames);
}
}
-
- private String validateDataSource(final Entry<String,
DataSourceConfiguration> dataSource) {
- try {
- dataSourceValidator.validate(dataSource.getValue());
- // CHECKSTYLE:OFF
- } catch (final Exception ex) {
- // CHECKSTYLE:ON
- return String.format("`%s` %s", dataSource.getKey(),
ex.getMessage());
- }
- return null;
- }
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandler.java
index 5d340f0..0d69a88 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandler.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandler.java
@@ -20,11 +20,12 @@ package
org.apache.shardingsphere.proxy.backend.text.distsql.rdl.resource;
import org.apache.shardingsphere.distsql.parser.segment.DataSourceSegment;
import
org.apache.shardingsphere.distsql.parser.statement.rdl.alter.AlterResourceStatement;
import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
-import org.apache.shardingsphere.infra.config.datasource.DataSourceValidator;
+import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfigurationValidator;
+import
org.apache.shardingsphere.infra.config.datasource.InvalidDataSourceConfigurationException;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
import
org.apache.shardingsphere.infra.distsql.exception.resource.DuplicateResourceException;
-import
org.apache.shardingsphere.infra.distsql.exception.resource.InvalidResourceException;
+import
org.apache.shardingsphere.infra.distsql.exception.resource.InvalidResourcesException;
import
org.apache.shardingsphere.infra.distsql.exception.resource.RequiredResourceMissedException;
import
org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
@@ -36,9 +37,9 @@ import
org.apache.shardingsphere.proxy.converter.ResourceSegmentsConverter;
import javax.sql.DataSource;
import java.util.Collection;
+import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
-import java.util.Objects;
import java.util.stream.Collectors;
/**
@@ -48,12 +49,12 @@ public final class AlterResourceBackendHandler extends
SchemaRequiredBackendHand
private final DatabaseType databaseType;
- private final DataSourceValidator dataSourceValidator;
+ private final DataSourceConfigurationValidator dataSourceConfigValidator;
public AlterResourceBackendHandler(final DatabaseType databaseType, final
AlterResourceStatement sqlStatement, final BackendConnection backendConnection)
{
super(sqlStatement, backendConnection);
this.databaseType = databaseType;
- dataSourceValidator = new DataSourceValidator();
+ dataSourceConfigValidator = new DataSourceConfigurationValidator();
}
@Override
@@ -61,7 +62,7 @@ public final class AlterResourceBackendHandler extends
SchemaRequiredBackendHand
check(schemaName, sqlStatement);
Map<String, DataSourceConfiguration> dataSourceConfigs =
DataSourceParameterConverter.getDataSourceConfigurationMap(
DataSourceParameterConverter.getDataSourceParameterMapFromYamlConfiguration(ResourceSegmentsConverter.convert(databaseType,
sqlStatement.getDataSources())));
- validate(dataSourceConfigs);
+ validateDataSourceConfigurations(dataSourceConfigs);
// TODO update meta data context in memory
ProxyContext.getInstance().getContextManager()
.getMetaDataContexts().getMetaDataPersistService().ifPresent(optional ->
optional.getDataSourceService().append(schemaName, dataSourceConfigs));
@@ -74,20 +75,18 @@ public final class AlterResourceBackendHandler extends
SchemaRequiredBackendHand
checkResourceNameExisted(schemaName, toBeAlteredResourceNames);
}
- private void validate(final Map<String, DataSourceConfiguration>
dataSourceConfigs) throws DistSQLException {
- Collection<String> invalidResources =
dataSourceConfigs.entrySet().stream().map(entry ->
validateDataSource(entry)).filter(Objects::nonNull).collect(Collectors.toList());
- DistSQLException.predictionThrow(invalidResources.isEmpty(), new
InvalidResourceException(invalidResources));
- }
-
- private String validateDataSource(final Entry<String,
DataSourceConfiguration> dataSource) {
- try {
- dataSourceValidator.validate(dataSource.getValue());
- // CHECKSTYLE:OFF
- } catch (final Exception ex) {
- // CHECKSTYLE:ON
- return String.format("`%s` %s", dataSource.getKey(),
ex.getMessage());
+ private void validateDataSourceConfigurations(final Map<String,
DataSourceConfiguration> dataSourceConfigs) throws DistSQLException {
+ Collection<String> errorMessages = new LinkedList<>();
+ for (Entry<String, DataSourceConfiguration> entry :
dataSourceConfigs.entrySet()) {
+ try {
+ dataSourceConfigValidator.validate(entry.getKey(),
entry.getValue());
+ } catch (final InvalidDataSourceConfigurationException ex) {
+ errorMessages.add(ex.getMessage());
+ }
+ }
+ if (!errorMessages.isEmpty()) {
+ throw new InvalidResourcesException(errorMessages);
}
- return null;
}
private Collection<String> getToBeAlteredResourceNames(final
AlterResourceStatement sqlStatement) {
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AddResourceBackendHandlerTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AddResourceBackendHandlerTest.java
index c24f0f2..e226f56 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AddResourceBackendHandlerTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AddResourceBackendHandlerTest.java
@@ -19,15 +19,14 @@ package
org.apache.shardingsphere.proxy.backend.text.distsql.rdl.resource;
import org.apache.shardingsphere.distsql.parser.segment.DataSourceSegment;
import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.AddResourceStatement;
-import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
-import org.apache.shardingsphere.infra.config.datasource.DataSourceValidator;
-import
org.apache.shardingsphere.infra.distsql.exception.resource.DuplicateResourceException;
-import org.apache.shardingsphere.mode.manager.ContextManager;
-import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
+import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfigurationValidator;
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
+import
org.apache.shardingsphere.infra.distsql.exception.resource.DuplicateResourceException;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import
org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
+import org.apache.shardingsphere.mode.manager.ContextManager;
+import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import
org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
@@ -46,7 +45,6 @@ import java.util.List;
import java.util.Properties;
import static org.junit.Assert.assertTrue;
-import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -55,7 +53,7 @@ import static org.mockito.Mockito.when;
public final class AddResourceBackendHandlerTest {
@Mock
- private DataSourceValidator dataSourceValidator;
+ private DataSourceConfigurationValidator dataSourceConfigurationValidator;
@Mock
private AddResourceStatement addResourceStatement;
@@ -77,9 +75,9 @@ public final class AddResourceBackendHandlerTest {
@Before
public void setUp() throws Exception {
addResourceBackendHandler = new AddResourceBackendHandler(new
MySQLDatabaseType(), addResourceStatement, backendConnection);
- Field field =
addResourceBackendHandler.getClass().getDeclaredField("dataSourceValidator");
+ Field field =
addResourceBackendHandler.getClass().getDeclaredField("dataSourceConfigValidator");
field.setAccessible(true);
- field.set(addResourceBackendHandler, dataSourceValidator);
+ field.set(addResourceBackendHandler, dataSourceConfigurationValidator);
}
@Test
@@ -91,7 +89,6 @@ public final class AddResourceBackendHandlerTest {
when(metaDataContexts.getMetaData("test_schema")).thenReturn(metaData);
when(metaData.getResource()).thenReturn(resource);
when(resource.getDataSources()).thenReturn(Collections.emptyMap());
-
when(dataSourceValidator.validate(any(DataSourceConfiguration.class))).thenReturn(true);
ResponseHeader responseHeader =
addResourceBackendHandler.execute("test_schema", createAddResourceStatement());
assertTrue(responseHeader instanceof UpdateResponseHeader);
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandlerTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandlerTest.java
index b6fc597..5e89259 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandlerTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandlerTest.java
@@ -19,8 +19,7 @@ package
org.apache.shardingsphere.proxy.backend.text.distsql.rdl.resource;
import org.apache.shardingsphere.distsql.parser.segment.DataSourceSegment;
import
org.apache.shardingsphere.distsql.parser.statement.rdl.alter.AlterResourceStatement;
-import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
-import org.apache.shardingsphere.infra.config.datasource.DataSourceValidator;
+import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfigurationValidator;
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
import
org.apache.shardingsphere.infra.distsql.exception.resource.DuplicateResourceException;
@@ -48,7 +47,6 @@ import java.util.List;
import java.util.Properties;
import static org.junit.Assert.assertTrue;
-import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -57,7 +55,7 @@ import static org.mockito.Mockito.when;
public final class AlterResourceBackendHandlerTest {
@Mock
- private DataSourceValidator dataSourceValidator;
+ private DataSourceConfigurationValidator dataSourceConfigurationValidator;
@Mock
private AlterResourceStatement alterResourceStatement;
@@ -82,9 +80,9 @@ public final class AlterResourceBackendHandlerTest {
@Before
public void setUp() throws Exception {
alterResourceBackendHandler = new AlterResourceBackendHandler(new
MySQLDatabaseType(), alterResourceStatement, backendConnection);
- Field field =
alterResourceBackendHandler.getClass().getDeclaredField("dataSourceValidator");
+ Field field =
alterResourceBackendHandler.getClass().getDeclaredField("dataSourceConfigValidator");
field.setAccessible(true);
- field.set(alterResourceBackendHandler, dataSourceValidator);
+ field.set(alterResourceBackendHandler,
dataSourceConfigurationValidator);
}
@Test
@@ -96,7 +94,6 @@ public final class AlterResourceBackendHandlerTest {
when(metaDataContexts.getMetaData("test_schema")).thenReturn(metaData);
when(metaData.getResource()).thenReturn(resource);
when(resource.getDataSources()).thenReturn(Collections.singletonMap("ds_0",
dataSource));
-
when(dataSourceValidator.validate(any(DataSourceConfiguration.class))).thenReturn(true);
ResponseHeader responseHeader =
alterResourceBackendHandler.execute("test_schema",
createAlterResourceStatement("ds_0"));
assertTrue(responseHeader instanceof UpdateResponseHeader);
}