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 5542bbf8f47 Add test cases on ShardingSphereDataPersistService (#32965)
5542bbf8f47 is described below
commit 5542bbf8f47ae084efa4edc26d9c408672028d74
Author: Liang Zhang <[email protected]>
AuthorDate: Mon Sep 23 23:58:37 2024 +0800
Add test cases on ShardingSphereDataPersistService (#32965)
* Add test cases on ShardingSphereDataPersistService
* Add test cases on ShardingSphereDataPersistService
---
.../data/ShardingSphereDataPersistService.java | 44 +++----
.../data/ShardingSphereDataPersistServiceTest.java | 126 +++++++++++++++++++++
2 files changed, 141 insertions(+), 29 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 9a7fa807ca8..ca3f0a80705 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 @@ import java.util.stream.Collectors;
/**
* ShardingSphere data persist service.
*/
-@Getter
public final class ShardingSphereDataPersistService {
private final PersistRepository repository;
@@ -52,10 +50,10 @@ public final class ShardingSphereDataPersistService {
}
/**
- * Load ShardingSphere data.
+ * Load ShardingSphere statistics data.
*
* @param metaData meta data
- * @return ShardingSphere data
+ * @return ShardingSphere statistics data
*/
public Optional<ShardingSphereStatistics> load(final
ShardingSphereMetaData metaData) {
Collection<String> databaseNames =
repository.getChildrenKeys(ShardingSphereDataNode.getShardingSphereDataNodePath());
@@ -70,24 +68,16 @@ public final class ShardingSphereDataPersistService {
}
private ShardingSphereDatabaseData load(final String databaseName, final
ShardingSphereDatabase database) {
- Collection<String> 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<String> 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 +85,8 @@ public final class ShardingSphereDataPersistService {
}
/**
- * Persist table.
+ * Persist.
+ *
* @param databaseName database name
* @param schemaName schema name
* @param schemaData schema data
@@ -125,19 +116,14 @@ public final class ShardingSphereDataPersistService {
}
/**
- * 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 00000000000..a46dcc88091
--- /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<YamlShardingSphereRowData> addedRows =
Collections.singletonList(mock(YamlShardingSphereRowData.class));
+ Collection<YamlShardingSphereRowData> updatedRows =
Collections.singletonList(mock(YamlShardingSphereRowData.class));
+ Collection<YamlShardingSphereRowData> 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");
+ }
+}