Search in sources :

Example 31 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy in project carbon-apimgt by wso2.

the class PolicyDAOImplIT method testGetSubscriptionPolicyByUUIDandName.

@Test(description = "Get Subscription Policy by Name and UUID")
public void testGetSubscriptionPolicyByUUIDandName() throws Exception {
    SubscriptionPolicy policy = SampleTestObjectCreator.createDefaultSubscriptionPolicy();
    PolicyDAO policyDAO = DAOFactory.getPolicyDAO();
    // add policy
    policyDAO.addSubscriptionPolicy(policy);
    SubscriptionPolicy retrievedPolicy = policyDAO.getSubscriptionPolicy(policy.getPolicyName());
    Assert.assertNotNull(retrievedPolicy);
    Assert.assertEquals(retrievedPolicy.getPolicyName(), policy.getPolicyName(), "Subscription policy name mismatch");
    Assert.assertEquals(retrievedPolicy.getDisplayName(), policy.getDisplayName(), "Subscription policy display name mismatch");
    Assert.assertEquals(retrievedPolicy.getDescription(), policy.getDescription(), "Subscription policy description mismatch");
    Assert.assertEquals(retrievedPolicy.isDeployed(), policy.isDeployed(), "Subscription policy isDeployed mismatch");
    Assert.assertEquals(retrievedPolicy.getRateLimitCount(), policy.getRateLimitCount(), "Subscription policy rate limit mismatch");
    Assert.assertEquals(retrievedPolicy.getRateLimitTimeUnit(), policy.getRateLimitTimeUnit(), "Subscription policy rate limit mismatch");
    Assert.assertEquals(retrievedPolicy.isStopOnQuotaReach(), policy.isStopOnQuotaReach(), "Subscription policy stop on quota reach mismatch");
    Assert.assertEquals(retrievedPolicy.getCustomAttributes(), policy.getCustomAttributes(), "Subscription policy custom attribute mismatch");
    Assert.assertEquals(retrievedPolicy.getDefaultQuotaPolicy().getType(), policy.getDefaultQuotaPolicy().getType(), "Subscription policy default quota type mismatch");
    RequestCountLimit limitRetrieved = (RequestCountLimit) retrievedPolicy.getDefaultQuotaPolicy().getLimit();
    RequestCountLimit policyLimit = (RequestCountLimit) policy.getDefaultQuotaPolicy().getLimit();
    Assert.assertEquals(limitRetrieved.getRequestCount(), policyLimit.getRequestCount(), "Subscription policy default quota count mismatch");
    Assert.assertEquals(limitRetrieved.getTimeUnit(), policyLimit.getTimeUnit(), "Subscription policy default quota time unit mismatch");
    Assert.assertEquals(limitRetrieved.getUnitTime(), policyLimit.getUnitTime(), "Subscription policy default quota unit time mismatch");
    // get the UUID of the created policy. since this value is generated in the code, we use previously retrieved
    // policy to get the uuid and again query the policy
    String uuid = retrievedPolicy.getUuid();
    retrievedPolicy = policyDAO.getSubscriptionPolicyByUuid(uuid);
    Assert.assertNotNull(retrievedPolicy);
    Assert.assertEquals(retrievedPolicy.getPolicyName(), policy.getPolicyName(), "Subscription policy name mismatch");
    Assert.assertEquals(retrievedPolicy.getDisplayName(), policy.getDisplayName(), "Subscription policy display name mismatch");
    Assert.assertEquals(retrievedPolicy.getDescription(), policy.getDescription(), "Subscription policy description mismatch");
    Assert.assertEquals(retrievedPolicy.isDeployed(), policy.isDeployed(), "Subscription policy isDeployed mismatch");
    Assert.assertEquals(retrievedPolicy.getRateLimitCount(), policy.getRateLimitCount(), "Subscription policy rate limit mismatch");
    Assert.assertEquals(retrievedPolicy.getRateLimitTimeUnit(), policy.getRateLimitTimeUnit(), "Subscription policy rate limit mismatch");
    Assert.assertEquals(retrievedPolicy.isStopOnQuotaReach(), policy.isStopOnQuotaReach(), "Subscription policy stop on quota reach mismatch");
    Assert.assertEquals(retrievedPolicy.getCustomAttributes(), policy.getCustomAttributes(), "Subscription policy custom attribute mismatch");
    Assert.assertEquals(retrievedPolicy.getDefaultQuotaPolicy().getType(), policy.getDefaultQuotaPolicy().getType(), "Subscription policy default quota type mismatch");
    limitRetrieved = (RequestCountLimit) retrievedPolicy.getDefaultQuotaPolicy().getLimit();
    policyLimit = (RequestCountLimit) policy.getDefaultQuotaPolicy().getLimit();
    Assert.assertEquals(limitRetrieved.getRequestCount(), policyLimit.getRequestCount(), "Subscription policy default quota count mismatch");
    Assert.assertEquals(limitRetrieved.getTimeUnit(), policyLimit.getTimeUnit(), "Subscription policy default quota time unit mismatch");
    Assert.assertEquals(limitRetrieved.getUnitTime(), policyLimit.getUnitTime(), "Subscription policy default quota unit time mismatch");
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 32 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy in project carbon-apimgt by wso2.

the class APIStoreImplTestCase method testGetAPIWithNotAllowedLifecycleStates.

@Test(description = "Test get API with not allowed lifecycle states (Created, Maintenance, Retired)")
public void testGetAPIWithNotAllowedLifecycleStates() throws APIManagementException {
    String exceptionShouldThrowError = "Exception should be thrown when accessing an API in %s state";
    String matchedErrorMessage = "Attempt to access an API which is in a restricted state";
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    Policy policy = new SubscriptionPolicy(UUID, TIER);
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, TIER)).thenReturn(policy);
    APIStore apiStore = getApiStoreImpl(apiDAO, Mockito.mock(ApplicationDAO.class), Mockito.mock(APISubscriptionDAO.class), Mockito.mock(WorkflowDAO.class), Mockito.mock(APIGateway.class), policyDAO);
    API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI();
    String apiId = apiBuilder.getId();
    List<String> notAllowedStates = new ArrayList<String>() {

        {
            add(APIStatus.CREATED.getStatus());
            add(APIStatus.MAINTENANCE.getStatus());
            add(APIStatus.RETIRED.getStatus());
        }
    };
    for (String notAllowedState : notAllowedStates) {
        apiBuilder.lifeCycleStatus(notAllowedState);
        API api = apiBuilder.build();
        Mockito.when(apiDAO.getAPI(apiId)).thenReturn(api);
        try {
            apiStore.getAPIbyUUID(apiId);
            Assert.fail(exceptionShouldThrowError);
        } catch (APIManagementException e) {
            Assert.assertTrue(e.getMessage().contains(matchedErrorMessage));
        }
    }
}
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) ArrayList(java.util.ArrayList) ApplicationDAO(org.wso2.carbon.apimgt.core.dao.ApplicationDAO) WorkflowDAO(org.wso2.carbon.apimgt.core.dao.WorkflowDAO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) 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)

