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;
}
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();
}
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();
}
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());
}
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;
}
Aggregations