Search in sources :

Example 6 with Tier

use of org.wso2.carbon.apimgt.api.model.Tier in project carbon-apimgt by wso2.

the class APIStoreImpl method addApplication.

@Override
public ApplicationCreationResponse addApplication(Application application) throws APIManagementException {
    ApplicationCreationResponse applicationResponse = null;
    try {
        if (getApplicationDAO().isApplicationNameExists(application.getName())) {
            String message = "An application already exists with a duplicate name - " + application.getName();
            log.error(message);
            throw new APIMgtResourceAlreadyExistsException(message, ExceptionCodes.APPLICATION_ALREADY_EXISTS);
        }
        // Tier validation
        Policy tier = application.getPolicy();
        if (tier == null) {
            String message = "Tier name cannot be null - " + application.getName();
            log.error(message);
            throw new APIManagementException(message, ExceptionCodes.TIER_CANNOT_BE_NULL);
        } else {
            Policy policy = getPolicyDAO().getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, tier.getPolicyName());
            if (policy == null) {
                String message = "Specified tier " + tier.getPolicyName() + " is invalid";
                log.error(message);
                throw new APIManagementException(message, ExceptionCodes.TIER_CANNOT_BE_NULL);
            }
            application.setPolicy(policy);
        }
        // Generate UUID for application
        String generatedUuid = UUID.randomUUID().toString();
        application.setId(generatedUuid);
        String permissionString = application.getPermissionString();
        if (permissionString != null && !("").equals(permissionString)) {
            HashMap roleNamePermissionList;
            roleNamePermissionList = getAPIPermissionArray(permissionString);
            application.setPermissionMap(roleNamePermissionList);
        }
        application.setCreatedTime(LocalDateTime.now());
        getApplicationDAO().addApplication(application);
        WorkflowExecutor appCreationWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION);
        ApplicationCreationWorkflow workflow = new ApplicationCreationWorkflow(getApplicationDAO(), getWorkflowDAO(), getApiGateway());
        workflow.setApplication(application);
        workflow.setCreatedBy(getUsername());
        workflow.setWorkflowReference(application.getId());
        workflow.setExternalWorkflowReference(UUID.randomUUID().toString());
        workflow.setCreatedTime(LocalDateTime.now());
        String workflowDescription = "Application [ " + application.getName() + " ] creation request from application creator - " + getUsername() + " with throttling tier - " + tier.getPolicyName() + "";
        workflow.setWorkflowDescription(workflowDescription);
        WorkflowResponse response = appCreationWFExecutor.execute(workflow);
        workflow.setStatus(response.getWorkflowStatus());
        if (WorkflowStatus.CREATED != response.getWorkflowStatus()) {
            completeWorkflow(appCreationWFExecutor, workflow);
        } else {
            getApplicationDAO().updateApplicationState(generatedUuid, APIMgtConstants.ApplicationStatus.APPLICATION_ONHOLD);
            addWorkflowEntries(workflow);
        }
        APIUtils.logDebug("successfully added application with appId " + application.getId(), log);
        applicationResponse = new ApplicationCreationResponse(application.getId(), response);
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while creating the application - " + application.getName();
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    } catch (ParseException e) {
        String errorMsg = "Error occurred while parsing the permission json from swagger in application - " + application.getName();
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, ExceptionCodes.SWAGGER_PARSE_EXCEPTION);
    } catch (WorkflowException e) {
        String errorMsg = "Error occurred in workflow";
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, ExceptionCodes.WORKFLOW_EXCEPTION);
    }
    return applicationResponse;
}
Also used : Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) ApplicationCreationResponse(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) WorkflowException(org.wso2.carbon.apimgt.core.exception.WorkflowException) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceAlreadyExistsException) ApplicationCreationWorkflow(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationWorkflow) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) WorkflowExecutor(org.wso2.carbon.apimgt.core.api.WorkflowExecutor) ParseException(org.json.simple.parser.ParseException)

