AccessInformationUtil { public static JSONObject serializePrivilegesByRole(AccessControlList acl) { if (acl == null) throw new IllegalArgumentException("The parameter trans must not be null"); Map<String, JSONObject> privilegesByRole = new HashMap<String, JSONObject>(); for (AccessControlEntry entry : acl.getEntries()) { JSONObject rolePrivileges; if (privilegesByRole.containsKey(entry.getRole())) { rolePrivileges = privilegesByRole.get(entry.getRole()); } else { rolePrivileges = new JSONObject(); privilegesByRole.put(entry.getRole(), rolePrivileges); } try { rolePrivileges.put(entry.getAction(), entry.isAllow()); } catch (JSONException e) { logger.error("An unexpected error occured:", e); } } JSONObject privilegesJson = new JSONObject(); for (Entry<String, JSONObject> privilege : privilegesByRole.entrySet()) { try { privilegesJson.put(privilege.getKey(), privilege.getValue()); } catch (JSONException e) { logger.error("An unexpected error occured:", e); } } return privilegesJson; } private  AccessInformationUtil(); static JSONObject serializeManagedAcl(ManagedAcl managedAcl); static JSONObject serializePrivilegesByRole(AccessControlList acl); static Option<ManagedAcl> matchAcls(List<ManagedAcl> acls, final AccessControlList acl);  }
@Test public void testSerializePrivilegesByRole() throws Exception { AccessControlList acl = new AccessControlList(); acl.getEntries().add(ACE_ROLE_ADMIN_ALLOW_ACTION_READ); acl.getEntries().add(ACE_ROLE_ADMIN_ALLOW_ACTION_WRITE); acl.getEntries().add(ACE_ROLE_STUDENT_ALLOW_ACTION_READ); acl.getEntries().add(ACE_ROLE_STUDENT_DISALLOW_ACTION_WRITE); JSONObject privilegesByRole = AccessInformationUtil.serializePrivilegesByRole(acl); assertTrue(privilegesByRole.getJSONObject(ROLE_ADMIN).getBoolean(ACTION_READ)); assertTrue(privilegesByRole.getJSONObject(ROLE_ADMIN).getBoolean(ACTION_WRITE)); assertTrue(privilegesByRole.getJSONObject(ROLE_STUDENT).getBoolean(ACTION_READ)); assertFalse(privilegesByRole.getJSONObject(ROLE_STUDENT).getBoolean(ACTION_WRITE)); }
@Test(expected = IllegalArgumentException.class) public void testSerializePrivilegesByRoleWithNull() throws Exception { AccessInformationUtil.serializePrivilegesByRole(null); }