Search in sources :

Example 46 with APISubscriptionDAO

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

the class SubscriptionDAOImplIT method testGetSubscriptionByApplication.

@Test
public void testGetSubscriptionByApplication() throws Exception {
    // add test apis, apps and subscriptions
    ApisAndApps apisAndApps = createApisAppsAndSubscriptions();
    APISubscriptionDAO subscriptionDAO = DAOFactory.getAPISubscriptionDAO();
    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);
    // get subscriptions of app1 (api2)
    List<Subscription> subscriptions = subscriptionDAO.getAPISubscriptionsByApplication(app1.getId());
    // 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);
        }
    }
    // get subscriptions of app2 (api1, api2, api3)
    subscriptions = subscriptionDAO.getAPISubscriptionsByApplication(app2.getId());
    // validate subscriptions
    Assert.assertEquals(subscriptions.size(), 3, "There should be 3 subscriptions (only).");
    for (Subscription subscription : subscriptions) {
        Assert.assertNotNull(subscription);
        if (subscription.getApi().getId().equals(api1.getId())) {
            Assert.assertEquals(subscription.getApi(), TestUtil.createSummaryAPI(api1), TestUtil.printDiff(subscription.getApi(), TestUtil.createSummaryAPI(api1)));
        } else if (subscription.getApi().getId().equals(api2.getId())) {
            Assert.assertEquals(subscription.getApi(), TestUtil.createSummaryAPI(api2), TestUtil.printDiff(subscription.getApi(), TestUtil.createSummaryAPI(api2)));
        } else if (subscription.getApi().getId().equals(api3.getId())) {
            Assert.assertEquals(subscription.getApi(), TestUtil.createSummaryAPI(api3), TestUtil.printDiff(subscription.getApi(), TestUtil.createSummaryAPI(api3)));
        } else {
            Assert.fail(INVALID_SUBSCRIPTION_FOUND);
        }
    }
    // get subscriptions of app3 (api3)
    subscriptions = subscriptionDAO.getAPISubscriptionsByApplication(app3.getId());
    // 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(api3.getId())) {
            Assert.assertEquals(subscription.getApi(), TestUtil.createSummaryAPI(api3), TestUtil.printDiff(subscription.getApi(), TestUtil.createSummaryAPI(api3)));
        } else {
            Assert.fail(INVALID_SUBSCRIPTION_FOUND);
        }
    }
    // get subscriptions of app4 (api1, api2, api3, api4)
    subscriptions = subscriptionDAO.getAPISubscriptionsByApplication(app4.getId());
    // validate subscriptions
    Assert.assertEquals(subscriptions.size(), 4, "There should be 4 subscriptions (only).");
    for (Subscription subscription : subscriptions) {
        Assert.assertNotNull(subscription);
        if (subscription.getApi().getId().equals(api1.getId())) {
            Assert.assertEquals(subscription.getApi(), TestUtil.createSummaryAPI(api1), TestUtil.printDiff(subscription.getApi(), TestUtil.createSummaryAPI(api1)));
        } else if (subscription.getApi().getId().equals(api2.getId())) {
            Assert.assertEquals(subscription.getApi(), TestUtil.createSummaryAPI(api2), TestUtil.printDiff(subscription.getApi(), TestUtil.createSummaryAPI(api2)));
        } else if (subscription.getApi().getId().equals(api3.getId())) {
            Assert.assertEquals(subscription.getApi(), TestUtil.createSummaryAPI(api3), TestUtil.printDiff(subscription.getApi(), TestUtil.createSummaryAPI(api4)));
        } else if (subscription.getApi().getId().equals(api4.getId())) {
            Assert.assertEquals(subscription.getApi(), TestUtil.createSummaryAPI(api4), TestUtil.printDiff(subscription.getApi(), TestUtil.createSummaryAPI(api4)));
        } else {
            Assert.fail(INVALID_SUBSCRIPTION_FOUND);
        }
    }
}
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 47 with APISubscriptionDAO

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

the class SubscriptionDAOImplIT method testGetSubscriptionByApiId.

