Search in sources :

Example 31 with ThreatProtectionPolicy

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);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException)

Example 32 with ThreatProtectionPolicy

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());
}
Also used : ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy) ArrayList(java.util.ArrayList) ThreatProtectionDAO(org.wso2.carbon.apimgt.core.dao.ThreatProtectionDAO) Test(org.testng.annotations.Test)

Example 33 with ThreatProtectionPolicy

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());
}
Also used : ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy) API(org.wso2.carbon.apimgt.core.models.API) ThreatProtectionDAO(org.wso2.carbon.apimgt.core.dao.ThreatProtectionDAO) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 34 with ThreatProtectionPolicy

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());
}
Also used : ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy) ThreatProtectionDAO(org.wso2.carbon.apimgt.core.dao.ThreatProtectionDAO) Test(org.testng.annotations.Test)

Example 35 with ThreatProtectionPolicy

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());
}
Also used : Response(javax.ws.rs.core.Response) ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy) ThreatProtectionPolicyListDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.ThreatProtectionPolicyListDTO) ArrayList(java.util.ArrayList) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

ThreatProtectionPolicy (org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy)27 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)12 Test (org.testng.annotations.Test)10 ThreatProtectionDAO (org.wso2.carbon.apimgt.core.dao.ThreatProtectionDAO)9 ArrayList (java.util.ArrayList)7 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)6 Response (javax.ws.rs.core.Response)5 Test (org.junit.Test)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 PreparedStatement (java.sql.PreparedStatement)4 SQLException (java.sql.SQLException)4 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)3 APIManagerFactory (org.wso2.carbon.apimgt.core.impl.APIManagerFactory)3 APIMgtAdminServiceImpl (org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl)3 ThreatProtectionEvent (org.wso2.carbon.apimgt.core.models.events.ThreatProtectionEvent)3 ResultSet (java.sql.ResultSet)2 ThreatProtectionPolicyDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.ThreatProtectionPolicyDTO)2 ThreatProtectionPolicyDTO (org.wso2.carbon.apimgt.rest.api.core.dto.ThreatProtectionPolicyDTO)2