You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/05/06 04:17:58 UTC
[shardingsphere] branch master updated: Add more unit test for DatabaseAdminQueryBackendHandler (#16759) (#17262)
This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 1edd4f26890 Add more unit test for DatabaseAdminQueryBackendHandler (#16759) (#17262)
1edd4f26890 is described below
commit 1edd4f2689056321382d192f44e2c773c8e471c6
Author: 龙台 Long Tai <77...@users.noreply.github.com>
AuthorDate: Fri May 6 12:17:52 2022 +0800
Add more unit test for DatabaseAdminQueryBackendHandler (#16759) (#17262)
---
.../DatabaseAdminQueryBackendHandlerTest.java | 111 +++++++++++++++++++++
1 file changed, 111 insertions(+)
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/DatabaseAdminQueryBackendHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/DatabaseAdminQueryBackendHandlerTest.java
new file mode 100644
index 00000000000..74ec5f8e738
--- /dev/null
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/DatabaseAdminQueryBackendHandlerTest.java
@@ -0,0 +1,111 @@
+/*
+ * 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.proxy.backend.text.admin;
+
+import com.google.common.collect.Lists;
+import lombok.SneakyThrows;
+import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
+import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
+import org.apache.shardingsphere.infra.executor.kernel.ExecutorEngine;
+import org.apache.shardingsphere.infra.federation.optimizer.context.OptimizerContext;
+import org.apache.shardingsphere.infra.instance.InstanceContext;
+import org.apache.shardingsphere.infra.merge.result.MergedResult;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData;
+import org.apache.shardingsphere.mode.manager.ContextManager;
+import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
+import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import org.apache.shardingsphere.proxy.backend.response.header.query.QueryResponseHeader;
+import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
+import org.apache.shardingsphere.proxy.backend.text.admin.postgresql.executor.SelectTableExecutor;
+import org.apache.shardingsphere.sharding.merge.dal.common.SingleLocalDataMergedResult;
+import org.apache.shardingsphere.transaction.context.TransactionContexts;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class DatabaseAdminQueryBackendHandlerTest {
+
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+ private ContextManager contextManager;
+
+ @Mock
+ private ConnectionSession connectionSession;
+
+ private DatabaseAdminQueryBackendHandler handler;
+
+ @SneakyThrows
+ @Before
+ public void before() {
+ MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), getMetaDataMap(),
+ mock(ShardingSphereRuleMetaData.class), mock(ExecutorEngine.class), mock(OptimizerContext.class), new ConfigurationProperties(new Properties()));
+ contextManager.init(metaDataContexts, mock(TransactionContexts.class), mock(InstanceContext.class));
+ when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts);
+ ProxyContext.getInstance().init(contextManager);
+ when(connectionSession.getDatabaseName()).thenReturn("db");
+ SelectTableExecutor executor = mock(SelectTableExecutor.class, RETURNS_DEEP_STUBS);
+ MergedResult mergedResult = new SingleLocalDataMergedResult(Lists.newArrayList("demo_ds_0", "demo_ds_1"));
+ when(executor.getMergedResult()).thenReturn(mergedResult);
+ when(executor.getQueryResultMetaData().getColumnCount()).thenReturn(1);
+ handler = new DatabaseAdminQueryBackendHandler(connectionSession, executor);
+ }
+
+ private Map<String, ShardingSphereMetaData> getMetaDataMap() {
+ ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class, RETURNS_DEEP_STUBS);
+ when(metaData.getDatabaseName()).thenReturn("db");
+ when(metaData.getResource().getDatabaseType()).thenReturn(new MySQLDatabaseType());
+ when(metaData.getRuleMetaData().getRules()).thenReturn(Collections.emptyList());
+ return Collections.singletonMap("db", metaData);
+ }
+
+ @SneakyThrows
+ @Test
+ public void assertExecute() {
+ assertThat(((QueryResponseHeader) handler.execute()).getQueryHeaders().size(), is(1));
+ }
+
+ @SneakyThrows
+ @Test
+ public void assertNext() {
+ handler.execute();
+ assertTrue(handler.next());
+ }
+
+ @SneakyThrows
+ @Test
+ public void assertGetRowData() {
+ handler.execute();
+ assertThat(handler.getRowData().size(), is(1));
+ }
+}