Search in sources :

Example 21 with BlockConditions

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

the class MappingUtil method fromBlockConditionListToListDTO.

/**
 * Converts a List of Block Condition in to REST API LIST DTO Object.
 *
 * @param blockConditionList A List of Block Conditions
 * @return REST API List DTO object derived from Block Condition list
 */
public static BlockingConditionListDTO fromBlockConditionListToListDTO(List<BlockConditions> blockConditionList) {
    BlockingConditionListDTO listDTO = new BlockingConditionListDTO();
    List<BlockingConditionDTO> blockingConditionDTOList = new ArrayList<>();
    if (blockConditionList != null) {
        for (BlockConditions blockCondition : blockConditionList) {
            BlockingConditionDTO dto = fromBlockingConditionToDTO(blockCondition);
            blockingConditionDTOList.add(dto);
        }
    }
    listDTO.setCount(blockingConditionDTOList.size());
    listDTO.setList(blockingConditionDTOList);
    return listDTO;
}
Also used : BlockingConditionListDTO(org.wso2.carbon.apimgt.rest.api.core.dto.BlockingConditionListDTO) BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions) ArrayList(java.util.ArrayList) BlockingConditionDTO(org.wso2.carbon.apimgt.rest.api.core.dto.BlockingConditionDTO)

Example 22 with BlockConditions

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

the class MappingUtil method fromBlockingConditionToDTO.

/**
 * Converts a single Block Condition model object into REST API DTO object.
 *
 * @param blockCondition Block condition model object
 * @return Block condition DTO object derived from block condition model object
 */
public static BlockingConditionDTO fromBlockingConditionToDTO(BlockConditions blockCondition) {
    if (blockCondition.getUuid() == null) {
        return null;
    }
    BlockingConditionDTO dto = new BlockingConditionDTO();
    dto.setUuid(blockCondition.getUuid());
    dto.setConditionType(blockCondition.getConditionType());
    dto.setEnabled(blockCondition.isEnabled());
    if (blockCondition.getConditionType().equals(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE)) {
        dto.setStartingIP(APIUtils.ipToLong(blockCondition.getStartingIP()));
        dto.setEndingIP(APIUtils.ipToLong(blockCondition.getEndingIP()));
    }
    String conditionValue = blockCondition.getConditionValue();
    if (APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_IP.equals(blockCondition.getConditionType())) {
        dto.setFixedIp(APIUtils.ipToLong(conditionValue));
    }
    dto.setConditionValue(conditionValue);
    return dto;
}
Also used : BlockingConditionDTO(org.wso2.carbon.apimgt.rest.api.core.dto.BlockingConditionDTO)

Example 23 with BlockConditions

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

the class BlacklistApiServiceImplTestCase method blacklistGetTestCase.

@Test
public void blacklistGetTestCase() throws Exception {
    APIMgtAdminServiceImpl adminService = Mockito.mock(APIMgtAdminServiceImpl.class);
    APIManagerFactory instance = Mockito.mock(APIManagerFactory.class);
    PowerMockito.mockStatic(APIManagerFactory.class);
    PowerMockito.when(APIManagerFactory.getInstance()).thenReturn(instance);
    Mockito.when(instance.getAPIMgtAdminService()).thenReturn(adminService);
    BlacklistApiServiceImpl blacklistApiService = new BlacklistApiServiceImpl();
    BlockConditions blockConditionOne = SampleTestObjectCreator.createUniqueBlockConditions("IP_RANGE");
    BlockConditions blockConditionTwo = SampleTestObjectCreator.createUniqueBlockConditions("IP");
    BlockConditions blockConditionThree = SampleTestObjectCreator.createUniqueBlockConditions("IP_RANGE");
    List<BlockConditions> blockConditions = new ArrayList<>();
    blockConditions.add(blockConditionOne);
    blockConditions.add(blockConditionTwo);
    blockConditions.add(blockConditionThree);
    Mockito.when(adminService.getBlockConditions()).thenReturn(blockConditions);
    Response response = blacklistApiService.blacklistGet(null, getRequest());
    Assert.assertEquals(response.getStatus(), 200);
    Assert.assertEquals(((BlockingConditionListDTO) response.getEntity()).getList().size(), 3);
}
Also used : Response(javax.ws.rs.core.Response) BlockingConditionListDTO(org.wso2.carbon.apimgt.rest.api.core.dto.BlockingConditionListDTO) APIManagerFactory(org.wso2.carbon.apimgt.core.impl.APIManagerFactory) APIMgtAdminServiceImpl(org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl) BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions) ArrayList(java.util.ArrayList) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 24 with BlockConditions

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

