Search in sources :

Example 1 with BlockingConditionListDTO

use of org.wso2.carbon.apimgt.rest.api.core.dto.BlockingConditionListDTO in project carbon-apimgt by wso2.

the class BlacklistApiServiceImpl method blacklistGet.

/**
 * Get blacklist conditions.
 *
 * @param ifNoneMatch     If-None-Match header value
 * @param ifModifiedSince If-Modified-Since header value
 * @param request         msf4j request object
 * @return Response object
 * @throws NotFoundException if an error occurred when particular resource does not exits in the system.
 */
@Override
public Response blacklistGet(String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    if (log.isDebugEnabled()) {
        log.debug("Received BlockCondition GET request");
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        List<BlockConditions> blockConditions = apiMgtAdminService.getBlockConditions();
        BlockingConditionListDTO listDTO = BlockingConditionMappingUtil.fromBlockConditionListToListDTO(blockConditions);
        return Response.ok().entity(listDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while getting blacklist ";
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 2 with BlockingConditionListDTO

use of org.wso2.carbon.apimgt.rest.api.core.dto.BlockingConditionListDTO in project carbon-apimgt by wso2.

the class MappingUtilTestCase method fromBlockConditionListToListDTOTest.

@Test
public void fromBlockConditionListToListDTOTest() {
    List<BlockConditions> blockConditionsList = new ArrayList<>();
    blockConditionsList.add(SampleTestObjectCreator.createUniqueBlockConditions(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE));
    blockConditionsList.add(SampleTestObjectCreator.createUniqueBlockConditions(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_API));
    blockConditionsList.add(SampleTestObjectCreator.createUniqueBlockConditions(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_IP));
    BlockingConditionListDTO blockingConditionDTOList = MappingUtil.fromBlockConditionListToListDTO(blockConditionsList);
    Assert.assertEquals(blockingConditionDTOList.getList().size(), blockConditionsList.size());
}
Also used : BlockingConditionListDTO(org.wso2.carbon.apimgt.rest.api.core.dto.BlockingConditionListDTO) BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 3 with BlockingConditionListDTO

use of org.wso2.carbon.apimgt.rest.api.core.dto.BlockingConditionListDTO 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 4 with BlockingConditionListDTO

use of org.wso2.carbon.apimgt.rest.api.core.dto.BlockingConditionListDTO 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 5 with BlockingConditionListDTO

use of org.wso2.carbon.apimgt.rest.api.core.dto.BlockingConditionListDTO 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)

Aggregations

BlockConditions (org.wso2.carbon.apimgt.core.models.BlockConditions)7 ArrayList (java.util.ArrayList)5 BlockingConditionListDTO (org.wso2.carbon.apimgt.rest.api.core.dto.BlockingConditionListDTO)4 Test (org.testng.annotations.Test)2 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)2 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)2 BlockingConditionListDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.BlockingConditionListDTO)2 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)2 Response (javax.ws.rs.core.Response)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 APIManagerFactory (org.wso2.carbon.apimgt.core.impl.APIManagerFactory)1 APIMgtAdminServiceImpl (org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl)1 BlockingConditionDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.BlockingConditionDTO)1 BlockingConditionDTO (org.wso2.carbon.apimgt.rest.api.core.dto.BlockingConditionDTO)1