use of org.wso2.carbon.apimgt.core.models.policy.Condition 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.models.policy.Condition 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");
}
}
use of org.wso2.carbon.apimgt.core.models.policy.Condition in project carbon-apimgt by wso2.
the class BlockingConditionMappingUtilTestCase method fromBlockConditionListToListDTODTOTest.
@Test(description = "From Blocking Condition List to List DTO")
public void fromBlockConditionListToListDTODTOTest() throws Exception {
List<BlockConditions> blockConditionList = new ArrayList<>();
BlockConditions conditions1 = new BlockConditions();
conditions1.setUuid(UUID.randomUUID().toString());
conditions1.setConditionType(BLOCKING_CONDITION_IP_RANGE);
conditions1.setStartingIP("12.23.45.3");
conditions1.setEndingIP("23.45.2.1");
BlockConditions conditions2 = new BlockConditions();
conditions2.setUuid(UUID.randomUUID().toString());
conditions2.setConditionType("API");
conditions2.setConditionValue("DummyAPI");
blockConditionList.add(conditions1);
blockConditionList.add(conditions2);
BlockingConditionListDTO dto = BlockingConditionMappingUtil.fromBlockConditionListToListDTO(blockConditionList);
Assert.assertNotNull(dto);
Assert.assertEquals(dto.getCount(), (Integer) blockConditionList.size());
Assert.assertEquals(dto.getList().get(0).getIpCondition().getStartingIP(), conditions1.getStartingIP());
Assert.assertEquals(dto.getList().get(0).getIpCondition().getEndingIP(), conditions1.getEndingIP());
Assert.assertEquals(dto.getList().get(0).getConditionType(), conditions1.getConditionType());
Assert.assertEquals(dto.getList().get(0).getConditionId(), conditions1.getUuid());
Assert.assertEquals(dto.getList().get(1).getConditionType(), conditions2.getConditionType());
Assert.assertEquals(dto.getList().get(1).getConditionId(), conditions2.getUuid());
}
use of org.wso2.carbon.apimgt.core.models.policy.Condition in project carbon-apimgt by wso2.
the class BlockingConditionMappingUtilTestCase method fromBlockConditionToIpConditionDTOTest.
@Test(description = "From Blocking Condition to DTO")
public void fromBlockConditionToIpConditionDTOTest() throws Exception {
BlockConditions conditions = new BlockConditions();
conditions.setUuid(UUID.randomUUID().toString());
conditions.setConditionType(BLOCKING_CONDITION_IP_RANGE);
conditions.setStartingIP("12.23.45.3");
conditions.setEndingIP("23.45.2.1");
BlockingConditionDTO dto = BlockingConditionMappingUtil.fromBlockingConditionToDTO(conditions);
Assert.assertNotNull(dto);
Assert.assertEquals(dto.getConditionType(), BLOCKING_CONDITION_IP_RANGE);
Assert.assertEquals(dto.getIpCondition().getStartingIP(), "12.23.45.3");
Assert.assertEquals(dto.getIpCondition().getEndingIP(), "23.45.2.1");
}
use of org.wso2.carbon.apimgt.core.models.policy.Condition in project carbon-apimgt by wso2.
the class BlockingConditionMappingUtilTestCase method fromBlockingConditionToDTOTest.
@Test(description = "Convert Blocking Condition to DTO")
public void fromBlockingConditionToDTOTest() throws Exception {
BlockConditions conditions = new BlockConditions();
String uuid = UUID.randomUUID().toString();
conditions.setUuid(uuid);
conditions.setConditionType(BLOCKING_CONDITION_IP_RANGE);
BlockingConditionDTO dto = BlockingConditionMappingUtil.fromBlockingConditionToDTO(conditions);
Assert.assertNotNull(dto);
Assert.assertEquals(uuid, dto.getConditionId());
Assert.assertEquals(dto.getConditionType(), BLOCKING_CONDITION_IP_RANGE);
}
Aggregations