the class PolicyDAOImpl method isBlockConditionExist.

/**
 * Check if a blocking condition already exists.
 *
 * @param blockConditions BlockConditions object to be added
 * @return true/false depending on the success
 * @throws APIMgtDAOException If failed to check if block condition exist
 */
private boolean isBlockConditionExist(BlockConditions blockConditions) throws APIMgtDAOException {
    boolean status = false;
    if (blockConditions.getConditionType().equals(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE)) {
        String isExistQuery = "SELECT STARTING_IP, ENDING_IP FROM AM_IP_RANGE_CONDITION WHERE STARTING_IP =? " + "AND ENDING_IP =?";
        try (Connection connection = DAOUtil.getConnection();
            PreparedStatement checkIsExistPreparedStatement = connection.prepareStatement(isExistQuery)) {
            checkIsExistPreparedStatement.setString(1, blockConditions.getStartingIP());
            checkIsExistPreparedStatement.setString(2, blockConditions.getEndingIP());
            try (ResultSet checkIsResultSet = checkIsExistPreparedStatement.executeQuery()) {
                if (checkIsResultSet.next()) {
                    status = true;
                }
            }
        } catch (SQLException e) {
            String msg = DAOUtil.DAO_ERROR_PREFIX + "checking if the IP range blacklist condition exists with starting IP: " + blockConditions.getStartingIP() + ", ending IP: " + blockConditions.getEndingIP();
            throw new APIMgtDAOException(msg, e);
        }
    } else {
        String isExistQuery = "SELECT CONDITION_ID,TYPE,VALUE,ENABLED,UUID FROM AM_BLOCK_CONDITIONS WHERE TYPE =? " + "AND VALUE =?";
        try (Connection connection = DAOUtil.getConnection();
            PreparedStatement checkIsExistPreparedStatement = connection.prepareStatement(isExistQuery)) {
            connection.setAutoCommit(false);
            checkIsExistPreparedStatement.setString(1, blockConditions.getConditionType());
            checkIsExistPreparedStatement.setString(2, blockConditions.getConditionValue());
            try (ResultSet checkIsResultSet = checkIsExistPreparedStatement.executeQuery()) {
                if (checkIsResultSet.next()) {
                    status = true;
                }
            }
        } catch (SQLException e) {
            String msg = DAOUtil.DAO_ERROR_PREFIX + "checking if the Block Condition Exist with condition type: " + blockConditions.getConditionType() + ", condition value: " + blockConditions.getConditionValue();
            throw new APIMgtDAOException(msg, e);
        }
    }
    return status;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 25 with BlockConditions

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

the class APIMgtAdminServiceImplTestCase method testUpdateBlockConditionStateByUUID.

@Test(description = "Test updating block condition state by uuid")
public void testUpdateBlockConditionStateByUUID() 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.updateBlockConditionStateByUUID(blockConditions.getUuid(), true)).thenReturn(true);
    Boolean statusTrue = adminService.updateBlockConditionStateByUUID(blockConditions.getUuid(), true);
    Assert.assertTrue(statusTrue);
    // Error path
    // Failure updating
    Mockito.when(policyDAO.updateBlockConditionStateByUUID(blockConditions.getUuid(), true)).thenReturn(false);
    Boolean statusFalse = adminService.updateBlockConditionStateByUUID(blockConditions.getUuid(), true);
    Assert.assertFalse(statusFalse);
    // Error path
    // APIMgtDAOException
    Mockito.when(policyDAO.updateBlockConditionStateByUUID(blockConditions.getUuid(), true)).thenThrow(APIMgtDAOException.class);
    try {
        adminService.updateBlockConditionStateByUUID(blockConditions.getUuid(), true);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Couldn't update block condition with UUID: " + blockConditions.getUuid() + ", state: " + true);
    }
}
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)32 Test (org.testng.annotations.Test)14 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)12 ArrayList (java.util.ArrayList)8 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)8 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)7 BlockingConditionDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.BlockingConditionDTO)7 Response (javax.ws.rs.core.Response)5 Test (org.junit.Test)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)5 APIMgtAdminServiceImpl (org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl)5 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)5 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 SQLException (java.sql.SQLException)4 BlacklistApiServiceImpl (org.wso2.carbon.apimgt.rest.api.admin.impl.BlacklistApiServiceImpl)4 BlockingConditionDTO (org.wso2.carbon.apimgt.rest.api.core.dto.BlockingConditionDTO)4 BlockingConditionListDTO (org.wso2.carbon.apimgt.rest.api.core.dto.BlockingConditionListDTO)4 ResultSet (java.sql.ResultSet)3