Search in sources :

Example 66 with ApplicationPolicy

use of org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationPolicy in project carbon-apimgt by wso2.

the class PolicyDAOImpl method getApplicationPolicies.

@Override
public List<ApplicationPolicy> getApplicationPolicies() throws APIMgtDAOException {
    try {
        List<ApplicationPolicy> policyList = new ArrayList<>();
        String sqlQuery = "SELECT UUID, NAME, QUOTA_TYPE, TIME_UNIT, UNIT_TIME, QUOTA, QUOTA_UNIT, DESCRIPTION, " + "DISPLAY_NAME, IS_DEPLOYED from AM_APPLICATION_POLICY";
        try (Connection connection = DAOUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                while (resultSet.next()) {
                    ApplicationPolicy applicationPolicy = new ApplicationPolicy(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME));
                    setCommonPolicyDetails(applicationPolicy, resultSet);
                    policyList.add(applicationPolicy);
                }
            }
        }
        return policyList;
    } catch (SQLException e) {
        String errorMsg = "Error in retrieving Application policies";
        log.error(errorMsg, e);
        throw new APIMgtDAOException(errorMsg, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 67 with ApplicationPolicy

use of org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationPolicy in project carbon-apimgt by wso2.

the class PolicyDAOImpl method getApplicationPolicyByUuid.

@Override
public ApplicationPolicy getApplicationPolicyByUuid(String uuid) throws APIMgtDAOException {
    try {
        final String query = "SELECT UUID, NAME, QUOTA_TYPE, TIME_UNIT, UNIT_TIME, QUOTA, QUOTA_UNIT, DESCRIPTION, " + "DISPLAY_NAME, CUSTOM_ATTRIBUTES, IS_DEPLOYED from AM_APPLICATION_POLICY WHERE UUID = ?";
        ApplicationPolicy applicationPolicy;
        try (Connection conn = DAOUtil.getConnection();
            PreparedStatement statement = conn.prepareStatement(query)) {
            statement.setString(1, uuid);
            statement.execute();
            try (ResultSet rs = statement.getResultSet()) {
                if (rs.next()) {
                    applicationPolicy = new ApplicationPolicy(rs.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME));
                    setCommonPolicyDetails(applicationPolicy, rs);
                    InputStream inputStream = rs.getBinaryStream(APIMgtConstants.ThrottlePolicyConstants.COLUMN_CUSTOM_ATTRIB);
                    if (inputStream != null) {
                        applicationPolicy.setCustomAttributes(IOUtils.toByteArray(inputStream));
                    }
                    return applicationPolicy;
                } else {
                    // not found
                    String msg = "Application Policy not found for id: " + uuid;
                    log.warn(msg);
                    throw new APIMgtDAOException(msg, ExceptionCodes.POLICY_NOT_FOUND);
                }
            } catch (IOException e) {
                // error
                throw new APIMgtDAOException("Internal error", e);
            }
        }
    } catch (SQLException e) {
        String errorMsg = "Error in retrieving Application Policy with id: " + uuid;
        log.error(errorMsg, e);
        throw new APIMgtDAOException(errorMsg, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Example 68 with ApplicationPolicy

use of org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationPolicy in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImplTestCase method testAddApplicationPolicy.

@Test(description = "Test add application policy")
public void testAddApplicationPolicy() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIGateway apiGateway = Mockito.mock(APIGateway.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO, apiGateway);
    ApplicationPolicy policy = SampleTestObjectCreator.createDefaultApplicationPolicy();
    adminService.addApplicationPolicy(policy);
    Mockito.verify(policyDAO, Mockito.times(1)).addApplicationPolicy(policy);
    // Error path
    Mockito.doThrow(APIMgtDAOException.class).when(policyDAO).addApplicationPolicy(policy);
    try {
        adminService.addApplicationPolicy(policy);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Couldn't add Application for uuid: " + policy.getUuid());
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 69 with ApplicationPolicy

use of org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationPolicy in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImplTestCase method testGetApplicationPolicy.

@Test(description = "Test getting Application policy")
public void testGetApplicationPolicy() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO);
    ApplicationPolicy applicationPolicy = SampleTestObjectCreator.createDefaultApplicationPolicy();
    Mockito.when(policyDAO.getApplicationPolicy(applicationPolicy.getPolicyName())).thenReturn(applicationPolicy);
    adminService.getApplicationPolicy(applicationPolicy.getPolicyName());
    Mockito.verify(policyDAO, Mockito.times(1)).getApplicationPolicy(applicationPolicy.getPolicyName());
    // Error path
    Mockito.when(policyDAO.getApplicationPolicy(applicationPolicy.getPolicyName())).thenThrow(APIMgtDAOException.class);
    try {
        adminService.getApplicationPolicy(applicationPolicy.getPolicyName());
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Couldn't retrieve Application policy with name: " + applicationPolicy.getPolicyName());
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 70 with ApplicationPolicy

use of org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationPolicy in project carbon-apimgt by wso2.

the class PolicyExportManager method createArchiveFromExecutionPlans.

public String createArchiveFromExecutionPlans(String exportedPoliciesDirName, String archiveDir, String archiveName) throws APIManagementException {
    try {
        // retrieve all policies under each policy level
        List<APIPolicy> apiPolicies = apiMgtAdminService.getApiPolicies();
        List<ApplicationPolicy> applicationPolicies = apiMgtAdminService.getApplicationPolicies();
        List<SubscriptionPolicy> subscriptionPolicies = apiMgtAdminService.getSubscriptionPolicies();
        List<CustomPolicy> customPolicies = apiMgtAdminService.getCustomRules();
        // write all execution Plans/Siddhi Apps to exportPoliciesDirName directory
        String dirLocation = exportedPoliciesDirName + File.separator + EXPORT_POLICIES;
        APIFileUtils.createDirectory(dirLocation);
        if (!apiPolicies.isEmpty()) {
            for (Map<String, String> map : getApiPolicySiddhiApps(apiPolicies)) {
                prepareFile(dirLocation, map);
            }
        }
        if (!applicationPolicies.isEmpty()) {
            prepareFile(dirLocation, getAppPolicySiddhiApps(applicationPolicies));
        }
        if (!subscriptionPolicies.isEmpty()) {
            prepareFile(dirLocation, getSubscriptionPolicySiddhiApps(subscriptionPolicies));
        }
        if (!customPolicies.isEmpty()) {
            prepareFile(dirLocation, getCustomPolicySiddhiApps(customPolicies));
        }
        // create archive and get the archive location
        String zippedFilePath = createArchiveFromPolicies(exportedPoliciesDirName, archiveDir, archiveName);
        APIFileUtils.deleteDirectory(exportedPoliciesDirName);
        return zippedFilePath;
    } catch (APIManagementException e) {
        String errorMessage = "Error while exporting policies";
        log.error(errorMessage, e);
        throw new APIManagementException(errorMessage, e);
    }
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy)

Aggregations

ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)56 Test (org.testng.annotations.Test)30 ApplicationPolicy (org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy)29 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)24 ArrayList (java.util.ArrayList)23 Test (org.junit.Test)18 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)18 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)18 Application (org.wso2.carbon.apimgt.core.models.Application)17 Connection (java.sql.Connection)16 PreparedStatement (java.sql.PreparedStatement)16 SQLException (java.sql.SQLException)16 ResultSet (java.sql.ResultSet)13 BeforeTest (org.testng.annotations.BeforeTest)13 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)13 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)13 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)13 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)13 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)12 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)12