Search in sources :

Example 21 with GlobalPolicy

use of org.wso2.carbon.apimgt.api.model.subscription.GlobalPolicy in project carbon-apimgt by wso2.

the class APIProviderImplTest method testGetGlobalPolicyByUUID.

@Test
public void testGetGlobalPolicyByUUID() throws APIManagementException {
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    GlobalPolicy globalPolicy = Mockito.mock(GlobalPolicy.class);
    Mockito.when(apimgtDAO.getGlobalPolicyByUUID("1111")).thenReturn(globalPolicy, null);
    apiProvider.getGlobalPolicyByUUID("1111");
    try {
        assertNotNull(apiProvider.getGlobalPolicyByUUID("1111"));
    } catch (APIManagementException e) {
        assertEquals("Global Policy: 1111 was not found.", e.getMessage());
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 22 with GlobalPolicy

use of org.wso2.carbon.apimgt.api.model.subscription.GlobalPolicy in project carbon-apimgt by wso2.

the class APIProviderImplTest method testGetGlobalPolicy.

@Test
public void testGetGlobalPolicy() throws APIManagementException {
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    GlobalPolicy globalPolicy = Mockito.mock(GlobalPolicy.class);
    Mockito.when(apimgtDAO.getGlobalPolicy("testName")).thenReturn(globalPolicy);
    assertNotNull(apiProvider.getGlobalPolicy("testName"));
}
Also used : GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 23 with GlobalPolicy

use of org.wso2.carbon.apimgt.api.model.subscription.GlobalPolicy in project carbon-apimgt by wso2.

the class GlobalPoliciesApiServiceImpl method globalPoliciesGet.

public Response globalPoliciesGet(String xWSO2Tenant, String policyName, MessageContext messageContext) {
    SubscriptionValidationDAO subscriptionValidationDAO = new SubscriptionValidationDAO();
    xWSO2Tenant = SubscriptionValidationDataUtil.validateTenantDomain(xWSO2Tenant, messageContext);
    if (StringUtils.isNotEmpty(xWSO2Tenant)) {
        if (APIConstants.CHAR_ASTERIX.equals(xWSO2Tenant)) {
            return Response.ok().entity(SubscriptionValidationDataUtil.fromGlobalPolicyToGlobalPolicyListDTO(subscriptionValidationDAO.getAllGlobalPolicies())).build();
        } else if (StringUtils.isNotEmpty(policyName)) {
            List<GlobalPolicy> model = new ArrayList<>();
            GlobalPolicy globalPolicy = subscriptionValidationDAO.getGlobalPolicyByNameForTenant(policyName, xWSO2Tenant);
            if (globalPolicy != null) {
                model.add(globalPolicy);
            }
            return Response.ok().entity(SubscriptionValidationDataUtil.fromGlobalPolicyToGlobalPolicyListDTO(model)).build();
        } else {
            return Response.ok().entity(SubscriptionValidationDataUtil.fromGlobalPolicyToGlobalPolicyListDTO(subscriptionValidationDAO.getAllGlobalPolicies(xWSO2Tenant))).build();
        }
    } else {
        if (StringUtils.isNotEmpty(policyName)) {
            return Response.status(Response.Status.BAD_REQUEST.getStatusCode(), "X-WSo2-Tenant header is missing.").build();
        }
    }
    return Response.ok().entity(SubscriptionValidationDataUtil.fromGlobalPolicyToGlobalPolicyListDTO(subscriptionValidationDAO.getAllGlobalPolicies())).build();
}
Also used : GlobalPolicy(org.wso2.carbon.apimgt.api.model.subscription.GlobalPolicy) List(java.util.List) ArrayList(java.util.ArrayList) SubscriptionValidationDAO(org.wso2.carbon.apimgt.impl.dao.SubscriptionValidationDAO)

Example 24 with GlobalPolicy

use of org.wso2.carbon.apimgt.api.model.subscription.GlobalPolicy in project carbon-apimgt by wso2.

the class ApiMgtDAO method getGlobalPolicyByUUID.

/**
 * Get a particular Global level policy given UUID.
 *
 * @param uuid name of the global polixy
 * @return {@link GlobalPolicy}
 * @throws APIManagementException
 */
public GlobalPolicy getGlobalPolicyByUUID(String uuid) throws APIManagementException {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    String sqlQuery = SQLConstants.GET_GLOBAL_POLICY_BY_UUID;
    GlobalPolicy globalPolicy = null;
    try {
        conn = APIMgtDBUtil.getConnection();
        ps = conn.prepareStatement(sqlQuery);
        ps.setString(1, uuid);
        rs = ps.executeQuery();
        if (rs.next()) {
            String siddhiQuery = null;
            globalPolicy = new GlobalPolicy(rs.getString(ThrottlePolicyConstants.COLUMN_NAME));
            globalPolicy.setDescription(rs.getString(ThrottlePolicyConstants.COLUMN_DESCRIPTION));
            globalPolicy.setPolicyId(rs.getInt(ThrottlePolicyConstants.COLUMN_POLICY_ID));
            globalPolicy.setUUID(rs.getString(ThrottlePolicyConstants.COLUMN_UUID));
            globalPolicy.setTenantId(rs.getInt(ThrottlePolicyConstants.COLUMN_TENANT_ID));
            globalPolicy.setKeyTemplate(rs.getString(ThrottlePolicyConstants.COLUMN_KEY_TEMPLATE));
            globalPolicy.setDeployed(rs.getBoolean(ThrottlePolicyConstants.COLUMN_DEPLOYED));
            InputStream siddhiQueryBlob = rs.getBinaryStream(ThrottlePolicyConstants.COLUMN_SIDDHI_QUERY);
            if (siddhiQueryBlob != null) {
                siddhiQuery = APIMgtDBUtil.getStringFromInputStream(siddhiQueryBlob);
            }
            globalPolicy.setSiddhiQuery(siddhiQuery);
        }
    } catch (SQLException e) {
        handleException("Error while retrieving global policy by uuid " + uuid, e);
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, conn, rs);
    }
    return globalPolicy;
}
Also used : GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 25 with GlobalPolicy

use of org.wso2.carbon.apimgt.api.model.subscription.GlobalPolicy in project carbon-apimgt by wso2.

the class ApiMgtDAO method getGlobalPolicy.

/**
 * Get a particular Global level policy.
 *
 * @param policyName name of the global polixy
 * @return {@link GlobalPolicy}
 * @throws APIManagementException
 */
public GlobalPolicy getGlobalPolicy(String policyName) throws APIManagementException {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    String sqlQuery = SQLConstants.GET_GLOBAL_POLICY;
    GlobalPolicy globalPolicy = null;
    try {
        conn = APIMgtDBUtil.getConnection();
        ps = conn.prepareStatement(sqlQuery);
        ps.setString(1, policyName);
        rs = ps.executeQuery();
        if (rs.next()) {
            String siddhiQuery = null;
            globalPolicy = new GlobalPolicy(rs.getString(ThrottlePolicyConstants.COLUMN_NAME));
            globalPolicy.setDescription(rs.getString(ThrottlePolicyConstants.COLUMN_DESCRIPTION));
            globalPolicy.setPolicyId(rs.getInt(ThrottlePolicyConstants.COLUMN_POLICY_ID));
            globalPolicy.setUUID(rs.getString(ThrottlePolicyConstants.COLUMN_UUID));
            globalPolicy.setTenantId(rs.getInt(ThrottlePolicyConstants.COLUMN_TENANT_ID));
            globalPolicy.setKeyTemplate(rs.getString(ThrottlePolicyConstants.COLUMN_KEY_TEMPLATE));
            globalPolicy.setDeployed(rs.getBoolean(ThrottlePolicyConstants.COLUMN_DEPLOYED));
            InputStream siddhiQueryBlob = rs.getBinaryStream(ThrottlePolicyConstants.COLUMN_SIDDHI_QUERY);
            if (siddhiQueryBlob != null) {
                siddhiQuery = APIMgtDBUtil.getStringFromInputStream(siddhiQueryBlob);
            }
            globalPolicy.setSiddhiQuery(siddhiQuery);
        }
    } catch (SQLException e) {
        handleException("Error while executing SQL", e);
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, conn, rs);
    }
    return globalPolicy;
}
Also used : GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

GlobalPolicy (org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy)19 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)9 GlobalPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicy)9 InputStream (java.io.InputStream)7 GlobalPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.GlobalPolicyEvent)7 Connection (java.sql.Connection)6 PreparedStatement (java.sql.PreparedStatement)6 SQLException (java.sql.SQLException)6 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)6 ApplicationPolicy (org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy)6 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)6 ResultSet (java.sql.ResultSet)5 APIPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)4 ApplicationPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.ApplicationPolicyEvent)4 SubscriptionPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionPolicyEvent)4