Search in sources :

Example 31 with Condition

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");
    }
}
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 32 with Condition

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());
}
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 33 with Condition

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");
}
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 34 with Condition

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);
}
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 35 with Condition

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

Aggregations

HashMap (java.util.HashMap)39 Test (org.junit.Test)32 Test (org.testng.annotations.Test)31 ArrayList (java.util.ArrayList)30 List (java.util.List)26 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)26 ConditionDto (org.wso2.carbon.apimgt.impl.dto.ConditionDto)26 MessageContext (org.apache.synapse.MessageContext)25 PreparedStatement (java.sql.PreparedStatement)23 Map (java.util.Map)22 ResultSet (java.sql.ResultSet)20 BlockConditions (org.wso2.carbon.apimgt.core.models.BlockConditions)18 ThrottleProperties (org.wso2.carbon.apimgt.impl.dto.ThrottleProperties)18 Connection (java.sql.Connection)16 SQLException (java.sql.SQLException)16 TreeMap (java.util.TreeMap)16 HeaderCondition (org.wso2.carbon.apimgt.api.model.policy.HeaderCondition)15 JWTClaimsCondition (org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition)15 QueryParameterCondition (org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition)15 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)15