Search in sources :

Example 1 with SubscriptionCreationWorkflow

use of org.wso2.carbon.apimgt.core.workflow.SubscriptionCreationWorkflow in project carbon-apimgt by wso2.

the class ApprovalWorkflowExecutorTestCase method testWorkflowResponses.

@Test(description = "Test workflow responses")
public void testWorkflowResponses() throws WorkflowException {
    WorkflowExecutor executor = new ApprovalWorkflowExecutor();
    APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
    WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
    APIGateway apiGateway = Mockito.mock(APIGateway.class);
    Workflow workflow = new SubscriptionCreationWorkflow(apiSubscriptionDAO, workflowDAO, apiGateway);
    WorkflowResponse respone = executor.execute(workflow);
    Assert.assertEquals(respone.getJSONPayload(), "");
    Assert.assertEquals(respone.getWorkflowStatus(), WorkflowStatus.CREATED);
    workflow.setStatus(WorkflowStatus.APPROVED);
    respone = executor.complete(workflow);
    Assert.assertEquals(respone.getWorkflowStatus(), WorkflowStatus.APPROVED);
    executor.cleanUpPendingTask(workflow.getExternalWorkflowReference());
}
Also used : WorkflowDAO(org.wso2.carbon.apimgt.core.dao.WorkflowDAO) APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) WorkflowExecutor(org.wso2.carbon.apimgt.core.api.WorkflowExecutor) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) Test(org.testng.annotations.Test)

Example 2 with SubscriptionCreationWorkflow

use of org.wso2.carbon.apimgt.core.workflow.SubscriptionCreationWorkflow in project carbon-apimgt by wso2.

the class APIStoreImpl method addApiSubscription.

@Override
public SubscriptionResponse addApiSubscription(String apiId, String applicationId, String tier) throws APIManagementException {
    SubscriptionResponse subScriptionResponse;
    // Generate UUID for application
    String subscriptionId = UUID.randomUUID().toString();
    try {
        API api = getAPIbyUUID(apiId);
        Application application = getApplicationByUuid(applicationId);
        if (application == null) {
            String errorMsg = "Cannot find an application for given applicationId - " + applicationId;
            log.error(errorMsg);
            throw new APIManagementException(errorMsg, ExceptionCodes.APPLICATION_NOT_FOUND);
        }
        Policy policy = getPolicyDAO().getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, tier);
        if (policy == null) {
            String errorMsg = "Cannot find an subscription policy for given policy name - " + tier;
            log.error(errorMsg);
            throw new APIManagementException(errorMsg, ExceptionCodes.POLICY_NOT_FOUND);
        }
        getApiSubscriptionDAO().addAPISubscription(subscriptionId, apiId, applicationId, policy.getUuid(), APIMgtConstants.SubscriptionStatus.ON_HOLD);
        WorkflowExecutor addSubscriptionWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION);
        // Instead of quering the db, we create same subscription object
        Subscription subscription = new Subscription(subscriptionId, application, api, policy);
        subscription.setStatus(APIMgtConstants.SubscriptionStatus.ON_HOLD);
        SubscriptionCreationWorkflow workflow = new SubscriptionCreationWorkflow(getApiSubscriptionDAO(), getWorkflowDAO(), getApiGateway());
        workflow.setCreatedTime(LocalDateTime.now());
        workflow.setExternalWorkflowReference(UUID.randomUUID().toString());
        workflow.setWorkflowReference(subscriptionId);
        workflow.setWorkflowType(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION);
        workflow.setSubscription(subscription);
        workflow.setSubscriber(getUsername());
        String workflowDescription = "API [ " + subscription.getApi().getName() + " - " + subscription.getApi().getVersion() + " ] subscription creation request from subscriber - " + getUsername() + "  for the application - " + subscription.getApplication().getName() + "";
        workflow.setWorkflowDescription(workflowDescription);
        WorkflowResponse response = addSubscriptionWFExecutor.execute(workflow);
        workflow.setStatus(response.getWorkflowStatus());
        if (WorkflowStatus.CREATED != response.getWorkflowStatus()) {
            completeWorkflow(addSubscriptionWFExecutor, workflow);
        } else {
            // only add entry to workflow table if it is a pending task
            addWorkflowEntries(workflow);
        }
        subScriptionResponse = new SubscriptionResponse(subscriptionId, response);
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while adding api subscription for api - " + apiId;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    }
    return subScriptionResponse;
}
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) SubscriptionCreationWorkflow(org.wso2.carbon.apimgt.core.workflow.SubscriptionCreationWorkflow) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) SubscriptionResponse(org.wso2.carbon.apimgt.core.models.SubscriptionResponse) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) WorkflowExecutor(org.wso2.carbon.apimgt.core.api.WorkflowExecutor) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) Application(org.wso2.carbon.apimgt.core.models.Application)

Example 3 with SubscriptionCreationWorkflow

use of org.wso2.carbon.apimgt.core.workflow.SubscriptionCreationWorkflow 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 4 with SubscriptionCreationWorkflow

use of org.wso2.carbon.apimgt.core.workflow.SubscriptionCreationWorkflow in project carbon-apimgt by wso2.

