Search in sources :

Example 11 with ThreatProtectionPolicy

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

the class APIMgtAdminServiceImplTestCase method testAddThreatProtectionPolicy.

@Test(description = "Test adding threat protection policy")
public void testAddThreatProtectionPolicy() throws APIManagementException {
    ThreatProtectionDAO threatProtectionDAO = Mockito.mock(ThreatProtectionDAO.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(threatProtectionDAO);
    ThreatProtectionPolicy policy = SampleTestObjectCreator.createUniqueThreatProtectionPolicy();
    Mockito.doThrow(APIMgtDAOException.class).when(threatProtectionDAO).updatePolicy(policy);
    Mockito.doThrow(APIMgtDAOException.class).when(threatProtectionDAO).addPolicy(policy);
    // Error path
    try {
        adminService.addThreatProtectionPolicy(policy);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Error adding threat protection policy");
    }
    try {
        policy.setUuid("");
        adminService.addThreatProtectionPolicy(policy);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Error adding threat protection policy");
    }
}
Also used : ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ThreatProtectionDAO(org.wso2.carbon.apimgt.core.dao.ThreatProtectionDAO) Test(org.testng.annotations.Test)

Example 12 with ThreatProtectionPolicy

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

the class APIMgtAdminServiceImplTestCase method testGetThreatProtectionPolicy.

@Test(description = "Test getting a threat protection policy")
public void testGetThreatProtectionPolicy() throws APIManagementException {
    ThreatProtectionDAO threatProtectionDAO = Mockito.mock(ThreatProtectionDAO.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(threatProtectionDAO);
    ThreatProtectionPolicy policy = SampleTestObjectCreator.createUniqueThreatProtectionPolicy();
    Mockito.when(threatProtectionDAO.getPolicy(policy.getUuid())).thenReturn(policy);
    ThreatProtectionPolicy policyReturned = adminService.getThreatProtectionPolicy(policy.getUuid());
    Assert.assertEquals(policyReturned, policy);
    // Error path
    Mockito.when(threatProtectionDAO.getPolicy(policy.getUuid())).thenThrow(APIMgtDAOException.class);
    try {
        adminService.getThreatProtectionPolicy(policy.getUuid());
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Error while retrieving threat protection policy");
    }
}
Also used : ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ThreatProtectionDAO(org.wso2.carbon.apimgt.core.dao.ThreatProtectionDAO) Test(org.testng.annotations.Test)

Example 13 with ThreatProtectionPolicy

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

the class MappingUtil method toThreatProtectionPolicyDTO.

/**
 * Converts ThreatProtectionPolicy core model ThreatProtectionPolicyDTO rest api core model
 * @param policy apimgt core model of ThreatProtectionPolicy
 * @return ThreatProtectionPolicyDTO rest api core model
 */
public static ThreatProtectionPolicyDTO toThreatProtectionPolicyDTO(ThreatProtectionPolicy policy) {
    if (policy == null)
        return null;
    ThreatProtectionPolicyDTO dto = new ThreatProtectionPolicyDTO();
    dto.setUuid(policy.getUuid());
    dto.setName(policy.getName());
    dto.setType(policy.getType());
    dto.setPolicy(policy.getPolicy());
    return dto;
}
Also used : ThreatProtectionPolicyDTO(org.wso2.carbon.apimgt.rest.api.core.dto.ThreatProtectionPolicyDTO)

Example 14 with ThreatProtectionPolicy

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

the class ThreatProtectionPoliciesApiServiceImpl method threatProtectionPoliciesGet.

/**
 * Get a list of all threat protection policies
 *
 * @param request
 * @return List of threat protection policies
 * @throws NotFoundException
 */
@Override
public Response threatProtectionPoliciesGet(Request request) throws NotFoundException {
    try {
        APIMgtAdminService apiMgtAdminService = APIManagerFactory.getInstance().getAPIMgtAdminService();
        List<ThreatProtectionPolicy> policyList = apiMgtAdminService.getThreatProtectionPolicyList();
        ThreatProtectionPolicyListDTO listDTO = new ThreatProtectionPolicyListDTO();
        for (ThreatProtectionPolicy policy : policyList) {
            listDTO.addListItem(MappingUtil.toThreatProtectionPolicyDTO(policy));
        }
        return Response.ok().entity(listDTO).build();
    } catch (APIManagementException e) {
        log.error(e.getMessage(), e);
    }
    return Response.status(500).entity("Internal Server Error.").build();
}
Also used : APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException)

Example 15 with ThreatProtectionPolicy

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

the class APIGatewayPublisherImpl method updateThreatProtectionPolicy.

/**
 * {@inheritDoc}
 */
@Override
public void updateThreatProtectionPolicy(ThreatProtectionPolicy policy) throws GatewayException {
    ThreatProtectionEvent event = new ThreatProtectionEvent(APIMgtConstants.GatewayEventTypes.THREAT_PROTECTION_POLICY_UPDATE);
    event.setPolicy(policy);
    publishToThreatProtectionTopic(event);
}
Also used : ThreatProtectionEvent(org.wso2.carbon.apimgt.core.models.events.ThreatProtectionEvent)

Aggregations

ThreatProtectionPolicy (org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy)27 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)12 Test (org.testng.annotations.Test)10 ThreatProtectionDAO (org.wso2.carbon.apimgt.core.dao.ThreatProtectionDAO)9 ArrayList (java.util.ArrayList)7 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)6 Response (javax.ws.rs.core.Response)5 Test (org.junit.Test)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 PreparedStatement (java.sql.PreparedStatement)4 SQLException (java.sql.SQLException)4 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)3 APIManagerFactory (org.wso2.carbon.apimgt.core.impl.APIManagerFactory)3 APIMgtAdminServiceImpl (org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl)3 ThreatProtectionEvent (org.wso2.carbon.apimgt.core.models.events.ThreatProtectionEvent)3 ResultSet (java.sql.ResultSet)2 ThreatProtectionPolicyDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.ThreatProtectionPolicyDTO)2 ThreatProtectionPolicyDTO (org.wso2.carbon.apimgt.rest.api.core.dto.ThreatProtectionPolicyDTO)2