Search in sources :

Example 16 with CustomPolicy

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

the class APIMgtAdminServiceImplTestCase method testAddCustomRule.

@Test(description = "Test adding a custom rule")
public void testAddCustomRule() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO);
    CustomPolicy customPolicy = SampleTestObjectCreator.createDefaultCustomPolicy();
    Mockito.when(policyDAO.addCustomPolicy(customPolicy)).thenReturn(customPolicy.getUuid());
    String uuid = adminService.addCustomRule(customPolicy);
    Assert.assertEquals(uuid, customPolicy.getUuid());
    // Error path
    Mockito.when(policyDAO.addCustomPolicy(customPolicy)).thenThrow(APIMgtDAOException.class);
    try {
        adminService.addCustomRule(customPolicy);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Couldn't add custom policy with policy name: " + customPolicy.getPolicyName());
    }
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 17 with CustomPolicy

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

the class ThrottleTemplateBuilderTestCase method testSiddhiQueryForCustomPolicy.

@Test
public void testSiddhiQueryForCustomPolicy() throws APITemplateException {
    CustomPolicy policy = SampleTestObjectCreator.createDefaultCustomPolicy();
    CustomThrottlePolicyTemplateBuilder templateBuilder = new CustomThrottlePolicyTemplateBuilder(policy);
    String siddhiQuery = templateBuilder.getThrottlePolicyTemplateForCustomPolicy();
    String sampleQuery = SampleTestObjectCreator.createDefaultCustomPolicySiddhiApp();
    Assert.assertEquals(siddhiQuery, sampleQuery);
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) Test(org.testng.annotations.Test)

Example 18 with CustomPolicy

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

the class PolicyDAOImpl method addCustomPolicy.

@Override
public String addCustomPolicy(CustomPolicy customPolicy) throws APIMgtDAOException {
    String addQuery = "INSERT INTO AM_CUSTOM_POLICY (NAME , KEY_TEMPLATE, DESCRIPTION ,SIDDHI_QUERY," + "IS_DEPLOYED, UUID) VALUES (?,?,?,?,?,?)";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement policyStatement = connection.prepareStatement(addQuery)) {
        policyStatement.setString(1, customPolicy.getPolicyName());
        policyStatement.setString(2, customPolicy.getKeyTemplate());
        policyStatement.setString(3, customPolicy.getDescription());
        String uuid = UUID.randomUUID().toString();
        byte[] byteArray = customPolicy.getSiddhiQuery().getBytes(Charset.defaultCharset());
        policyStatement.setBinaryStream(4, new ByteArrayInputStream(byteArray));
        // todo:change IS_DEPLOYED status after publishing policy to gateway
        policyStatement.setBoolean(5, false);
        policyStatement.setString(6, uuid);
        policyStatement.executeUpdate();
        return uuid;
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "adding custom policy with name " + customPolicy.getPolicyName(), e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) ByteArrayInputStream(java.io.ByteArrayInputStream) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 19 with CustomPolicy

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

the class PolicyDAOImpl method updateCustomPolicy.

@Override
public void updateCustomPolicy(CustomPolicy customPolicy) throws APIMgtDAOException {
    String query = "UPDATE AM_CUSTOM_POLICY SET DESCRIPTION = ?, SIDDHI_QUERY = ?, KEY_TEMPLATE = ?, NAME = ? " + "WHERE UUID = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement updateStatement = connection.prepareStatement(query)) {
        byte[] byteArray = customPolicy.getSiddhiQuery().getBytes(Charset.defaultCharset());
        updateStatement.setString(1, customPolicy.getDescription());
        updateStatement.setBinaryStream(2, new ByteArrayInputStream(byteArray));
        updateStatement.setString(3, customPolicy.getKeyTemplate());
        updateStatement.setString(4, customPolicy.getPolicyName());
        updateStatement.setString(5, customPolicy.getUuid());
        updateStatement.executeUpdate();
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "updating custom policy with UUID : " + customPolicy.getUuid(), e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) ByteArrayInputStream(java.io.ByteArrayInputStream) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 20 with CustomPolicy

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

the class PolicyDAOImpl method getCustomPolicyByUuid.

@Override
public CustomPolicy getCustomPolicyByUuid(String uuid) throws APIMgtDAOException {
    String query = "SELECT NAME, DESCRIPTION, UUID, KEY_TEMPLATE, IS_DEPLOYED, SIDDHI_QUERY FROM AM_CUSTOM_POLICY" + " WHERE UUID = ? ";
    CustomPolicy customPolicy = null;
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(query)) {
        preparedStatement.setString(1, uuid);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            if (resultSet.next()) {
                String siddhiQuery = null;
                customPolicy = new CustomPolicy(resultSet.getString("NAME"));
                customPolicy.setDescription(resultSet.getString("DESCRIPTION"));
                customPolicy.setUuid(resultSet.getString("UUID"));
                customPolicy.setKeyTemplate(resultSet.getString("KEY_TEMPLATE"));
                customPolicy.setDeployed(resultSet.getBoolean("IS_DEPLOYED"));
                InputStream siddhiQueryBlob = resultSet.getBinaryStream("SIDDHI_QUERY");
                if (siddhiQueryBlob != null) {
                    try {
                        siddhiQuery = IOUtils.toString(siddhiQueryBlob);
                    } catch (IOException e) {
                        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "reading siddhi query stream", e);
                    }
                }
                customPolicy.setSiddhiQuery(siddhiQuery);
            }
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting custom policy with UUID : " + uuid, e);
    }
    return customPolicy;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Aggregations

CustomPolicy (org.wso2.carbon.apimgt.core.models.policy.CustomPolicy)29 Test (org.testng.annotations.Test)12 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)10 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)7 ArrayList (java.util.ArrayList)6 CustomRuleDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleDTO)6 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 SQLException (java.sql.SQLException)4 Response (javax.ws.rs.core.Response)4 Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)4 APIMgtAdminServiceImpl (org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl)4 PoliciesApiServiceImpl (org.wso2.carbon.apimgt.rest.api.admin.impl.PoliciesApiServiceImpl)4 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)4 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2