From 645c63b03ea186690affb4b245839e00b9772437 Mon Sep 17 00:00:00 2001 From: yx9o Date: Fri, 5 Jan 2024 13:36:09 +0800 Subject: [PATCH] Add test cases for authenticators & defaultAuthenticator (#29656) * Add test cases for authenticators & defaultAuthenticator * Add test cases --- ...AuthorityRuleConfigurationBuilderTest.java | 4 +++ ...AuthorityRuleConfigurationSwapperTest.java | 19 +++++++++++-- ...AuthorityRuleConfigurationSwapperTest.java | 27 ++++++++++++++++--- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/rule/builder/DefaultAuthorityRuleConfigurationBuilderTest.java b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/rule/builder/DefaultAuthorityRuleConfigurationBuilderTest.java index 74774d25ab851..efb733b358276 100644 --- a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/rule/builder/DefaultAuthorityRuleConfigurationBuilderTest.java +++ b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/rule/builder/DefaultAuthorityRuleConfigurationBuilderTest.java @@ -22,6 +22,8 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; class DefaultAuthorityRuleConfigurationBuilderTest { @@ -30,5 +32,7 @@ void assertBuild() { AuthorityRuleConfiguration actual = new DefaultAuthorityRuleConfigurationBuilder().build(); assertThat(actual.getPrivilegeProvider().getType(), is("ALL_PERMITTED")); assertThat(actual.getUsers().size(), is(1)); + assertNull(actual.getDefaultAuthenticator()); + assertTrue(actual.getAuthenticators().isEmpty()); } } diff --git a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/yaml/swapper/NewYamlAuthorityRuleConfigurationSwapperTest.java b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/yaml/swapper/NewYamlAuthorityRuleConfigurationSwapperTest.java index 2c5ef8f511249..1f5dff7586cc8 100644 --- a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/yaml/swapper/NewYamlAuthorityRuleConfigurationSwapperTest.java +++ b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/yaml/swapper/NewYamlAuthorityRuleConfigurationSwapperTest.java @@ -27,6 +27,7 @@ import java.util.Collections; import java.util.Properties; +import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -38,7 +39,21 @@ class NewYamlAuthorityRuleConfigurationSwapperTest { @Test void assertSwapToDataNodes() { Collection users = Collections.singleton(new ShardingSphereUser("root", "", "localhost")); - Collection actual = swapper.swapToDataNodes(new AuthorityRuleConfiguration(users, new AlgorithmConfiguration("ALL_PERMITTED", new Properties()), Collections.emptyMap(), null)); - assertThat(actual.iterator().next().getKey(), is("authority")); + Collection actual = swapper.swapToDataNodes(new AuthorityRuleConfiguration(users, new AlgorithmConfiguration("ALL_PERMITTED", new Properties()), + Collections.singletonMap("md5", new AlgorithmConfiguration("MD5", createProperties())), "scram_sha256")); + YamlDataNode yamlDataNode = actual.iterator().next(); + assertThat(yamlDataNode.getKey(), is("authority")); + assertThat(yamlDataNode.getValue(), containsString("user: root@localhost")); + assertThat(yamlDataNode.getValue(), containsString("password: ''")); + assertThat(yamlDataNode.getValue(), containsString("type: ALL_PERMITTED")); + assertThat(yamlDataNode.getValue(), containsString("defaultAuthenticator: scram_sha256")); + assertThat(yamlDataNode.getValue(), containsString("type: MD5")); + assertThat(yamlDataNode.getValue(), containsString("proxy-frontend-database-protocol-type: openGauss")); + } + + private Properties createProperties() { + Properties result = new Properties(); + result.put("proxy-frontend-database-protocol-type", "openGauss"); + return result; } } diff --git a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/yaml/swapper/YamlAuthorityRuleConfigurationSwapperTest.java b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/yaml/swapper/YamlAuthorityRuleConfigurationSwapperTest.java index d6ec7208fc778..e479bcfd8c721 100644 --- a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/yaml/swapper/YamlAuthorityRuleConfigurationSwapperTest.java +++ b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/yaml/swapper/YamlAuthorityRuleConfigurationSwapperTest.java @@ -30,6 +30,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; class YamlAuthorityRuleConfigurationSwapperTest { @@ -38,22 +39,27 @@ class YamlAuthorityRuleConfigurationSwapperTest { @Test void assertSwapToYamlConfiguration() { - AuthorityRuleConfiguration authorityRuleConfig = new AuthorityRuleConfiguration(Collections.emptyList(), new AlgorithmConfiguration("type", new Properties()), Collections.emptyMap(), null); + AuthorityRuleConfiguration authorityRuleConfig = new AuthorityRuleConfiguration(Collections.emptyList(), new AlgorithmConfiguration("ALL_PERMITTED", new Properties()), + Collections.singletonMap("md5", createAlgorithmConfiguration()), "scram_sha256"); YamlAuthorityRuleConfiguration actual = swapper.swapToYamlConfiguration(authorityRuleConfig); assertTrue(actual.getUsers().isEmpty()); assertNotNull(actual.getPrivilege()); + assertThat(actual.getDefaultAuthenticator(), is("scram_sha256")); + assertThat(actual.getAuthenticators().size(), is(1)); } @Test void assertSwapToObject() { YamlAuthorityRuleConfiguration authorityRuleConfig = new YamlAuthorityRuleConfiguration(); authorityRuleConfig.setUsers(Collections.singletonList(getYamlUser())); - YamlAlgorithmConfiguration yamlAlgorithmConfig = new YamlAlgorithmConfiguration(); - yamlAlgorithmConfig.setType("type"); - authorityRuleConfig.setPrivilege(yamlAlgorithmConfig); + authorityRuleConfig.setPrivilege(createYamlAlgorithmConfiguration()); + authorityRuleConfig.setDefaultAuthenticator("scram_sha256"); + authorityRuleConfig.setAuthenticators(Collections.singletonMap("md5", createYamlAlgorithmConfiguration())); AuthorityRuleConfiguration actual = swapper.swapToObject(authorityRuleConfig); assertThat(actual.getUsers().size(), is(1)); assertNotNull(actual.getPrivilegeProvider()); + assertThat(actual.getDefaultAuthenticator(), is("scram_sha256")); + assertThat(actual.getAuthenticators().size(), is(1)); } @Test @@ -63,6 +69,9 @@ void assertSwapToObjectWithDefaultProvider() { AuthorityRuleConfiguration actual = swapper.swapToObject(authorityRuleConfig); assertThat(actual.getUsers().size(), is(1)); assertThat(actual.getPrivilegeProvider().getType(), is("ALL_PERMITTED")); + assertThat(actual.getUsers().size(), is(1)); + assertNull(actual.getDefaultAuthenticator()); + assertTrue(actual.getAuthenticators().isEmpty()); } private YamlUserConfiguration getYamlUser() { @@ -71,4 +80,14 @@ private YamlUserConfiguration getYamlUser() { result.setPassword("password"); return result; } + + private AlgorithmConfiguration createAlgorithmConfiguration() { + return new AlgorithmConfiguration("MD5", new Properties()); + } + + private YamlAlgorithmConfiguration createYamlAlgorithmConfiguration() { + YamlAlgorithmConfiguration result = new YamlAlgorithmConfiguration(); + result.setType("MD5"); + return result; + } }