This is an automated email from the ASF dual-hosted git repository. kimmking 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 d4f7330 Optimize scaling unit tests. (#7814) d4f7330 is described below commit d4f7330c3abaff4be3b447b1a55013f8b7acbbbd Author: 邱鹿 Lucas <lucas...@163.com> AuthorDate: Fri Oct 16 20:47:41 2020 +0800 Optimize scaling unit tests. (#7814) * Optimize scaling unit tests. * Optimize scaling unit tests. Co-authored-by: qiulu3 <Lucas209910> --- .../scaling/web/HttpServerHandler.java | 2 +- .../scaling/utils/ReflectionUtil.java | 85 ++++++++++++++ .../scaling/utils/ScalingConfigurationUtil.java | 50 +++++++++ .../scaling/web/HttpServerHandlerTest.java | 62 +++-------- .../job/preparer/ShardingScalingJobPreparer.java | 2 +- .../scaling/core/ScalingJobControllerTest.java | 123 ++++++++------------- .../check/AbstractDataConsistencyCheckerTest.java | 31 ++---- .../core/datasource/DataSourceManagerTest.java | 31 ++---- ...IncrementalPositionResumeBreakPointManager.java | 2 +- ...UtilTest.java => ScalingConfigurationUtil.java} | 49 ++++---- 10 files changed, 239 insertions(+), 198 deletions(-) diff --git a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/main/java/org/apache/shardingsphere/scaling/web/HttpServerHandler.java b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/main/java/org/apache/shardingsphere/scaling/web/HttpServerHandler.java index a21b5df..8c5b676 100644 --- a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/main/java/org/apache/shardingsphere/scaling/web/HttpServerHandler.java +++ b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/main/java/org/apache/shardingsphere/scaling/web/HttpServerHandler.java @@ -142,7 +142,7 @@ public final class HttpServerHandler extends SimpleChannelInboundHandler<FullHtt @Override public void exceptionCaught(final ChannelHandlerContext context, final Throwable cause) { - log.warn("Http request handle occur error:", cause); + log.error("Http request handle occur error:", cause); response(GSON.toJson(ResponseContentUtil.handleException(cause.toString())), context, HttpResponseStatus.INTERNAL_SERVER_ERROR); context.close(); } diff --git a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/utils/ReflectionUtil.java b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/utils/ReflectionUtil.java index 1bb1e69..fb037e2 100644 --- a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/utils/ReflectionUtil.java +++ b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/utils/ReflectionUtil.java @@ -21,6 +21,8 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; @NoArgsConstructor(access = AccessLevel.PRIVATE) public final class ReflectionUtil { @@ -68,4 +70,87 @@ public final class ReflectionUtil { } throw new ClassCastException("field " + fieldName + " is " + target.getClass().getName() + " can cast to " + valueClass.getName()); } + + /** + * Get field value from instance target object. + * + * @param targetClass target class + * @param fieldName field name + * @param <T> expected value class + * @return target filed value + * @throws NoSuchFieldException no such field exception + * @throws IllegalAccessException illegal access exception + */ + @SuppressWarnings("unchecked") + public static <T> T getStaticFieldValueFromClass(final Class<?> targetClass, final String fieldName) throws NoSuchFieldException, IllegalAccessException { + Field field = getFieldFromClass(targetClass, fieldName, true); + Object value = field.get(null); + if (null == value) { + return null; + } + if (value.getClass().isAssignableFrom(value.getClass())) { + return (T) value; + } + return null; + } + + /** + * Set field value into target object. + * + * @param target target object + * @param fieldName field name + * @param value target filed value + * @throws NoSuchFieldException no such field exception + * @throws IllegalAccessException illegal access exception + */ + public static void setFieldValue(final Object target, final String fieldName, final Object value) throws NoSuchFieldException, IllegalAccessException { + setFieldValue(target.getClass(), target, fieldName, value); + } + + /** + * Set field value into target object. + * + * @param targetClass target class + * @param targetObject target object + * @param fieldName field name + * @param value target filed value + * @throws NoSuchFieldException no such field exception + * @throws IllegalAccessException illegal access exception + */ + public static void setFieldValue(final Class<?> targetClass, final Object targetObject, final String fieldName, final Object value) throws NoSuchFieldException, IllegalAccessException { + Field field = getFieldFromClass(targetClass, fieldName, true); + field.setAccessible(true); + field.set(targetObject, value); + } + + /** + * Invoke method. + * + * @param target target object + * @param methodName method name + * @throws NoSuchMethodException no such field exception + * @throws InvocationTargetException invocation target exception + * @throws IllegalAccessException illegal access exception + */ + public static void invokeMethod(final Object target, final String methodName) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + invokeMethod(target, methodName, new Class[0], new Object[0]); + } + + /** + * Invoke method. + * + * @param target target object + * @param methodName method name + * @param parameterTypes parameter types + * @param parameterValues parameter values + * @throws NoSuchMethodException no such field exception + * @throws InvocationTargetException invocation target exception + * @throws IllegalAccessException illegal access exception + */ + public static void invokeMethod(final Object target, final String methodName, final Class<?>[] parameterTypes, final Object[] parameterValues) + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + Method method = target.getClass().getDeclaredMethod(methodName, parameterTypes); + method.setAccessible(true); + method.invoke(target, parameterValues); + } } diff --git a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/utils/ScalingConfigurationUtil.java b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/utils/ScalingConfigurationUtil.java new file mode 100644 index 0000000..6a48acc --- /dev/null +++ b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/utils/ScalingConfigurationUtil.java @@ -0,0 +1,50 @@ +/* + * 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.scaling.utils; + +import com.google.gson.Gson; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import org.apache.shardingsphere.scaling.core.config.ScalingConfiguration; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +/** + * Scaling configuration util. + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class ScalingConfigurationUtil { + + private static final Gson GSON = new Gson(); + + /** + * Init job config. + * + * @param configFile config file + * @return ScalingConfiguration + * @throws IOException IO exception + */ + public static ScalingConfiguration initConfig(final String configFile) throws IOException { + try (InputStream fileInputStream = ScalingConfigurationUtil.class.getResourceAsStream(configFile); + InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream)) { + return GSON.fromJson(inputStreamReader, ScalingConfiguration.class); + } + } +} diff --git a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/web/HttpServerHandlerTest.java b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/web/HttpServerHandlerTest.java index 1f44933..69edd67 100644 --- a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/web/HttpServerHandlerTest.java +++ b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/web/HttpServerHandlerTest.java @@ -28,13 +28,11 @@ import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpVersion; import io.netty.util.CharsetUtil; import lombok.SneakyThrows; -import org.apache.shardingsphere.scaling.core.config.ScalingConfiguration; import org.apache.shardingsphere.scaling.core.config.ScalingContext; -import org.apache.shardingsphere.scaling.core.config.ScalingDataSourceConfiguration; import org.apache.shardingsphere.scaling.core.config.ServerConfiguration; -import org.apache.shardingsphere.scaling.core.config.SyncConfiguration; -import org.apache.shardingsphere.scaling.core.datasource.DataSourceManager; -import org.apache.shardingsphere.scaling.core.utils.SyncConfigurationUtil; +import org.apache.shardingsphere.scaling.core.execute.engine.ShardingScalingExecuteEngine; +import org.apache.shardingsphere.scaling.utils.ReflectionUtil; +import org.apache.shardingsphere.scaling.utils.ScalingConfigurationUtil; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -42,12 +40,7 @@ import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; -import javax.sql.DataSource; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.sql.Connection; -import java.sql.SQLException; -import java.sql.Statement; +import java.io.IOException; import java.util.HashMap; import java.util.Map; @@ -61,29 +54,23 @@ public final class HttpServerHandlerTest { private static final Gson GSON = new Gson(); + private final HttpServerHandler httpServerHandler = new HttpServerHandler(); + @Mock private ChannelHandlerContext channelHandlerContext; private FullHttpRequest fullHttpRequest; - private HttpServerHandler httpServerHandler; - - private ScalingConfiguration scalingConfig; - - private SyncConfiguration syncConfiguration; - @Before + @SneakyThrows(ReflectiveOperationException.class) public void setUp() { - initConfig("/config.json"); ScalingContext.getInstance().init(new ServerConfiguration()); - httpServerHandler = new HttpServerHandler(); - initTableData(syncConfiguration.getDumperConfiguration().getDataSourceConfiguration()); - initTableData(syncConfiguration.getImporterConfiguration().getDataSourceConfiguration()); + ReflectionUtil.setFieldValue(ScalingContext.getInstance(), "taskExecuteEngine", mock(ShardingScalingExecuteEngine.class)); } @Test public void assertChannelReadStartSuccess() { - startScalingJob(); + startScalingJob("/config.json"); ArgumentCaptor<FullHttpResponse> argumentCaptor = ArgumentCaptor.forClass(FullHttpResponse.class); verify(channelHandlerContext).writeAndFlush(argumentCaptor.capture()); FullHttpResponse fullHttpResponse = argumentCaptor.getValue(); @@ -92,8 +79,7 @@ public final class HttpServerHandlerTest { @Test public void assertShardingSphereJDBCTargetChannelReadStartSuccess() { - initConfig("/config_sharding_sphere_jdbc_target.json"); - startScalingJob(); + startScalingJob("/config_sharding_sphere_jdbc_target.json"); ArgumentCaptor<FullHttpResponse> argumentCaptor = ArgumentCaptor.forClass(FullHttpResponse.class); verify(channelHandlerContext).writeAndFlush(argumentCaptor.capture()); FullHttpResponse fullHttpResponse = argumentCaptor.getValue(); @@ -112,7 +98,7 @@ public final class HttpServerHandlerTest { @Test public void assertChannelReadProgressSuccess() { - startScalingJob(); + startScalingJob("/config.json"); fullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/scaling/job/progress/1"); httpServerHandler.channelRead0(channelHandlerContext, fullHttpRequest); ArgumentCaptor<FullHttpResponse> argumentCaptor = ArgumentCaptor.forClass(FullHttpResponse.class); @@ -170,29 +156,11 @@ public final class HttpServerHandlerTest { httpServerHandler.exceptionCaught(channelHandlerContext, throwable); verify(channelHandlerContext).close(); } - - private void startScalingJob() { - ByteBuf byteBuf = Unpooled.copiedBuffer(GSON.toJson(scalingConfig), CharsetUtil.UTF_8); + + @SneakyThrows(IOException.class) + private void startScalingJob(final String configFile) { + ByteBuf byteBuf = Unpooled.copiedBuffer(GSON.toJson(ScalingConfigurationUtil.initConfig(configFile)), CharsetUtil.UTF_8); fullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/scaling/job/start", byteBuf); httpServerHandler.channelRead0(channelHandlerContext, fullHttpRequest); } - - private void initConfig(final String configFile) { - InputStream fileInputStream = HttpServerHandlerTest.class.getResourceAsStream(configFile); - InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream); - scalingConfig = GSON.fromJson(inputStreamReader, ScalingConfiguration.class); - syncConfiguration = SyncConfigurationUtil.toSyncConfigurations(scalingConfig).iterator().next(); - } - - @SneakyThrows(SQLException.class) - private void initTableData(final ScalingDataSourceConfiguration dataSourceConfig) { - DataSource dataSource = new DataSourceManager().getDataSource(dataSourceConfig); - try (Connection connection = dataSource.getConnection(); - Statement statement = connection.createStatement()) { - statement.execute("DROP TABLE IF EXISTS t1"); - statement.execute("CREATE TABLE t1 (id INT PRIMARY KEY, user_id VARCHAR(12))"); - statement.execute("DROP TABLE IF EXISTS t2"); - statement.execute("CREATE TABLE t2 (id INT PRIMARY KEY, user_id VARCHAR(12))"); - } - } } diff --git a/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/job/preparer/ShardingScalingJobPreparer.java b/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/job/preparer/ShardingScalingJobPreparer.java index 8b6db59..e387ee6 100644 --- a/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/job/preparer/ShardingScalingJobPreparer.java +++ b/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/job/preparer/ShardingScalingJobPreparer.java @@ -77,7 +77,7 @@ public final class ShardingScalingJobPreparer { } shardingScalingJob.setDataConsistencyChecker(initDataConsistencyChecker(databaseType, shardingScalingJob)); } catch (final PrepareFailedException ex) { - log.warn("Preparing sharding scaling job {} : {} failed", shardingScalingJob.getJobId(), shardingScalingJob.getJobName(), ex); + log.error("Preparing sharding scaling job {} : {} failed", shardingScalingJob.getJobId(), shardingScalingJob.getJobName(), ex); shardingScalingJob.setStatus(SyncTaskControlStatus.PREPARING_FAILURE.name()); } } diff --git a/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/ScalingJobControllerTest.java b/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/ScalingJobControllerTest.java index 8b78517..5c829f9 100644 --- a/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/ScalingJobControllerTest.java +++ b/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/ScalingJobControllerTest.java @@ -17,16 +17,14 @@ package org.apache.shardingsphere.scaling.core; -import org.apache.shardingsphere.scaling.core.config.ScalingDataSourceConfiguration; -import org.apache.shardingsphere.scaling.core.config.DumperConfiguration; -import org.apache.shardingsphere.scaling.core.config.ImporterConfiguration; -import org.apache.shardingsphere.scaling.core.config.JDBCScalingDataSourceConfiguration; -import org.apache.shardingsphere.scaling.core.config.JobConfiguration; -import org.apache.shardingsphere.scaling.core.config.ScalingConfiguration; +import com.google.common.collect.Maps; +import lombok.SneakyThrows; +import org.apache.shardingsphere.scaling.core.check.DataConsistencyCheckResult; +import org.apache.shardingsphere.scaling.core.check.DataConsistencyChecker; import org.apache.shardingsphere.scaling.core.config.ScalingContext; import org.apache.shardingsphere.scaling.core.config.ServerConfiguration; -import org.apache.shardingsphere.scaling.core.config.SyncConfiguration; import org.apache.shardingsphere.scaling.core.exception.ScalingJobNotFoundException; +import org.apache.shardingsphere.scaling.core.execute.engine.ShardingScalingExecuteEngine; import org.apache.shardingsphere.scaling.core.job.ScalingJobProgress; import org.apache.shardingsphere.scaling.core.job.ShardingScalingJob; import org.apache.shardingsphere.scaling.core.job.SyncProgress; @@ -35,38 +33,32 @@ import org.apache.shardingsphere.scaling.core.job.position.resume.IncrementalPos import org.apache.shardingsphere.scaling.core.job.position.resume.ResumeBreakPointManagerFactory; import org.apache.shardingsphere.scaling.core.schedule.SyncTaskControlStatus; import org.apache.shardingsphere.scaling.core.util.ReflectionUtil; +import org.apache.shardingsphere.scaling.core.util.ScalingConfigurationUtil; import org.junit.Before; import org.junit.Test; -import java.util.HashMap; +import java.io.IOException; import java.util.Map; import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.not; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; public final class ScalingJobControllerTest { - private static final String DATA_SOURCE_URL = "jdbc:h2:mem:test_db;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL"; - - private static final String USERNAME = "root"; - - private static final String PASSWORD = "password"; - - private ScalingJobController scalingJobController; - - private ShardingScalingJob shardingScalingJob; + private final ScalingJobController scalingJobController = new ScalingJobController(); @Before + @SneakyThrows(ReflectiveOperationException.class) public void setUp() { - ScalingContext.getInstance().init(mockServerConfiguration()); - scalingJobController = new ScalingJobController(); - shardingScalingJob = mockShardingScalingJob(); + ScalingContext.getInstance().init(new ServerConfiguration()); + ReflectionUtil.setFieldValue(ScalingContext.getInstance(), "taskExecuteEngine", mock(ShardingScalingExecuteEngine.class)); } @Test - public void assertStartPreparedJob() { + public void assertStartJob() { + ShardingScalingJob shardingScalingJob = mockShardingScalingJob(); scalingJobController.start(shardingScalingJob); SyncProgress progress = scalingJobController.getProgresses(shardingScalingJob.getJobId()); assertTrue(progress instanceof ScalingJobProgress); @@ -77,22 +69,13 @@ public final class ScalingJobControllerTest { } @Test - public void assertStartPreparingFailureJob() { - ShardingScalingJob shardingScalingJob = mockPreparingFailureShardingScalingJob(); - scalingJobController.start(shardingScalingJob); - SyncProgress progress = scalingJobController.getProgresses(shardingScalingJob.getJobId()); - assertTrue(progress instanceof ScalingJobProgress); - assertThat(((ScalingJobProgress) progress).getIncrementalDataTasks().size(), is(0)); - assertThat(((ScalingJobProgress) progress).getInventoryDataTasks().size(), is(0)); - } - - @Test public void assertStopExistJob() { + ShardingScalingJob shardingScalingJob = mockShardingScalingJob(); scalingJobController.start(shardingScalingJob); scalingJobController.stop(shardingScalingJob.getJobId()); SyncProgress progress = scalingJobController.getProgresses(shardingScalingJob.getJobId()); assertTrue(progress instanceof ScalingJobProgress); - assertThat(((ScalingJobProgress) progress).getStatus(), not("RUNNING")); + assertThat(((ScalingJobProgress) progress).getStatus(), is(SyncTaskControlStatus.STOPPED.name())); } @Test(expected = ScalingJobNotFoundException.class) @@ -102,14 +85,14 @@ public final class ScalingJobControllerTest { } @Test - public void assertListShardingScalingJobs() { + public void assertListJobs() { assertThat(scalingJobController.listShardingScalingJobs().size(), is(0)); - scalingJobController.start(shardingScalingJob); + scalingJobController.start(mockShardingScalingJob()); assertThat(scalingJobController.listShardingScalingJobs().size(), is(1)); } @Test - public void assertOnlyIncrementalDataTasks() throws NoSuchFieldException, IllegalAccessException { + public void assertIncrementalDataTasksOnly() throws NoSuchFieldException, IllegalAccessException { ReflectionUtil.setFieldValue(ResumeBreakPointManagerFactory.class, null, "clazz", IncrementalPositionResumeBreakPointManager.class); ShardingScalingJob shardingScalingJob = mockShardingScalingJob(); scalingJobController.start(shardingScalingJob); @@ -119,48 +102,38 @@ public final class ScalingJobControllerTest { ReflectionUtil.setFieldValue(ResumeBreakPointManagerFactory.class, null, "clazz", FakeResumeBreakPointManager.class); } - private ServerConfiguration mockServerConfiguration() { - ServerConfiguration result = new ServerConfiguration(); - result.setBlockQueueSize(1000); - result.setPort(8080); - result.setPushTimeout(1000); - result.setWorkerThread(30); - return result; - } - - private ShardingScalingJob mockShardingScalingJob() { - ShardingScalingJob result = new ShardingScalingJob(mockScalingConfiguration()); - result.getSyncConfigurations().add(new SyncConfiguration(3, mockDumperConfig(), mockImporterConfiguration())); - return result; - } - - private ScalingConfiguration mockScalingConfiguration() { - ScalingConfiguration result = new ScalingConfiguration(); - result.setJobConfiguration(new JobConfiguration()); - return result; - } - - private ImporterConfiguration mockImporterConfiguration() { - ImporterConfiguration result = new ImporterConfiguration(); - result.setDataSourceConfiguration(new JDBCScalingDataSourceConfiguration(DATA_SOURCE_URL, USERNAME, PASSWORD)); - return result; + @Test + public void assertCheckExistJob() { + ShardingScalingJob shardingScalingJob = mockShardingScalingJob(); + scalingJobController.start(shardingScalingJob); + shardingScalingJob.setDataConsistencyChecker(new DataConsistencyChecker() { + @Override + public Map<String, DataConsistencyCheckResult> countCheck() { + Map<String, DataConsistencyCheckResult> result = Maps.newHashMapWithExpectedSize(1); + result.put("t1", new DataConsistencyCheckResult(1, 1)); + return result; + } + + @Override + public Map<String, Boolean> dataCheck() { + Map<String, Boolean> result = Maps.newHashMapWithExpectedSize(1); + result.put("t1", true); + return result; + } + }); + Map<String, DataConsistencyCheckResult> checkResult = scalingJobController.check(shardingScalingJob.getJobId()); + assertTrue(checkResult.get("t1").isCountValid()); + assertTrue(checkResult.get("t1").isDataValid()); } - private DumperConfiguration mockDumperConfig() { - ScalingDataSourceConfiguration dataSourceConfig = new JDBCScalingDataSourceConfiguration(DATA_SOURCE_URL, USERNAME, PASSWORD); - DumperConfiguration result = new DumperConfiguration(); - result.setDataSourceName("ds0"); - result.setDataSourceConfiguration(dataSourceConfig); - Map<String, String> tableMap = new HashMap<>(1, 1); - tableMap.put("t_order", "t_order"); - result.setTableNameMap(tableMap); - return result; + @Test(expected = ScalingJobNotFoundException.class) + public void assertCheckNotExistJob() { + ShardingScalingJob shardingScalingJob = mockShardingScalingJob(); + scalingJobController.check(shardingScalingJob.getJobId()); } - private ShardingScalingJob mockPreparingFailureShardingScalingJob() { - ShardingScalingJob result = new ShardingScalingJob(mockScalingConfiguration()); - result.getSyncConfigurations().add(new SyncConfiguration(3, mockDumperConfig(), mockImporterConfiguration())); - result.setStatus(SyncTaskControlStatus.PREPARING_FAILURE.name()); - return result; + @SneakyThrows(IOException.class) + private ShardingScalingJob mockShardingScalingJob() { + return ScalingConfigurationUtil.initJob("/config.json"); } } diff --git a/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/check/AbstractDataConsistencyCheckerTest.java b/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/check/AbstractDataConsistencyCheckerTest.java index f82d351..84a0a51 100644 --- a/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/check/AbstractDataConsistencyCheckerTest.java +++ b/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/check/AbstractDataConsistencyCheckerTest.java @@ -17,19 +17,15 @@ package org.apache.shardingsphere.scaling.core.check; -import com.google.gson.Gson; import lombok.SneakyThrows; import org.apache.shardingsphere.scaling.core.config.ScalingDataSourceConfiguration; -import org.apache.shardingsphere.scaling.core.config.ScalingConfiguration; import org.apache.shardingsphere.scaling.core.datasource.DataSourceManager; import org.apache.shardingsphere.scaling.core.job.ShardingScalingJob; -import org.apache.shardingsphere.scaling.core.utils.SyncConfigurationUtil; -import org.junit.Before; +import org.apache.shardingsphere.scaling.core.util.ScalingConfigurationUtil; import org.junit.Test; import javax.sql.DataSource; -import java.io.InputStream; -import java.io.InputStreamReader; +import java.io.IOException; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; @@ -41,20 +37,10 @@ import static org.junit.Assert.assertTrue; public final class AbstractDataConsistencyCheckerTest { - private static final Gson GSON = new Gson(); - - private DataConsistencyChecker dataConsistencyChecker; - - private ShardingScalingJob shardingScalingJob; - - @Before - public void setUp() { - mockShardingScalingJob(); - dataConsistencyChecker = DataConsistencyCheckerFactory.newInstance("H2", shardingScalingJob); - } - @Test public void assertCountCheck() { + ShardingScalingJob shardingScalingJob = mockShardingScalingJob(); + DataConsistencyChecker dataConsistencyChecker = DataConsistencyCheckerFactory.newInstance("H2", shardingScalingJob); initTableData(shardingScalingJob.getSyncConfigurations().get(0).getDumperConfiguration().getDataSourceConfiguration()); initTableData(shardingScalingJob.getSyncConfigurations().get(0).getImporterConfiguration().getDataSourceConfiguration()); Map<String, DataConsistencyCheckResult> resultMap = dataConsistencyChecker.countCheck(); @@ -73,11 +59,8 @@ public final class AbstractDataConsistencyCheckerTest { } } - private void mockShardingScalingJob() { - InputStream fileInputStream = AbstractDataConsistencyCheckerTest.class.getResourceAsStream("/config.json"); - InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream); - ScalingConfiguration scalingConfig = GSON.fromJson(inputStreamReader, ScalingConfiguration.class); - shardingScalingJob = new ShardingScalingJob(scalingConfig); - shardingScalingJob.getSyncConfigurations().addAll(SyncConfigurationUtil.toSyncConfigurations(scalingConfig)); + @SneakyThrows(IOException.class) + private ShardingScalingJob mockShardingScalingJob() { + return ScalingConfigurationUtil.initJob("/config.json"); } } diff --git a/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/datasource/DataSourceManagerTest.java b/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/datasource/DataSourceManagerTest.java index fe0eb43..9112e72 100644 --- a/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/datasource/DataSourceManagerTest.java +++ b/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/datasource/DataSourceManagerTest.java @@ -17,17 +17,15 @@ package org.apache.shardingsphere.scaling.core.datasource; -import com.google.gson.Gson; -import org.apache.shardingsphere.scaling.core.config.ScalingConfiguration; +import lombok.SneakyThrows; import org.apache.shardingsphere.scaling.core.config.SyncConfiguration; -import org.apache.shardingsphere.scaling.core.utils.SyncConfigurationUtil; import org.apache.shardingsphere.scaling.core.util.ReflectionUtil; +import org.apache.shardingsphere.scaling.core.util.ScalingConfigurationUtil; import org.junit.Before; import org.junit.Test; import javax.sql.DataSource; -import java.io.InputStream; -import java.io.InputStreamReader; +import java.io.IOException; import java.util.List; import java.util.Map; @@ -38,21 +36,12 @@ import static org.junit.Assert.assertThat; public final class DataSourceManagerTest { - private static final Gson GSON = new Gson(); - private List<SyncConfiguration> syncConfigurations; @Before + @SneakyThrows(IOException.class) public void setUp() { - initConfig("/config.json"); - } - - @Test - public void assertCreateWithConfiguration() throws NoSuchFieldException, IllegalAccessException { - DataSourceManager dataSourceManager = new DataSourceManager(syncConfigurations); - Map<?, ?> cachedDataSources = ReflectionUtil.getFieldValueFromClass(dataSourceManager, "cachedDataSources", Map.class); - assertNotNull(cachedDataSources); - assertThat(cachedDataSources.size(), is(2)); + syncConfigurations = ScalingConfigurationUtil.initJob("/config.json").getSyncConfigurations(); } @Test @@ -65,16 +54,10 @@ public final class DataSourceManagerTest { @Test public void assertClose() throws NoSuchFieldException, IllegalAccessException { DataSourceManager dataSourceManager = new DataSourceManager(syncConfigurations); - dataSourceManager.close(); Map<?, ?> cachedDataSources = ReflectionUtil.getFieldValueFromClass(dataSourceManager, "cachedDataSources", Map.class); assertNotNull(cachedDataSources); + assertThat(cachedDataSources.size(), is(2)); + dataSourceManager.close(); assertThat(cachedDataSources.size(), is(0)); } - - private void initConfig(final String configFile) { - InputStream fileInputStream = DataSourceManagerTest.class.getResourceAsStream(configFile); - InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream); - ScalingConfiguration scalingConfig = GSON.fromJson(inputStreamReader, ScalingConfiguration.class); - syncConfigurations = (List<SyncConfiguration>) SyncConfigurationUtil.toSyncConfigurations(scalingConfig); - } } diff --git a/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/job/position/resume/IncrementalPositionResumeBreakPointManager.java b/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/job/position/resume/IncrementalPositionResumeBreakPointManager.java index 76d72cc..45ce657 100644 --- a/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/job/position/resume/IncrementalPositionResumeBreakPointManager.java +++ b/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/job/position/resume/IncrementalPositionResumeBreakPointManager.java @@ -27,7 +27,7 @@ import java.util.Map; public final class IncrementalPositionResumeBreakPointManager extends AbstractResumeBreakPointManager { public IncrementalPositionResumeBreakPointManager(final String databaseType, final String taskPath) { - getIncrementalPositionManagerMap().put("ds0", new FixtureNopManager("")); + getIncrementalPositionManagerMap().put("ds_0", new FixtureNopManager("")); } @Override diff --git a/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/util/SyncConfigurationUtilTest.java b/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/util/ScalingConfigurationUtil.java similarity index 50% rename from shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/util/SyncConfigurationUtilTest.java rename to shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/util/ScalingConfigurationUtil.java index b78aafb..c20b5ad 100644 --- a/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/util/SyncConfigurationUtilTest.java +++ b/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/util/ScalingConfigurationUtil.java @@ -18,39 +18,38 @@ package org.apache.shardingsphere.scaling.core.util; import com.google.gson.Gson; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; import org.apache.shardingsphere.scaling.core.config.ScalingConfiguration; -import org.apache.shardingsphere.scaling.core.config.SyncConfiguration; +import org.apache.shardingsphere.scaling.core.job.ShardingScalingJob; import org.apache.shardingsphere.scaling.core.utils.SyncConfigurationUtil; -import org.junit.Before; -import org.junit.Test; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.util.List; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; - -public final class SyncConfigurationUtilTest { +/** + * Scaling configuration util. + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class ScalingConfigurationUtil { private static final Gson GSON = new Gson(); - private ScalingConfiguration scalingConfig; - - @Before - public void setUp() { - initConfig("/config.json"); - } - - @Test - public void assertFilterByShardingDataSourceTables() { - List<SyncConfiguration> syncConfigs = (List<SyncConfiguration>) SyncConfigurationUtil.toSyncConfigurations(scalingConfig); - assertThat(syncConfigs.get(0).getDumperConfiguration().getTableNameMap().size(), is(1)); - } - - private void initConfig(final String configFile) { - InputStream fileInputStream = SyncConfigurationUtilTest.class.getResourceAsStream(configFile); - InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream); - scalingConfig = GSON.fromJson(inputStreamReader, ScalingConfiguration.class); + /** + * Init job from config file. + * + * @param configFile config file + * @return ShardingScalingJob + * @throws IOException IO exception + */ + public static ShardingScalingJob initJob(final String configFile) throws IOException { + try (InputStream fileInputStream = ScalingConfigurationUtil.class.getResourceAsStream(configFile); + InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream)) { + ScalingConfiguration scalingConfiguration = GSON.fromJson(inputStreamReader, ScalingConfiguration.class); + ShardingScalingJob shardingScalingJob = new ShardingScalingJob(scalingConfiguration); + shardingScalingJob.getSyncConfigurations().addAll(SyncConfigurationUtil.toSyncConfigurations(scalingConfiguration)); + return shardingScalingJob; + } } }