Skip to content

Commit

Permalink
Refactor DataSourceNodePersistService (apache#32989)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Sep 24, 2024
1 parent 3ec8dd9 commit 036f2e3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;

/**
* Data source node persist service.
Expand Down Expand Up @@ -67,13 +68,11 @@ public Map<String, DataSourcePoolProperties> load(final String databaseName) {
* @return data source pool configurations
*/
@SuppressWarnings("unchecked")
public Map<String, DataSourcePoolProperties> load(final String databaseName, final String name) {
Map<String, DataSourcePoolProperties> result = new LinkedHashMap<>(1, 1F);
public Optional<DataSourcePoolProperties> load(final String databaseName, final String name) {
String dataSourceValue = repository.query(DataSourceMetaDataNode.getDataSourceNodeVersionNode(databaseName, name, getDataSourceActiveVersion(databaseName, name)));
if (!Strings.isNullOrEmpty(dataSourceValue)) {
result.put(name, new YamlDataSourceConfigurationSwapper().swapToDataSourcePoolProperties(YamlEngine.unmarshal(dataSourceValue, Map.class)));
}
return result;
return Strings.isNullOrEmpty(dataSourceValue)
? Optional.empty()
: Optional.of(new YamlDataSourceConfigurationSwapper().swapToDataSourcePoolProperties(YamlEngine.unmarshal(dataSourceValue, Map.class)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
Expand Down Expand Up @@ -65,17 +67,16 @@ void assertLoad() {
void assertLoadWithVersions() {
when(repository.query("/metadata/foo_db/data_sources/nodes/foo_ds/active_version")).thenReturn("10");
when(repository.query("/metadata/foo_db/data_sources/nodes/foo_ds/versions/10")).thenReturn("{dataSourceClassName: org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource}");
Map<String, DataSourcePoolProperties> actual = dataSourceNodePersistService.load("foo_db", "foo_ds");
assertThat(actual.size(), is(1));
assertThat(actual.get("foo_ds").getPoolClassName(), is("org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource"));
Optional<DataSourcePoolProperties> actual = dataSourceNodePersistService.load("foo_db", "foo_ds");
assertTrue(actual.isPresent());
assertThat(actual.get().getPoolClassName(), is("org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource"));
}

@Test
void assertLoadWithoutVersions() {
when(repository.query("/metadata/foo_db/data_sources/nodes/foo_ds/active_version")).thenReturn("");
when(repository.query("/metadata/foo_db/data_sources/nodes/foo_ds/versions/")).thenReturn("");
Map<String, DataSourcePoolProperties> actual = dataSourceNodePersistService.load("foo_db", "foo_ds");
assertTrue(actual.isEmpty());
assertFalse(dataSourceNodePersistService.load("foo_db", "foo_ds").isPresent());
}

@Test
Expand Down

0 comments on commit 036f2e3

Please sign in to comment.