From 43709893a333768d966b66babd47f154f87f1090 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Mon, 23 Sep 2024 23:51:15 +0800 Subject: [PATCH 1/2] Add test cases on ShardingSphereDataPersistService --- .../ShardingSphereDataPersistService.java | 36 ++--- .../ShardingSphereDataPersistServiceTest.java | 126 ++++++++++++++++++ 2 files changed, 138 insertions(+), 24 deletions(-) create mode 100644 kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistServiceTest.java diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistService.java index 9a7fa807ca8a1..1bd71bbe4698e 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistService.java @@ -52,10 +52,10 @@ public ShardingSphereDataPersistService(final PersistRepository repository) { } /** - * Load ShardingSphere data. + * Load ShardingSphere statistics data. * * @param metaData meta data - * @return ShardingSphere data + * @return ShardingSphere statistics data */ public Optional load(final ShardingSphereMetaData metaData) { Collection databaseNames = repository.getChildrenKeys(ShardingSphereDataNode.getShardingSphereDataNodePath()); @@ -70,24 +70,16 @@ public Optional load(final ShardingSphereMetaData meta } private ShardingSphereDatabaseData load(final String databaseName, final ShardingSphereDatabase database) { - Collection schemaNames = repository.getChildrenKeys(ShardingSphereDataNode.getSchemasPath(databaseName)); - if (schemaNames.isEmpty()) { - return new ShardingSphereDatabaseData(); - } ShardingSphereDatabaseData result = new ShardingSphereDatabaseData(); - for (String each : schemaNames.stream().filter(database::containsSchema).collect(Collectors.toList())) { + for (String each : repository.getChildrenKeys(ShardingSphereDataNode.getSchemasPath(databaseName)).stream().filter(database::containsSchema).collect(Collectors.toList())) { result.getSchemaData().put(each, load(databaseName, each, database.getSchema(each))); } return result; } private ShardingSphereSchemaData load(final String databaseName, final String schemaName, final ShardingSphereSchema schema) { - Collection tableNames = repository.getChildrenKeys(ShardingSphereDataNode.getTablesPath(databaseName, schemaName)); - if (tableNames.isEmpty()) { - return new ShardingSphereSchemaData(); - } ShardingSphereSchemaData result = new ShardingSphereSchemaData(); - for (String each : tableNames.stream().filter(schema::containsTable).collect(Collectors.toList())) { + for (String each : repository.getChildrenKeys(ShardingSphereDataNode.getTablesPath(databaseName, schemaName)).stream().filter(schema::containsTable).collect(Collectors.toList())) { result.getTableData().put(each, tableRowDataPersistService.load(databaseName, schemaName, each, schema.getTable(each))); } @@ -95,7 +87,8 @@ private ShardingSphereSchemaData load(final String databaseName, final String sc } /** - * Persist table. + * Persist. + * * @param databaseName database name * @param schemaName schema name * @param schemaData schema data @@ -125,19 +118,14 @@ private void persistTableData(final String databaseName, final String schemaName } /** - * Update sharding sphere database data. + * Update ShardingSphere database data. * - * @param alteredShardingSphereDatabaseData altered ShardingSphere database data + * @param alteredData altered ShardingSphere database data */ - public void update(final AlteredShardingSphereDatabaseData alteredShardingSphereDatabaseData) { - String databaseName = alteredShardingSphereDatabaseData.getDatabaseName(); - String schemaName = alteredShardingSphereDatabaseData.getSchemaName(); - tableRowDataPersistService.persist(databaseName, schemaName, alteredShardingSphereDatabaseData.getTableName(), - alteredShardingSphereDatabaseData.getAddedRows()); - tableRowDataPersistService.persist(databaseName, schemaName, alteredShardingSphereDatabaseData.getTableName(), - alteredShardingSphereDatabaseData.getUpdatedRows()); - tableRowDataPersistService.delete(databaseName, schemaName, alteredShardingSphereDatabaseData.getTableName(), - alteredShardingSphereDatabaseData.getDeletedRows()); + public void update(final AlteredShardingSphereDatabaseData alteredData) { + tableRowDataPersistService.persist(alteredData.getDatabaseName(), alteredData.getSchemaName(), alteredData.getTableName(), alteredData.getAddedRows()); + tableRowDataPersistService.persist(alteredData.getDatabaseName(), alteredData.getSchemaName(), alteredData.getTableName(), alteredData.getUpdatedRows()); + tableRowDataPersistService.delete(alteredData.getDatabaseName(), alteredData.getSchemaName(), alteredData.getTableName(), alteredData.getDeletedRows()); } /** diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistServiceTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistServiceTest.java new file mode 100644 index 0000000000000..a46dcc880914e --- /dev/null +++ b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistServiceTest.java @@ -0,0 +1,126 @@ +/* + * 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.metadata.persist.data; + +import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn; +import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereSchemaData; +import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData; +import org.apache.shardingsphere.infra.yaml.data.pojo.YamlShardingSphereRowData; +import org.apache.shardingsphere.metadata.persist.service.schema.ShardingSphereTableRowDataPersistService; +import org.apache.shardingsphere.mode.spi.PersistRepository; +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.internal.configuration.plugins.Plugins; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class ShardingSphereDataPersistServiceTest { + + private ShardingSphereDataPersistService persistService; + + @Mock + private PersistRepository repository; + + @Mock + private ShardingSphereTableRowDataPersistService tableRowDataPersistService; + + @BeforeEach + void setUp() throws ReflectiveOperationException { + persistService = new ShardingSphereDataPersistService(repository); + Plugins.getMemberAccessor().set(ShardingSphereDataPersistService.class.getDeclaredField("tableRowDataPersistService"), persistService, tableRowDataPersistService); + } + + @Test + void assertLoadWithEmptyDatabases() { + assertFalse(persistService.load(mock(ShardingSphereMetaData.class)).isPresent()); + } + + @Test + void assertLoad() { + when(repository.getChildrenKeys("/statistics/databases")).thenReturn(Arrays.asList("foo_db", "bar_db")); + when(repository.getChildrenKeys("/statistics/databases/foo_db/schemas")).thenReturn(Collections.singletonList("foo_schema")); + when(repository.getChildrenKeys("/statistics/databases/foo_db/schemas/foo_schema/tables")).thenReturn(Collections.singletonList("foo_tbl")); + assertTrue(persistService.load(mockMetaData()).isPresent()); + } + + private ShardingSphereMetaData mockMetaData() { + ShardingSphereMetaData result = mock(ShardingSphereMetaData.class, RETURNS_DEEP_STUBS); + when(result.containsDatabase("foo_db")).thenReturn(true); + when(result.getDatabase("foo_db").containsSchema("foo_schema")).thenReturn(true); + when(result.getDatabase("foo_db").getSchema("foo_schema").containsTable("foo_tbl")).thenReturn(true); + when(result.getDatabase("foo_db").getSchema("foo_schema").getTable("foo_tbl").getColumnValues()).thenReturn(Collections.emptyList()); + when(result.containsDatabase("bar_db")).thenReturn(true); + when(result.getDatabase("bar_db")).thenReturn(mock(ShardingSphereDatabase.class)); + return result; + } + + @Test + void assertPersistWithEmptyTableData() { + persistService.persist("foo_db", "foo_schema", mock(ShardingSphereSchemaData.class), Collections.singletonMap("foo_db", mock(ShardingSphereDatabase.class))); + verify(repository).persist("/statistics/databases/foo_db/schemas/foo_schema", ""); + } + + @Test + void assertPersist() { + ShardingSphereSchemaData schemaData = mock(ShardingSphereSchemaData.class, RETURNS_DEEP_STUBS); + when(schemaData.getTableData().isEmpty()).thenReturn(false); + ShardingSphereTableData tableData = mock(ShardingSphereTableData.class); + when(tableData.getName()).thenReturn("foo_tbl"); + when(schemaData.getTableData().values()).thenReturn(Collections.singleton(tableData)); + ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getSchema("foo_schema").getTable("foo_tbl").getColumnValues()).thenReturn(Collections.singleton(mock(ShardingSphereColumn.class))); + persistService.persist("foo_db", "foo_schema", schemaData, Collections.singletonMap("foo_db", database)); + verify(tableRowDataPersistService).persist("foo_db", "foo_schema", "foo_tbl", Collections.emptyList()); + } + + @Test + void assertUpdate() { + Collection addedRows = Collections.singletonList(mock(YamlShardingSphereRowData.class)); + Collection updatedRows = Collections.singletonList(mock(YamlShardingSphereRowData.class)); + Collection deletedRows = Collections.singletonList(mock(YamlShardingSphereRowData.class)); + AlteredShardingSphereDatabaseData alteredData = new AlteredShardingSphereDatabaseData("foo_db", "foo_schema", "foo_tbl"); + alteredData.getAddedRows().addAll(addedRows); + alteredData.getUpdatedRows().addAll(updatedRows); + alteredData.getDeletedRows().addAll(deletedRows); + persistService.update(alteredData); + verify(tableRowDataPersistService).persist("foo_db", "foo_schema", "foo_tbl", addedRows); + verify(tableRowDataPersistService).persist("foo_db", "foo_schema", "foo_tbl", updatedRows); + verify(tableRowDataPersistService).delete("foo_db", "foo_schema", "foo_tbl", deletedRows); + } + + @Test + void assertDelete() { + persistService.delete("foo_db"); + verify(repository).delete("/statistics/databases/foo_db"); + } +} From f6fa5b120f8ac59fd97fc654a6e3178799137b49 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Mon, 23 Sep 2024 23:53:38 +0800 Subject: [PATCH 2/2] Add test cases on ShardingSphereDataPersistService --- .../persist/data/ShardingSphereDataPersistService.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistService.java index 1bd71bbe4698e..ca3f0a80705fc 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistService.java @@ -17,13 +17,12 @@ package org.apache.shardingsphere.metadata.persist.data; -import lombok.Getter; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; -import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics; -import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereDatabaseData; -import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereSchemaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; +import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereDatabaseData; +import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereSchemaData; +import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics; import org.apache.shardingsphere.infra.yaml.data.pojo.YamlShardingSphereRowData; import org.apache.shardingsphere.infra.yaml.data.swapper.YamlShardingSphereRowDataSwapper; import org.apache.shardingsphere.metadata.persist.node.ShardingSphereDataNode; @@ -39,7 +38,6 @@ /** * ShardingSphere data persist service. */ -@Getter public final class ShardingSphereDataPersistService { private final PersistRepository repository; @@ -88,7 +86,7 @@ private ShardingSphereSchemaData load(final String databaseName, final String sc /** * Persist. - * + * * @param databaseName database name * @param schemaName schema name * @param schemaData schema data