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