Search in sources :

Example 26 with Condition

use of org.wso2.carbon.apimgt.core.models.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)

Example 27 with Condition

use of org.wso2.carbon.apimgt.core.models.policy.Condition in project carbon-apimgt by wso2.

the class CommonThrottleMappingUtilTestCase method fromQueryParamsConditionDtoToQueryParamsConditionModelTest.

@Test(description = "Convert Query param Condition DTO to QueryParamCondition Model object")
public void fromQueryParamsConditionDtoToQueryParamsConditionModelTest() throws Exception {
    ThrottleConditionDTO throttleConditionDTO = new ThrottleConditionDTO();
    throttleConditionDTO.setType(PolicyConstants.QUERY_PARAMS_CONDITION_TYPE);
    QueryParameterConditionDTO queryParameterConditionDTO = new QueryParameterConditionDTO();
    queryParameterConditionDTO.setParameterName("testParam");
    queryParameterConditionDTO.setParameterValue("testParamValue");
    throttleConditionDTO.setQueryParameterCondition(queryParameterConditionDTO);
    QueryParameterCondition condition = (QueryParameterCondition) CommonThrottleMappingUtil.fromDTOToCondition(throttleConditionDTO);
    Assert.assertNotNull(condition);
    assertEquals(condition.getParameter(), "testParam");
    assertEquals(condition.getValue(), "testParamValue");
}
Also used : QueryParameterCondition(org.wso2.carbon.apimgt.core.models.policy.QueryParameterCondition) Test(org.testng.annotations.Test)

Example 28 with Condition

use of org.wso2.carbon.apimgt.core.models.policy.Condition in project carbon-apimgt by wso2.

the class CommonThrottleMappingUtilTestCase method fromHeaderConditionDtoToHeaderConditionModelTest.

@Test(description = "Convert Header Condition DTO to HeaderCondition Model object")
public void fromHeaderConditionDtoToHeaderConditionModelTest() throws Exception {
    ThrottleConditionDTO throttleConditionDTO = new ThrottleConditionDTO();
    throttleConditionDTO.setType(PolicyConstants.HEADER_CONDITION_TYPE);
    HeaderConditionDTO headerConditionDTO = new HeaderConditionDTO();
    headerConditionDTO.setHeaderName("testHeader");
    headerConditionDTO.setHeaderValue("testHeaderValue");
    throttleConditionDTO.setHeaderCondition(headerConditionDTO);
    HeaderCondition condition = (HeaderCondition) CommonThrottleMappingUtil.fromDTOToCondition(throttleConditionDTO);
    Assert.assertNotNull(condition);
    assertEquals(condition.getHeaderName(), "testHeader");
    assertEquals(condition.getValue(), "testHeaderValue");
}
Also used : HeaderCondition(org.wso2.carbon.apimgt.core.models.policy.HeaderCondition) Test(org.testng.annotations.Test)

Example 29 with Condition

use of org.wso2.carbon.apimgt.core.models.policy.Condition in project carbon-apimgt by wso2.

the class CommonThrottleMappingUtilTestCase method fromIPRangeConditionDtoToIPConditionModelTest.

@Test(description = "Convert IP range IPCondition DTO to IPCondition Model object")
public void fromIPRangeConditionDtoToIPConditionModelTest() throws Exception {
    ThrottleConditionDTO throttleConditionDTO = new ThrottleConditionDTO();
    throttleConditionDTO.setType(PolicyConstants.IP_CONDITION_TYPE);
    IPConditionDTO ipConditionDTO = new IPConditionDTO();
    ipConditionDTO.setIpConditionType(IP_RANGE_TYPE);
    ipConditionDTO.setStartingIP("10.100.0.158");
    ipConditionDTO.setEndingIP("10.100.0.178");
    throttleConditionDTO.setIpCondition(ipConditionDTO);
    IPCondition condition = (IPCondition) CommonThrottleMappingUtil.fromDTOToCondition(throttleConditionDTO);
    Assert.assertNotNull(condition);
    assertEquals(condition.getStartingIP(), "10.100.0.158");
    assertEquals(condition.getEndingIP(), "10.100.0.178");
    assertEquals(condition.getType(), IP_RANGE_TYPE);
}
Also used : IPCondition(org.wso2.carbon.apimgt.core.models.policy.IPCondition) Test(org.testng.annotations.Test)

Example 30 with Condition

use of org.wso2.carbon.apimgt.core.models.policy.Condition in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImplTestCase method testDeleteBlockConditionByUuid.

@Test(description = "Test deleting block condition by uuid")
public void testDeleteBlockConditionByUuid() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIGateway apiGateway = Mockito.mock(APIGateway.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO, apiGateway);
    BlockConditions blockConditions = SampleTestObjectCreator.createDefaultBlockCondition(BLOCK_CONDITION_TYPE);
    Mockito.when(policyDAO.deleteBlockConditionByUuid(blockConditions.getUuid())).thenReturn(true);
    Boolean statusTrue = adminService.deleteBlockConditionByUuid(blockConditions.getUuid());
    Assert.assertTrue(statusTrue);
    // Error path
    // Failure deleting
    Mockito.when(policyDAO.deleteBlockConditionByUuid(blockConditions.getUuid())).thenReturn(false);
    Boolean statusFalse = adminService.deleteBlockConditionByUuid(blockConditions.getUuid());
    Assert.assertFalse(statusFalse);
    // Error path
    // APIMgtDAOException
    Mockito.when(policyDAO.deleteBlockConditionByUuid(blockConditions.getUuid())).thenThrow(APIMgtDAOException.class);
    try {
        adminService.deleteBlockConditionByUuid(blockConditions.getUuid());
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Couldn't delete block condition with UUID: " + blockConditions.getUuid());
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) 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