You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2021/01/28 06:19:04 UTC
[shardingsphere] branch master updated: Refactor
shardingsphere-scaling-bootstrap unit tests (#9196)
This is an automated email from the ASF dual-hosted git repository.
panjuan 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 022868f Refactor shardingsphere-scaling-bootstrap unit tests (#9196)
022868f is described below
commit 022868f6a914579d1b235f12ff984e881e74754d
Author: 邱鹿 Lucas <lu...@163.com>
AuthorDate: Thu Jan 28 14:18:47 2021 +0800
Refactor shardingsphere-scaling-bootstrap unit tests (#9196)
* Refactor shardingsphere-scaling-bootstrap unit tests
* Refactor shardingsphere-scaling-bootstrap unit tests
Co-authored-by: qiulu3 <Lucas209910>
---
.../scaling/web/HttpServerHandler.java | 14 +-
.../fixture/FixtureDataConsistencyChecker.java | 42 ------
.../fixture/FixtureH2DataSourceChecker.java | 42 ------
.../scaling/fixture/FixtureH2JDBCDumper.java | 38 -----
.../scaling/fixture/FixtureH2ScalingEntry.java | 70 ---------
.../scaling/fixture/FixtureNopImporter.java | 51 -------
.../fixture/FixturePositionInitializer.java | 31 ----
.../scaling/web/HttpServerHandlerTest.java | 164 ++++++++++-----------
.../scaling/web/HttpServerInitializerTest.java | 5 +-
9 files changed, 86 insertions(+), 371 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 ae69bc5..323b038 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
@@ -53,7 +53,7 @@ public final class HttpServerHandler extends SimpleChannelInboundHandler<FullHtt
private static final Gson GSON = new GsonBuilder().disableHtmlEscaping().serializeNulls().setLongSerializationPolicy(LongSerializationPolicy.STRING).create();
- private static final ScalingJobService SCALING_JOB_SERVICE = ScalingJobServiceFactory.getInstance();
+ private final ScalingJobService scalingJobService = ScalingJobServiceFactory.getInstance();
@Override
protected void channelRead0(final ChannelHandlerContext context, final FullHttpRequest request) {
@@ -89,7 +89,7 @@ public final class HttpServerHandler extends SimpleChannelInboundHandler<FullHtt
}
private void startJob(final ChannelHandlerContext context, final String requestBody) {
- Optional<ScalingJob> scalingJob = SCALING_JOB_SERVICE.start(GSON.fromJson(requestBody, ScalingConfiguration.class));
+ Optional<ScalingJob> scalingJob = scalingJobService.start(GSON.fromJson(requestBody, ScalingConfiguration.class));
if (scalingJob.isPresent()) {
response(ResponseContentUtil.build(scalingJob.get()), context, HttpResponseStatus.OK);
return;
@@ -98,26 +98,26 @@ public final class HttpServerHandler extends SimpleChannelInboundHandler<FullHtt
}
private void listJobs(final ChannelHandlerContext context) {
- List<ScalingJob> scalingJobs = SCALING_JOB_SERVICE.listJobs();
+ List<ScalingJob> scalingJobs = scalingJobService.listJobs();
response(ResponseContentUtil.build(scalingJobs), context, HttpResponseStatus.OK);
}
private void getJobProgress(final ChannelHandlerContext context, final String requestPath) {
try {
- response(ResponseContentUtil.build(SCALING_JOB_SERVICE.getProgress(getJobId(requestPath))), context, HttpResponseStatus.OK);
+ response(ResponseContentUtil.build(scalingJobService.getProgress(getJobId(requestPath))), context, HttpResponseStatus.OK);
} catch (final ScalingJobNotFoundException ex) {
response(ResponseContentUtil.handleBadRequest(ex.getMessage()), context, HttpResponseStatus.BAD_REQUEST);
}
}
private void stopJob(final ChannelHandlerContext context, final String requestPath) {
- SCALING_JOB_SERVICE.stop(getJobId(requestPath));
+ scalingJobService.stop(getJobId(requestPath));
response(ResponseContentUtil.success(), context, HttpResponseStatus.OK);
}
private void checkJob(final ChannelHandlerContext context, final String requestPath) {
try {
- response(ResponseContentUtil.build(SCALING_JOB_SERVICE.check(getJobId(requestPath))), context, HttpResponseStatus.OK);
+ response(ResponseContentUtil.build(scalingJobService.check(getJobId(requestPath))), context, HttpResponseStatus.OK);
} catch (final ScalingJobNotFoundException ex) {
response(ResponseContentUtil.handleBadRequest(ex.getMessage()), context, HttpResponseStatus.BAD_REQUEST);
}
@@ -125,7 +125,7 @@ public final class HttpServerHandler extends SimpleChannelInboundHandler<FullHtt
private void resetJob(final ChannelHandlerContext context, final String requestPath) {
try {
- SCALING_JOB_SERVICE.reset(getJobId(requestPath));
+ scalingJobService.reset(getJobId(requestPath));
response(ResponseContentUtil.success(), context, HttpResponseStatus.OK);
} catch (final ScalingJobNotFoundException | SQLException ex) {
response(ResponseContentUtil.handleBadRequest(ex.getMessage()), context, HttpResponseStatus.BAD_REQUEST);
diff --git a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixtureDataConsistencyChecker.java b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixtureDataConsistencyChecker.java
deleted file mode 100644
index 00bf023..0000000
--- a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixtureDataConsistencyChecker.java
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * 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.fixture;
-
-import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.scaling.core.job.ScalingJob;
-import org.apache.shardingsphere.scaling.core.job.check.DataConsistencyCheckResult;
-import org.apache.shardingsphere.scaling.core.job.check.DataConsistencyChecker;
-
-import java.util.Collections;
-import java.util.Map;
-
-@RequiredArgsConstructor
-public final class FixtureDataConsistencyChecker implements DataConsistencyChecker {
-
- private final ScalingJob scalingJob;
-
- @Override
- public Map<String, DataConsistencyCheckResult> countCheck() {
- return Collections.emptyMap();
- }
-
- @Override
- public Map<String, Boolean> dataCheck() {
- return Collections.emptyMap();
- }
-}
diff --git a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixtureH2DataSourceChecker.java b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixtureH2DataSourceChecker.java
deleted file mode 100644
index e2c9f67..0000000
--- a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixtureH2DataSourceChecker.java
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * 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.fixture;
-
-import org.apache.shardingsphere.scaling.core.job.preparer.checker.DataSourceChecker;
-
-import javax.sql.DataSource;
-import java.util.Collection;
-
-public final class FixtureH2DataSourceChecker implements DataSourceChecker {
-
- @Override
- public void checkConnection(final Collection<? extends DataSource> dataSources) {
- }
-
- @Override
- public void checkPrivilege(final Collection<? extends DataSource> dataSources) {
- }
-
- @Override
- public void checkVariable(final Collection<? extends DataSource> dataSources) {
- }
-
- @Override
- public void checkTargetTable(final Collection<? extends DataSource> dataSources, final Collection<String> tableNames) {
- }
-}
diff --git a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixtureH2JDBCDumper.java b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixtureH2JDBCDumper.java
deleted file mode 100644
index 09b736a..0000000
--- a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixtureH2JDBCDumper.java
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * 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.fixture;
-
-import org.apache.shardingsphere.scaling.core.config.InventoryDumperConfiguration;
-import org.apache.shardingsphere.scaling.core.datasource.DataSourceManager;
-import org.apache.shardingsphere.scaling.core.execute.executor.dumper.AbstractJDBCDumper;
-
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.SQLException;
-
-public final class FixtureH2JDBCDumper extends AbstractJDBCDumper {
-
- public FixtureH2JDBCDumper(final InventoryDumperConfiguration dumperConfig, final DataSourceManager dataSourceManager) {
- super(dumperConfig, dataSourceManager);
- }
-
- @Override
- protected PreparedStatement createPreparedStatement(final Connection conn, final String sql) throws SQLException {
- return conn.prepareStatement(sql);
- }
-}
diff --git a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixtureH2ScalingEntry.java b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixtureH2ScalingEntry.java
deleted file mode 100644
index 99229ac..0000000
--- a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixtureH2ScalingEntry.java
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * 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.fixture;
-
-import org.apache.shardingsphere.scaling.core.execute.executor.dumper.JDBCDumper;
-import org.apache.shardingsphere.scaling.core.execute.executor.dumper.LogDumper;
-import org.apache.shardingsphere.scaling.core.execute.executor.importer.Importer;
-import org.apache.shardingsphere.scaling.core.execute.executor.sqlbuilder.ScalingSQLBuilder;
-import org.apache.shardingsphere.scaling.core.job.check.DataConsistencyChecker;
-import org.apache.shardingsphere.scaling.core.job.position.PositionInitializer;
-import org.apache.shardingsphere.scaling.core.job.preparer.checker.DataSourceChecker;
-import org.apache.shardingsphere.scaling.core.spi.ScalingEntry;
-
-public final class FixtureH2ScalingEntry implements ScalingEntry {
-
- @Override
- public Class<? extends JDBCDumper> getJdbcDumperClass() {
- return FixtureH2JDBCDumper.class;
- }
-
- @Override
- public Class<? extends LogDumper> getLogDumperClass() {
- return null;
- }
-
- @Override
- public Class<? extends PositionInitializer> getPositionInitializer() {
- return FixturePositionInitializer.class;
- }
-
- @Override
- public Class<? extends Importer> getImporterClass() {
- return FixtureNopImporter.class;
- }
-
- @Override
- public Class<? extends DataSourceChecker> getDataSourceCheckerClass() {
- return FixtureH2DataSourceChecker.class;
- }
-
- @Override
- public Class<? extends DataConsistencyChecker> getDataConsistencyCheckerClass() {
- return FixtureDataConsistencyChecker.class;
- }
-
- @Override
- public Class<? extends ScalingSQLBuilder> getSQLBuilderClass() {
- return null;
- }
-
- @Override
- public String getDatabaseType() {
- return "H2";
- }
-}
diff --git a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixtureNopImporter.java b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixtureNopImporter.java
deleted file mode 100644
index 4119c7d..0000000
--- a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixtureNopImporter.java
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * 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.fixture;
-
-import org.apache.shardingsphere.scaling.core.config.ImporterConfiguration;
-import org.apache.shardingsphere.scaling.core.datasource.DataSourceManager;
-import org.apache.shardingsphere.scaling.core.execute.executor.channel.Channel;
-import org.apache.shardingsphere.scaling.core.execute.executor.importer.Importer;
-
-public final class FixtureNopImporter implements Importer {
-
- public FixtureNopImporter(final ImporterConfiguration importerConfig, final DataSourceManager dataSourceManager) {
- }
-
- @Override
- public void setChannel(final Channel channel) {
- }
-
- @Override
- public void write() {
- }
-
- @Override
- public void start() {
- }
-
- @Override
- public void stop() {
- }
-
- @Override
- public void run() {
- start();
- write();
- }
-}
diff --git a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixturePositionInitializer.java b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixturePositionInitializer.java
deleted file mode 100644
index 201d333..0000000
--- a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/fixture/FixturePositionInitializer.java
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * 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.fixture;
-
-import org.apache.shardingsphere.scaling.core.job.position.PlaceholderPosition;
-import org.apache.shardingsphere.scaling.core.job.position.PositionInitializer;
-
-import javax.sql.DataSource;
-
-public final class FixturePositionInitializer implements PositionInitializer<PlaceholderPosition> {
-
- @Override
- public PlaceholderPosition init(final DataSource dataSource) {
- return new PlaceholderPosition();
- }
-}
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 4f60b9e..1d091e47 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
@@ -17,8 +17,9 @@
package org.apache.shardingsphere.scaling.web;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
import com.google.gson.Gson;
-import com.google.gson.JsonObject;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
@@ -29,11 +30,15 @@ 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.ServerConfiguration;
-import org.apache.shardingsphere.scaling.core.execute.engine.TaskExecuteEngine;
+import org.apache.shardingsphere.scaling.core.job.JobProgress;
+import org.apache.shardingsphere.scaling.core.job.ScalingJob;
+import org.apache.shardingsphere.scaling.core.job.check.DataConsistencyCheckResult;
+import org.apache.shardingsphere.scaling.core.service.ScalingJobService;
import org.apache.shardingsphere.scaling.core.utils.ReflectionUtil;
-import org.apache.shardingsphere.scaling.util.ScalingConfigurationUtil;
+import org.apache.shardingsphere.scaling.web.entity.ResponseContent;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -41,12 +46,18 @@ import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
-import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public final class HttpServerHandlerTest {
@@ -54,117 +65,79 @@ public final class HttpServerHandlerTest {
private HttpServerHandler httpServerHandler;
@Mock
- private ChannelHandlerContext channelHandlerContext;
+ private ScalingJobService scalingJobService;
- private FullHttpRequest fullHttpRequest;
+ @Mock
+ private ChannelHandlerContext channelHandlerContext;
@Before
@SneakyThrows(ReflectiveOperationException.class)
public void setUp() {
ReflectionUtil.setFieldValue(ScalingContext.getInstance(), "serverConfig", new ServerConfiguration());
- ReflectionUtil.setFieldValue(ScalingContext.getInstance(), "inventoryDumperExecuteEngine", mock(TaskExecuteEngine.class));
httpServerHandler = new HttpServerHandler();
+ ReflectionUtil.setFieldValue(httpServerHandler, "scalingJobService", scalingJobService);
}
@Test
- public void assertChannelReadStartSuccess() {
- startScalingJob("/config.json");
- ArgumentCaptor<FullHttpResponse> argumentCaptor = ArgumentCaptor.forClass(FullHttpResponse.class);
- verify(channelHandlerContext).writeAndFlush(argumentCaptor.capture());
- FullHttpResponse fullHttpResponse = argumentCaptor.getValue();
- assertTrue(fullHttpResponse.content().toString(CharsetUtil.UTF_8).contains("{\"success\":true"));
+ public void assertStartJobSuccess() {
+ when(scalingJobService.start(any(ScalingConfiguration.class))).thenReturn(Optional.of(new ScalingJob()));
+ ResponseContent<?> responseContent = execute("/scaling/job/start");
+ assertTrue(responseContent.isSuccess());
}
@Test
- public void assertShardingSphereJDBCTargetChannelReadStartSuccess() {
- startScalingJob("/config_sharding_sphere_jdbc_target.json");
- ArgumentCaptor<FullHttpResponse> argumentCaptor = ArgumentCaptor.forClass(FullHttpResponse.class);
- verify(channelHandlerContext).writeAndFlush(argumentCaptor.capture());
- FullHttpResponse fullHttpResponse = argumentCaptor.getValue();
- assertTrue(fullHttpResponse.content().toString(CharsetUtil.UTF_8).contains("{\"success\":true"));
+ public void assertStartJobFailure() {
+ when(scalingJobService.start(any(ScalingConfiguration.class))).thenReturn(Optional.empty());
+ ResponseContent<?> responseContent = execute("/scaling/job/start");
+ assertFalse(responseContent.isSuccess());
}
@Test
- public void assertChannelReadProgressFail() {
- fullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/scaling/job/progress/9");
- httpServerHandler.channelRead0(channelHandlerContext, fullHttpRequest);
- ArgumentCaptor<FullHttpResponse> argumentCaptor = ArgumentCaptor.forClass(FullHttpResponse.class);
- verify(channelHandlerContext).writeAndFlush(argumentCaptor.capture());
- FullHttpResponse fullHttpResponse = argumentCaptor.getValue();
- assertTrue(fullHttpResponse.content().toString(CharsetUtil.UTF_8).contains("Can't find scaling job id 9"));
+ @SuppressWarnings("unchecked")
+ public void assertListJobs() {
+ when(scalingJobService.listJobs()).thenReturn(mockScalingJobs());
+ ResponseContent<?> responseContent = execute("/scaling/job/list");
+ assertThat(((List<ScalingJob>) responseContent.getModel()).size(), is(2));
}
@Test
- public void assertChannelReadProgressSuccess() {
- long jobId = startScalingJob("/config.json");
- fullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/scaling/job/progress/" + jobId);
- httpServerHandler.channelRead0(channelHandlerContext, fullHttpRequest);
- ArgumentCaptor<FullHttpResponse> argumentCaptor = ArgumentCaptor.forClass(FullHttpResponse.class);
- verify(channelHandlerContext, times(2)).writeAndFlush(argumentCaptor.capture());
- FullHttpResponse fullHttpResponse = argumentCaptor.getValue();
- assertTrue(fullHttpResponse.content().toString(CharsetUtil.UTF_8).contains("{\"success\":true"));
+ @SuppressWarnings("unchecked")
+ public void assertGetJobProgress() {
+ when(scalingJobService.getProgress(1L)).thenReturn(mockJobProgress());
+ ResponseContent<?> responseContent = execute("/scaling/job/progress/1");
+ Map<String, String> map = (Map<String, String>) responseContent.getModel();
+ assertThat(map.get("status"), is("RUNNING"));
}
@Test
- public void assertChannelReadStop() {
- long jobId = startScalingJob("/config.json");
- fullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/scaling/job/stop/" + jobId);
- httpServerHandler.channelRead0(channelHandlerContext, fullHttpRequest);
- ArgumentCaptor<FullHttpResponse> argumentCaptor = ArgumentCaptor.forClass(FullHttpResponse.class);
- verify(channelHandlerContext, times(2)).writeAndFlush(argumentCaptor.capture());
- FullHttpResponse fullHttpResponse = argumentCaptor.getValue();
- assertTrue(fullHttpResponse.content().toString(CharsetUtil.UTF_8).contains("{\"success\":true"));
+ public void assertStopJob() {
+ ResponseContent<?> responseContent = execute("/scaling/job/stop/1");
+ assertTrue(responseContent.isSuccess());
}
@Test
- public void assertCheckJob() {
- long jobId = startScalingJob("/config.json");
- fullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/scaling/job/check/" + jobId);
- httpServerHandler.channelRead0(channelHandlerContext, fullHttpRequest);
- ArgumentCaptor<FullHttpResponse> argumentCaptor = ArgumentCaptor.forClass(FullHttpResponse.class);
- verify(channelHandlerContext, times(2)).writeAndFlush(argumentCaptor.capture());
- FullHttpResponse fullHttpResponse = argumentCaptor.getValue();
- assertTrue(fullHttpResponse.content().toString(CharsetUtil.UTF_8).contains("{\"success\":true"));
+ public void assertDataConsistencyCheck() {
+ when(scalingJobService.check(1L)).thenReturn(mockDataConsistency());
+ ResponseContent<?> responseContent = execute("/scaling/job/check/1");
+ assertTrue(responseContent.isSuccess());
}
@Test
- public void assertCheckJobFail() {
- fullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/scaling/job/check/9");
- httpServerHandler.channelRead0(channelHandlerContext, fullHttpRequest);
- ArgumentCaptor<FullHttpResponse> argumentCaptor = ArgumentCaptor.forClass(FullHttpResponse.class);
- verify(channelHandlerContext).writeAndFlush(argumentCaptor.capture());
- FullHttpResponse fullHttpResponse = argumentCaptor.getValue();
- assertTrue(fullHttpResponse.content().toString(CharsetUtil.UTF_8).contains("Can't find scaling job id 9"));
- }
-
- @Test
- public void assertChannelReadList() {
- fullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/scaling/job/list");
- httpServerHandler.channelRead0(channelHandlerContext, fullHttpRequest);
- ArgumentCaptor<FullHttpResponse> argumentCaptor = ArgumentCaptor.forClass(FullHttpResponse.class);
- verify(channelHandlerContext).writeAndFlush(argumentCaptor.capture());
- FullHttpResponse fullHttpResponse = argumentCaptor.getValue();
- assertTrue(fullHttpResponse.content().toString(CharsetUtil.UTF_8).contains("{\"success\":true"));
+ public void assertResetTargetTable() {
+ ResponseContent<?> responseContent = execute("/scaling/job/reset/1");
+ assertTrue(responseContent.isSuccess());
}
@Test
public void assertChannelReadUnsupportedUrl() {
- fullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.DELETE, "/scaling/1");
- httpServerHandler.channelRead0(channelHandlerContext, fullHttpRequest);
- ArgumentCaptor<FullHttpResponse> argumentCaptor = ArgumentCaptor.forClass(FullHttpResponse.class);
- verify(channelHandlerContext).writeAndFlush(argumentCaptor.capture());
- FullHttpResponse fullHttpResponse = argumentCaptor.getValue();
- assertTrue(fullHttpResponse.content().toString(CharsetUtil.UTF_8).contains("Not support request!"));
+ ResponseContent<?> responseContent = execute("/scaling/1");
+ assertThat(responseContent.getErrorMsg(), is("Not support request!"));
}
@Test
public void assertChannelReadUnsupportedMethod() {
- fullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.DELETE, "/scaling/job/stop");
- httpServerHandler.channelRead0(channelHandlerContext, fullHttpRequest);
- ArgumentCaptor<FullHttpResponse> argumentCaptor = ArgumentCaptor.forClass(FullHttpResponse.class);
- verify(channelHandlerContext).writeAndFlush(argumentCaptor.capture());
- FullHttpResponse fullHttpResponse = argumentCaptor.getValue();
- assertTrue(fullHttpResponse.content().toString(CharsetUtil.UTF_8).contains("Not support request!"));
+ ResponseContent<?> responseContent = execute("/scaling/1", HttpMethod.DELETE);
+ assertThat(responseContent.getErrorMsg(), is("Not support request!"));
}
@Test
@@ -174,15 +147,34 @@ public final class HttpServerHandlerTest {
verify(channelHandlerContext).close();
}
- @SneakyThrows(IOException.class)
- private long startScalingJob(final String configFile) {
- ByteBuf byteBuf = Unpooled.copiedBuffer(new Gson().toJson(ScalingConfigurationUtil.initConfig(configFile)), CharsetUtil.UTF_8);
- fullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/scaling/job/start", byteBuf);
+ private ResponseContent<?> execute(final String uri) {
+ return execute(uri, HttpMethod.POST);
+ }
+
+ private ResponseContent<?> execute(final String uri, final HttpMethod httpMethod) {
+ ByteBuf byteBuf = Unpooled.copiedBuffer("{}", CharsetUtil.UTF_8);
+ FullHttpRequest fullHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, httpMethod, uri, byteBuf);
httpServerHandler.channelRead0(channelHandlerContext, fullHttpRequest);
ArgumentCaptor<FullHttpResponse> argumentCaptor = ArgumentCaptor.forClass(FullHttpResponse.class);
verify(channelHandlerContext).writeAndFlush(argumentCaptor.capture());
FullHttpResponse fullHttpResponse = argumentCaptor.getValue();
- JsonObject jsonObject = new Gson().fromJson(fullHttpResponse.content().toString(CharsetUtil.UTF_8), JsonObject.class);
- return jsonObject.get("model").getAsJsonObject().get("jobId").getAsLong();
+ return new Gson().fromJson(fullHttpResponse.content().toString(CharsetUtil.UTF_8), ResponseContent.class);
+ }
+
+ private List<ScalingJob> mockScalingJobs() {
+ List<ScalingJob> result = Lists.newArrayList();
+ result.add(new ScalingJob());
+ result.add(new ScalingJob());
+ return result;
+ }
+
+ private JobProgress mockJobProgress() {
+ return new JobProgress(1, "RUNNING");
+ }
+
+ private Map<String, DataConsistencyCheckResult> mockDataConsistency() {
+ Map<String, DataConsistencyCheckResult> result = Maps.newHashMap();
+ result.put("t_order", new DataConsistencyCheckResult(1, 1));
+ return result;
}
}
diff --git a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/web/HttpServerInitializerTest.java b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/web/HttpServerInitializerTest.java
index 764bb99..0103f54 100644
--- a/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/web/HttpServerInitializerTest.java
+++ b/shardingsphere-scaling/shardingsphere-scaling-bootstrap/src/test/java/org/apache/shardingsphere/scaling/web/HttpServerInitializerTest.java
@@ -23,7 +23,6 @@ import io.netty.channel.socket.SocketChannel;
import lombok.SneakyThrows;
import org.apache.shardingsphere.scaling.core.config.ScalingContext;
import org.apache.shardingsphere.scaling.core.config.ServerConfiguration;
-import org.apache.shardingsphere.scaling.core.execute.engine.TaskExecuteEngine;
import org.apache.shardingsphere.scaling.core.utils.ReflectionUtil;
import org.junit.Before;
import org.junit.Test;
@@ -32,7 +31,6 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -50,12 +48,11 @@ public final class HttpServerInitializerTest {
@SneakyThrows(ReflectiveOperationException.class)
public void setUp() {
ReflectionUtil.setFieldValue(ScalingContext.getInstance(), "serverConfig", new ServerConfiguration());
- ReflectionUtil.setFieldValue(ScalingContext.getInstance(), "inventoryDumperExecuteEngine", mock(TaskExecuteEngine.class));
- when(socketChannel.pipeline()).thenReturn(channelPipeline);
}
@Test
public void assertInitChannel() {
+ when(socketChannel.pipeline()).thenReturn(channelPipeline);
HttpServerInitializer httpServerInitializer = new HttpServerInitializer();
httpServerInitializer.initChannel(socketChannel);
verify(channelPipeline, times(3)).addLast(any(ChannelHandler.class));