@Test
public void testAddDeleteRoleForGroup() throws Exception {

  runTestAsSubject(new TestOperation() {
    @Override
    public void runTestAsSubject() throws Exception {
      // create the role for test
      client.createRole(requestorName, TEST_ROLE_NAME_1);
      client.createRole(requestorName, TEST_ROLE_NAME_2);
      // test: add role to group with -arg
      String[] args = { "-arg", "-r", TEST_ROLE_NAME_1, "-g", "testGroup1", "-conf",
          confPath.getAbsolutePath() };
      SentryShellHive.main(args);
      // test: add role to multiple groups
      args = new String[] { "-arg", "-r", TEST_ROLE_NAME_1, "-g", "testGroup2,testGroup3",
          "-conf",
          confPath.getAbsolutePath() };
      SentryShellHive.main(args);
      // test: add role to group with --add_role_group
      args = new String[] { "--add_role_group", "-r", TEST_ROLE_NAME_2, "-g", "testGroup1",
          "-conf",
          confPath.getAbsolutePath() };
      SentryShellHive.main(args);
      // validate the result list roles with -lr and -g
      args = new String[] { "-lr", "-g", "testGroup1", "-conf", confPath.getAbsolutePath() };
      SentryShellHive sentryShell = new SentryShellHive();
      Set<String> roleNames = getShellResultWithOSRedirect(sentryShell, args, true);
      validateRoleNames(roleNames, TEST_ROLE_NAME_1, TEST_ROLE_NAME_2);
      // list roles with --list_role and -g
      args = new String[] { "--list_role", "-g", "testGroup2", "-conf",
          confPath.getAbsolutePath() };
      sentryShell = new SentryShellHive();
      roleNames = getShellResultWithOSRedirect(sentryShell, args, true);
      validateRoleNames(roleNames, TEST_ROLE_NAME_1);
      args = new String[] { "--list_role", "-g", "testGroup3", "-conf",
          confPath.getAbsolutePath() };
      sentryShell = new SentryShellHive();
      roleNames = getShellResultWithOSRedirect(sentryShell, args, true);
      validateRoleNames(roleNames, TEST_ROLE_NAME_1);
      // test: delete role from group with -drg
      args = new String[] { "-drg", "-r", TEST_ROLE_NAME_1, "-g", "testGroup1", "-conf",
          confPath.getAbsolutePath() };
      SentryShellHive.main(args);
      // test: delete role to multiple groups
      args = new String[] { "-drg", "-r", TEST_ROLE_NAME_1, "-g", "testGroup2,testGroup3",
          "-conf",
          confPath.getAbsolutePath() };
      SentryShellHive.main(args);
      // test: delete role from group with --delete_role_group
      args = new String[] { "--delete_role_group", "-r", TEST_ROLE_NAME_2, "-g", "testGroup1",
          "-conf", confPath.getAbsolutePath() };
      SentryShellHive.main(args);
      // validate the result
      Set<TSentryRole> roles = client.listRolesByGroupName(requestorName, "testGroup1");
      assertEquals("Incorrect number of roles", 0, roles.size());
      roles = client.listRolesByGroupName(requestorName, "testGroup2");
      assertEquals("Incorrect number of roles", 0, roles.size());
      roles = client.listRolesByGroupName(requestorName, "testGroup3");
      assertEquals("Incorrect number of roles", 0, roles.size());
      // clear the test data
      client.dropRole(requestorName, TEST_ROLE_NAME_1);
      client.dropRole(requestorName, TEST_ROLE_NAME_2);
    }
  });
}