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());
}
}
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);
}
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);
}
}
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);
}
}
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;
}
Aggregations