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 2025/11/27 04:59:56 UTC
(shardingsphere) branch master updated: Add unit tests for view federation metadata refreshers (#37194)
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 3822b05eb25 Add unit tests for view federation metadata refreshers (#37194)
3822b05eb25 is described below
commit 3822b05eb255f98555c393cca733339b37896278
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Thu Nov 27 12:59:50 2025 +0800
Add unit tests for view federation metadata refreshers (#37194)
---
.../AlterViewFederationMetaDataRefresherTest.java | 110 +++++++++++++++++++++
.../CreateViewFederationMetaDataRefresherTest.java | 85 ++++++++++++++++
.../DropViewFederationMetaDataRefresherTest.java | 67 +++++++++++++
3 files changed, 262 insertions(+)
diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/federation/type/AlterViewFederationMetaDataRefresherTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/federation/type/AlterViewFederationMetaDataRefresherTest.java
new file mode 100644
index 00000000000..74042f3addc
--- /dev/null
+++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/federation/type/AlterViewFederationMetaDataRefresherTest.java
@@ -0,0 +1,110 @@
+/*
+ * 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.mode.metadata.refresher.federation.type;
+
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereView;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.mode.metadata.refresher.federation.FederationMetaDataRefresher;
+import org.apache.shardingsphere.mode.persist.service.MetaDataManagerPersistService;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.statement.type.ddl.view.AlterViewStatement;
+import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.Collection;
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.Matchers.hasSize;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class AlterViewFederationMetaDataRefresherTest {
+
+ private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
+
+ private final AlterViewFederationMetaDataRefresher refresher = (AlterViewFederationMetaDataRefresher) TypedSPILoader.getService(FederationMetaDataRefresher.class, AlterViewStatement.class);
+
+ private final String schemaName = "foo_schema";
+
+ @Mock
+ private MetaDataManagerPersistService metaDataManagerPersistService;
+
+ @Mock
+ private ShardingSphereDatabase database;
+
+ private AlterViewStatement sqlStatement;
+
+ @BeforeEach
+ void setUp() {
+ sqlStatement = new AlterViewStatement(databaseType);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ void assertRefreshWithRenameView() {
+ ShardingSphereSchema schema = new ShardingSphereSchema(schemaName, Collections.emptyList(), Collections.singleton(new ShardingSphereView("foo_view", "SELECT * FROM foo_tbl")));
+ when(database.getSchema(schemaName)).thenReturn(schema);
+ sqlStatement.setView(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("foo_view"))));
+ sqlStatement.setRenameView(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("bar_view"))));
+ refresher.refresh(metaDataManagerPersistService, databaseType, database, schemaName, sqlStatement);
+ ArgumentCaptor<Collection<ShardingSphereView>> alteredViewsCaptor = ArgumentCaptor.forClass(Collection.class);
+ ArgumentCaptor<Collection<String>> droppedViewsCaptor = ArgumentCaptor.forClass(Collection.class);
+ verify(metaDataManagerPersistService).alterViews(eq(database), eq(schemaName), alteredViewsCaptor.capture());
+ verify(metaDataManagerPersistService).dropViews(eq(database), eq(schemaName), droppedViewsCaptor.capture());
+ Collection<ShardingSphereView> alteredViews = alteredViewsCaptor.getValue();
+ assertThat(alteredViews, hasSize(1));
+ ShardingSphereView actualView = alteredViews.iterator().next();
+ assertThat(actualView.getName(), is("bar_view"));
+ assertThat(actualView.getViewDefinition(), is("SELECT * FROM foo_tbl"));
+ assertThat(droppedViewsCaptor.getValue(), contains("foo_view"));
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ void assertRefreshWithViewDefinition() {
+ sqlStatement.setView(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("foo_view"))));
+ String expectedViewDefinition = "SELECT * FROM foo_tbl";
+ sqlStatement.setViewDefinition(expectedViewDefinition);
+ refresher.refresh(metaDataManagerPersistService, databaseType, database, schemaName, sqlStatement);
+ ArgumentCaptor<Collection<ShardingSphereView>> alteredViewsCaptor = ArgumentCaptor.forClass(Collection.class);
+ ArgumentCaptor<Collection<String>> droppedViewsCaptor = ArgumentCaptor.forClass(Collection.class);
+ verify(metaDataManagerPersistService).alterViews(eq(database), eq(schemaName), alteredViewsCaptor.capture());
+ verify(metaDataManagerPersistService).dropViews(eq(database), eq(schemaName), droppedViewsCaptor.capture());
+ Collection<ShardingSphereView> alteredViews = alteredViewsCaptor.getValue();
+ assertThat(alteredViews, hasSize(1));
+ ShardingSphereView actualView = alteredViews.iterator().next();
+ assertThat(actualView.getName(), is("foo_view"));
+ assertThat(actualView.getViewDefinition(), is(expectedViewDefinition));
+ assertTrue(droppedViewsCaptor.getValue().isEmpty());
+ }
+}
diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/federation/type/CreateViewFederationMetaDataRefresherTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/federation/type/CreateViewFederationMetaDataRefresherTest.java
new file mode 100644
index 00000000000..ae1ed5aba6b
--- /dev/null
+++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/federation/type/CreateViewFederationMetaDataRefresherTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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.mode.metadata.refresher.federation.type;
+
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereView;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.mode.metadata.refresher.federation.FederationMetaDataRefresher;
+import org.apache.shardingsphere.mode.persist.service.MetaDataManagerPersistService;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.statement.type.ddl.view.CreateViewStatement;
+import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.Collection;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.hasSize;
+import static org.mockito.ArgumentMatchers.anyCollection;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+
+@ExtendWith(MockitoExtension.class)
+class CreateViewFederationMetaDataRefresherTest {
+
+ private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
+
+ private final CreateViewFederationMetaDataRefresher refresher = (CreateViewFederationMetaDataRefresher) TypedSPILoader.getService(FederationMetaDataRefresher.class, CreateViewStatement.class);
+
+ @Mock
+ private MetaDataManagerPersistService metaDataManagerPersistService;
+
+ @Mock
+ private ShardingSphereDatabase database;
+
+ private CreateViewStatement sqlStatement;
+
+ @BeforeEach
+ void setUp() {
+ sqlStatement = new CreateViewStatement(databaseType);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ void assertRefresh() {
+ String schemaName = "foo_schema";
+ sqlStatement.setView(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("foo_view"))));
+ String expectedViewDefinition = "SELECT * FROM foo_tbl";
+ sqlStatement.setViewDefinition(expectedViewDefinition);
+ refresher.refresh(metaDataManagerPersistService, databaseType, database, schemaName, sqlStatement);
+ ArgumentCaptor<Collection<ShardingSphereView>> alteredViewsCaptor = ArgumentCaptor.forClass(Collection.class);
+ verify(metaDataManagerPersistService).alterViews(eq(database), eq(schemaName), alteredViewsCaptor.capture());
+ verify(metaDataManagerPersistService, never()).dropViews(eq(database), anyString(), anyCollection());
+ Collection<ShardingSphereView> alteredViews = alteredViewsCaptor.getValue();
+ assertThat(alteredViews, hasSize(1));
+ ShardingSphereView actualView = alteredViews.iterator().next();
+ assertThat(actualView.getName(), is("foo_view"));
+ assertThat(actualView.getViewDefinition(), is(expectedViewDefinition));
+ }
+}
diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/federation/type/DropViewFederationMetaDataRefresherTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/federation/type/DropViewFederationMetaDataRefresherTest.java
new file mode 100644
index 00000000000..a82020d665c
--- /dev/null
+++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/federation/type/DropViewFederationMetaDataRefresherTest.java
@@ -0,0 +1,67 @@
+/*
+ * 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.mode.metadata.refresher.federation.type;
+
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.mode.metadata.refresher.federation.FederationMetaDataRefresher;
+import org.apache.shardingsphere.mode.persist.service.MetaDataManagerPersistService;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.statement.type.ddl.view.DropViewStatement;
+import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.Arrays;
+
+import static org.mockito.Mockito.verify;
+
+@ExtendWith(MockitoExtension.class)
+class DropViewFederationMetaDataRefresherTest {
+
+ private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
+
+ private final DropViewFederationMetaDataRefresher refresher = (DropViewFederationMetaDataRefresher) TypedSPILoader.getService(FederationMetaDataRefresher.class, DropViewStatement.class);
+
+ @Mock
+ private MetaDataManagerPersistService metaDataManagerPersistService;
+
+ @Mock
+ private ShardingSphereDatabase database;
+
+ private DropViewStatement sqlStatement;
+
+ @BeforeEach
+ void setUp() {
+ sqlStatement = new DropViewStatement(databaseType);
+ }
+
+ @Test
+ void assertRefresh() {
+ String schemaName = "foo_schema";
+ sqlStatement.getViews().add(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("foo_view"))));
+ sqlStatement.getViews().add(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("bar_view"))));
+ refresher.refresh(metaDataManagerPersistService, databaseType, database, schemaName, sqlStatement);
+ verify(metaDataManagerPersistService).dropViews(database, schemaName, Arrays.asList("foo_view", "bar_view"));
+ }
+}