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;
}
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;
}
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);
}
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;
}
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);
}
}
Aggregations