use of org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy 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.core.models.policy.ThreatProtectionPolicy 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.core.models.policy.ThreatProtectionPolicy 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());
}
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("JSON POLICY 1");
String policyString = "{\"maxFieldLength\": 20, \"maxDepth\": 20, \"maxFieldCount\": 20, " + "\"maxStringLength\": 100, \"maxArrayElementCount\": 100}";
policy.setPolicy(policyString);
return policy;
}
use of org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy in project carbon-apimgt by wso2.
the class APIMgtAdminServiceImpl method deleteThreatProtectionPolicy.
@Override
public void deleteThreatProtectionPolicy(String policyId) throws APIManagementException {
try {
threatProtectionDAO.deletePolicy(policyId);
ThreatProtectionPolicy policy = new ThreatProtectionPolicy();
policy.setUuid(policyId);
apiGateway.deleteThreatProtectionPolicy(policy);
} catch (APIMgtDAOException e) {
String message = "Error deleting threat protection policy";
log.error(message, e);
throw new APIManagementException(message, e);
}
}
Aggregations