policy.setId("policyId1");
policy.getApiIds().add(api.getId());
dataManager.addPolicy(policy);
// Create bucket to add new Auth
QuotaRLBucket authIds = new QuotaRLBucket();
authIds.setId("bucketId1");
authIds.getAuthIds().add(auth.getId());
dataManager.createBucket(policy.getId(), authIds);
// fetch policy from the cache
policy = dataManager.getPolicyById(policy.getId());
// Now, get the API and check that it's OK
Api gApi1 = dataManager.getApiById(api.getId());
assertEquals("API has new policy", policy.getId(), gApi1.getPolicyIds().get(0));
// Now, get the Auth and check that it's OK
Auth gAuth1 = dataManager.getAuthById(auth.getId());
assertEquals("Auth has new policy", policy.getId(), gAuth1.getPolicyContexts().get(0).getPolicyId());
// Check that bucket is added correctly to policy
assertEquals("Correct number of buckets for policy", 1, policy.getAuthIds().size());
assertEquals("Policy has new bucket", authIds.getId(), policy.getAuthIds().get(0).getId());
// Append a new Auth to the bucket
QuotaRLBucket authIdsToAppend = new QuotaRLBucket();
authIdsToAppend.getAuthIds().add(auth2.getId());
dataManager.addAuthsToBucket(policy.getId(), authIds.getId(), authIdsToAppend);
// fetch policy from the cache
policy = dataManager.getPolicyById(policy.getId());
// Check that auth2 has been appended to original bucket
Auth gAuth2 = dataManager.getAuthById(auth2.getId());
assertEquals("Auth has new policy", policy.getId(), gAuth2.getPolicyContexts().get(0).getPolicyId());
assertEquals("Correct number of buckets for policy", 1, policy.getAuthIds().size());
assertEquals("Correct number of auths in the bucket", 2, policy.getAuthIds().get(0).getAuthIds().size());
// Retry appending the same auth to the same bucket
dataManager.addAuthsToBucket(policy.getId(), authIds.getId(), authIdsToAppend);
// fetch policy from the cache
policy = dataManager.getPolicyById(policy.getId());
// Check that auth hasn't been added twice
assertEquals("Correct number of auths in the bucket", 2, policy.getAuthIds().get(0).getAuthIds().size());
// Create a new bucket containing a third auth
QuotaRLBucket authIds2 = new QuotaRLBucket();
authIds2.setId("bucketId2");
authIds2.getAuthIds().add(auth3.getId());
dataManager.createBucket(policy.getId(), authIds2);
// fetch policy from the cache
policy = dataManager.getPolicyById(policy.getId());
// Check that bucket has been added correctly
assertEquals("Correct number of buckets for policy", 2, policy.getAuthIds().size());
// Just to be sure that order is maintained
QuotaRLBucket bucket1, bucket2;
if(policy.getAuthIds().get(0).getId().equals(authIds.getId())) {
bucket1 = policy.getAuthIds().get(0);
bucket2 = policy.getAuthIds().get(1);
} else {
bucket1 = policy.getAuthIds().get(1);
bucket2 = policy.getAuthIds().get(0);
}
assertEquals("Correct number of auths in the bucket 1", 2, bucket1.getAuthIds().size());
assertEquals("Correct number of auths in the bucket 2", 1, bucket2.getAuthIds().size());
// removing auth3 from bucket2
dataManager.removeAuthFromBucket(policy.getId(), bucket2.getId(), auth3.getId());
// fetch policy from the cache
policy = dataManager.getPolicyById(policy.getId());
assertEquals("Correct number of buckets for policy", 2, policy.getAuthIds().size());
assertEquals("Correct number of auths in the bucket 1", 2, bucket1.getAuthIds().size());
assertEquals("Correct number of auths in the bucket 2", 0, bucket2.getAuthIds().size());
// Now, get auth3 and check that it's no more associated with the policy
Auth gAuth3_2 = dataManager.getAuthById(auth3.getId());
assertEquals("Auth has no policy", 0, gAuth3_2.getPolicyContexts().size());