use of org.wso2.carbon.apimgt.core.dao.PolicyDAO in project carbon-apimgt by wso2.
the class PolicyDAOImplIT method testAddGetAndDeleteApiPolicy.
@Test(description = "Add, Get and Delete an API policy")
public void testAddGetAndDeleteApiPolicy() throws Exception {
APIPolicy policy = SampleTestObjectCreator.createDefaultAPIPolicy();
PolicyDAO policyDAO = DAOFactory.getPolicyDAO();
// add policy
policyDAO.addApiPolicy(policy);
// get added policy
Policy addedPolicy = policyDAO.getApiPolicy(policy.getPolicyName());
Assert.assertNotNull(addedPolicy);
Assert.assertEquals(addedPolicy.getPolicyName(), policy.getPolicyName());
// delete policy
policyDAO.deletePolicy(APIMgtAdminService.PolicyLevel.api, policy.getPolicyName());
// get policy after deletion
try {
policyDAO.getApiPolicy(policy.getPolicyName());
Assert.fail("Exception expected, but not thrown.");
} catch (APIMgtDAOException ex) {
Assert.assertEquals(ex.getMessage(), "API Policy not found for name: " + addedPolicy.getPolicyName());
}
try {
policyDAO.getApiPolicyByUuid(policy.getUuid());
Assert.fail("Exception expected, but not thrown.");
} catch (APIMgtDAOException ex) {
Assert.assertEquals(ex.getMessage(), "API Policy not found for id: " + addedPolicy.getUuid());
}
}
use of org.wso2.carbon.apimgt.core.dao.PolicyDAO in project carbon-apimgt by wso2.
the class PolicyDAOImplIT method testFingerprintAfterUpdatingSubscriptionPolicy.
@Test
public void testFingerprintAfterUpdatingSubscriptionPolicy() throws Exception {
SubscriptionPolicy policy = SampleTestObjectCreator.createDefaultSubscriptionPolicy();
PolicyDAO policyDAO = DAOFactory.getPolicyDAO();
policyDAO.addSubscriptionPolicy(policy);
String fingerprintBeforeUpdatingPolicy = ETagUtils.generateETag(policyDAO.getLastUpdatedTimeOfThrottlingPolicy(APIMgtAdminService.PolicyLevel.subscription, policy.getPolicyName()));
Assert.assertNotNull(fingerprintBeforeUpdatingPolicy);
SubscriptionPolicy updatedPolicy = SampleTestObjectCreator.updateSubscriptionPolicy(policy);
Thread.sleep(100L);
policyDAO.updateSubscriptionPolicy(updatedPolicy);
String fingerprintAfterUpdatingPolicy = ETagUtils.generateETag(policyDAO.getLastUpdatedTimeOfThrottlingPolicy(APIMgtAdminService.PolicyLevel.subscription, updatedPolicy.getPolicyName()));
Assert.assertNotNull(fingerprintAfterUpdatingPolicy);
Assert.assertNotEquals(fingerprintBeforeUpdatingPolicy, fingerprintAfterUpdatingPolicy, TestUtil.printDiff(fingerprintBeforeUpdatingPolicy, fingerprintAfterUpdatingPolicy));
}
use of org.wso2.carbon.apimgt.core.dao.PolicyDAO in project carbon-apimgt by wso2.
the class PolicyDAOImplIT method testAddGetUpdateDeleteBlockConditions.
@Test(description = "Add, Get, Delete block condition")
public void testAddGetUpdateDeleteBlockConditions() throws Exception {
PolicyDAO policyDAO = DAOFactory.getPolicyDAO();
BlockConditions blockConditionsIP = SampleTestObjectCreator.createDefaultBlockCondition(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_IP);
BlockConditions blockConditionsIpRange = SampleTestObjectCreator.createDefaultBlockCondition(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE);
BlockConditions blockConditionsApi = SampleTestObjectCreator.createDefaultBlockCondition(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_API);
BlockConditions blockConditionsApp = SampleTestObjectCreator.createDefaultBlockCondition(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_APPLICATION);
BlockConditions blockConditionsUser = SampleTestObjectCreator.createDefaultBlockCondition(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_USER);
String uuidIp = policyDAO.addBlockConditions(blockConditionsIP);
String uuidIpRange = policyDAO.addBlockConditions(blockConditionsIpRange);
String uuidApi = policyDAO.addBlockConditions(blockConditionsApi);
String uuidApp = policyDAO.addBlockConditions(blockConditionsApp);
String uuidUser = policyDAO.addBlockConditions(blockConditionsUser);
BlockConditions blockConditionsAddedIP = policyDAO.getBlockConditionByUUID(uuidIp);
BlockConditions blockConditionsAddedIpRange = policyDAO.getBlockConditionByUUID(uuidIpRange);
BlockConditions blockConditionsAddedApi = policyDAO.getBlockConditionByUUID(uuidApi);
BlockConditions blockConditionsAddedApp = policyDAO.getBlockConditionByUUID(uuidApp);
BlockConditions blockConditionsAddedUser = policyDAO.getBlockConditionByUUID(uuidUser);
Assert.assertEquals(blockConditionsIP.getConditionValue(), blockConditionsAddedIP.getConditionValue());
Assert.assertEquals(blockConditionsApi.getConditionValue(), blockConditionsAddedApi.getConditionValue());
Assert.assertEquals(blockConditionsApp.getConditionValue(), blockConditionsAddedApp.getConditionValue());
Assert.assertEquals(blockConditionsUser.getConditionValue(), blockConditionsAddedUser.getConditionValue());
Assert.assertEquals(blockConditionsIpRange.getStartingIP(), blockConditionsAddedIpRange.getStartingIP());
Assert.assertTrue(policyDAO.updateBlockConditionStateByUUID(uuidIp, true));
Assert.assertTrue(policyDAO.getBlockConditionByUUID(uuidIp).isEnabled());
Assert.assertTrue(policyDAO.getBlockConditions().size() == 5);
Assert.assertTrue(policyDAO.deleteBlockConditionByUuid(uuidIp));
Assert.assertTrue(policyDAO.deleteBlockConditionByUuid(uuidIpRange));
Assert.assertTrue(policyDAO.deleteBlockConditionByUuid(uuidApi));
Assert.assertTrue(policyDAO.deleteBlockConditionByUuid(uuidApp));
Assert.assertTrue(policyDAO.deleteBlockConditionByUuid(uuidUser));
}
use of org.wso2.carbon.apimgt.core.dao.PolicyDAO in project carbon-apimgt by wso2.
the class PolicyDAOImplIT method testGetAllPolicies.
@Test
public void testGetAllPolicies() throws Exception {
PolicyDAO policyDAO = DAOFactory.getPolicyDAO();
int size = policyDAO.getAllPolicies().size();
APIPolicy apiPolicy = SampleTestObjectCreator.createDefaultAPIPolicy();
ApplicationPolicy applicationPolicy = SampleTestObjectCreator.createDefaultApplicationPolicy();
SubscriptionPolicy subscriptionPolicy = SampleTestObjectCreator.createDefaultSubscriptionPolicy();
policyDAO.addApiPolicy(apiPolicy);
policyDAO.addApplicationPolicy(applicationPolicy);
policyDAO.addSubscriptionPolicy(subscriptionPolicy);
Set<PolicyValidationData> policyValidationData = policyDAO.getAllPolicies();
Assert.assertTrue(policyValidationData.size() == size + 3);
}
use of org.wso2.carbon.apimgt.core.dao.PolicyDAO in project carbon-apimgt by wso2.
the class PolicyDAOImplIT method testValidityOfBlockCondition.
@Test
public void testValidityOfBlockCondition() throws Exception {
PolicyDAO policyDAO = DAOFactory.getPolicyDAO();
BlockConditions blockConditionIPRange = SampleTestObjectCreator.createDefaultBlockCondition(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE);
BlockConditions blockConditionAPI = SampleTestObjectCreator.createDefaultBlockCondition(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_API);
BlockConditions blockConditionsApp = SampleTestObjectCreator.createDefaultBlockCondition(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_APPLICATION);
// Making starting IP > ending IP
blockConditionIPRange.setStartingIP("12.34.13.12");
blockConditionIPRange.setEndingIP("10.32.44.32");
// Giving invalid API context
blockConditionAPI.setConditionValue("invalid");
// giving invalid app name and invalid UUID
String[] appArray = blockConditionsApp.getConditionValue().split(":");
UUID appUuid = UUID.randomUUID();
String appName = appArray[1];
blockConditionsApp.setConditionValue(appUuid + ":" + appName);
try {
policyDAO.addBlockConditions(blockConditionIPRange);
} catch (APIMgtDAOException ex) {
Assert.assertEquals(ex.getMessage(), "Couldn't Save Block Condition Due to Invalid IP Range -> Starting IP : " + blockConditionIPRange.getStartingIP() + " EndingIP : " + blockConditionIPRange.getEndingIP());
}
try {
policyDAO.addBlockConditions(blockConditionAPI);
} catch (APIMgtDAOException ex) {
Assert.assertEquals(ex.getMessage(), "Couldn't Save Block Condition Due to Invalid API Context : " + blockConditionAPI.getConditionValue());
}
try {
policyDAO.addBlockConditions(blockConditionsApp);
} catch (APIMgtDAOException ex) {
Assert.assertEquals(ex.getMessage(), "Couldn't Save Block Condition Due to Invalid Application : " + appName + ", UUID :" + appUuid);
}
// Making IP block condition valid and add twice to check if it shows already existing
blockConditionIPRange.setEndingIP("29.23.12.12");
policyDAO.addBlockConditions(blockConditionIPRange);
try {
policyDAO.addBlockConditions(blockConditionIPRange);
} catch (APIMgtDAOException ex) {
Assert.assertEquals(ex.getMessage(), "Condition with type: " + blockConditionIPRange.getConditionType() + ", value: " + blockConditionIPRange.getConditionValue() + " already exists");
}
}
Aggregations