Copilot commented on code in PR #11066:
URL: https://github.com/apache/gravitino/pull/11066#discussion_r3246739887


##########
plugins/idp-basic/src/test/java/org/apache/gravitino/idp/basic/storage/relational/mapper/AbstractIdpUserMetaStorageTest.java:
##########
@@ -0,0 +1,313 @@
+/*
+ * 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.gravitino.idp.basic.storage.relational.mapper;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.UUID;
+import java.util.stream.Stream;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.gravitino.Config;
+import org.apache.gravitino.Configs;
+import org.apache.gravitino.config.ConfigConstants;
+import org.apache.gravitino.idp.basic.storage.relational.po.IdpUserPO;
+import org.apache.gravitino.integration.test.container.ContainerSuite;
+import org.apache.gravitino.integration.test.container.MySQLContainer;
+import org.apache.gravitino.integration.test.container.PostgreSQLContainer;
+import org.apache.gravitino.integration.test.util.TestDatabaseName;
+import org.apache.gravitino.storage.relational.JDBCBackend;
+import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper;
+import org.apache.ibatis.session.SqlSession;
+import org.junit.jupiter.api.AfterEach;
+
+abstract class AbstractIdpUserMetaStorageTest {
+  private static final String H2_BACKEND = "h2";
+  private static final String MYSQL_BACKEND = "mysql";
+  private static final String POSTGRESQL_BACKEND = "postgresql";
+  private static final TestDatabaseName MYSQL_TEST_DATABASE = 
TestDatabaseName.MYSQL_JDBC_BACKEND;
+  private static final TestDatabaseName POSTGRESQL_TEST_DATABASE = 
TestDatabaseName.PG_JDBC_BACKEND;
+
+  protected JDBCBackend backend;
+  protected SqlSession sharedSession;
+  protected IdpUserMetaMapper idpUserMetaMapper;
+
+  private Config config;
+  private Path h2Path;
+
+  static Stream<String> storageProvider() {
+    return Stream.of(H2_BACKEND, MYSQL_BACKEND, POSTGRESQL_BACKEND);
+  }
+
+  @AfterEach
+  void closeSuite() throws IOException {
+    closeSession();
+    if (backend != null) {
+      backend.close();
+      backend = null;
+    }
+
+    SqlSessionFactoryHelper.getInstance().close();
+    ContainerSuite.getInstance().close();

Review Comment:
   `ContainerSuite` is a process-wide singleton intended to be shared across 
tests; closing it in `@AfterEach` tears down (and re-starts) the 
MySQL/PostgreSQL Docker containers on every parameterized invocation. Combined 
with the `storageProvider()` source running ~8 tests × 3 backends, this will 
start/stop containers ~16 times per class and dramatically slow the suite (and 
may flake on resource-constrained CI). Consider moving container teardown to 
`@AfterAll` (or omitting it and relying on `ContainerSuite`'s own lifecycle), 
and only resetting per-test state (schema/database) in `@AfterEach`.



##########
plugins/idp-basic/src/main/java/org/apache/gravitino/idp/basic/storage/relational/mapper/IdpUserMetaSQLProviderFactory.java:
##########
@@ -0,0 +1,131 @@
+/*
+ * 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.gravitino.idp.basic.storage.relational.mapper;
+
+import com.google.common.collect.ImmutableMap;
+import java.util.List;
+import java.util.Map;
+import 
org.apache.gravitino.idp.basic.storage.relational.mapper.provider.base.IdpUserMetaBaseSQLProvider;
+import 
org.apache.gravitino.idp.basic.storage.relational.mapper.provider.mysql.IdpUserMetaMySQLProvider;
+import 
org.apache.gravitino.idp.basic.storage.relational.mapper.provider.postgresql.IdpUserMetaPostgreSQLProvider;
+import org.apache.gravitino.idp.basic.storage.relational.po.IdpUserPO;
+import org.apache.gravitino.storage.relational.JDBCBackend.JDBCBackendType;
+import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper;
+import org.apache.ibatis.annotations.Param;
+
+public class IdpUserMetaSQLProviderFactory {
+  private static final IdpUserMetaBaseSQLProvider IDP_USER_META_H2_PROVIDER =
+      new IdpUserMetaH2Provider();
+  private static final IdpUserMetaBaseSQLProvider IDP_USER_META_MYSQL_PROVIDER 
=
+      new IdpUserMetaMySQLProvider();
+  private static final IdpUserMetaBaseSQLProvider 
IDP_USER_META_POSTGRESQL_PROVIDER =
+      new IdpUserMetaPostgreSQLProvider();
+
+  private static final Map<JDBCBackendType, IdpUserMetaBaseSQLProvider>
+      IDP_USER_META_SQL_PROVIDER_MAP =
+          ImmutableMap.of(
+              JDBCBackendType.MYSQL, IDP_USER_META_MYSQL_PROVIDER,
+              JDBCBackendType.H2, IDP_USER_META_H2_PROVIDER,
+              JDBCBackendType.POSTGRESQL, IDP_USER_META_POSTGRESQL_PROVIDER);
+
+  static IdpUserMetaBaseSQLProvider getProvider(
+      String databaseId, Map<JDBCBackendType, IdpUserMetaBaseSQLProvider> 
providerMap) {
+    if (databaseId == null) {
+      throw new IllegalStateException(
+          "MyBatis databaseId is not configured for IdP user SQL providers.");

Review Comment:
   If `databaseId` is null because the MyBatis `DatabaseIdProvider` hasn't been 
registered, this exception will surface deep in a mapper invocation with no 
hint of how to fix it. Consider expanding the message to point to the 
configuration location (e.g., that `SqlSessionFactoryHelper` must register a 
`DatabaseIdProvider` mapping to `h2`/`mysql`/`postgresql`), which will save 
debugging time for callers integrating the plugin.
   



##########
plugins/idp-basic/src/test/java/org/apache/gravitino/idp/basic/storage/relational/mapper/AbstractIdpUserMetaStorageTest.java:
##########
@@ -0,0 +1,313 @@
+/*
+ * 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.gravitino.idp.basic.storage.relational.mapper;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.UUID;
+import java.util.stream.Stream;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.gravitino.Config;
+import org.apache.gravitino.Configs;
+import org.apache.gravitino.config.ConfigConstants;
+import org.apache.gravitino.idp.basic.storage.relational.po.IdpUserPO;
+import org.apache.gravitino.integration.test.container.ContainerSuite;
+import org.apache.gravitino.integration.test.container.MySQLContainer;
+import org.apache.gravitino.integration.test.container.PostgreSQLContainer;
+import org.apache.gravitino.integration.test.util.TestDatabaseName;
+import org.apache.gravitino.storage.relational.JDBCBackend;
+import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper;
+import org.apache.ibatis.session.SqlSession;
+import org.junit.jupiter.api.AfterEach;
+
+abstract class AbstractIdpUserMetaStorageTest {
+  private static final String H2_BACKEND = "h2";
+  private static final String MYSQL_BACKEND = "mysql";
+  private static final String POSTGRESQL_BACKEND = "postgresql";
+  private static final TestDatabaseName MYSQL_TEST_DATABASE = 
TestDatabaseName.MYSQL_JDBC_BACKEND;
+  private static final TestDatabaseName POSTGRESQL_TEST_DATABASE = 
TestDatabaseName.PG_JDBC_BACKEND;
+
+  protected JDBCBackend backend;
+  protected SqlSession sharedSession;
+  protected IdpUserMetaMapper idpUserMetaMapper;
+
+  private Config config;
+  private Path h2Path;
+
+  static Stream<String> storageProvider() {
+    return Stream.of(H2_BACKEND, MYSQL_BACKEND, POSTGRESQL_BACKEND);
+  }
+
+  @AfterEach
+  void closeSuite() throws IOException {
+    closeSession();
+    if (backend != null) {
+      backend.close();
+      backend = null;
+    }
+
+    SqlSessionFactoryHelper.getInstance().close();
+    ContainerSuite.getInstance().close();
+
+    if (h2Path != null && Files.exists(h2Path)) {
+      deleteDirectory(h2Path);
+      h2Path = null;
+    }
+  }
+
+  protected void init(String type) throws IOException {
+    config = createBackendConfig(type);
+    backend = new JDBCBackend();
+    backend.initialize(config);
+    sharedSession = 
SqlSessionFactoryHelper.getInstance().getSqlSessionFactory().openSession(true);
+    idpUserMetaMapper = sharedSession.getMapper(IdpUserMetaMapper.class);
+  }
+
+  protected void restartBackend() throws IOException {
+    closeSession();
+    backend.close();
+    backend = new JDBCBackend();
+    backend.initialize(config);
+    sharedSession = 
SqlSessionFactoryHelper.getInstance().getSqlSessionFactory().openSession(true);
+    idpUserMetaMapper = sharedSession.getMapper(IdpUserMetaMapper.class);
+  }
+
+  protected IdpUserPO insertUser(
+      long userId,
+      String userName,
+      String passwordHash,
+      long currentVersion,
+      long lastVersion,
+      long deletedAt) {
+    IdpUserPO userPO =
+        IdpUserPO.builder()
+            .withUserId(userId)
+            .withUserName(userName)
+            .withPasswordHash(passwordHash)
+            .withCurrentVersion(currentVersion)
+            .withLastVersion(lastVersion)
+            .withDeletedAt(deletedAt)
+            .build();
+    idpUserMetaMapper.insertIdpUser(userPO);
+    return userPO;
+  }
+
+  protected long queryLongValue(String table, String column, String idColumn, 
long idValue) {
+    try (SqlSession sqlSession =
+            
SqlSessionFactoryHelper.getInstance().getSqlSessionFactory().openSession(true);
+        Connection connection = sqlSession.getConnection();
+        PreparedStatement statement =
+            connection.prepareStatement(
+                "SELECT " + column + " FROM " + table + " WHERE " + idColumn + 
" = ?")) {
+      statement.setLong(1, idValue);
+      try (ResultSet resultSet = statement.executeQuery()) {
+        assertTrue(resultSet.next());
+        return resultSet.getLong(1);
+      }
+    } catch (SQLException e) {
+      throw new RuntimeException("Query " + column + " from " + table + " 
failed", e);
+    }
+  }
+
+  protected int countRows(String table, String idColumn, long idValue) {
+    try (SqlSession sqlSession =
+            
SqlSessionFactoryHelper.getInstance().getSqlSessionFactory().openSession(true);
+        Connection connection = sqlSession.getConnection();
+        PreparedStatement statement =
+            connection.prepareStatement(
+                "SELECT COUNT(1) FROM " + table + " WHERE " + idColumn + " = 
?")) {
+      statement.setLong(1, idValue);
+      try (ResultSet resultSet = statement.executeQuery()) {
+        assertTrue(resultSet.next());
+        return resultSet.getInt(1);
+      }
+    } catch (SQLException e) {
+      throw new RuntimeException("Count rows from " + table + " failed", e);
+    }
+  }
+
+  protected String currentJdbcUrl() {
+    try (SqlSession sqlSession =
+            
SqlSessionFactoryHelper.getInstance().getSqlSessionFactory().openSession(true);
+        Connection connection = sqlSession.getConnection()) {
+      DatabaseMetaData metaData = connection.getMetaData();
+      return metaData.getURL();
+    } catch (SQLException e) {
+      throw new RuntimeException("Get current JDBC URL failed", e);
+    }
+  }
+
+  protected void closeSession() {
+    if (sharedSession != null) {
+      sharedSession.close();
+      sharedSession = null;
+    }
+  }
+
+  private Config createBackendConfig(String type) throws IOException {
+    Config backendConfig = new Config(false) {};
+    backendConfig.set(Configs.ENTITY_STORE, Configs.RELATIONAL_ENTITY_STORE);
+    backendConfig.set(Configs.ENTITY_RELATIONAL_STORE, type);
+    backendConfig.set(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_MAX_CONNECTIONS, 
20);
+    
backendConfig.set(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_WAIT_MILLISECONDS, 
1000L);
+
+    switch (type) {
+      case MYSQL_BACKEND:
+        initializeMySQLBackend(backendConfig);
+        break;
+      case POSTGRESQL_BACKEND:
+        initializePostgreSQLBackend(backendConfig);
+        break;
+      case H2_BACKEND:
+        initializeH2Backend(backendConfig);
+        break;
+      default:
+        throw new IllegalArgumentException("Unsupported backend type: " + 
type);
+    }
+
+    return backendConfig;
+  }
+
+  private void initializeMySQLBackend(Config backendConfig) throws IOException 
{
+    ContainerSuite containerSuite = ContainerSuite.getInstance();
+    containerSuite.startMySQLContainer(MYSQL_TEST_DATABASE);
+    MySQLContainer mySQLContainer = containerSuite.getMySQLContainer();
+    String jdbcUrl = mySQLContainer.getJdbcUrl(MYSQL_TEST_DATABASE);
+
+    backendConfig.set(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_URL, jdbcUrl);
+    backendConfig.set(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_USER, 
mySQLContainer.getUsername());
+    backendConfig.set(
+        Configs.ENTITY_RELATIONAL_JDBC_BACKEND_PASSWORD, 
mySQLContainer.getPassword());
+    backendConfig.set(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_DRIVER, 
"com.mysql.cj.jdbc.Driver");
+
+    try (Connection connection =
+            DriverManager.getConnection(
+                StringUtils.substringBeforeLast(jdbcUrl, "/"),
+                mySQLContainer.getUsername(),
+                mySQLContainer.getPassword());
+        Statement statement = connection.createStatement()) {
+      statement.execute("DROP DATABASE IF EXISTS " + MYSQL_TEST_DATABASE);
+      statement.execute("CREATE DATABASE " + MYSQL_TEST_DATABASE);
+      statement.execute("USE " + MYSQL_TEST_DATABASE);
+      executeSqlStatements(statement, loadSchemaStatements(MYSQL_BACKEND));
+    } catch (SQLException e) {
+      throw new RuntimeException("Failed to initialize MySQL backend for IdP 
user tests", e);
+    }
+  }
+
+  private void initializePostgreSQLBackend(Config backendConfig) throws 
IOException {
+    ContainerSuite containerSuite = ContainerSuite.getInstance();
+    containerSuite.startPostgreSQLContainer(POSTGRESQL_TEST_DATABASE);
+    PostgreSQLContainer postgreSQLContainer = 
containerSuite.getPostgreSQLContainer();
+    String schemaName = "idp_user_" + 
UUID.randomUUID().toString().replace("-", "");
+    String jdbcUrl = postgreSQLContainer.getJdbcUrl(POSTGRESQL_TEST_DATABASE);
+
+    backendConfig.set(
+        Configs.ENTITY_RELATIONAL_JDBC_BACKEND_URL, jdbcUrl + 
"?currentSchema=" + schemaName);
+    backendConfig.set(
+        Configs.ENTITY_RELATIONAL_JDBC_BACKEND_USER, 
postgreSQLContainer.getUsername());
+    backendConfig.set(
+        Configs.ENTITY_RELATIONAL_JDBC_BACKEND_PASSWORD, 
postgreSQLContainer.getPassword());
+    backendConfig.set(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_DRIVER, 
"org.postgresql.Driver");
+
+    try (Connection connection =
+            DriverManager.getConnection(
+                jdbcUrl, postgreSQLContainer.getUsername(), 
postgreSQLContainer.getPassword());
+        Statement statement = connection.createStatement()) {
+      statement.execute("DROP SCHEMA IF EXISTS " + schemaName + " CASCADE");
+      statement.execute("CREATE SCHEMA " + schemaName);
+      statement.execute("SET search_path TO " + schemaName);
+      executeSqlStatements(statement, 
loadSchemaStatements(POSTGRESQL_BACKEND));
+    } catch (SQLException e) {
+      throw new RuntimeException("Failed to initialize PostgreSQL backend for 
IdP user tests", e);
+    }
+  }
+
+  private void initializeH2Backend(Config backendConfig) throws IOException {
+    h2Path = Files.createTempDirectory("gravitino_idp_basic_h2_");
+    backendConfig.set(
+        Configs.ENTITY_RELATIONAL_JDBC_BACKEND_URL,
+        String.format("jdbc:h2:file:%s;DB_CLOSE_DELAY=-1;MODE=MYSQL", h2Path));

Review Comment:
   `Files.createTempDirectory` returns a directory path, but 
`jdbc:h2:file:<path>` expects a file base name. H2 will create database files 
using the directory path itself as the base name (e.g., `<tempdir>.mv.db`), 
leaving the empty temp directory behind and writing the actual DB files as 
siblings of it. `deleteDirectory(h2Path)` then only removes the empty 
directory, not the `.mv.db`/`.trace.db` files. Either create the DB under the 
temp directory (e.g., append `/db` to the path) or delete the sibling files in 
cleanup.
   



##########
plugins/idp-basic/src/main/java/org/apache/gravitino/idp/basic/storage/relational/mapper/IdpUserMetaSQLProviderFactory.java:
##########
@@ -0,0 +1,131 @@
+/*
+ * 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.gravitino.idp.basic.storage.relational.mapper;
+
+import com.google.common.collect.ImmutableMap;
+import java.util.List;
+import java.util.Map;
+import 
org.apache.gravitino.idp.basic.storage.relational.mapper.provider.base.IdpUserMetaBaseSQLProvider;
+import 
org.apache.gravitino.idp.basic.storage.relational.mapper.provider.mysql.IdpUserMetaMySQLProvider;
+import 
org.apache.gravitino.idp.basic.storage.relational.mapper.provider.postgresql.IdpUserMetaPostgreSQLProvider;
+import org.apache.gravitino.idp.basic.storage.relational.po.IdpUserPO;
+import org.apache.gravitino.storage.relational.JDBCBackend.JDBCBackendType;
+import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper;
+import org.apache.ibatis.annotations.Param;
+
+public class IdpUserMetaSQLProviderFactory {
+  private static final IdpUserMetaBaseSQLProvider IDP_USER_META_H2_PROVIDER =
+      new IdpUserMetaH2Provider();

Review Comment:
   The H2 provider is declared as an inner class at the bottom of this factory, 
while the MySQL and PostgreSQL providers each live in their own files under 
`provider/mysql` and `provider/postgresql`. This inconsistency makes the 
package layout harder to discover and extend. Consider moving 
`IdpUserMetaH2Provider` to its own file under a `provider/h2` package for 
symmetry.



##########
plugins/idp-basic/src/test/java/org/apache/gravitino/idp/basic/storage/relational/mapper/AbstractIdpUserMetaStorageTest.java:
##########
@@ -0,0 +1,313 @@
+/*
+ * 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.gravitino.idp.basic.storage.relational.mapper;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.UUID;
+import java.util.stream.Stream;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.gravitino.Config;
+import org.apache.gravitino.Configs;
+import org.apache.gravitino.config.ConfigConstants;
+import org.apache.gravitino.idp.basic.storage.relational.po.IdpUserPO;
+import org.apache.gravitino.integration.test.container.ContainerSuite;
+import org.apache.gravitino.integration.test.container.MySQLContainer;
+import org.apache.gravitino.integration.test.container.PostgreSQLContainer;
+import org.apache.gravitino.integration.test.util.TestDatabaseName;
+import org.apache.gravitino.storage.relational.JDBCBackend;
+import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper;
+import org.apache.ibatis.session.SqlSession;
+import org.junit.jupiter.api.AfterEach;
+
+abstract class AbstractIdpUserMetaStorageTest {
+  private static final String H2_BACKEND = "h2";
+  private static final String MYSQL_BACKEND = "mysql";
+  private static final String POSTGRESQL_BACKEND = "postgresql";
+  private static final TestDatabaseName MYSQL_TEST_DATABASE = 
TestDatabaseName.MYSQL_JDBC_BACKEND;
+  private static final TestDatabaseName POSTGRESQL_TEST_DATABASE = 
TestDatabaseName.PG_JDBC_BACKEND;
+
+  protected JDBCBackend backend;
+  protected SqlSession sharedSession;
+  protected IdpUserMetaMapper idpUserMetaMapper;
+
+  private Config config;
+  private Path h2Path;
+
+  static Stream<String> storageProvider() {
+    return Stream.of(H2_BACKEND, MYSQL_BACKEND, POSTGRESQL_BACKEND);
+  }
+
+  @AfterEach
+  void closeSuite() throws IOException {
+    closeSession();
+    if (backend != null) {
+      backend.close();
+      backend = null;
+    }
+
+    SqlSessionFactoryHelper.getInstance().close();
+    ContainerSuite.getInstance().close();
+
+    if (h2Path != null && Files.exists(h2Path)) {
+      deleteDirectory(h2Path);
+      h2Path = null;
+    }
+  }
+
+  protected void init(String type) throws IOException {
+    config = createBackendConfig(type);
+    backend = new JDBCBackend();
+    backend.initialize(config);
+    sharedSession = 
SqlSessionFactoryHelper.getInstance().getSqlSessionFactory().openSession(true);
+    idpUserMetaMapper = sharedSession.getMapper(IdpUserMetaMapper.class);
+  }
+
+  protected void restartBackend() throws IOException {
+    closeSession();
+    backend.close();
+    backend = new JDBCBackend();
+    backend.initialize(config);
+    sharedSession = 
SqlSessionFactoryHelper.getInstance().getSqlSessionFactory().openSession(true);
+    idpUserMetaMapper = sharedSession.getMapper(IdpUserMetaMapper.class);
+  }
+
+  protected IdpUserPO insertUser(
+      long userId,
+      String userName,
+      String passwordHash,
+      long currentVersion,
+      long lastVersion,
+      long deletedAt) {
+    IdpUserPO userPO =
+        IdpUserPO.builder()
+            .withUserId(userId)
+            .withUserName(userName)
+            .withPasswordHash(passwordHash)
+            .withCurrentVersion(currentVersion)
+            .withLastVersion(lastVersion)
+            .withDeletedAt(deletedAt)
+            .build();
+    idpUserMetaMapper.insertIdpUser(userPO);
+    return userPO;
+  }
+
+  protected long queryLongValue(String table, String column, String idColumn, 
long idValue) {
+    try (SqlSession sqlSession =
+            
SqlSessionFactoryHelper.getInstance().getSqlSessionFactory().openSession(true);
+        Connection connection = sqlSession.getConnection();
+        PreparedStatement statement =
+            connection.prepareStatement(
+                "SELECT " + column + " FROM " + table + " WHERE " + idColumn + 
" = ?")) {
+      statement.setLong(1, idValue);
+      try (ResultSet resultSet = statement.executeQuery()) {
+        assertTrue(resultSet.next());
+        return resultSet.getLong(1);
+      }
+    } catch (SQLException e) {
+      throw new RuntimeException("Query " + column + " from " + table + " 
failed", e);
+    }
+  }
+
+  protected int countRows(String table, String idColumn, long idValue) {
+    try (SqlSession sqlSession =
+            
SqlSessionFactoryHelper.getInstance().getSqlSessionFactory().openSession(true);
+        Connection connection = sqlSession.getConnection();
+        PreparedStatement statement =
+            connection.prepareStatement(
+                "SELECT COUNT(1) FROM " + table + " WHERE " + idColumn + " = 
?")) {
+      statement.setLong(1, idValue);
+      try (ResultSet resultSet = statement.executeQuery()) {
+        assertTrue(resultSet.next());
+        return resultSet.getInt(1);
+      }
+    } catch (SQLException e) {
+      throw new RuntimeException("Count rows from " + table + " failed", e);
+    }
+  }
+
+  protected String currentJdbcUrl() {
+    try (SqlSession sqlSession =
+            
SqlSessionFactoryHelper.getInstance().getSqlSessionFactory().openSession(true);
+        Connection connection = sqlSession.getConnection()) {
+      DatabaseMetaData metaData = connection.getMetaData();
+      return metaData.getURL();
+    } catch (SQLException e) {
+      throw new RuntimeException("Get current JDBC URL failed", e);
+    }
+  }
+

Review Comment:
   `currentJdbcUrl()` does not appear to be referenced from any of the test 
cases in this PR. If it is unused, please remove it to avoid dead helper code. 
If it's intended for future use, consider adding it together with the tests 
that need it.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to