Search in sources :

Example 11 with APISubscriptionDAO

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

the class SubscriptionDAOImplIT method testAddAndGetSubscription.

@Test
public void testAddAndGetSubscription() throws Exception {
    // add new app
    Application app = TestUtil.addTestApplication();
    // add new api
    API api = TestUtil.addTestAPI();
    // add subscription
    String subscriptionTier = GOLD_TIER;
    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.assertNotNull(subscription);
    Assert.assertEquals(subscription.getId(), uuid);
    Assert.assertEquals(subscription.getStatus(), APIMgtConstants.SubscriptionStatus.ACTIVE);
    Assert.assertEquals(subscription.getPolicy().getPolicyName(), subscriptionTier);
    Assert.assertEquals(subscription.getApi(), TestUtil.createSummaryAPI(api), TestUtil.printDiff(subscription.getApi(), TestUtil.createSummaryAPI(api)));
    Assert.assertEquals(subscription.getApplication(), TestUtil.createSummaryApplication(app), TestUtil.printDiff(subscription.getApplication(), TestUtil.createSummaryApplication(app)));
}
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 12 with APISubscriptionDAO

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

the class SubscriptionDAOImplIT method testUpdateSubscription.

@Test
public void testUpdateSubscription() throws Exception {
    // add new app
    Application app = TestUtil.addTestApplication();
    // add new api
    API api = TestUtil.addTestAPI();
    // add subscription
    String subscriptionPolicy = GOLD_TIER;
    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 fingerprint
    String fingerprintBeforeUpdate = ETagUtils.generateETag(apiSubscriptionDAO.getLastUpdatedTimeOfSubscription(uuid));
    Assert.assertNotNull(fingerprintBeforeUpdate);
    // validate tier and status
    Assert.assertNotNull(subscription);
    Assert.assertEquals(subscription.getId(), uuid);
    Assert.assertEquals(subscription.getStatus(), APIMgtConstants.SubscriptionStatus.ACTIVE);
    Assert.assertEquals(subscription.getPolicy().getPolicyName(), subscriptionPolicy);
    // update subscription policy
    String newSubscriptionPolicy = SILVER_TIER;
    apiSubscriptionDAO.updateSubscriptionPolicy(uuid, newSubscriptionPolicy);
    // get subscription
    subscription = apiSubscriptionDAO.getAPISubscription(uuid);
    // validate
    Assert.assertNotNull(subscription);
    Assert.assertEquals(subscription.getId(), uuid);
    Assert.assertEquals(subscription.getStatus(), APIMgtConstants.SubscriptionStatus.ACTIVE);
    Assert.assertEquals(subscription.getPolicy().getPolicyName(), newSubscriptionPolicy);
    Assert.assertEquals(subscription.getApi(), TestUtil.createSummaryAPI(api), TestUtil.printDiff(subscription.getApi(), TestUtil.createSummaryAPI(api)));
    Assert.assertEquals(subscription.getApplication(), TestUtil.createSummaryApplication(app), TestUtil.printDiff(subscription.getApplication(), TestUtil.createSummaryApplication(app)));
    // update subscription status
    APIMgtConstants.SubscriptionStatus newSubscriptionStatus = APIMgtConstants.SubscriptionStatus.PROD_ONLY_BLOCKED;
    apiSubscriptionDAO.updateSubscriptionStatus(uuid, newSubscriptionStatus);
    // get subscription
    subscription = apiSubscriptionDAO.getAPISubscription(uuid);
    String fingerprintAfterUpdate = ETagUtils.generateETag(apiSubscriptionDAO.getLastUpdatedTimeOfSubscription(uuid));
    // validate
    Assert.assertNotNull(subscription);
    Assert.assertNotNull(fingerprintAfterUpdate);
    Assert.assertEquals(subscription.getId(), uuid);
    Assert.assertEquals(subscription.getStatus(), newSubscriptionStatus);
    Assert.assertEquals(subscription.getPolicy().getPolicyName(), newSubscriptionPolicy);
    Assert.assertEquals(subscription.getApi(), TestUtil.createSummaryAPI(api), TestUtil.printDiff(subscription.getApi(), TestUtil.createSummaryAPI(api)));
    Assert.assertEquals(subscription.getApplication(), TestUtil.createSummaryApplication(app), TestUtil.printDiff(subscription.getApplication(), TestUtil.createSummaryApplication(app)));
    Assert.assertNotEquals(fingerprintBeforeUpdate, fingerprintAfterUpdate);
}
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) APIMgtConstants(org.wso2.carbon.apimgt.core.util.APIMgtConstants) Test(org.testng.annotations.Test)

