Search in sources :

Example 36 with APISubscriptionDAO

use of org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO in project carbon-apimgt by wso2.

the class ApiDAOImpl method getCompositeAPIApplicationId.

private String getCompositeAPIApplicationId(Connection connection, String apiId) throws APIMgtDAOException {
    APISubscriptionDAO apiSubscriptionDAO = DAOFactory.getAPISubscriptionDAO();
    List<Subscription> subscriptions = apiSubscriptionDAO.getAPISubscriptionsByAPI(apiId);
    if (!subscriptions.isEmpty()) {
        return subscriptions.get(0).getApplication().getId();
    }
    throw new IllegalStateException("Composite API ID " + apiId + " has no associated Application subscription");
}
Also used : APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) Subscription(org.wso2.carbon.apimgt.core.models.Subscription)

Example 37 with APISubscriptionDAO

use of org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO in project carbon-apimgt by wso2.

the class ApiDAOImpl method addApplicationAssociatedAPI.

@Override
public void addApplicationAssociatedAPI(CompositeAPI api) throws APIMgtDAOException {
    final String query = "INSERT INTO AM_API (PROVIDER, NAME, CONTEXT, VERSION, " + "DESCRIPTION, UUID, API_TYPE_ID, CREATED_BY, CREATED_TIME, LAST_UPDATED_TIME, COPIED_FROM_API, " + "UPDATED_BY, LC_WORKFLOW_STATUS) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        try {
            connection.setAutoCommit(false);
            addCompositeAPIRelatedInformation(connection, statement, api);
            String policyUuid = DAOFactory.getPolicyDAO().getSubscriptionPolicy(ThrottleConstants.DEFAULT_SUB_POLICY_UNLIMITED).getUuid();
            APISubscriptionDAOImpl apiSubscriptionDAO = (APISubscriptionDAOImpl) DAOFactory.getAPISubscriptionDAO();
            apiSubscriptionDAO.createSubscription(api.getId(), api.getApplicationId(), UUID.randomUUID().toString(), policyUuid, APIMgtConstants.SubscriptionStatus.ACTIVE, connection);
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "adding Composite API: " + api.getProvider() + " - " + api.getName() + " - " + api.getVersion(), e);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "adding Composite API: " + api.getProvider() + " - " + api.getName() + " - " + api.getVersion(), e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 38 with APISubscriptionDAO

use of org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO in project carbon-apimgt by wso2.

the class SubscriptionDAOImplIT method testGetSubscriptionForApplicationAndApiType.

@Test
public void testGetSubscriptionForApplicationAndApiType() throws Exception {
    // add test apis, apps and subscriptions
    // app1: api2
    // app2: api1, api2, api3
    // app3: api3
    // app4: api1, api2, api3, api4
    ApisAndApps apisAndApps = createApisAppsAndSubscriptions();
    APISubscriptionDAO subscriptionDAO = DAOFactory.getAPISubscriptionDAO();
    ApplicationDAO applicationDAO = DAOFactory.getApplicationDAO();
    API api2 = apisAndApps.getApis().get(1);
    Application app1 = apisAndApps.getApps().get(0);
    registerOAuthAppForApplication(applicationDAO, "client-key-for-app-1", app1.getId());
    // get subscriptions of app1 (api2)
    List<Subscription> subscriptions = subscriptionDAO.getAPISubscriptionsByApplication(app1.getId(), ApiType.STANDARD);
    // validate subscriptions
    Assert.assertEquals(subscriptions.size(), 1, "There should be 1 subscriptions (only).");
    for (Subscription subscription : subscriptions) {
        Assert.assertNotNull(subscription);
        if (subscription.getApi().getId().equals(api2.getId())) {
            Assert.assertEquals(subscription.getApi(), TestUtil.createSummaryAPI(api2), TestUtil.printDiff(subscription.getApi(), TestUtil.createSummaryAPI(api2)));
        } else {
            Assert.fail(INVALID_SUBSCRIPTION_FOUND);
        }
    }
}
Also used : APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) API(org.wso2.carbon.apimgt.core.models.API) ApplicationDAO(org.wso2.carbon.apimgt.core.dao.ApplicationDAO) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) Application(org.wso2.carbon.apimgt.core.models.Application) Test(org.testng.annotations.Test)

Example 39 with APISubscriptionDAO

