use of org.wso2.carbon.apimgt.core.workflow.ApplicationUpdateWorkflow 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());
}
}
use of org.wso2.carbon.apimgt.core.workflow.ApplicationUpdateWorkflow in project carbon-apimgt by wso2.
the class APIStoreImplTestCase method testAddApplicationUpdateWorkflowReject.
@Test(description = "Test Application update workflow reject")
public void testAddApplicationUpdateWorkflowReject() throws APIManagementException {
/*
* This test is to validate the rollback the application to its previous state for application
* update request rejection
*/
ApplicationDAO applicationDAO = Mockito.mock(ApplicationDAO.class);
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
Policy policy = Mockito.mock(Policy.class);
APIStore apiStore = getApiStoreImpl(applicationDAO, policyDAO, workflowDAO);
Application application = new Application(APP_NAME, USER_NAME);
application.setStatus(ApplicationStatus.APPLICATION_APPROVED);
application.setPolicy(new ApplicationPolicy(TIER));
application.setId(UUID);
application.setPermissionString("[{\"groupId\": \"testGroup\",\"permission\":[\"READ\",\"UPDATE\",\"DELETE\",\"SUBSCRIPTION\"]}]");
Mockito.when(applicationDAO.isApplicationNameExists(APP_NAME)).thenReturn(false);
Mockito.when(policyDAO.getPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, TIER)).thenReturn(policy);
// following section mock the workflow callback api
DefaultWorkflowExecutor executor = Mockito.mock(DefaultWorkflowExecutor.class);
APIGateway apiGateway = Mockito.mock(APIGateway.class);
Workflow workflow = new ApplicationUpdateWorkflow(applicationDAO, workflowDAO, apiGateway);
workflow.setWorkflowReference(application.getId());
workflow.setExternalWorkflowReference(UUID);
// validate the rejection flow
// here we assume the application is an approve state before update
// this attribute is set internally based on the workflow data
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_EXISTIN_APP_STATUS, ApplicationStatus.APPLICATION_APPROVED);
WorkflowResponse response = new GeneralWorkflowResponse();
response.setWorkflowStatus(WorkflowStatus.REJECTED);
Mockito.when(executor.complete(workflow)).thenReturn(response);
apiStore.completeWorkflow(executor, workflow);
Mockito.verify(applicationDAO, Mockito.times(1)).updateApplicationState(application.getId(), ApplicationStatus.APPLICATION_APPROVED);
// here we assume the application is an rejected state before update.
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_APPLICATION_EXISTIN_APP_STATUS, ApplicationStatus.APPLICATION_REJECTED);
apiStore.completeWorkflow(executor, workflow);
Mockito.verify(applicationDAO, Mockito.times(1)).updateApplicationState(application.getId(), ApplicationStatus.APPLICATION_REJECTED);
}
use of org.wso2.carbon.apimgt.core.workflow.ApplicationUpdateWorkflow 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