Search in sources :

Example 6 with ThreatProtectionPolicyDTO

use of org.wso2.carbon.apimgt.rest.api.admin.dto.ThreatProtectionPolicyDTO 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 7 with ThreatProtectionPolicyDTO

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

Example 8 with ThreatProtectionPolicyDTO

use of org.wso2.carbon.apimgt.rest.api.admin.dto.ThreatProtectionPolicyDTO in project carbon-apimgt by wso2.

the class ThreatProtectionPoliciesApiServiceImplTestCase method testThreatProtectionPoliciesThreatProtectionPolicyIdGet.

@Test
public void testThreatProtectionPoliciesThreatProtectionPolicyIdGet() 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);
    ThreatProtectionPolicy policy = createThreatProtectionPolicy();
    Mockito.when(adminService.getThreatProtectionPolicy(policy.getUuid())).thenReturn(policy);
    ThreatProtectionPoliciesApiServiceImpl apiService = new ThreatProtectionPoliciesApiServiceImpl();
    Response response = apiService.threatProtectionPoliciesThreatProtectionPolicyIdGet(policy.getUuid(), getRequest());
    Assert.assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    ThreatProtectionPolicyDTO returnedPolicy = (ThreatProtectionPolicyDTO) response.getEntity();
    Assert.assertEquals(policy.getUuid(), returnedPolicy.getUuid());
    Assert.assertEquals(policy.getName(), returnedPolicy.getName());
    Assert.assertEquals(policy.getType(), returnedPolicy.getType());
    // Error path
    Mockito.when(adminService.getThreatProtectionPolicy(policy.getUuid())).thenThrow(APIManagementException.class);
    response = apiService.threatProtectionPoliciesThreatProtectionPolicyIdGet(policy.getUuid(), 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) ThreatProtectionPolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.ThreatProtectionPolicyDTO) APIManagerFactory(org.wso2.carbon.apimgt.core.impl.APIManagerFactory) APIMgtAdminServiceImpl(org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 9 with ThreatProtectionPolicyDTO

use of org.wso2.carbon.apimgt.rest.api.admin.dto.ThreatProtectionPolicyDTO in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdThreatProtectionPoliciesGet.

/**
 * Get all threat protection policies associated with an API
 * @param apiId APIID
 * @param request MSF4J Request
 * @return List of threat protection policy ids
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response apisApiIdThreatProtectionPoliciesGet(String apiId, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
        List<ThreatProtectionPolicy> policyList = apiPublisher.getThreatProtectionPolicies();
        List<ThreatProtectionPolicyDTO> dtoList = new ArrayList<>();
        for (ThreatProtectionPolicy policy : policyList) {
            dtoList.add(MappingUtil.toThreatProtectionPolicyDTO(policy));
        }
        return Response.ok().entity(dtoList).build();
    } catch (APIManagementException e) {
        log.error(e.getMessage(), e);
        return Response.status(500).entity("Internal Server Error").build();
    }
}
Also used : ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy) ThreatProtectionPolicyDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.ThreatProtectionPolicyDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ArrayList(java.util.ArrayList) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher)

Example 10 with ThreatProtectionPolicyDTO

use of org.wso2.carbon.apimgt.rest.api.admin.dto.ThreatProtectionPolicyDTO in project carbon-apimgt by wso2.

the class MappingUtilTestCase method toThreatProtectionPolicyDTOTest.

@Test
public void toThreatProtectionPolicyDTOTest() {
    ThreatProtectionPolicy policy = SampleTestObjectCreator.createUniqueThreatProtectionPolicy();
    ThreatProtectionPolicyDTO dto = MappingUtil.toThreatProtectionPolicyDTO(policy);
    Assert.assertEquals(policy.getUuid(), dto.getUuid());
    Assert.assertEquals(policy.getType(), dto.getType());
    Assert.assertEquals(policy.getName(), dto.getName());
    Assert.assertEquals(policy.getPolicy(), dto.getPolicy());
}
Also used : ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy) ThreatProtectionPolicyDTO(org.wso2.carbon.apimgt.rest.api.core.dto.ThreatProtectionPolicyDTO) Test(org.testng.annotations.Test)

Aggregations

ThreatProtectionPolicy (org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy)4 ThreatProtectionPolicyDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.ThreatProtectionPolicyDTO)4 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)3 ThreatProtectionPolicyDTO (org.wso2.carbon.apimgt.rest.api.core.dto.ThreatProtectionPolicyDTO)3 Response (javax.ws.rs.core.Response)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)2 APIManagerFactory (org.wso2.carbon.apimgt.core.impl.APIManagerFactory)2 APIMgtAdminServiceImpl (org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl)2 ThreatProtectionPolicyDTO (org.wso2.carbon.apimgt.rest.api.publisher.dto.ThreatProtectionPolicyDTO)2 ArrayList (java.util.ArrayList)1 Test (org.testng.annotations.Test)1 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)1