Example 13 with APISubscriptionDAO

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

the class SubscriptionDAOImplIT method createApisAppsAndSubscriptions.

private ApisAndApps createApisAppsAndSubscriptions() throws Exception {
    List<Application> apps = new ArrayList<>();
    // add 4 apps
    String username = ADMIN;
    Application app1 = TestUtil.addCustomApplication(APP_1, username);
    apps.add(app1);
    Application app2 = TestUtil.addCustomApplication(APP_2, username);
    apps.add(app2);
    Application app3 = TestUtil.addCustomApplication(APP_3, username);
    apps.add(app3);
    Application app4 = TestUtil.addCustomApplication(APP_4, username);
    apps.add(app4);
    // add 4 apis
    List<API> apis = new ArrayList<>();
    API api1 = TestUtil.addCustomAPI(API_1, API_VERSION, API1_CONTEXT);
    apis.add(api1);
    API api2 = TestUtil.addCustomAPI(API_2, API_VERSION, API2_CONTEXT);
    apis.add(api2);
    API api3 = TestUtil.addCustomAPI(API_3, API_VERSION, API3_CONTEXT);
    apis.add(api3);
    API api4 = TestUtil.addCustomAPI(API_4, API_VERSION, API4_CONTEXT);
    apis.add(api4);
    // Add subscriptions
    APISubscriptionDAO subscriptionDAO = DAOFactory.getAPISubscriptionDAO();
    // app1: api2
    subscriptionDAO.addAPISubscription(UUID.randomUUID().toString(), api2.getId(), app1.getId(), goldSubscriptionPolicy.getUuid(), APIMgtConstants.SubscriptionStatus.ACTIVE);
    // app2: api1, api2, api3
    subscriptionDAO.addAPISubscription(UUID.randomUUID().toString(), api1.getId(), app2.getId(), goldSubscriptionPolicy.getUuid(), APIMgtConstants.SubscriptionStatus.ACTIVE);
    subscriptionDAO.addAPISubscription(UUID.randomUUID().toString(), api2.getId(), app2.getId(), goldSubscriptionPolicy.getUuid(), APIMgtConstants.SubscriptionStatus.ACTIVE);
    subscriptionDAO.addAPISubscription(UUID.randomUUID().toString(), api3.getId(), app2.getId(), goldSubscriptionPolicy.getUuid(), APIMgtConstants.SubscriptionStatus.ACTIVE);
    // app3: api3
    subscriptionDAO.addAPISubscription(UUID.randomUUID().toString(), api3.getId(), app3.getId(), goldSubscriptionPolicy.getUuid(), APIMgtConstants.SubscriptionStatus.ACTIVE);
    // app4: api1, api2, api3, api4
    subscriptionDAO.addAPISubscription(UUID.randomUUID().toString(), api1.getId(), app4.getId(), goldSubscriptionPolicy.getUuid(), APIMgtConstants.SubscriptionStatus.ACTIVE);
    subscriptionDAO.addAPISubscription(UUID.randomUUID().toString(), api2.getId(), app4.getId(), goldSubscriptionPolicy.getUuid(), APIMgtConstants.SubscriptionStatus.ACTIVE);
    subscriptionDAO.addAPISubscription(UUID.randomUUID().toString(), api3.getId(), app4.getId(), goldSubscriptionPolicy.getUuid(), APIMgtConstants.SubscriptionStatus.ACTIVE);
    subscriptionDAO.addAPISubscription(UUID.randomUUID().toString(), api4.getId(), app4.getId(), goldSubscriptionPolicy.getUuid(), APIMgtConstants.SubscriptionStatus.ACTIVE);
    return new ApisAndApps(apis, apps);
}
Also used : APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) ArrayList(java.util.ArrayList) API(org.wso2.carbon.apimgt.core.models.API) Application(org.wso2.carbon.apimgt.core.models.Application)

Example 14 with APISubscriptionDAO

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

the class APIStoreImplTestCase method testUpdateCompositeApi.

