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


##########
plugins/idp-basic/src/test/java/org/apache/gravitino/idp/basic/storage/relational/mapper/AbstractIdpUserMetaStorageTest.java:
##########
@@ -0,0 +1,301 @@
+/*
+ * 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.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 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));

Review Comment:
   Consistent with the core module



-- 
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