@Test
public void testAddPolicyAndUpdateApiAuthThenRemovePolicy() {
// Add a new API
Api api = new Api();
api.setId("id1");
api.setApiDetail(new ApiDetail());
dataManager.addApi(api);
// Add a new Auth
Auth auth = new Auth();
auth.setId("id2");
auth.setStatus(StatusType.ACTIVE);
auth.setAuthDetail(new AuthDetail());
auth.getAuthDetail().setType(NBAuthType.AUTHKEY);
auth.getAuthDetail().setAuthKeyValue("authKey10");
dataManager.addAuth(auth);
// Add a new Policy
Policy policy = new Policy();
policy.setId("id3");
policy.getApiIds().add(api.getId());
dataManager.addPolicy(policy);
// Create bucket
QuotaRLBucket authIds = new QuotaRLBucket();
authIds.setId("id3");
authIds.getAuthIds().add(auth.getId());
dataManager.createBucket(policy.getId(), authIds);
// Now, get the API and check that it's OK
Api api2 = dataManager.getApiById("id1");
assertEquals("API has new policy", policy.getId(), api2.getPolicyIds().get(0));
// Now, get the Auth and check that it's OK
Auth auth2 = dataManager.getAuthById("id2");
assertEquals("Auth has new policy", policy.getId(), auth2.getPolicyContexts().get(0).getPolicyId());
// Check that bucket is added correctly to policy
policy = dataManager.getPolicyById(policy.getId());
assertEquals("Correct number of buckets for policy", 1, policy.getAuthIds().size());
assertEquals("Policy has new bucket", authIds.getId(), policy.getAuthIds().get(0).getId());
dataManager.removePolicy(policy.getId());
// Now, get the API and check that it's OK
Api api3 = dataManager.getApiById("id1");
assertEquals("API has no policy", 0, api3.getPolicyIds().size());
// Now, get the API and check that it's OK
Auth auth3 = dataManager.getAuthById("id2");
assertEquals("Auth has no policy", 0, auth3.getPolicyContexts().size());