You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2026/03/08 09:59:45 UTC

(shardingsphere) branch master updated: Add more test cases on OpenGaussDatabasePrivilegeCheckerTest (#38372)

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 873701b8bb7 Add more test cases on OpenGaussDatabasePrivilegeCheckerTest (#38372)
873701b8bb7 is described below

commit 873701b8bb78a3305881a9aa262672d67a64eb94
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun Mar 8 17:59:39 2026 +0800

    Add more test cases on OpenGaussDatabasePrivilegeCheckerTest (#38372)
    
    * Add more test cases on OpenGaussDatabasePrivilegeCheckerTest
    
    * Add more test cases on OpenGaussDatabasePrivilegeCheckerTest
---
 .../OpenGaussDatabasePrivilegeCheckerTest.java     | 60 ++++++++++++++--------
 1 file changed, 40 insertions(+), 20 deletions(-)

diff --git a/database/connector/dialect/opengauss/src/test/java/org/apache/shardingsphere/database/connector/opengauss/checker/OpenGaussDatabasePrivilegeCheckerTest.java b/database/connector/dialect/opengauss/src/test/java/org/apache/shardingsphere/database/connector/opengauss/checker/OpenGaussDatabasePrivilegeCheckerTest.java
index 11c251edf2b..a999e4be9c3 100644
--- a/database/connector/dialect/opengauss/src/test/java/org/apache/shardingsphere/database/connector/opengauss/checker/OpenGaussDatabasePrivilegeCheckerTest.java
+++ b/database/connector/dialect/opengauss/src/test/java/org/apache/shardingsphere/database/connector/opengauss/checker/OpenGaussDatabasePrivilegeCheckerTest.java
@@ -21,22 +21,28 @@ import org.apache.shardingsphere.database.connector.core.checker.DialectDatabase
 import org.apache.shardingsphere.database.connector.core.checker.PrivilegeCheckType;
 import org.apache.shardingsphere.database.connector.core.exception.CheckDatabaseEnvironmentFailedException;
 import org.apache.shardingsphere.database.connector.core.exception.MissingRequiredPrivilegeException;
+import org.apache.shardingsphere.database.connector.core.exception.MissingRequiredUserException;
 import org.apache.shardingsphere.database.connector.core.spi.DatabaseTypedSPILoader;
 import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
 
 import javax.sql.DataSource;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.util.stream.Stream;
 
-import static org.hamcrest.Matchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verifyNoInteractions;
 import static org.mockito.Mockito.when;
 
 class OpenGaussDatabasePrivilegeCheckerTest {
@@ -47,49 +53,63 @@ class OpenGaussDatabasePrivilegeCheckerTest {
     
     @Test
     void assertCheckWithNoneType() {
-        assertDoesNotThrow(() -> checker.check(mock(DataSource.class), PrivilegeCheckType.NONE));
+        DataSource dataSource = mock(DataSource.class);
+        assertDoesNotThrow(() -> checker.check(dataSource, PrivilegeCheckType.NONE));
+        verifyNoInteractions(dataSource);
     }
     
-    @Test
-    void assertCheckWithSuperRole() {
-        assertDoesNotThrow(() -> checker.check(mockPipelineDataSource("admin", "t", "f"), PrivilegeCheckType.PIPELINE));
+    @ParameterizedTest(name = "{0}")
+    @MethodSource("pipelinePrivilegeRoles")
+    void assertCheckWithPipelinePrivilegeRole(final String name, final String username, final String isSuperRole, final String isReplicationRole, final String isSystemAdminRole) {
+        assertDoesNotThrow(() -> checker.check(mockPipelineDataSource(username, true, isSuperRole, isReplicationRole, isSystemAdminRole), PrivilegeCheckType.PIPELINE));
     }
     
     @Test
-    void assertCheckWithReplicationRole() {
-        assertDoesNotThrow(() -> checker.check(mockPipelineDataSource("replication_user", "f", "t"), PrivilegeCheckType.PIPELINE));
+    void assertCheckWhenUserMissing() {
+        MissingRequiredUserException actual = assertThrows(MissingRequiredUserException.class,
+                () -> checker.check(mockPipelineDataSource("ghost", false, "f", "f", "f"), PrivilegeCheckType.PIPELINE));
+        assertThat(actual.getMessage(), is("User 'ghost' does exist"));
     }
     
     @Test
     void assertCheckWhenPrivilegeMissing() {
         MissingRequiredPrivilegeException actual = assertThrows(MissingRequiredPrivilegeException.class,
-                () -> checker.check(mockPipelineDataSource("normal", "f", "f"), PrivilegeCheckType.PIPELINE));
+                () -> checker.check(mockPipelineDataSource("normal", true, "f", "f", "f"), PrivilegeCheckType.PIPELINE));
         assertThat(actual.getMessage(), is("Missing required privilege(s) `REPLICATION`"));
     }
     
-    @Test
-    void assertCheckWhenSQLExceptionThrown() throws SQLException {
-        DataSource dataSource = mock(DataSource.class);
-        when(dataSource.getConnection()).thenThrow(SQLException.class);
-        assertThrows(CheckDatabaseEnvironmentFailedException.class, () -> checker.check(dataSource, PrivilegeCheckType.PIPELINE));
-    }
-    
     @SuppressWarnings({"JDBCResourceOpenedButNotSafelyClosed", "resource"})
-    private DataSource mockPipelineDataSource(final String username, final String isSuperRole, final String isReplicationRole) throws SQLException {
+    private DataSource mockPipelineDataSource(final String username, final boolean hasUser,
+                                              final String isSuperRole, final String isReplicationRole, final String isSystemAdminRole) throws SQLException {
         DataSource result = mock(DataSource.class, RETURNS_DEEP_STUBS);
         when(result.getConnection().getMetaData().getUserName()).thenReturn(username);
         PreparedStatement preparedStatement = result.getConnection().prepareStatement("SELECT * FROM pg_roles WHERE rolname = ?");
-        ResultSet resultSet = mockResultSet(isSuperRole, isReplicationRole);
+        ResultSet resultSet = mockResultSet(hasUser, isSuperRole, isReplicationRole, isSystemAdminRole);
         when(preparedStatement.executeQuery()).thenReturn(resultSet);
         return result;
     }
     
-    private ResultSet mockResultSet(final String isSuperRole, final String isReplicationRole) throws SQLException {
+    private ResultSet mockResultSet(final boolean hasUser, final String isSuperRole, final String isReplicationRole, final String isSystemAdminRole) throws SQLException {
         ResultSet result = mock(ResultSet.class);
-        when(result.next()).thenReturn(true);
+        when(result.next()).thenReturn(hasUser);
         when(result.getString("rolsuper")).thenReturn(isSuperRole);
         when(result.getString("rolreplication")).thenReturn(isReplicationRole);
-        when(result.getString("rolsystemadmin")).thenReturn("f");
+        when(result.getString("rolsystemadmin")).thenReturn(isSystemAdminRole);
         return result;
     }
+    
+    @Test
+    void assertCheckWhenSQLExceptionThrown() throws SQLException {
+        DataSource dataSource = mock(DataSource.class);
+        when(dataSource.getConnection()).thenThrow(SQLException.class);
+        CheckDatabaseEnvironmentFailedException actual = assertThrows(CheckDatabaseEnvironmentFailedException.class, () -> checker.check(dataSource, PrivilegeCheckType.PIPELINE));
+        assertThat(actual.getMessage(), is("Check database environment failed"));
+    }
+    
+    private static Stream<Arguments> pipelinePrivilegeRoles() {
+        return Stream.of(
+                Arguments.of("super role", "admin", "t", "f", "f"),
+                Arguments.of("replication role", "replication_user", "f", "t", "f"),
+                Arguments.of("system admin role", "system_admin", "f", "f", "t"));
+    }
 }