use of org.wso2.carbon.apimgt.core.workflow.APIStateChangeWorkflow in project carbon-apimgt by wso2.
the class WorkflowsApiServiceImplTestCase method testWorkflowsWorkflowReferenceIdPutAlreadyPublished.
@Test
public void testWorkflowsWorkflowReferenceIdPutAlreadyPublished() throws Exception {
printTestMethodName();
WorkflowsApiServiceImpl workflowsApiService = new WorkflowsApiServiceImpl();
APIMgtAdminServiceImpl adminService = Mockito.mock(APIMgtAdminServiceImpl.class);
PowerMockito.mockStatic(RestApiUtil.class);
PowerMockito.when(RestApiUtil.getAPIMgtAdminService()).thenReturn(adminService);
String workflowRefId = UUID.randomUUID().toString();
WorkflowRequestDTO workflowRequestDTO = new WorkflowRequestDTO();
workflowRequestDTO.setDescription("Test Desc");
workflowRequestDTO.setStatus(WorkflowRequestDTO.StatusEnum.APPROVED);
Workflow workflow = new APIStateChangeWorkflow(null, null, null, null, null, null);
workflow.setStatus(WorkflowStatus.APPROVED);
Mockito.doReturn(workflow).doThrow(new IllegalArgumentException()).when(adminService).retrieveWorkflow(workflowRefId);
Response response = workflowsApiService.workflowsWorkflowReferenceIdPut(workflowRefId, workflowRequestDTO, getRequest());
assertEquals(400, response.getStatus());
}
use of org.wso2.carbon.apimgt.core.workflow.APIStateChangeWorkflow in project carbon-apimgt by wso2.
the class APIPublisherImpl method updateAPIStatus.
/**
* @see org.wso2.carbon.apimgt.core.api.APIPublisher#updateAPIStatus(String, String, Map)
*/
@Override
public WorkflowResponse updateAPIStatus(String apiId, String status, Map<String, Boolean> checkListItemMap) throws APIManagementException {
WorkflowResponse workflowResponse = null;
try {
API api = getApiDAO().getAPI(apiId);
if (api != null && !APILCWorkflowStatus.PENDING.toString().equals(api.getWorkflowStatus())) {
API.APIBuilder apiBuilder = new API.APIBuilder(api);
apiBuilder.lastUpdatedTime(LocalDateTime.now());
apiBuilder.updatedBy(getUsername());
LifecycleState currentState = getApiLifecycleManager().getLifecycleDataForState(apiBuilder.getLifecycleInstanceId(), apiBuilder.getLifeCycleStatus());
apiBuilder.lifecycleState(currentState);
for (Map.Entry<String, Boolean> checkListItem : checkListItemMap.entrySet()) {
getApiLifecycleManager().checkListItemEvent(api.getLifecycleInstanceId(), api.getLifeCycleStatus(), checkListItem.getKey(), checkListItem.getValue());
}
API originalAPI = apiBuilder.build();
WorkflowExecutor executor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_API_STATE);
APIStateChangeWorkflow workflow = new APIStateChangeWorkflow(getApiDAO(), getApiSubscriptionDAO(), getWorkflowDAO(), getApiLifecycleManager(), getApiGateway(), getLabelDAO());
workflow.setApiName(originalAPI.getName());
workflow.setApiProvider(originalAPI.getProvider());
workflow.setApiVersion(originalAPI.getVersion());
workflow.setCurrentState(currentState.getState());
workflow.setTransitionState(status);
workflow.setWorkflowReference(originalAPI.getId());
workflow.setExternalWorkflowReference(UUID.randomUUID().toString());
workflow.setCreatedTime(LocalDateTime.now());
workflow.setWorkflowType(WorkflowConstants.WF_TYPE_AM_API_STATE);
workflow.setInvoker(getUsername());
// 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
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_API_CUR_STATE, currentState.getState());
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_API_TARGET_STATE, status);
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_API_LC_INVOKER, getUsername());
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_API_NAME, originalAPI.getId());
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_API_HAS_OWN_GATEWAY, String.valueOf(originalAPI.hasOwnGateway()));
if (originalAPI.hasOwnGateway()) {
List<String> gwLabels = originalAPI.getLabels();
for (String label : gwLabels) {
if (label.contains(ContainerBasedGatewayConstants.PRIVATE_JET_API_PREFIX)) {
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_API_AUTOGEN_LABEL, label);
break;
}
}
}
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_HAS_OWN_GATEWAY, String.valueOf(originalAPI.hasOwnGateway()));
workflow.setAttribute(WorkflowConstants.ATTRIBUTE_API_LAST_UPTIME, originalAPI.getLastUpdatedTime().toString());
String workflowDescription = "API [" + workflow.getApiName() + " - " + workflow.getApiVersion() + "] state change [" + workflow.getCurrentState() + " to " + workflow.getTransitionState() + "] request from " + getUsername();
workflow.setWorkflowDescription(workflowDescription);
workflowResponse = executor.execute(workflow);
workflow.setStatus(workflowResponse.getWorkflowStatus());
if (WorkflowStatus.CREATED != workflowResponse.getWorkflowStatus()) {
completeWorkflow(executor, workflow);
} else {
// add entry to workflow table if it is only in pending state
addWorkflowEntries(workflow);
getApiDAO().updateAPIWorkflowStatus(api.getId(), APILCWorkflowStatus.PENDING);
}
} else if (api != null && APILCWorkflowStatus.PENDING.toString().equals(api.getWorkflowStatus())) {
String message = "Pending state transition for api :" + api.getName();
log.error(message);
throw new APIManagementException(message, ExceptionCodes.WORKFLOW_PENDING);
} else {
throw new APIMgtResourceNotFoundException("Requested API " + apiId + " Not Available");
}
} catch (APIMgtDAOException e) {
String errorMsg = "Couldn't change the status of api ID " + apiId;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
} catch (LifecycleException e) {
String errorMsg = "Couldn't change the status of api ID " + apiId;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, ExceptionCodes.APIMGT_LIFECYCLE_EXCEPTION);
}
return workflowResponse;
}
use of org.wso2.carbon.apimgt.core.workflow.APIStateChangeWorkflow 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