use of org.wso2.carbon.apimgt.api.model.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.api.model.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.api.model.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.api.model.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);
}
use of org.wso2.carbon.apimgt.api.model.policy.Condition in project carbon-apimgt by wso2.
the class BlockingConditionMappingUtilTestCase method fromBlockingConditionDTOToBlockConditionTest.
@Test(description = "From Blocking Condition DTO to Model")
public void fromBlockingConditionDTOToBlockConditionTest() throws Exception {
BlockingConditionDTO dto = new BlockingConditionDTO();
dto.setConditionId(UUID.randomUUID().toString());
dto.setConditionType(BLOCKING_CONDITION_IP_RANGE);
IPConditionDTO ipCondition = new IPConditionDTO();
ipCondition.setStartingIP("12.32.45.3");
ipCondition.setEndingIP("12.32.45.31");
dto.setIpCondition(ipCondition);
dto.setStatus(true);
BlockConditions conditions = BlockingConditionMappingUtil.fromBlockingConditionDTOToBlockCondition(dto);
Assert.assertNotNull(conditions);
Assert.assertEquals(BLOCKING_CONDITION_IP_RANGE, conditions.getConditionType());
Assert.assertEquals(conditions.getStartingIP(), dto.getIpCondition().getStartingIP());
Assert.assertEquals(conditions.getEndingIP(), dto.getIpCondition().getEndingIP());
}
Aggregations