use of org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO in project carbon-apimgt by wso2.

the class SubscriptionDAOImplIT method testDeleteSubscription.

@Test
public void testDeleteSubscription() throws Exception {
    // add new app
    Application app = TestUtil.addTestApplication();
    // add new api
    API api = TestUtil.addTestAPI();
    // add subscription
    APISubscriptionDAO apiSubscriptionDAO = DAOFactory.getAPISubscriptionDAO();
    String uuid = UUID.randomUUID().toString();
    apiSubscriptionDAO.addAPISubscription(uuid, api.getId(), app.getId(), goldSubscriptionPolicy.getUuid(), APIMgtConstants.SubscriptionStatus.ACTIVE);
    // get subscription
    Subscription subscription = apiSubscriptionDAO.getAPISubscription(uuid);
    // validate
    Assert.assertEquals(subscription.getId(), uuid);
    // delete subscription
    apiSubscriptionDAO.deleteAPISubscription(uuid);
    // validate
    Assert.assertNull(apiSubscriptionDAO.getAPISubscription(uuid));
}
Also used : APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) API(org.wso2.carbon.apimgt.core.models.API) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) Application(org.wso2.carbon.apimgt.core.models.Application) Test(org.testng.annotations.Test)

Example 40 with APISubscriptionDAO

use of org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO in project carbon-apimgt by wso2.

the class SubscriptionDAOImplIT method testCopySubscriptions.

@Test
public void testCopySubscriptions() throws Exception {
    API api1v1 = TestUtil.addCustomAPI(API_1, API_VERSION, API1_CONTEXT);
    API api1v2 = TestUtil.addCustomAPI(API_1, API_VERSION2, API1_CONTEXT);
    Application app1 = TestUtil.addCustomApplication(APP_1, ADMIN);
    Application app2 = TestUtil.addCustomApplication(APP_2, ADMIN);
    // Add subscriptions
    APISubscriptionDAO subscriptionDAO = DAOFactory.getAPISubscriptionDAO();
    // api1v1: app1, app2
    subscriptionDAO.addAPISubscription(UUID.randomUUID().toString(), api1v1.getId(), app1.getId(), goldSubscriptionPolicy.getUuid(), APIMgtConstants.SubscriptionStatus.ACTIVE);
    subscriptionDAO.addAPISubscription(UUID.randomUUID().toString(), api1v1.getId(), app2.getId(), goldSubscriptionPolicy.getUuid(), APIMgtConstants.SubscriptionStatus.ACTIVE);
    List<Subscription> subscriptionsOfAPI1v1 = subscriptionDAO.getAPISubscriptionsByAPI(api1v1.getId());
    List<Subscription> subscriptionsForAPI1v2 = new ArrayList<>();
    for (Subscription subscription : subscriptionsOfAPI1v1) {
        subscriptionsForAPI1v2.add(new Subscription(UUID.randomUUID().toString(), subscription.getApplication(), api1v2, goldSubscriptionPolicy));
    }
    subscriptionDAO.copySubscriptions(subscriptionsForAPI1v2);
    List<Subscription> subscriptionsOfAPI1v2 = subscriptionDAO.getAPISubscriptionsByAPI(api1v2.getId());
    Assert.assertEquals(subscriptionsOfAPI1v2.size(), 2);
}
Also used : APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) ArrayList(java.util.ArrayList) API(org.wso2.carbon.apimgt.core.models.API) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) Application(org.wso2.carbon.apimgt.core.models.Application) Test(org.testng.annotations.Test)

Aggregations

APISubscriptionDAO (org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO)59 Test (org.testng.annotations.Test)54 API (org.wso2.carbon.apimgt.core.models.API)30 Application (org.wso2.carbon.apimgt.core.models.Application)24 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)22 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)18 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)18 Subscription (org.wso2.carbon.apimgt.core.models.Subscription)18 BeforeTest (org.testng.annotations.BeforeTest)14 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)14 WorkflowDAO (org.wso2.carbon.apimgt.core.dao.WorkflowDAO)14 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)12 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)11 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)10 SQLException (java.sql.SQLException)8 IdentityProvider (org.wso2.carbon.apimgt.core.api.IdentityProvider)8 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)8 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)6 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)5 SubscriptionValidationData (org.wso2.carbon.apimgt.core.models.SubscriptionValidationData)5