protected void dropDb(String user, String... dbs) throws Exception {
   Connection connection = context.createConnection(user);
   Statement statement = connection.createStatement();
   for (String db : dbs) {
     statement.execute("DROP DATABASE IF EXISTS " + db + " CASCADE");
   }
   statement.close();
   connection.close();
 }
 /**
  * A convenient function to run a sequence of sql commands
  *
  * @param user
  * @param sqls
  * @throws Exception
  */
 protected static void execBatch(String user, List<String> sqls) throws Exception {
   Connection conn = context.createConnection(user);
   Statement stmt = context.createStatement(conn);
   for (String sql : sqls) {
     exec(stmt, sql);
   }
   if (stmt != null) {
     stmt.close();
   }
   if (conn != null) {
     conn.close();
   }
 }
 protected void createDb(String user, String... dbs) throws Exception {
   Connection connection = context.createConnection(user);
   Statement statement = connection.createStatement();
   ArrayList<String> allowedDBs = new ArrayList<String>(Arrays.asList(DB1, DB2, DB3));
   for (String db : dbs) {
     assertTrue(
         db + " is not part of known test dbs which will be cleaned up after the test",
         allowedDBs.contains(db));
     statement.execute("CREATE DATABASE " + db);
   }
   statement.close();
   connection.close();
 }
 protected static void setupAdmin() throws Exception {
   if (useSentryService) {
     LOGGER.info("setupAdmin to create admin_role");
     Connection connection = context.createConnection(ADMIN1);
     Statement statement = connection.createStatement();
     try {
       statement.execute("CREATE ROLE admin_role");
     } catch (Exception e) {
       // It is ok if admin_role already exists
     }
     statement.execute(
         "GRANT ALL ON SERVER "
             + HiveServerFactory.DEFAULT_AUTHZ_SERVER_NAME
             + " TO ROLE admin_role");
     statement.execute("GRANT ROLE admin_role TO GROUP " + ADMINGROUP);
     statement.close();
     connection.close();
   }
 }
 protected void createTable(String user, String db, File dataFile, String... tables)
     throws Exception {
   Connection connection = context.createConnection(user);
   Statement statement = connection.createStatement();
   statement.execute("USE " + db);
   for (String table : tables) {
     statement.execute("DROP TABLE IF EXISTS " + table);
     statement.execute(
         "create table " + table + " (under_col int comment 'the under column', value string)");
     if (dataFile != null) {
       statement.execute(
           "load data local inpath '" + dataFile.getPath() + "' into table " + table);
       ResultSet res = statement.executeQuery("select * from " + table);
       Assert.assertTrue("Table should have data after load", res.next());
       res.close();
     }
   }
   statement.close();
   connection.close();
 }
  private static void grantPermissions(PolicyFile policyFile) throws Exception {
    Connection connection = context.createConnection(ADMIN1);
    Statement statement = context.createStatement(connection);

    // remove existing metadata
    ResultSet resultSet = statement.executeQuery("SHOW ROLES");
    while (resultSet.next()) {
      Statement statement1 = context.createStatement(connection);
      String roleName = resultSet.getString(1).trim();
      if (!roleName.equalsIgnoreCase("admin_role")) {
        LOGGER.info("Dropping role :" + roleName);
        statement1.execute("DROP ROLE " + roleName);
      }
    }

    // create roles and add privileges
    for (Map.Entry<String, Collection<String>> roleEntry :
        policyFile.getRolesToPermissions().asMap().entrySet()) {
      String roleName = roleEntry.getKey();
      if (!roleEntry.getKey().equalsIgnoreCase("admin_role")) {
        LOGGER.info("Creating role : " + roleName);
        statement.execute("CREATE ROLE " + roleName);
        for (String privilege : roleEntry.getValue()) {
          addPrivilege(roleEntry.getKey(), privilege, statement);
        }
      }
    }
    // grant roles to groups
    for (Map.Entry<String, Collection<String>> groupEntry :
        policyFile.getGroupsToRoles().asMap().entrySet()) {
      for (String roleNames : groupEntry.getValue()) {
        for (String roleName : roleNames.split(",")) {
          String sql = "GRANT ROLE " + roleName + " TO GROUP " + groupEntry.getKey();
          LOGGER.info("Granting role to group: " + sql);
          statement.execute(sql);
        }
      }
    }
  }
  protected static void clearAll(boolean clearDb) throws Exception {
    LOGGER.info("About to run clearAll");
    ResultSet resultSet;
    Connection connection = context.createConnection(ADMIN1);
    Statement statement = context.createStatement(connection);

    if (clearDb) {
      LOGGER.info("About to clear all databases and default database tables");
      resultSet = execQuery(statement, "SHOW DATABASES");
      while (resultSet.next()) {
        String db = resultSet.getString(1);
        if (!db.equalsIgnoreCase("default")) {
          try (Statement statement1 = context.createStatement(connection)) {
            exec(statement1, "DROP DATABASE IF EXISTS " + db + " CASCADE");
          } catch (Exception ex) {
            // For database and tables managed by other processes than Sentry
            // drop them might run into exception
            LOGGER.error("Exception: " + ex);
          }
        }
      }
      if (resultSet != null) {
        resultSet.close();
      }
      exec(statement, "USE default");
      resultSet = execQuery(statement, "SHOW TABLES");
      while (resultSet.next()) {
        try (Statement statement1 = context.createStatement(connection)) {
          exec(statement1, "DROP TABLE IF EXISTS " + resultSet.getString(1));
        } catch (Exception ex) {
          // For table managed by other processes than Sentry
          // drop it might run into exception
          LOGGER.error("Exception: " + ex);
        }
      }
      if (resultSet != null) {
        resultSet.close();
      }
    }

    if (useSentryService) {
      LOGGER.info("About to clear all roles");
      resultSet = execQuery(statement, "SHOW ROLES");
      while (resultSet.next()) {
        try (Statement statement1 = context.createStatement(connection)) {
          String role = resultSet.getString(1);
          if (!role.toLowerCase().contains("admin")) {
            exec(statement1, "DROP ROLE " + role);
          }
        }
      }
      if (resultSet != null) {
        resultSet.close();
      }
    }

    if (statement != null) {
      statement.close();
    }
    if (connection != null) {
      connection.close();
    }
  }