@Test
public void testFavorites() throws Exception{
Realm realm = getDefaultRealm();
Policy policy = createPolicy(realm);
T resource = createResource();
User user = createAccount();
PropertyList selectedPolicies = PropertyList.createFromArray(new int[] {policy.getResourceId()});
PolicyDatabaseFactory.getInstance().attachResourceToPolicyList(resource, selectedPolicies, getSessionInfo());
getPolicyService().grantPolicyToPrincipal(policy, user);
assertEquals("There should be no favorites", 0, SystemDatabaseFactory.getInstance().getFavorites(resource.getResourceType().getResourceTypeId(), user).size());
SystemDatabaseFactory.getInstance().addFavorite(resource.getResourceType().getResourceTypeId(), resource.getResourceId(), user.getPrincipalName());
assertEquals("There should now be one favorites", 1, SystemDatabaseFactory.getInstance().getFavorites(resource.getResourceType().getResourceTypeId(), user).size());
Favorite favorite = SystemDatabaseFactory.getInstance().getFavorite(resource.getResourceType().getResourceTypeId(), user, resource.getResourceId());
assertEquals("The favorite and resource id should match.", resource.getResourceId(), favorite.getFavoriteKey());
assertEquals("The favorite username should match the users principle name.", user.getPrincipalName(), favorite.getUsername());
assertEquals("The favorite type id and resource type id should match.", resource.getResourceType().getResourceTypeId(), favorite.getType());
SystemDatabaseFactory.getInstance().removeFavorite(resource.getResourceType().getResourceTypeId(), resource.getResourceId(), user.getPrincipalName());
assertEquals("There should be no favorites", 0, SystemDatabaseFactory.getInstance().getFavorites(resource.getResourceType().getResourceTypeId(), user).size());
getPolicyService().revokePolicyFromPrincipal(policy, user);
PolicyDatabaseFactory.getInstance().detachResourceFromPolicyList(resource, getSessionInfo());
deleteResource(resource);