use of org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy in project carbon-apimgt by wso2.
the class APIMgtAdminServiceImpl method addThreatProtectionPolicy.
@Override
public void addThreatProtectionPolicy(ThreatProtectionPolicy policy) throws APIManagementException {
try {
if (StringUtils.isBlank(policy.getUuid())) {
policy.setUuid(UUID.randomUUID().toString());
threatProtectionDAO.addPolicy(policy);
apiGateway.addThreatProtectionPolicy(policy);
} else {
threatProtectionDAO.updatePolicy(policy);
apiGateway.updateThreatProtectionPolicy(policy);
}
} catch (APIMgtDAOException e) {
String message = "Error adding threat protection policy";
log.error(message, e);
throw new APIManagementException(message, e);
}
}
use of org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy in project carbon-apimgt by wso2.
the class ThreatProtectionDAOImplIT method testGetPolicies.
@Test
public void testGetPolicies() throws Exception {
List<ThreatProtectionPolicy> policyList = new ArrayList<>();
policyList.add(SampleTestObjectCreator.createUniqueThreatProtectionPolicy());
policyList.add(SampleTestObjectCreator.createUniqueThreatProtectionPolicy());
policyList.add(SampleTestObjectCreator.createUniqueThreatProtectionPolicy());
ThreatProtectionDAO dao = DAOFactory.getThreatProtectionDAO();
for (ThreatProtectionPolicy policy : policyList) {
dao.addPolicy(policy);
}
int matchedCount = 0;
List<ThreatProtectionPolicy> fromDb = dao.getPolicies();
for (ThreatProtectionPolicy policyFromDb : fromDb) {
for (ThreatProtectionPolicy originalPolicy : policyList) {
if (originalPolicy.getUuid().equals(policyFromDb.getUuid())) {
matchedCount += 1;
}
}
}
Assert.assertEquals(matchedCount, policyList.size());
}
use of org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy in project carbon-apimgt by wso2.
the class ThreatProtectionDAOImplIT method testGetThreatProtectionPolicyIdsForApi.
@Test
public void testGetThreatProtectionPolicyIdsForApi() throws Exception {
ThreatProtectionPolicy policy = SampleTestObjectCreator.createUniqueThreatProtectionPolicy();
ThreatProtectionDAO threatProtectionDAO = DAOFactory.getThreatProtectionDAO();
threatProtectionDAO.addPolicy(policy);
API.APIBuilder builder = SampleTestObjectCreator.createDefaultAPI();
Set<String> policyIds = new HashSet<>();
policyIds.add(policy.getUuid());
builder = builder.threatProtectionPolicies(policyIds);
ApiDAO dao = DAOFactory.getApiDAO();
dao.addAPI(builder.build());
Set<String> fromDb = threatProtectionDAO.getThreatProtectionPolicyIdsForApi(builder.getId());
Assert.assertEquals(fromDb.toArray()[0].toString(), policy.getUuid());
}
use of org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy in project carbon-apimgt by wso2.
the class ThreatProtectionDAOImplIT method testUpdatePolicy.
@Test
public void testUpdatePolicy() throws Exception {
ThreatProtectionPolicy policy = SampleTestObjectCreator.createUniqueThreatProtectionPolicy();
ThreatProtectionDAO dao = DAOFactory.getThreatProtectionDAO();
dao.addPolicy(policy);
// make some changes, except UUID
policy.setName("Changed name");
policy.setPolicy("{}");
policy.setType("XML");
dao.updatePolicy(policy);
ThreatProtectionPolicy fromDb = dao.getPolicy(policy.getUuid());
Assert.assertEquals(fromDb.getUuid(), policy.getUuid());
Assert.assertEquals(fromDb.getName(), policy.getName());
Assert.assertEquals(fromDb.getType(), policy.getType());
Assert.assertEquals(fromDb.getPolicy(), policy.getPolicy());
}
use of org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy in project carbon-apimgt by wso2.
the class ThreatProtectionApiServiceImplTestCase method threatProtectionPoliciesGetTestCase.
@Test
public void threatProtectionPoliciesGetTestCase() throws Exception {
PowerMockito.mockStatic(RestApiUtil.class);
PowerMockito.when(RestApiUtil.getLoggedInUsername(Mockito.any())).thenReturn("username");
PowerMockito.mockStatic(RestAPIPublisherUtil.class);
APIPublisher publisher = Mockito.mock(APIPublisher.class);
Mockito.when(RestAPIPublisherUtil.getApiPublisher(Mockito.anyString())).thenReturn(publisher);
List<ThreatProtectionPolicy> list = new ArrayList<>();
list.add(Mockito.mock(ThreatProtectionPolicy.class));
list.add(Mockito.mock(ThreatProtectionPolicy.class));
Mockito.when(publisher.getThreatProtectionPolicies()).thenReturn(list);
ThreatProtectionPoliciesApiServiceImpl apiService = new ThreatProtectionPoliciesApiServiceImpl();
Response response = apiService.threatProtectionPoliciesGet(getRequest());
Assert.assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
ThreatProtectionPolicyListDTO resList = (ThreatProtectionPolicyListDTO) response.getEntity();
Assert.assertEquals(2, resList.getList().size());
}
Aggregations