Search in sources :

Example 16 with SubscriptionValidationData

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

the class APISubscriptionDAOImpl method getAPISubscriptionsOfAPIForValidation.

/**
 * Retrieve the list of subscriptions of an API for validation
 *
 * @param apiContext    Context of the API
 * @param apiVersion    Version of the API.
 * @param applicationId UUID of the application
 * @return A list of {@link SubscriptionValidationData} objects
 * @throws APIMgtDAOException If failed to get subscriptions.
 */
@Override
public List<SubscriptionValidationData> getAPISubscriptionsOfAPIForValidation(String apiContext, String apiVersion, String applicationId) throws APIMgtDAOException {
    final String getSubscriptionsByAPISql = "SELECT SUBS.API_ID AS API_ID, SUBS.APPLICATION_ID AS APP_ID,SUBS" + ".SUB_STATUS AS SUB_STATUS, API.PROVIDER AS API_PROVIDER, API.NAME AS API_NAME, API.CONTEXT AS " + "API_CONTEXT, API.VERSION AS API_VERSION, SUBS.TIER_ID AS SUBS_POLICY , KEY_MAP.CLIENT_ID AS " + "CLIENT_ID,KEY_MAP.KEY_TYPE AS KEY_ENV_TYPE FROM AM_SUBSCRIPTION SUBS, AM_API API,AM_APP_KEY_MAPPING " + "KEY_MAP WHERE SUBS.API_ID = API.UUID AND KEY_MAP.APPLICATION_ID = SUBS.APPLICATION_ID AND API" + ".CONTEXT = ? AND API.VERSION = ? AND SUBS.APPLICATION_ID = ?";
    try (Connection conn = DAOUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(getSubscriptionsByAPISql)) {
        ps.setString(1, apiContext);
        ps.setString(2, apiVersion);
        ps.setString(3, applicationId);
        try (ResultSet rs = ps.executeQuery()) {
            return createSubscriptionValidationDataFromResultSet(rs);
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting api subscriptions for validation(apiContext: " + apiContext + ", apiVersion: " + apiVersion + ", appId: " + applicationId + ")", e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 17 with SubscriptionValidationData

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

the class APISubscriptionDAOImpl method createSubscriptionValidationDataFromResultSet.

private List<SubscriptionValidationData> createSubscriptionValidationDataFromResultSet(ResultSet rs) throws APIMgtDAOException {
    List<SubscriptionValidationData> subscriptionList = new ArrayList<>();
    if (rs == null) {
        return new ArrayList<>();
    }
    try {
        while (rs.next()) {
            SubscriptionValidationData subValidationData = new SubscriptionValidationData(rs.getString("API_CONTEXT"), rs.getString("API_VERSION"), rs.getString("CLIENT_ID"));
            subValidationData.setSubscriptionPolicy(rs.getString("SUBS_POLICY"));
            subValidationData.setApiName(rs.getString("API_NAME"));
            subValidationData.setApiProvider(rs.getString("API_PROVIDER"));
            subValidationData.setKeyEnvType(rs.getString("KEY_ENV_TYPE"));
            subValidationData.setApplicationId(rs.getString("APP_ID"));
            subValidationData.setStatus(rs.getString("SUB_STATUS"));
            subscriptionList.add(subValidationData);
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "creating subscription validation data", e);
    }
    return subscriptionList;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) SubscriptionValidationData(org.wso2.carbon.apimgt.core.models.SubscriptionValidationData)

Example 18 with SubscriptionValidationData

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

the class APIStoreImpl method generateApplicationKeys.

@Override
public OAuthApplicationInfo generateApplicationKeys(String applicationId, String keyType, String callbackUrl, List<String> grantTypes) throws APIManagementException {
    if (log.isDebugEnabled()) {
        log.debug("Generating application keys for application: " + applicationId);
    }
    Application application = getApplicationByUuid(applicationId);
    OAuthAppRequest oauthAppRequest = new OAuthAppRequest(application.getName(), callbackUrl, keyType, grantTypes);
    OAuthApplicationInfo oauthAppInfo = getKeyManager().createApplication(oauthAppRequest);
    if (log.isDebugEnabled()) {
        log.debug("Application key generation was successful for application: " + application.getName() + " Client Id: " + oauthAppInfo.getClientId());
    }
    try {
        getApplicationDAO().addApplicationKeys(applicationId, keyType, oauthAppInfo.getClientId());
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while saving key data for application: " + application.getName();
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    }
    if (log.isDebugEnabled()) {
        log.debug("Application keys are successfully saved in the database for application: " + application.getName() + " Client Id: " + oauthAppInfo.getClientId());
    }
    List<SubscriptionValidationData> subscriptionValidationData = getApiSubscriptionDAO().getAPISubscriptionsOfAppForValidation(applicationId, keyType);
    if (subscriptionValidationData != null && !subscriptionValidationData.isEmpty()) {
        getApiGateway().addAPISubscription(subscriptionValidationData);
    }
    return oauthAppInfo;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) OAuthAppRequest(org.wso2.carbon.apimgt.core.models.OAuthAppRequest) OAuthApplicationInfo(org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo) SubscriptionValidationData(org.wso2.carbon.apimgt.core.models.SubscriptionValidationData) Application(org.wso2.carbon.apimgt.core.models.Application)

Example 19 with SubscriptionValidationData

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

the class SubscriptionDAOImplIT method testGetSubscriptionForValidationByApiContextVersionAndApp.

@Test
public void testGetSubscriptionForValidationByApiContextVersionAndApp() throws Exception {
    // add test apis, apps and subscriptions
    // app1: api2
    // app2: api1, api2
    ApisAndApps apisAndApps = createApisAppsAndSubscriptions();
    APISubscriptionDAO subscriptionDAO = DAOFactory.getAPISubscriptionDAO();
    ApplicationDAO applicationDAO = DAOFactory.getApplicationDAO();
    API api1 = apisAndApps.getApis().get(0);
    API api2 = apisAndApps.getApis().get(1);
    Application app1 = apisAndApps.getApps().get(0);
    Application app2 = apisAndApps.getApps().get(1);
    registerOAuthAppForApplication(applicationDAO, "client-key-for-app-1", app1.getId());
    registerOAuthAppForApplication(applicationDAO, "client-key-for-app-2", app2.getId());
    List<SubscriptionValidationData> availableSubs = subscriptionDAO.getAPISubscriptionsOfAPIForValidation(api2.getContext(), api2.getVersion(), app2.getId());
    Assert.assertEquals(availableSubs.size(), 1, "There should be 1 subscription only but found " + availableSubs.size());
    validateSubscriptionsOfApi(availableSubs.get(0), api2, app2);
    List<SubscriptionValidationData> notAvailableSubs = subscriptionDAO.getAPISubscriptionsOfAPIForValidation(api1.getContext(), api1.getVersion(), app1.getId());
    Assert.assertEquals(notAvailableSubs.size(), 0, "There can't be any subscriptions but found " + notAvailableSubs.size());
}
Also used : APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) API(org.wso2.carbon.apimgt.core.models.API) SubscriptionValidationData(org.wso2.carbon.apimgt.core.models.SubscriptionValidationData) ApplicationDAO(org.wso2.carbon.apimgt.core.dao.ApplicationDAO) Application(org.wso2.carbon.apimgt.core.models.Application) Test(org.testng.annotations.Test)

Example 20 with SubscriptionValidationData

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

the class SubscriptionDAOImplIT method testGetSubscriptionsForValidation.

@Test
public void testGetSubscriptionsForValidation() throws Exception {
    // add test apis, apps and subscriptions
    ApisAndApps apisAndApps = createApisAppsAndSubscriptions();
    APISubscriptionDAO subscriptionDAO = DAOFactory.getAPISubscriptionDAO();
    ApplicationDAO applicationDAO = DAOFactory.getApplicationDAO();
    API api1 = apisAndApps.getApis().get(0);
    API api2 = apisAndApps.getApis().get(1);
    API api3 = apisAndApps.getApis().get(2);
    API api4 = apisAndApps.getApis().get(3);
    Application app1 = apisAndApps.getApps().get(0);
    Application app2 = apisAndApps.getApps().get(1);
    Application app3 = apisAndApps.getApps().get(2);
    Application app4 = apisAndApps.getApps().get(3);
    registerOAuthAppForApplication(applicationDAO, "client-key-for-app-1", app1.getId());
    registerOAuthAppForApplication(applicationDAO, "client-key-for-app-2", app2.getId());
    registerOAuthAppForApplication(applicationDAO, "client-key-for-app-3", app3.getId());
    registerOAuthAppForApplication(applicationDAO, "client-key-for-app-4", app4.getId());
    // get all subscriptions
    List<SubscriptionValidationData> subscriptions = subscriptionDAO.getAPISubscriptionsOfAPIForValidation(-1);
    // validate subscription count
    Assert.assertEquals(subscriptions.size(), 9, "There should be 9 subscriptions (only).");
    // validate subscriptions
    for (SubscriptionValidationData subscription : subscriptions) {
        Assert.assertNotNull(subscription);
        if (subscription.getApplicationId().equals(app1.getId()) && subscription.getApiName().equals(api2.getName())) {
            // app1  & api 2
            validateSubscriptionsOfApi(subscription, api2, app1);
        } else if (subscription.getApplicationId().equals(app2.getId()) && subscription.getApiName().equals(api1.getName())) {
            // app2  & api 1
            validateSubscriptionsOfApi(subscription, api1, app2);
        } else if (subscription.getApplicationId().equals(app2.getId()) && subscription.getApiName().equals(api2.getName())) {
            // app2  & api 2
            validateSubscriptionsOfApi(subscription, api2, app2);
        } else if (subscription.getApplicationId().equals(app2.getId()) && subscription.getApiName().equals(api3.getName())) {
            // app2  & api 3
            validateSubscriptionsOfApi(subscription, api3, app2);
        } else if (subscription.getApplicationId().equals(app2.getId()) && subscription.getApiName().equals(api4.getName())) {
            // app2  & api 4
            validateSubscriptionsOfApi(subscription, api4, app2);
        } else if (subscription.getApplicationId().equals(app3.getId()) && subscription.getApiName().equals(api3.getName())) {
            // app3  & api 3
            validateSubscriptionsOfApi(subscription, api3, app3);
        } else if (subscription.getApplicationId().equals(app4.getId()) && subscription.getApiName().equals(api1.getName())) {
            // app4  & api 1
            validateSubscriptionsOfApi(subscription, api1, app4);
        } else if (subscription.getApplicationId().equals(app4.getId()) && subscription.getApiName().equals(api2.getName())) {
            // app4  & api 2
            validateSubscriptionsOfApi(subscription, api2, app4);
        } else if (subscription.getApplicationId().equals(app4.getId()) && subscription.getApiName().equals(api3.getName())) {
            // app4  & api 3
            validateSubscriptionsOfApi(subscription, api3, app4);
        } else if (subscription.getApplicationId().equals(app4.getId()) && subscription.getApiName().equals(api4.getName())) {
            // app4  & api 4
            validateSubscriptionsOfApi(subscription, api4, app4);
        } else {
            Assert.fail("Invalid subscription found!!! API: " + subscription.getApiName() + " Application: " + subscription.getApplicationId());
        }
    }
}
Also used : APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) API(org.wso2.carbon.apimgt.core.models.API) SubscriptionValidationData(org.wso2.carbon.apimgt.core.models.SubscriptionValidationData) ApplicationDAO(org.wso2.carbon.apimgt.core.dao.ApplicationDAO) Application(org.wso2.carbon.apimgt.core.models.Application) Test(org.testng.annotations.Test)

Aggregations

SubscriptionValidationData (org.wso2.carbon.apimgt.core.models.SubscriptionValidationData)16 Test (org.testng.annotations.Test)6 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)6 APISubscriptionDAO (org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO)5 API (org.wso2.carbon.apimgt.core.models.API)5 Application (org.wso2.carbon.apimgt.core.models.Application)5 ArrayList (java.util.ArrayList)4 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)4 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)4 SQLException (java.sql.SQLException)3 SubscriptionEvent (org.wso2.carbon.apimgt.core.models.events.SubscriptionEvent)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)2 OAuthApplicationInfo (org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo)2 SubscriptionDTO (org.wso2.carbon.apimgt.rest.api.core.dto.SubscriptionDTO)2 HashMap (java.util.HashMap)1 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)1 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)1