use of org.wso2.carbon.apimgt.core.models.BlockConditions in project carbon-apimgt by wso2.
the class APIMgtAdminServiceImplTestCase method testAddBlockCondition.
@Test(description = "Test adding block condition")
public void testAddBlockCondition() 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);
String uuid = adminService.addBlockCondition(blockConditions);
Assert.assertNotNull(uuid);
// Error path
Mockito.when(policyDAO.addBlockConditions(blockConditions)).thenThrow(APIMgtDAOException.class);
try {
adminService.addBlockCondition(blockConditions);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Couldn't add block condition with condition type: " + blockConditions.getConditionType() + ", condition value: " + blockConditions.getConditionValue());
}
}
use of org.wso2.carbon.apimgt.core.models.BlockConditions in project carbon-apimgt by wso2.
the class APIMgtAdminServiceImplTestCase method testGetBlockConditionByUUID.
@Test(description = "Test getting block condition by uuid")
public void testGetBlockConditionByUUID() throws APIManagementException {
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO);
BlockConditions blockConditions = SampleTestObjectCreator.createDefaultBlockCondition(BLOCK_CONDITION_TYPE);
Mockito.when(policyDAO.getBlockConditionByUUID(blockConditions.getUuid())).thenReturn(blockConditions);
BlockConditions blockConditionsReturned = adminService.getBlockConditionByUUID(blockConditions.getUuid());
Assert.assertEquals(blockConditionsReturned, blockConditions);
// Error path
Mockito.when(policyDAO.getBlockConditionByUUID(blockConditions.getUuid())).thenThrow(APIMgtDAOException.class);
try {
adminService.getBlockConditionByUUID(blockConditions.getUuid());
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Couldn't get block condition by UUID: " + blockConditions.getUuid());
}
}
use of org.wso2.carbon.apimgt.core.models.BlockConditions in project carbon-apimgt by wso2.
the class BlacklistApiServiceImpl method blacklistGet.
@Override
public Response blacklistGet(String accept, Request request) throws NotFoundException {
try {
APIMgtAdminService apiMgtAdminService = APIManagerFactory.getInstance().getAPIMgtAdminService();
List<BlockConditions> blockConditionsList = apiMgtAdminService.getBlockConditions();
BlockingConditionListDTO blockingConditionListDTO = MappingUtil.fromBlockConditionListToListDTO(blockConditionsList);
return Response.ok(blockingConditionListDTO).build();
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving block conditions";
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.core.models.BlockConditions in project carbon-apimgt by wso2.
the class APIGatewayPublisherImpl method updateBlockCondition.
@Override
public void updateBlockCondition(BlockConditions blockConditions) throws GatewayException {
if (blockConditions != null) {
BlockEvent blockEvent = new BlockEvent(APIMgtConstants.GatewayEventTypes.BLOCK_CONDITION_ADD);
blockEvent.setConditionId(blockConditions.getConditionId());
blockEvent.setUuid(blockConditions.getUuid());
blockEvent.setConditionType(blockConditions.getConditionType());
blockEvent.setEnabled(blockConditions.isEnabled());
blockEvent.setConditionValue(blockConditions.getConditionValue());
if (APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_IP.equals(blockConditions.getConditionType())) {
blockEvent.setFixedIp(APIUtils.ipToLong(blockConditions.getConditionValue()));
}
if (APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE.equals(blockConditions.getConditionType())) {
blockEvent.setStartingIP(APIUtils.ipToLong(blockConditions.getStartingIP()));
blockEvent.setEndingIP(APIUtils.ipToLong(blockConditions.getEndingIP()));
}
publishToThrottleTopic(blockEvent);
if (log.isDebugEnabled()) {
log.debug("BlockCondition : " + blockConditions.getUuid() + " update event has been successfully " + "published " + "to broker");
}
}
}
use of org.wso2.carbon.apimgt.core.models.BlockConditions in project carbon-apimgt by wso2.
the class APIGatewayPublisherImpl method addBlockCondition.
@Override
public void addBlockCondition(BlockConditions blockConditions) throws GatewayException {
if (blockConditions != null) {
BlockEvent blockEvent = new BlockEvent(APIMgtConstants.GatewayEventTypes.BLOCK_CONDITION_ADD);
blockEvent.setConditionId(blockConditions.getConditionId());
blockEvent.setUuid(blockConditions.getUuid());
blockEvent.setConditionType(blockConditions.getConditionType());
blockEvent.setEnabled(blockConditions.isEnabled());
blockEvent.setConditionValue(blockConditions.getConditionValue());
if (APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_IP.equals(blockConditions.getConditionType())) {
blockEvent.setFixedIp(APIUtils.ipToLong(blockConditions.getConditionValue()));
}
if (APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE.equals(blockConditions.getConditionType())) {
blockEvent.setStartingIP(APIUtils.ipToLong(blockConditions.getStartingIP()));
blockEvent.setEndingIP(APIUtils.ipToLong(blockConditions.getEndingIP()));
}
publishToThrottleTopic(blockEvent);
if (log.isDebugEnabled()) {
log.debug("BlockCondition : " + blockConditions.getUuid() + " add event has been successfully " + "published " + "to broker");
}
}
}
Aggregations