Example 7 with Tier

use of org.wso2.carbon.apimgt.api.model.Tier in project carbon-apimgt by wso2.

the class APIStoreImpl method updateApplication.

@Override
public WorkflowResponse updateApplication(String uuid, Application application) throws APIManagementException {
    try {
        // get old app
        Application existingApplication = getApplicationDAO().getApplication(uuid);
        if (existingApplication != null) {
            WorkflowExecutor executor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_APPLICATION_UPDATE);
            ApplicationUpdateWorkflow workflow = new ApplicationUpdateWorkflow(getApplicationDAO(), getWorkflowDAO(), getApiGateway());
            application.setId(uuid);
            application.setUpdatedUser(getUsername());
            application.setUpdatedTime(LocalDateTime.now());
            Policy appTier = application.getPolicy();
            if (appTier != null && !appTier.getPolicyName().equals(existingApplication.getPolicy().getPolicyName())) {
                Policy policy = getPolicyDAO().getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, appTier.getPolicyName());
                if (policy == null) {
                    String message = "Specified tier " + appTier + " is invalid";
                    log.error(message);
                    throw new APIManagementException(message, ExceptionCodes.TIER_NAME_INVALID);
                }
                application.setPolicy(policy);
            }
            workflow.setExistingApplication(existingApplication);
            workflow.setUpdatedApplication(application);
            workflow.setCreatedBy(getUsername());
            workflow.setWorkflowReference(application.getId());
            workflow.setExternalWorkflowReference(UUID.randomUUID().toString());
            workflow.setCreatedTime(LocalDateTime.now());
            String workflowDescription = "Update application " + existingApplication.getName() + " with tier " + existingApplication.getPolicy().getPolicyName() + " and description \'" + existingApplication.getDescription() + "\' To " + application.getName() + " with tier " + application.getPolicy().getPolicyName() + " and description \'" + application.getDescription() + "\' by " + getUsername();
            workflow.setWorkflowDescription(workflowDescription);
            // setting attributes for internal use. These are set to use from outside the executor's method
            // these will be saved in the AM_WORKFLOW table so these can be retrieved later for external wf approval
            // scenarios. this won't get stored for simple wfs
            workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_NAME, application.getName());
            workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_UPDATEDBY, application.getUpdatedUser());
            workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_TIER, application.getPolicy().getPolicyName());
            workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_POLICY_ID, application.getPolicy().getUuid());
            workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_DESCRIPTION, application.getDescription());
            workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_PERMISSION, application.getPermissionString());
            workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_EXISTIN_APP_STATUS, existingApplication.getStatus());
            WorkflowResponse response = executor.execute(workflow);
            workflow.setStatus(response.getWorkflowStatus());
            if (WorkflowStatus.CREATED != response.getWorkflowStatus()) {
                completeWorkflow(executor, workflow);
            } else {
                getApplicationDAO().updateApplicationState(uuid, ApplicationStatus.APPLICATION_ONHOLD);
                addWorkflowEntries(workflow);
            }
            return response;
        } else {
            String errorMsg = "Applicaiton does not exist - " + uuid;
            log.error(errorMsg);
            throw new APIManagementException(errorMsg, ExceptionCodes.APPLICATION_NOT_FOUND);
        }
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while updating the application - " + uuid;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    }
}
Also used : Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ApplicationUpdateWorkflow(org.wso2.carbon.apimgt.core.workflow.ApplicationUpdateWorkflow) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) WorkflowExecutor(org.wso2.carbon.apimgt.core.api.WorkflowExecutor) Application(org.wso2.carbon.apimgt.core.models.Application)

Example 8 with Tier

use of org.wso2.carbon.apimgt.api.model.Tier 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 9 with Tier

use of org.wso2.carbon.apimgt.api.model.Tier in project carbon-apimgt by wso2.

the class APIStoreImplTestCase method testUpdateApplication.

