Skip to content

Commit

Permalink
Add test cases on ShardingSphereDataPersistService (#32965)
Browse files Browse the repository at this point in the history
* Add test cases on ShardingSphereDataPersistService

* Add test cases on ShardingSphereDataPersistService
  • Loading branch information
terrymanu authored Sep 23, 2024
1 parent 5cc2f09 commit 5542bbf
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,7 +38,6 @@
/**
* ShardingSphere data persist service.
*/
@Getter
public final class ShardingSphereDataPersistService {

private final PersistRepository repository;
Expand All @@ -52,10 +50,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<ShardingSphereStatistics> load(final ShardingSphereMetaData metaData) {
Collection<String> databaseNames = repository.getChildrenKeys(ShardingSphereDataNode.getShardingSphereDataNodePath());
Expand All @@ -70,32 +68,25 @@ public Optional<ShardingSphereStatistics> load(final ShardingSphereMetaData meta
}

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)));

}
return result;
}

/**
* Persist table.
* Persist.
*
* @param databaseName database name
* @param schemaName schema name
* @param schemaData schema data
Expand Down Expand Up @@ -125,19 +116,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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit 5542bbf

Please sign in to comment.