@Test
public void testGetSubscriptionByApiId() throws Exception {
    // add test apis, apps and subscriptions
    ApisAndApps apisAndApps = createApisAppsAndSubscriptions();
    APISubscriptionDAO subscriptionDAO = DAOFactory.getAPISubscriptionDAO();
    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);
    // get subscriptions of api1 (app2, app4)
    List<Subscription> subscriptions = subscriptionDAO.getAPISubscriptionsByAPI(api1.getId());
    // validate subscription count
    Assert.assertEquals(subscriptions.size(), 2, "There should be 2 subscriptions (only).");
    Assert.assertEquals(subscriptionDAO.getSubscriptionCountByAPI(api1.getId()), 2L, "There should be 2 subscriptions (only).");
    // validate subscriptions
    for (Subscription subscription : subscriptions) {
        Assert.assertNotNull(subscription);
        if (subscription.getApplication().getId().equals(app2.getId())) {
            Assert.assertEquals(subscription.getApplication(), TestUtil.createSummaryApplication(app2), TestUtil.printDiff(subscription.getApplication(), TestUtil.createSummaryApplication(app2)));
        } else if (subscription.getApplication().getId().equals(app4.getId())) {
            Assert.assertEquals(subscription.getApplication(), TestUtil.createSummaryApplication(app4), TestUtil.printDiff(subscription.getApplication(), TestUtil.createSummaryApplication(app4)));
        } else {
            Assert.fail(INVALID_SUBSCRIPTION_FOUND);
        }
    }
    // get subscriptions of api2 (app1, app2, app4)
    subscriptions = subscriptionDAO.getAPISubscriptionsByAPI(api2.getId());
    // validate subscription count
    Assert.assertEquals(subscriptions.size(), 3, "There should be 3 subscriptions (only).");
    Assert.assertEquals(subscriptionDAO.getSubscriptionCountByAPI(api2.getId()), 3L, "There should be 3 subscriptions (only).");
    // validate subscriptions
    for (Subscription subscription : subscriptions) {
        Assert.assertNotNull(subscription);
        if (subscription.getApplication().getId().equals(app1.getId())) {
            Assert.assertEquals(subscription.getApplication(), TestUtil.createSummaryApplication(app1), TestUtil.printDiff(subscription.getApplication(), TestUtil.createSummaryApplication(app1)));
        } else if (subscription.getApplication().getId().equals(app2.getId())) {
            Assert.assertEquals(subscription.getApplication(), TestUtil.createSummaryApplication(app2), TestUtil.printDiff(subscription.getApplication(), TestUtil.createSummaryApplication(app2)));
        } else if (subscription.getApplication().getId().equals(app4.getId())) {
            Assert.assertEquals(subscription.getApplication(), TestUtil.createSummaryApplication(app4), TestUtil.printDiff(subscription.getApplication(), TestUtil.createSummaryApplication(app4)));
        } else {
            Assert.fail(INVALID_SUBSCRIPTION_FOUND);
        }
    }
    // get subscriptions of api3 (app2, app3, app4)
    subscriptions = subscriptionDAO.getAPISubscriptionsByAPI(api3.getId());
    // validate subscription count
    Assert.assertEquals(subscriptions.size(), 3, "There should be 3 subscription (only).");
    Assert.assertEquals(subscriptionDAO.getSubscriptionCountByAPI(api3.getId()), 3L, "There should be 3 subscription (only).");
    // validate subscriptions
    for (Subscription subscription : subscriptions) {
        Assert.assertNotNull(subscription);
        if (subscription.getApplication().getId().equals(app2.getId())) {
            Assert.assertEquals(subscription.getApplication(), TestUtil.createSummaryApplication(app2), TestUtil.printDiff(subscription.getApplication(), TestUtil.createSummaryApplication(app2)));
        } else if (subscription.getApplication().getId().equals(app3.getId())) {
            Assert.assertEquals(subscription.getApplication(), TestUtil.createSummaryApplication(app3), TestUtil.printDiff(subscription.getApplication(), TestUtil.createSummaryApplication(app3)));
        } else if (subscription.getApplication().getId().equals(app4.getId())) {
            Assert.assertEquals(subscription.getApplication(), TestUtil.createSummaryApplication(app4), TestUtil.printDiff(subscription.getApplication(), TestUtil.createSummaryApplication(app4)));
        } else {
            Assert.fail(INVALID_SUBSCRIPTION_FOUND);
        }
    }
    // get subscriptions of api4 (app4)
    subscriptions = subscriptionDAO.getAPISubscriptionsByAPI(api4.getId());
    // validate subscription count
    Assert.assertEquals(subscriptions.size(), 1, "There should be 1 subscriptions (only).");
    Assert.assertEquals(subscriptionDAO.getSubscriptionCountByAPI(api4.getId()), 1L, "There should be 1 subscriptions (only).");
    // validate subscriptions
    for (Subscription subscription : subscriptions) {
        Assert.assertNotNull(subscription);
        if (subscription.getApplication().getId().equals(app4.getId())) {
            Assert.assertEquals(subscription.getApplication(), TestUtil.createSummaryApplication(app4), TestUtil.printDiff(subscription.getApplication(), TestUtil.createSummaryApplication(app4)));
        } else {
            Assert.fail(INVALID_SUBSCRIPTION_FOUND);
        }
    }
}
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 48 with APISubscriptionDAO

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

the class APIStoreImplTestCase method testAddSubscriptionForInvalidApplication.

