This is an automated email from the ASF dual-hosted git repository.
menghaoran 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 982a79b Add test case for DropResourceBackendHandler (#11508)
982a79b is described below
commit 982a79beedd02e0baa3b1efeebd50e43f92d1a10
Author: yinyanghuafa <[email protected]>
AuthorDate: Fri Jul 30 14:19:20 2021 +0800
Add test case for DropResourceBackendHandler (#11508)
* Add test case for DropResourceBackendHandler
* modify DropResourceBackendHandlerTest.java
---
.../resource/DropResourceBackendHandlerTest.java | 136 +++++++++++++++++++++
1 file changed, 136 insertions(+)
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/DropResourceBackendHandlerTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/DropResourceBackendHandlerTest.java
new file mode 100644
index 0000000..20a264a
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/DropResourceBackendHandlerTest.java
@@ -0,0 +1,136 @@
+/*
+ * 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.distsql.rdl.resource;
+
+import
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.DropResourceStatement;
+import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
+import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import
org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
+import
org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData;
+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;
+import
org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
+import org.apache.shardingsphere.shadow.rule.ShadowRule;
+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 javax.sql.DataSource;
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class DropResourceBackendHandlerTest {
+
+ @Mock
+ private DropResourceStatement dropResourceStatement;
+
+ @Mock
+ private BackendConnection backendConnection;
+
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+ private MetaDataContexts metaDataContexts;
+
+ @Mock
+ private TransactionContexts transactionContexts;
+
+ @Mock
+ private ShardingSphereMetaData metaData;
+
+ @Mock
+ private ShardingSphereResource resource;
+
+ @Mock
+ private DataSource dataSource;
+
+ @Mock
+ private ShardingSphereRuleMetaData ruleMetaData;
+
+ @Mock
+ private ShadowRule shadowRule;
+
+ private DropResourceBackendHandler dropResourceBackendHandler;
+
+ @Before
+ public void setUp() throws Exception {
+ dropResourceBackendHandler = new
DropResourceBackendHandler(dropResourceStatement, backendConnection);
+ ProxyContext.getInstance().init(metaDataContexts, transactionContexts);
+
when(metaDataContexts.getAllSchemaNames()).thenReturn(Collections.singleton("test"));
+ when(metaDataContexts.getMetaData("test")).thenReturn(metaData);
+ when(metaData.getRuleMetaData()).thenReturn(ruleMetaData);
+ when(metaData.getResource()).thenReturn(resource);
+ }
+
+ @Test
+ public void assertExecute() throws DistSQLException {
+ when(ruleMetaData.getRules()).thenReturn(Collections.emptyList());
+ when(resource.getDataSources()).thenReturn(new HashMap<String,
DataSource>() {
+ {
+ put("test0", dataSource);
+ }
+ }
+ );
+ ResponseHeader responseHeader =
dropResourceBackendHandler.execute("test", createDropResourceStatement());
+ assertTrue(responseHeader instanceof UpdateResponseHeader);
+ assertNull(resource.getDataSources().get("test0"));
+ }
+
+ @Test
+ public void assertResourceNameNotExistedExecute() {
+ try {
+ dropResourceBackendHandler.execute("test",
createDropResourceStatement());
+ } catch (final SQLException ex) {
+ assertThat(ex.getMessage(), is("Resources [test0] do not exist in
schema test."));
+ }
+ }
+
+ @Test
+ public void assertResourceNameInUseExecute() {
+
when(ruleMetaData.getRules()).thenReturn(Collections.singleton(shadowRule));
+
when(shadowRule.getDataSourceMapper()).thenReturn(Collections.singletonMap("",
Arrays.asList("test0")));
+ when(resource.getDataSources()).thenReturn(new HashMap<String,
DataSource>() {
+ {
+ put("test0", dataSource);
+ }
+ }
+ );
+ try {
+ dropResourceBackendHandler.execute("test",
createDropResourceStatement());
+ } catch (final SQLException ex) {
+ assertThat(ex.getMessage(), is("Resources [test0] in the rule are
still in used."));
+ }
+ }
+
+ private DropResourceStatement createDropResourceStatement() {
+ return new DropResourceStatement(Collections.singleton("test0"));
+ }
+}