Search in sources :

Example 21 with Condition

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));
}
Also used : BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 22 with Condition

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");
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions) UUID(java.util.UUID) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 23 with Condition

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());
}
Also used : BlockingConditionListDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.BlockingConditionListDTO) BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 24 with Condition

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");
}
Also used : BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions) BlockingConditionDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.BlockingConditionDTO) Test(org.testng.annotations.Test)

Example 25 with Condition

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);
}
Also used : BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions) BlockingConditionDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.BlockingConditionDTO) Test(org.testng.annotations.Test)

Aggregations

BlockConditions (org.wso2.carbon.apimgt.core.models.BlockConditions)18 Test (org.testng.annotations.Test)16 PreparedStatement (java.sql.PreparedStatement)12 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)11 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)10 ResultSet (java.sql.ResultSet)9 IPCondition (org.wso2.carbon.apimgt.core.models.policy.IPCondition)9 HeaderCondition (org.wso2.carbon.apimgt.core.models.policy.HeaderCondition)8 JWTClaimsCondition (org.wso2.carbon.apimgt.core.models.policy.JWTClaimsCondition)8 QueryParameterCondition (org.wso2.carbon.apimgt.core.models.policy.QueryParameterCondition)8 ArrayList (java.util.ArrayList)7 Condition (org.wso2.carbon.apimgt.core.models.policy.Condition)7 Pipeline (org.wso2.carbon.apimgt.core.models.policy.Pipeline)7 Connection (java.sql.Connection)6 SQLException (java.sql.SQLException)6 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)6 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)6 BlockingConditionDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.BlockingConditionDTO)5 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)4 BandwidthLimit (org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)4