@AfterClass
 public static void teardown() throws Exception {
   try {
     if (investigationClient.getQueryDebuggingWriter() != null) {
       investigationClient.getQueryDebuggingWriter().close();
     }
   } catch (Exception e) {
   }
   tbclient.logout();
   tbclient.close();
 }
 private static void verifyOwnerOnlyPolicyRules(Investigation i) throws Throwable {
   List<PolicyRule> testRules = new ArrayList<PolicyRule>();
   PolicyRule ownerRule =
       new PolicyRule(tbclient.getLoggedInUser(USER_SERVICE_ROOT), true, true, true, true);
   testRules.add(ownerRule);
   verifyPolicyRules(i, testRules);
 }
 private String getUserUrl() throws Exception {
   User user = tbclient.getLoggedInUser(USER_SERVICE_ROOT);
   if (user == null) {
     throw new RuntimeException("Logged in user could not be found in user service");
   }
   return user.getResourceURL().toString();
 }
 private static Project getProjectByGroupName(String groupName) throws Exception {
   List<Project> allProjects = tbclient.getProjectClient().get(new URL(PROJECT_SERVICE_ROOT));
   for (Project project : allProjects) {
     if (groupName.equals(project.getGroupName())) {
       return project;
     }
   }
   return null;
 }
 private static Organisation getOrganisationByGroupName(String groupName) throws Exception {
   List<Organisation> allOrgs =
       tbclient.getOrganisationClient().get(new URL(ORGANISATION_SERVICE_ROOT));
   for (Organisation org : allOrgs) {
     if (groupName.equals(org.getGroupName())) {
       return org;
     }
   }
   return null;
 }
  @BeforeClass
  public static void setup() throws Exception {
    config();

    String username = properties.getProperty(aa_user_property);
    String pass = properties.getProperty(aa_pass_property);
    // ensure maven profile properties are configured and set corgrectly
    Assert.assertNotNull(username);
    Assert.assertNotNull(pass);
    if (String.format("${%s}", aa_user_property).equals(username)
        || String.format("${%s}", aa_pass_property).equals(pass))
      throw new Exception(
          String.format(
              "The following properties are not found in the acive Maven profile ${%s} ${%s}",
              aa_user_property, aa_pass_property));
    boolean ok = tbclient.login(username, pass);
    // FileWriter fw = new FileWriter(new File("InvestigationClientTest-queries.txt"));
    investigationClient = tbclient.getInvestigationClient();
    // investigationClient.setQueryDebuggingWriter(fw);
    Assert.assertTrue(ok);
  }
  private static void verifyPolicyRules(Investigation i, List<PolicyRule> testRules)
      throws Throwable {
    List<AccessRights> rights = tbclient.readPolicy(i.getResourceURL());
    List<PolicyRule> rules = new ArrayList<PolicyRule>();
    for (AccessRights right : rights) {
      rules.addAll(right.getRules());
    }

    for (PolicyRule testRule : testRules) {
      PolicyRule matchingRule = null;
      for (PolicyRule rule : rules) {
        fillInSubject(rule);
        IToxBankResource subject = rule.getSubject();
        TestCase.assertNotNull("PolicyRule subject is null", subject.getResourceURL());
        IToxBankResource testSubject = testRule.getSubject();
        if (subject.getResourceURL().equals(testSubject.getResourceURL())) {
          TestCase.assertEquals(
              "Should have same get for " + testSubject.getResourceURL(),
              testRule.allowsGET(),
              rule.allowsGET());
          TestCase.assertEquals(
              "Should have same put for " + testSubject.getResourceURL(),
              testRule.allowsPUT(),
              rule.allowsPUT());
          TestCase.assertEquals(
              "Should have same post for " + testSubject.getResourceURL(),
              testRule.allowsPUT(),
              rule.allowsPOST());
          TestCase.assertEquals(
              "Should have same delete for " + testSubject.getResourceURL(),
              testRule.allowsPUT(),
              rule.allowsDELETE());
          matchingRule = rule;
        }
      }
      if (matchingRule == null) {
        TestCase.fail("Did not have a rule for: " + testRule.getSubject().getResourceURL());
      } else {
        rules.remove(matchingRule);
      }
    }

    if (rules.size() > 0) {
      StringBuilder sb = new StringBuilder();
      for (PolicyRule rule : rules) {
        sb.append(rule.getSubject().getResourceURL());
        sb.append("\n");
      }
      TestCase.fail("Had extraneous rules: " + rules.size() + "\n  " + sb.toString());
    }
  }
 private static void fillInSubject(PolicyRule rule) throws Throwable {
   if (rule instanceof UserPolicyRule) {
     User user = ((UserPolicyRule<User>) rule).getSubject();
     User filledInUser = tbclient.getUserByUsername(USER_SERVICE_ROOT, user.getUserName());
     if (filledInUser == null) {
       throw new RuntimeException("Could not find real user for: " + user.getUserName());
     }
     rule.setSubject(filledInUser);
   } else if (rule instanceof GroupPolicyRule) {
     Group group = ((GroupPolicyRule<? extends Group>) rule).getSubject();
     Project filledInProject = getProjectByGroupName(group.getGroupName());
     if (filledInProject != null) {
       rule.setSubject(filledInProject);
     } else {
       Organisation filledInOrg = getOrganisationByGroupName(group.getGroupName());
       if (filledInOrg != null) {
         rule.setSubject(filledInOrg);
       } else {
         throw new RuntimeException(
             "Could not find project or organisation for group name: " + group.getGroupName());
       }
     }
   }
 }