jerryshao commented on code in PR #10913:
URL: https://github.com/apache/gravitino/pull/10913#discussion_r3199633592


##########
core/src/test/java/org/apache/gravitino/storage/relational/mapper/provider/base/TestAuthMappers.java:
##########
@@ -0,0 +1,372 @@
+/*
+ * 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.storage.relational.mapper.provider.base;
+
+import com.google.common.collect.Lists;
+import java.io.File;
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.time.Instant;
+import java.util.Collections;
+import java.util.List;
+import java.util.UUID;
+import org.apache.commons.io.FileUtils;
+import org.apache.gravitino.Config;
+import org.apache.gravitino.Configs;
+import org.apache.gravitino.meta.AuditInfo;
+import org.apache.gravitino.storage.relational.JDBCBackend;
+import org.apache.gravitino.storage.relational.mapper.GroupMetaMapper;
+import org.apache.gravitino.storage.relational.mapper.MetalakeMetaMapper;
+import org.apache.gravitino.storage.relational.mapper.OwnerMetaMapper;
+import org.apache.gravitino.storage.relational.mapper.RoleMetaMapper;
+import org.apache.gravitino.storage.relational.mapper.UserMetaMapper;
+import org.apache.gravitino.storage.relational.po.GroupPO;
+import org.apache.gravitino.storage.relational.po.MetalakePO;
+import org.apache.gravitino.storage.relational.po.OwnerRelPO;
+import org.apache.gravitino.storage.relational.po.RolePO;
+import org.apache.gravitino.storage.relational.po.UserPO;
+import org.apache.gravitino.storage.relational.po.auth.ChangedOwnerInfo;
+import org.apache.gravitino.storage.relational.po.auth.OwnerInfo;
+import org.apache.gravitino.storage.relational.po.auth.RoleUpdatedAt;
+import org.apache.gravitino.storage.relational.po.auth.UserAuthInfo;
+import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper;
+import org.apache.ibatis.session.SqlSession;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+public class TestAuthMappers {
+
+  private static final String JDBC_STORE_PATH =
+      "/tmp/gravitino_jdbc_authMappers_" + 
UUID.randomUUID().toString().replace("-", "");
+  private static final String DB_DIR = JDBC_STORE_PATH + "/testdb";
+  private static JDBCBackend backend;
+  private static RoleMetaMapper roleMetaMapper;
+  private static UserMetaMapper userMetaMapper;
+  private static GroupMetaMapper groupMetaMapper;
+  private static OwnerMetaMapper ownerMetaMapper;
+  private static MetalakeMetaMapper metalakeMetaMapper;
+  private static SqlSession sharedSession;
+
+  @BeforeAll
+  public static void setup() throws Exception {
+    File dir = new File(DB_DIR);
+    if (dir.exists()) {
+      FileUtils.deleteDirectory(dir);
+    }
+    dir.mkdirs();
+
+    Config config = Mockito.mock(Config.class);
+    Mockito.when(config.get(Configs.ENTITY_STORE)).thenReturn("relational");
+    Mockito.when(config.get(Configs.ENTITY_RELATIONAL_STORE)).thenReturn("h2");
+    Mockito.when(config.get(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_URL))
+        
.thenReturn(String.format("jdbc:h2:file:%s;DB_CLOSE_DELAY=-1;MODE=MYSQL", 
DB_DIR));
+    
Mockito.when(config.get(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_USER)).thenReturn("gravitino");
+    Mockito.when(config.get(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_PASSWORD))
+        .thenReturn("gravitino");
+    Mockito.when(config.get(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_DRIVER))
+        .thenReturn("org.h2.Driver");
+    
Mockito.when(config.get(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_MAX_CONNECTIONS)).thenReturn(20);
+    
Mockito.when(config.get(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_WAIT_MILLISECONDS))
+        .thenReturn(1000L);
+
+    backend = new JDBCBackend();
+    backend.initialize(config);
+
+    sharedSession = 
SqlSessionFactoryHelper.getInstance().getSqlSessionFactory().openSession(true);
+    roleMetaMapper = sharedSession.getMapper(RoleMetaMapper.class);
+    userMetaMapper = sharedSession.getMapper(UserMetaMapper.class);
+    groupMetaMapper = sharedSession.getMapper(GroupMetaMapper.class);
+    ownerMetaMapper = sharedSession.getMapper(OwnerMetaMapper.class);
+    metalakeMetaMapper = sharedSession.getMapper(MetalakeMetaMapper.class);
+  }
+
+  @AfterAll
+  public static void tearDown() throws IOException {
+    if (sharedSession != null) {
+      sharedSession.close();
+    }
+    if (backend != null) {
+      backend.close();
+    }
+    File dir = new File(JDBC_STORE_PATH);
+    if (dir.exists()) {
+      FileUtils.deleteDirectory(dir);
+    }
+  }
+
+  @BeforeEach
+  public void truncateTables() {
+    try (SqlSession sqlSession =
+        
SqlSessionFactoryHelper.getInstance().getSqlSessionFactory().openSession(true)) 
{
+      try (Connection connection = sqlSession.getConnection()) {
+        try (Statement statement = connection.createStatement()) {
+          statement.execute("TRUNCATE TABLE entity_change_log");
+          statement.execute("TRUNCATE TABLE owner_meta");
+          statement.execute("TRUNCATE TABLE user_role_rel");
+          statement.execute("TRUNCATE TABLE group_role_rel");
+          statement.execute("TRUNCATE TABLE role_meta_securable_object");
+          statement.execute("TRUNCATE TABLE user_meta");
+          statement.execute("TRUNCATE TABLE group_meta");
+          statement.execute("TRUNCATE TABLE role_meta");
+          statement.execute("TRUNCATE TABLE metalake_meta");
+        }
+      }
+    } catch (SQLException e) {
+      throw new RuntimeException("Truncate table failed", e);
+    }
+  }
+
+  private AuditInfo buildAuditInfo() {
+    return 
AuditInfo.builder().withCreator("test").withCreateTime(Instant.now()).build();
+  }
+
+  private MetalakePO insertMetalake(long metalakeId, String metalakeName) {
+    AuditInfo auditInfo = buildAuditInfo();
+    MetalakePO metalakePO =
+        MetalakePO.builder()
+            .withMetalakeId(metalakeId)
+            .withMetalakeName(metalakeName)
+            .withAuditInfo(auditInfo.toString())
+            .withSchemaVersion("1.0")
+            .withCurrentVersion(1L)
+            .withLastVersion(0L)
+            .withDeletedAt(0L)
+            .build();
+    metalakeMetaMapper.insertMetalakeMeta(metalakePO);
+    return metalakePO;
+  }
+
+  private UserPO insertUser(long userId, String userName, long metalakeId) {
+    AuditInfo auditInfo = buildAuditInfo();
+    UserPO userPO =
+        UserPO.builder()
+            .withUserId(userId)
+            .withUserName(userName)
+            .withMetalakeId(metalakeId)
+            .withAuditInfo(auditInfo.toString())
+            .withCurrentVersion(1L)
+            .withLastVersion(0L)
+            .withDeletedAt(0L)
+            .build();
+    userMetaMapper.insertUserMeta(userPO);
+    return userPO;
+  }
+
+  private RolePO insertRole(long roleId, String roleName, long metalakeId) {
+    AuditInfo auditInfo = buildAuditInfo();
+    RolePO rolePO =
+        RolePO.builder()
+            .withRoleId(roleId)
+            .withRoleName(roleName)
+            .withMetalakeId(metalakeId)
+            .withAuditInfo(auditInfo.toString())
+            .withCurrentVersion(1L)
+            .withLastVersion(0L)
+            .withDeletedAt(0L)
+            .build();
+    roleMetaMapper.insertRoleMeta(rolePO);
+    return rolePO;
+  }
+
+  private GroupPO insertGroup(long groupId, String groupName, long metalakeId) 
{
+    AuditInfo auditInfo = buildAuditInfo();
+    GroupPO groupPO =
+        GroupPO.builder()
+            .withGroupId(groupId)
+            .withGroupName(groupName)
+            .withMetalakeId(metalakeId)
+            .withAuditInfo(auditInfo.toString())
+            .withCurrentVersion(1L)
+            .withLastVersion(0L)
+            .withDeletedAt(0L)
+            .build();
+    groupMetaMapper.insertGroupMeta(groupPO);
+    return groupPO;
+  }
+
+  private long queryUpdatedAt(String table, String idColumn, long idValue) {
+    try (SqlSession sqlSession =
+        
SqlSessionFactoryHelper.getInstance().getSqlSessionFactory().openSession(true)) 
{
+      try (Connection connection = sqlSession.getConnection()) {
+        String query = "SELECT updated_at FROM " + table + " WHERE " + 
idColumn + " = ?";
+        try (PreparedStatement statement = connection.prepareStatement(query)) 
{
+          statement.setLong(1, idValue);
+          try (ResultSet rs = statement.executeQuery()) {
+            if (rs.next()) {
+              return rs.getLong(1);
+            }
+            return -1;
+          }
+        }
+      }
+    } catch (SQLException e) {
+      throw new RuntimeException("SQL execution failed", e);
+    }
+  }

Review Comment:
   Move the private methods down to the public methods.



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