@Test(description = "Update an application")
public void testUpdateApplication() throws APIManagementException {
    ApplicationDAO applicationDAO = Mockito.mock(ApplicationDAO.class);
    WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
    APIGateway apiGateway = Mockito.mock(APIGateway.class);
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIStore apiStore = getApiStoreImpl(applicationDAO, policyDAO, workflowDAO, apiGateway);
    Application existingApplication = SampleTestObjectCreator.createDefaultApplication();
    String appUUID = existingApplication.getUuid();
    existingApplication.setStatus(ApplicationStatus.APPLICATION_APPROVED);
    Mockito.when(applicationDAO.getApplication(appUUID)).thenReturn(existingApplication);
    // Updating the existing application
    Application updatedApplication = SampleTestObjectCreator.createDefaultApplication();
    updatedApplication.setDescription("updated description");
    ApplicationPolicy applicationPolicy = SampleTestObjectCreator.createDefaultApplicationPolicy();
    applicationPolicy.setPolicyName(TIER);
    updatedApplication.setPolicy(applicationPolicy);
    updatedApplication.setStatus(ApplicationStatus.APPLICATION_APPROVED);
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, applicationPolicy.getPolicyName())).thenReturn(applicationPolicy);
    apiStore.updateApplication(appUUID, updatedApplication);
    Mockito.verify(applicationDAO, Mockito.times(1)).updateApplication(appUUID, updatedApplication);
    // Error
    // APIMgtDAOException
    Mockito.doThrow(APIMgtDAOException.class).when(applicationDAO).updateApplication(appUUID, updatedApplication);
    try {
        apiStore.updateApplication(appUUID, updatedApplication);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Error occurred while updating the application - " + appUUID);
    }
    // Error path
    // When specified tier in the updated application is invalid
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, applicationPolicy.getPolicyName())).thenReturn(null);
    try {
        apiStore.updateApplication(appUUID, updatedApplication);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Specified tier " + applicationPolicy + " is invalid");
    }
}
Also used : WorkflowDAO(org.wso2.carbon.apimgt.core.dao.WorkflowDAO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) ApplicationDAO(org.wso2.carbon.apimgt.core.dao.ApplicationDAO) Application(org.wso2.carbon.apimgt.core.models.Application) 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 10 with Tier

use of org.wso2.carbon.apimgt.api.model.Tier in project carbon-apimgt by wso2.

the class APIStoreImplTestCase method testAddApplicationNullPolicy.

@Test(description = "Add an application with null policy", expectedExceptions = APIManagementException.class)
public void testAddApplicationNullPolicy() throws Exception {
    ApplicationDAO applicationDAO = Mockito.mock(ApplicationDAO.class);
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
    APIStore apiStore = getApiStoreImpl(applicationDAO, policyDAO, workflowDAO);
    Application application = new Application(APP_NAME, USER_NAME);
    application.setPolicy(new ApplicationPolicy(TIER));
    Mockito.when(applicationDAO.isApplicationNameExists(APP_NAME)).thenReturn(false);
    Mockito.when(policyDAO.getPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, TIER)).thenReturn(null);
    apiStore.addApplication(application);
}
Also used : WorkflowDAO(org.wso2.carbon.apimgt.core.dao.WorkflowDAO) 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) 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

Tier (org.wso2.carbon.apimgt.api.model.Tier)108 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)53 ArrayList (java.util.ArrayList)42 Test (org.junit.Test)40 HashSet (java.util.HashSet)39 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)37 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)37 API (org.wso2.carbon.apimgt.api.model.API)33 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)32 HashMap (java.util.HashMap)28 Application (org.wso2.carbon.apimgt.api.model.Application)26 Test (org.testng.annotations.Test)22 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)22 Application (org.wso2.carbon.apimgt.core.models.Application)22 LinkedHashSet (java.util.LinkedHashSet)21 JSONObject (org.json.simple.JSONObject)20 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)20 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)20 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)20 BeforeTest (org.testng.annotations.BeforeTest)19