import org.wso2.carbon.governance.api.policies.dataobjects.Policy;
import org.wso2.carbon.governance.api.test.utils.BaseTestCase;
public class PolicyTest extends BaseTestCase {
public void testAddPolicy() throws Exception {
PolicyManager policyManager = new PolicyManager(registry);
Policy policy = policyManager.newPolicy("http://svn.wso2.org/repos/wso2/trunk/carbon/components/governance/org.wso2.carbon.governance.api/src/test/resources/test-resources/policy/policy.xml");
policy.addAttribute("creator", "it is me");
policy.addAttribute("version", "0.01");
policyManager.addPolicy(policy);
Policy newPolicy = policyManager.getPolicy(policy.getId());
assertEquals(policy.getPolicyContent(), newPolicy.getPolicyContent());
assertEquals("it is me", newPolicy.getAttribute("creator"));
assertEquals("0.01", newPolicy.getAttribute("version"));
// change the target namespace and check
String oldPolicyPath = newPolicy.getPath();
assertEquals(oldPolicyPath, "/policies/policy.xml");
assertTrue(registry.resourceExists("/policies/policy.xml"));
newPolicy.setName("my-policy.xml");
policyManager.updatePolicy(newPolicy);
assertEquals("/policies/my-policy.xml", newPolicy.getPath());
assertFalse(registry.resourceExists("/policies/policy.xml"));
// doing an update without changing anything.
policyManager.updatePolicy(newPolicy);
assertEquals("/policies/my-policy.xml", newPolicy.getPath());
assertEquals("0.01", newPolicy.getAttribute("version"));
newPolicy = policyManager.getPolicy(policy.getId());
assertEquals("it is me", newPolicy.getAttribute("creator"));
assertEquals("0.01", newPolicy.getAttribute("version"));
Policy[] policies = policyManager.findPolicies(new PolicyFilter() {
public boolean matches(Policy policy) throws GovernanceException {
if (policy.getAttribute("version").equals("0.01")) {
return true;
}
return false;
}
});
assertEquals(1, policies.length);
assertEquals(newPolicy.getId(), policies[0].getId());
// deleting the policy
policyManager.removePolicy(newPolicy.getId());
Policy deletedPolicy = policyManager.getPolicy(newPolicy.getId());
assertNull(deletedPolicy);
}