Search in sources :

Example 11 with PolicyValidationData

use of org.wso2.carbon.apimgt.core.models.PolicyValidationData in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImplTestCase method testGetAllPolicies.

@Test(description = "Test getting all policies")
public void testGetAllPolicies() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO);
    Set<PolicyValidationData> policyValidationDataSetExpected = new HashSet<>();
    PolicyValidationData policyValidationData = new PolicyValidationData(POLICY_ID, POLICY_NAME, true);
    policyValidationDataSetExpected.add(policyValidationData);
    Mockito.when(policyDAO.getAllPolicies()).thenReturn(policyValidationDataSetExpected);
    Set<PolicyValidationData> policyValidationDataSetReturned = adminService.getAllPolicies();
    Assert.assertEquals(policyValidationDataSetReturned, policyValidationDataSetExpected);
    // Error path
    Mockito.when(policyDAO.getAllPolicies()).thenThrow(APIMgtDAOException.class);
    try {
        adminService.getAllPolicies();
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Error occurred while retrieving policies");
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) PolicyValidationData(org.wso2.carbon.apimgt.core.models.PolicyValidationData) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 12 with PolicyValidationData

use of org.wso2.carbon.apimgt.core.models.PolicyValidationData in project carbon-apimgt by wso2.

the class MappingUtil method convertToPolicyDtoList.

/**
 * Convert policy validation data list to policy dto list
 *
 * @param allPolicies all policies
 * @return PolicyDTO list
 */
public static List<PolicyDTO> convertToPolicyDtoList(Set<PolicyValidationData> allPolicies) {
    List<PolicyDTO> policyDTOList = new ArrayList<>();
    allPolicies.forEach(v -> {
        PolicyDTO policyDTO = new PolicyDTO();
        policyDTO.setId(v.getId());
        policyDTO.setName(v.getName());
        policyDTO.setStopOnQuotaReach(v.isStopOnQuotaReach());
        policyDTOList.add(policyDTO);
    });
    return policyDTOList;
}
Also used : PolicyDTO(org.wso2.carbon.apimgt.rest.api.core.dto.PolicyDTO) ThreatProtectionPolicyDTO(org.wso2.carbon.apimgt.rest.api.core.dto.ThreatProtectionPolicyDTO) ArrayList(java.util.ArrayList)

Example 13 with PolicyValidationData

use of org.wso2.carbon.apimgt.core.models.PolicyValidationData in project carbon-apimgt by wso2.

the class APIGatewayPublisherImpl method updatePolicy.

@Override
public void updatePolicy(PolicyValidationData policyValidationData) throws GatewayException {
    if (policyValidationData != null) {
        PolicyEvent policyEvent = new PolicyEvent(APIMgtConstants.GatewayEventTypes.POLICY_UPDATE);
        policyEvent.setId(policyValidationData.getId());
        policyEvent.setName(policyValidationData.getName());
        policyEvent.setStopOnQuotaReach(policyValidationData.isStopOnQuotaReach());
        publishToThrottleTopic(policyEvent);
        if (log.isDebugEnabled()) {
            log.debug("Policy : " + policyValidationData.getName() + " update event has been successfully " + "published " + "to broker");
        }
    }
}
Also used : PolicyEvent(org.wso2.carbon.apimgt.core.models.events.PolicyEvent)

Example 14 with PolicyValidationData

use of org.wso2.carbon.apimgt.core.models.PolicyValidationData in project carbon-apimgt by wso2.

the class PoliciesApiServiceImplTestCase method policiesGetTest.

@Test
public void policiesGetTest() throws Exception {
    APIMgtAdminServiceImpl apiMgtAdminService = Mockito.mock(APIMgtAdminServiceImpl.class);
    APIManagerFactory instance = Mockito.mock(APIManagerFactory.class);
    PowerMockito.mockStatic(APIManagerFactory.class);
    PowerMockito.when(APIManagerFactory.getInstance()).thenReturn(instance);
    Mockito.when(instance.getAPIMgtAdminService()).thenReturn(apiMgtAdminService);
    PoliciesApiServiceImpl policiesApiService = new PoliciesApiServiceImpl();
    PolicyValidationData policyValidationDataOne = new PolicyValidationData(UUID.randomUUID().toString(), "APPLICATION_POLICY", true);
    PolicyValidationData policyValidationDataTwo = new PolicyValidationData(UUID.randomUUID().toString(), "APPLICATION_POLICY", true);
    PolicyValidationData policyValidationDataThree = new PolicyValidationData(UUID.randomUUID().toString(), "APPLICATION_POLICY", true);
    Set<PolicyValidationData> policyValidationDataHashSet = new LinkedHashSet<>();
    policyValidationDataHashSet.add(policyValidationDataOne);
    policyValidationDataHashSet.add(policyValidationDataTwo);
    policyValidationDataHashSet.add(policyValidationDataThree);
    Mockito.when(apiMgtAdminService.getAllPolicies()).thenReturn(policyValidationDataHashSet);
    Response response = policiesApiService.policiesGet(null, getRequest());
    Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
    Assert.assertEquals(((PolicyListDTO) response.getEntity()).getList().size(), 3);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Response(javax.ws.rs.core.Response) APIManagerFactory(org.wso2.carbon.apimgt.core.impl.APIManagerFactory) APIMgtAdminServiceImpl(org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl) PolicyListDTO(org.wso2.carbon.apimgt.rest.api.core.dto.PolicyListDTO) PolicyValidationData(org.wso2.carbon.apimgt.core.models.PolicyValidationData) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 15 with PolicyValidationData

use of org.wso2.carbon.apimgt.core.models.PolicyValidationData in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImpl method updateApiPolicy.

@Override
public void updateApiPolicy(APIPolicy policy) throws APIManagementException {
    try {
        policyDAO.updateApiPolicy(policy);
        PolicyValidationData policyValidationData = new PolicyValidationData(policy.getUuid(), policy.getPolicyName(), false);
        apiGateway.updatePolicy(policyValidationData);
    } catch (APIMgtDAOException e) {
        String errorMessage = "Couldn't update API policy for uuid: " + policy.getUuid();
        log.error(errorMessage, e);
        throw new APIManagementException(errorMessage, e, e.getErrorHandler());
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) PolicyValidationData(org.wso2.carbon.apimgt.core.models.PolicyValidationData)

Aggregations

PolicyValidationData (org.wso2.carbon.apimgt.core.models.PolicyValidationData)12 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)9 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)9 PolicyEvent (org.wso2.carbon.apimgt.core.models.events.PolicyEvent)3 LinkedHashSet (java.util.LinkedHashSet)2 Test (org.testng.annotations.Test)2 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)2 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)2 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)2 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)2 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Response (javax.ws.rs.core.Response)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1