use of org.wso2.carbon.apimgt.rest.api.core.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();
}
use of org.wso2.carbon.apimgt.rest.api.core.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;
}
use of org.wso2.carbon.apimgt.rest.api.core.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());
}
use of org.wso2.carbon.apimgt.rest.api.core.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();
}
}
use of org.wso2.carbon.apimgt.rest.api.core.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());
}
Aggregations