Skip to content

Commit

Permalink
Refactor ContextManagerBuilderParameter (#31198)
Browse files Browse the repository at this point in the history
* Refactor ContextManagerBuilderParameter

* Refactor ContextManagerBuilderParameter
  • Loading branch information
terrymanu authored May 10, 2024
1 parent 3872fec commit 425c8c4
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,6 @@ public final class ContextManagerBuilderParameter {

private final boolean force;

/**
* Whether parameter is empty.
*
* @return parameter is empty or not
*/
public boolean isEmpty() {
return globalRuleConfigs.isEmpty() && props.isEmpty()
&& databaseConfigs.entrySet().stream().allMatch(entry -> entry.getValue().getStorageUnits().isEmpty() && entry.getValue().getRuleConfigurations().isEmpty());
}

/**
* Get mode configuration.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,69 +17,21 @@

package org.apache.shardingsphere.mode.manager;

import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
import org.apache.shardingsphere.infra.config.mode.PersistRepositoryConfiguration;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.Map;
import java.util.Properties;

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.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
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.when;

class ContextManagerBuilderParameterTest {

@Test
void assertIsEmptyWithoutAllParameters() {
assertTrue(new ContextManagerBuilderParameter(null, Collections.emptyMap(), Collections.emptyMap(), Collections.emptyList(), new Properties(), null, null, false).isEmpty());
}

@Test
void assertIsEmptyWithDatabaseAndWithoutConfigurations() {
assertTrue(new ContextManagerBuilderParameter(null, mockDatabaseConfigurations(true, true), Collections.emptyMap(), Collections.emptyList(), new Properties(), null, null, false).isEmpty());
}

@Test
void assertIsNotEmptyWhenGlobalRuleIsNotEmpty() {
assertFalse(new ContextManagerBuilderParameter(null, Collections.emptyMap(), Collections.emptyMap(), Collections.singleton(mock(RuleConfiguration.class)),
new Properties(), null, null, false).isEmpty());
}

@Test
void assertIsNotEmptyWhenPropsIsNotEmpty() {
assertFalse(new ContextManagerBuilderParameter(null, Collections.emptyMap(), Collections.emptyMap(), Collections.emptyList(), PropertiesBuilder.build(new Property("foo", "foo_value")),
null, null, false).isEmpty());
}

@Test
void assertIsEmptyWhenDataSourceIsNotEmpty() {
assertFalse(new ContextManagerBuilderParameter(null, mockDatabaseConfigurations(false, true), Collections.emptyMap(), Collections.emptyList(), new Properties(), null, null, false).isEmpty());
}

@Test
void assertIsEmptyWhenDatabaseRuleIsNotEmpty() {
assertFalse(new ContextManagerBuilderParameter(null, mockDatabaseConfigurations(true, false), Collections.emptyMap(), Collections.emptyList(), new Properties(), null, null, false).isEmpty());
}

private Map<String, DatabaseConfiguration> mockDatabaseConfigurations(final boolean isEmptyStorageUnits, final boolean isEmptyRuleConfigs) {
DatabaseConfiguration databaseConfig = mock(DatabaseConfiguration.class, RETURNS_DEEP_STUBS);
when(databaseConfig.getStorageUnits().isEmpty()).thenReturn(isEmptyStorageUnits);
when(databaseConfig.getRuleConfigurations().isEmpty()).thenReturn(isEmptyRuleConfigs);
return Collections.singletonMap("foo_ds", databaseConfig);
}

@Test
void assertGetDefaultModeConfiguration() {
ContextManagerBuilderParameter param = new ContextManagerBuilderParameter(null, Collections.emptyMap(), Collections.emptyMap(), Collections.emptyList(), new Properties(), null, null, false);
Expand Down

0 comments on commit 425c8c4

Please sign in to comment.