Search in sources :

Example 21 with ThreatProtectionPolicy

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

the class SampleTestObjectCreator method createUniqueThreatProtectionPolicy.

/**
 * Creates a new {@link ThreatProtectionPolicy} for testing purposes
 *
 * @return {@link ThreatProtectionPolicy}
 */
public static ThreatProtectionPolicy createUniqueThreatProtectionPolicy() {
    ThreatProtectionPolicy policy = new ThreatProtectionPolicy();
    policy.setUuid(UUID.randomUUID().toString());
    policy.setType("JSON");
    policy.setName("POLICY" + UUID.randomUUID().toString());
    String policyString = "{\"maxFieldLength\": 20, \"maxDepth\": 20, \"maxFieldCount\": 20, " + "\"maxStringLength\": 100, \"maxArrayElementCount\": 100}";
    policy.setPolicy(policyString);
    return policy;
}
Also used : ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy)

Example 22 with ThreatProtectionPolicy

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

the class ThreatProtectionPoliciesApiServiceImpl method threatProtectionPoliciesPost.

/**
 * Add a new threat protection policy
 *
 * @param threatProtectionPolicy Threat protection policy
 * @param request
 * @return HTTP Status 200, 500 if there was an error adding policy
 * @throws NotFoundException
 */
@Override
public Response threatProtectionPoliciesPost(ThreatProtectionPolicyDTO threatProtectionPolicy, Request request) throws NotFoundException {
    try {
        APIMgtAdminService apiMgtAdminService = APIManagerFactory.getInstance().getAPIMgtAdminService();
        apiMgtAdminService.addThreatProtectionPolicy(ThreatProtectionMappingUtil.toThreatProtectionPolicy(threatProtectionPolicy));
        return Response.ok().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) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException)

Example 23 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(ThreatProtectionMappingUtil.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 24 with ThreatProtectionPolicy

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

the class ThreatProtectionPoliciesApiServiceImplTestCase method testThreatProtectionPoliciesGet.

@Test
public void testThreatProtectionPoliciesGet() throws Exception {
    PowerMockito.mockStatic(APIManagerFactory.class);
    APIManagerFactory apiManagerFactory = Mockito.mock(APIManagerFactory.class);
    APIMgtAdminServiceImpl adminService = Mockito.mock(APIMgtAdminServiceImpl.class);
    PowerMockito.when(APIManagerFactory.getInstance()).thenReturn(apiManagerFactory);
    Mockito.when(apiManagerFactory.getAPIMgtAdminService()).thenReturn(adminService);
    List<ThreatProtectionPolicy> policyList = new ArrayList<>();
    policyList.add(createThreatProtectionPolicy());
    policyList.add(createThreatProtectionPolicy());
    policyList.add(createThreatProtectionPolicy());
    Mockito.when(adminService.getThreatProtectionPolicyList()).thenReturn(policyList);
    ThreatProtectionPoliciesApiServiceImpl apiService = new ThreatProtectionPoliciesApiServiceImpl();
    Response response = apiService.threatProtectionPoliciesGet(getRequest());
    Assert.assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    // Error path
    Mockito.when(adminService.getThreatProtectionPolicyList()).thenThrow(APIManagementException.class);
    response = apiService.threatProtectionPoliciesGet(getRequest());
    Assert.assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy) APIManagerFactory(org.wso2.carbon.apimgt.core.impl.APIManagerFactory) APIMgtAdminServiceImpl(org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl) ArrayList(java.util.ArrayList) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 25 with ThreatProtectionPolicy

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

the class ThreatProtectionMappingUtil method toThreatProtectionPolicy.

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

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