Example 33 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy 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)

Example 34 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy in project carbon-apimgt by wso2.

the class APIStoreImplTestCase method testDeleteSubscriptionWithOnholdState.

@Test(description = "Delete subscription with on_hold state")
public void testDeleteSubscriptionWithOnholdState() throws APIManagementException {
    ApplicationDAO applicationDAO = Mockito.mock(ApplicationDAO.class);
    APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
    WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
    APIGateway apiGateway = Mockito.mock(APIGateway.class);
    APIStore apiStore = getApiStoreImpl(applicationDAO, apiSubscriptionDAO, workflowDAO, apiGateway);
    Application application = SampleTestObjectCreator.createDefaultApplication();
    APIBuilder builder = SampleTestObjectCreator.createDefaultAPI();
    API api = builder.build();
    Subscription subscription = new Subscription(UUID, application, api, new SubscriptionPolicy("Gold"));
    subscription.setStatus(SubscriptionStatus.ON_HOLD);
    String externalRef = java.util.UUID.randomUUID().toString();
    Mockito.when(workflowDAO.getExternalWorkflowReferenceForPendingTask(subscription.getId(), WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION)).thenReturn(Optional.of(externalRef));
    Mockito.when(apiSubscriptionDAO.getAPISubscription(UUID)).thenReturn(subscription);
    apiStore.deleteAPISubscription(UUID);
    Mockito.verify(apiSubscriptionDAO, Mockito.times(1)).deleteAPISubscription(UUID);
    Mockito.verify(workflowDAO, Mockito.times(1)).getExternalWorkflowReferenceForPendingTask(subscription.getId(), WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION);
}
Also used : WorkflowDAO(org.wso2.carbon.apimgt.core.dao.WorkflowDAO) APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) APIBuilder(org.wso2.carbon.apimgt.core.models.API.APIBuilder) ApplicationDAO(org.wso2.carbon.apimgt.core.dao.ApplicationDAO) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) 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 35 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy in project carbon-apimgt by wso2.

the class APIStoreImplTestCase method testDeleteSubscription.

@Test(description = "Delete subscription")
public void testDeleteSubscription() throws APIManagementException {
    ApplicationDAO applicationDAO = Mockito.mock(ApplicationDAO.class);
    APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
    WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
    APIGateway apiGateway = Mockito.mock(APIGateway.class);
    APIStore apiStore = getApiStoreImpl(applicationDAO, apiSubscriptionDAO, workflowDAO, apiGateway);
    Application application = SampleTestObjectCreator.createDefaultApplication();
    APIBuilder builder = SampleTestObjectCreator.createDefaultAPI();
    API api = builder.build();
    Subscription subscription = new Subscription(UUID, application, api, new SubscriptionPolicy("Gold"));
    Mockito.when(apiSubscriptionDAO.getAPISubscription(UUID)).thenReturn(subscription);
    apiStore.deleteAPISubscription(UUID);
    Mockito.verify(apiSubscriptionDAO, Mockito.times(1)).deleteAPISubscription(UUID);
}
Also used : WorkflowDAO(org.wso2.carbon.apimgt.core.dao.WorkflowDAO) APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) APIBuilder(org.wso2.carbon.apimgt.core.models.API.APIBuilder) ApplicationDAO(org.wso2.carbon.apimgt.core.dao.ApplicationDAO) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) 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

SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)74 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)42 Test (org.testng.annotations.Test)41 ArrayList (java.util.ArrayList)37 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)33 API (org.wso2.carbon.apimgt.core.models.API)30 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)30 Test (org.junit.Test)25 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)25 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)24 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)21 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)19 HashMap (java.util.HashMap)18 HashSet (java.util.HashSet)18 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)16 SQLException (java.sql.SQLException)15 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)15 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)15 ApplicationPolicy (org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy)15 QuotaPolicy (org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy)15