@Test(description = "Add subscription without a valid app", expectedExceptions = APIManagementException.class)
public void testAddSubscriptionForInvalidApplication() throws APIManagementException {
    ApplicationDAO applicationDAO = Mockito.mock(ApplicationDAO.class);
    APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    APIGateway apiGateway = Mockito.mock(APIGateway.class);
    WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
    APIStore apiStore = getApiStoreImpl(apiDAO, applicationDAO, apiSubscriptionDAO, workflowDAO, apiGateway);
    API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI();
    API api = apiBuilder.build();
    String apiId = api.getId();
    /*        Application application = new Application("TestApp", USER_ID);
        application.setId(UUID);*/
    Mockito.when(apiDAO.getAPI(apiId)).thenReturn(api);
    Mockito.when(applicationDAO.getApplication(UUID)).thenReturn(null);
    SubscriptionResponse subscriptionResponse = apiStore.addApiSubscription(apiId, UUID, TIER);
    String subscriptionId = subscriptionResponse.getSubscriptionUUID();
    Assert.assertNotNull(subscriptionId);
    // subscription should not be added
    Mockito.verify(apiSubscriptionDAO, Mockito.times(0)).addAPISubscription(subscriptionId, apiId, UUID, TIER, APIMgtConstants.SubscriptionStatus.ON_HOLD);
}
Also used : WorkflowDAO(org.wso2.carbon.apimgt.core.dao.WorkflowDAO) APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) APIBuilder(org.wso2.carbon.apimgt.core.models.API.APIBuilder) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) SubscriptionResponse(org.wso2.carbon.apimgt.core.models.SubscriptionResponse) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) ApplicationDAO(org.wso2.carbon.apimgt.core.dao.ApplicationDAO) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 49 with APISubscriptionDAO

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

the class APIStoreImplTestCase method testGetAPISubscriptionsByApplication.

@Test(description = "Get API subscriptions by application")
public void testGetAPISubscriptionsByApplication() throws APIManagementException {
    ApplicationDAO applicationDAO = Mockito.mock(ApplicationDAO.class);
    APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
    APIStore apiStore = getApiStoreImpl(applicationDAO, apiSubscriptionDAO);
    Application application = new Application(APP_NAME, USER_NAME);
    application.setPolicy(new ApplicationPolicy(TIER));
    application.setId(UUID);
    apiStore.getAPISubscriptionsByApplication(application);
    Mockito.verify(apiSubscriptionDAO, Mockito.times(1)).getAPISubscriptionsByApplication(UUID);
}
Also used : APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) ApplicationDAO(org.wso2.carbon.apimgt.core.dao.ApplicationDAO) Application(org.wso2.carbon.apimgt.core.models.Application) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 50 with APISubscriptionDAO

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

the class APIStoreImplTestCase method testDeleteApplicationWithOnHoldState.

@Test(description = "Delete application which is in on_hold state")
public void testDeleteApplicationWithOnHoldState() throws APIManagementException {
    ApplicationDAO applicationDAO = Mockito.mock(ApplicationDAO.class);
    APISubscriptionDAO subscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
    WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
    APIGateway apiGateway = Mockito.mock(APIGateway.class);
    APIStore apiStore = getApiStoreImpl(applicationDAO, subscriptionDAO, workflowDAO, apiGateway);
    Application application = SampleTestObjectCreator.createDefaultApplication();
    application.setStatus(APIMgtConstants.ApplicationStatus.APPLICATION_ONHOLD);
    application.setId(UUID);
    String externalRef = java.util.UUID.randomUUID().toString();
    Mockito.when(applicationDAO.getApplication(UUID)).thenReturn(application);
    Mockito.when(workflowDAO.getExternalWorkflowReferenceForPendingTask(application.getId(), WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION)).thenReturn(Optional.of(externalRef));
    Mockito.when(workflowDAO.getExternalWorkflowReferenceForPendingTask(application.getId(), WorkflowConstants.WF_TYPE_AM_APPLICATION_UPDATE)).thenReturn(Optional.of(externalRef));
    apiStore.deleteApplication(UUID);
    Mockito.verify(applicationDAO, Mockito.times(1)).deleteApplication(UUID);
    Mockito.verify(workflowDAO, Mockito.times(1)).getExternalWorkflowReferenceForPendingTask(application.getId(), WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION);
    Mockito.verify(workflowDAO, Mockito.times(1)).getExternalWorkflowReferenceForPendingTask(application.getId(), WorkflowConstants.WF_TYPE_AM_APPLICATION_UPDATE);
}
Also used : WorkflowDAO(org.wso2.carbon.apimgt.core.dao.WorkflowDAO) APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) ApplicationDAO(org.wso2.carbon.apimgt.core.dao.ApplicationDAO) Application(org.wso2.carbon.apimgt.core.models.Application) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

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