@Test(description = "Update Composite API")
public void testUpdateCompositeApi() throws APIManagementException {
    // Add a new Composite API
    CompositeAPI.Builder apiBuilder = SampleTestObjectCreator.createUniqueCompositeAPI();
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
    GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
    APIGateway apiGateway = Mockito.mock(APIGateway.class);
    IdentityProvider idp = Mockito.mock(IdentityProvider.class);
    APIStore apiStore = getApiStoreImpl(idp, null, apiDAO, apiSubscriptionDAO, gatewaySourceGenerator, apiGateway);
    String ballerinaImpl = "Ballerina";
    apiStore.addCompositeApi(apiBuilder);
    CompositeAPI createdAPI = apiBuilder.build();
    // Update existing Composite API
    apiBuilder = SampleTestObjectCreator.createUniqueCompositeAPI();
    apiBuilder.id(createdAPI.getId());
    apiBuilder.name(createdAPI.getName());
    apiBuilder.provider(createdAPI.getProvider());
    apiBuilder.version(createdAPI.getVersion());
    apiBuilder.context(createdAPI.getContext());
    Mockito.when(apiDAO.getCompositeAPI(apiBuilder.getId())).thenReturn(createdAPI);
    Mockito.when(apiDAO.getCompositeAPIGatewayConfig(apiBuilder.getId())).thenReturn(new ByteArrayInputStream(ballerinaImpl.getBytes(StandardCharsets.UTF_8)));
    Mockito.when(gatewaySourceGenerator.getGatewayConfigFromSwagger(Matchers.anyString(), Matchers.anyString())).thenReturn(ballerinaImpl);
    apiStore.updateCompositeApi(apiBuilder);
    CompositeAPI updatedAPI = apiBuilder.build();
    Assert.assertEquals(updatedAPI.getId(), createdAPI.getId());
    Assert.assertEquals(updatedAPI.getName(), createdAPI.getName());
    Assert.assertEquals(updatedAPI.getProvider(), createdAPI.getProvider());
    Assert.assertEquals(updatedAPI.getVersion(), createdAPI.getVersion());
    Assert.assertEquals(updatedAPI.getContext(), createdAPI.getContext());
}
Also used : APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) ByteArrayInputStream(java.io.ByteArrayInputStream) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) IdentityProvider(org.wso2.carbon.apimgt.core.api.IdentityProvider) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 15 with APISubscriptionDAO

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

the class APIStoreImplTestCase method testAddSubscriptionWorkflowReject.

@Test(description = "Test Subscription workflow rejection")
public void testAddSubscriptionWorkflowReject() 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);
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    Policy policy = new SubscriptionPolicy(UUID, TIER);
    APIStore apiStore = getApiStoreImpl(apiDAO, applicationDAO, apiSubscriptionDAO, workflowDAO, apiGateway, policyDAO);
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, TIER)).thenReturn(policy);
    API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI();
    apiBuilder.lifeCycleStatus(APIStatus.PUBLISHED.getStatus());
    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(application);
    SubscriptionResponse response = apiStore.addApiSubscription(apiId, UUID, TIER);
    DefaultWorkflowExecutor executor = Mockito.mock(DefaultWorkflowExecutor.class);
    Workflow workflow = new SubscriptionCreationWorkflow(apiSubscriptionDAO, workflowDAO, apiGateway);
    workflow.setWorkflowType(APIMgtConstants.WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION);
    workflow.setWorkflowReference(response.getSubscriptionUUID());
    WorkflowResponse workflowResponse = new GeneralWorkflowResponse();
    workflowResponse.setWorkflowStatus(WorkflowStatus.REJECTED);
    Mockito.when(executor.complete(workflow)).thenReturn(workflowResponse);
    apiStore.completeWorkflow(executor, workflow);
    Mockito.verify(apiSubscriptionDAO, Mockito.times(1)).updateSubscriptionStatus(response.getSubscriptionUUID(), SubscriptionStatus.REJECTED);
}
Also used : ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) APIBuilder(org.wso2.carbon.apimgt.core.models.API.APIBuilder) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) Workflow(org.wso2.carbon.apimgt.core.workflow.Workflow) ApplicationCreationWorkflow(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationWorkflow) ApplicationUpdateWorkflow(org.wso2.carbon.apimgt.core.workflow.ApplicationUpdateWorkflow) SubscriptionCreationWorkflow(org.wso2.carbon.apimgt.core.workflow.SubscriptionCreationWorkflow) ApplicationDAO(org.wso2.carbon.apimgt.core.dao.ApplicationDAO) DefaultWorkflowExecutor(org.wso2.carbon.apimgt.core.workflow.DefaultWorkflowExecutor) WorkflowDAO(org.wso2.carbon.apimgt.core.dao.WorkflowDAO) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) SubscriptionCreationWorkflow(org.wso2.carbon.apimgt.core.workflow.SubscriptionCreationWorkflow) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) 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) Application(org.wso2.carbon.apimgt.core.models.Application) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) 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