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());
}
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;
}
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);
}
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());
}
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;
}
Aggregations