the class DefaultWorkflowExecutorTestCase method testWorkflowResponses.

@Test(description = "Test workflow responses")
public void testWorkflowResponses() throws WorkflowException {
    WorkflowExecutor executor = new DefaultWorkflowExecutor();
    APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
    WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
    APIGateway apiGateway = Mockito.mock(APIGateway.class);
    Workflow workflow = new SubscriptionCreationWorkflow(apiSubscriptionDAO, workflowDAO, apiGateway);
    WorkflowResponse response = executor.execute(workflow);
    Assert.assertEquals(response.getJSONPayload(), "");
    Assert.assertEquals(response.getWorkflowStatus(), WorkflowStatus.APPROVED);
    workflow.setStatus(WorkflowStatus.APPROVED);
    response = executor.complete(workflow);
    Assert.assertEquals(response.getWorkflowStatus(), WorkflowStatus.APPROVED);
    executor.cleanUpPendingTask(workflow.getExternalWorkflowReference());
}
Also used : WorkflowDAO(org.wso2.carbon.apimgt.core.dao.WorkflowDAO) APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) WorkflowExecutor(org.wso2.carbon.apimgt.core.api.WorkflowExecutor) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) Test(org.testng.annotations.Test)

Example 5 with SubscriptionCreationWorkflow

use of org.wso2.carbon.apimgt.core.workflow.SubscriptionCreationWorkflow in project carbon-apimgt by wso2.

the class WorkflowExecutorFactory method createWorkflow.

/**
 * Create a DTO object related to a given workflow type.
 *
 * @param workflowType Type of the workflow.
 * @return WorkFlow instance
 */
public Workflow createWorkflow(String workflowType) throws APIMgtDAOException {
    Workflow workflow = null;
    if (WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION.equals(workflowType)) {
        workflow = new ApplicationCreationWorkflow(DAOFactory.getApplicationDAO(), DAOFactory.getWorkflowDAO(), APIManagerFactory.getInstance().getApiGateway());
        workflow.setWorkflowType(workflowType);
    } else if (WorkflowConstants.WF_TYPE_AM_APPLICATION_DELETION.equals(workflowType)) {
        workflow = new ApplicationDeletionWorkflow(DAOFactory.getApplicationDAO(), DAOFactory.getWorkflowDAO(), APIManagerFactory.getInstance().getApiGateway());
        workflow.setWorkflowType(workflowType);
    } else if (WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION.equals(workflowType)) {
        workflow = new SubscriptionCreationWorkflow(DAOFactory.getAPISubscriptionDAO(), DAOFactory.getWorkflowDAO(), APIManagerFactory.getInstance().getApiGateway());
        workflow.setWorkflowType(workflowType);
    } else if (WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_DELETION.equals(workflowType)) {
        workflow = new SubscriptionDeletionWorkflow(DAOFactory.getAPISubscriptionDAO(), DAOFactory.getWorkflowDAO(), APIManagerFactory.getInstance().getApiGateway());
        workflow.setWorkflowType(workflowType);
    } else if (WorkflowConstants.WF_TYPE_AM_API_STATE.equals(workflowType)) {
        workflow = new APIStateChangeWorkflow(DAOFactory.getApiDAO(), DAOFactory.getAPISubscriptionDAO(), DAOFactory.getWorkflowDAO(), APIManagerFactory.getInstance().geApiLifecycleManager(), APIManagerFactory.getInstance().getApiGateway(), DAOFactory.getLabelDAO());
        workflow.setWorkflowType(workflowType);
    } else if (WorkflowConstants.WF_TYPE_AM_APPLICATION_UPDATE.equals(workflowType)) {
        workflow = new ApplicationUpdateWorkflow(DAOFactory.getApplicationDAO(), DAOFactory.getWorkflowDAO(), APIManagerFactory.getInstance().getApiGateway());
        workflow.setWorkflowType(workflowType);
    } else {
        throw new APIMgtDAOException("Invalid workflow type: " + workflowType + " specified", ExceptionCodes.WORKFLOW_INVALID_WFTYPE);
    }
    return workflow;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)

Aggregations

Test (org.testng.annotations.Test)4 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)4 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)3 WorkflowExecutor (org.wso2.carbon.apimgt.core.api.WorkflowExecutor)3 APISubscriptionDAO (org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO)3 WorkflowDAO (org.wso2.carbon.apimgt.core.dao.WorkflowDAO)3 SubscriptionCreationWorkflow (org.wso2.carbon.apimgt.core.workflow.SubscriptionCreationWorkflow)3 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)2 API (org.wso2.carbon.apimgt.core.models.API)2 Application (org.wso2.carbon.apimgt.core.models.Application)2 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)2 SubscriptionResponse (org.wso2.carbon.apimgt.core.models.SubscriptionResponse)2 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)2 ApplicationCreationWorkflow (org.wso2.carbon.apimgt.core.workflow.ApplicationCreationWorkflow)2 Workflow (org.wso2.carbon.apimgt.core.workflow.Workflow)2 LocalDateTime (java.time.LocalDateTime)1 ArrayList (java.util.ArrayList)1 BeforeTest (org.testng.annotations.BeforeTest)1 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)1 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)1