use of org.wso2.carbon.apimgt.core.workflow.Workflow in project carbon-apimgt by wso2.
the class WorkflowsApiServiceImplTestCase method testWorkflowsWorkflowReferenceIdGet.
@Test
public void testWorkflowsWorkflowReferenceIdGet() 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 ApplicationCreationWorkflow(null, null, null);
workflow.setStatus(WorkflowStatus.APPROVED);
LocalDateTime date1 = LocalDateTime.now();
workflow.setCreatedTime(date1);
workflow.setWorkflowDescription("Description 1");
workflow.setWorkflowType(WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION);
String ref1 = UUID.randomUUID().toString();
workflow.setExternalWorkflowReference(ref1);
Mockito.doReturn(workflow).doThrow(new IllegalArgumentException()).when(adminService).retrieveWorkflow(workflowRefId);
Response response = workflowsApiService.workflowsWorkflowReferenceIdGet(workflowRefId, getRequest());
assertEquals(200, response.getStatus());
}
use of org.wso2.carbon.apimgt.core.workflow.Workflow in project carbon-apimgt by wso2.
the class WorkflowMappingUtilTest method testToWorkflowListDTO.
@Test(description = "Convert List<Workflow> workflowList to WorkflowListDTO")
public void testToWorkflowListDTO() throws Exception {
List<Workflow> wfList = new ArrayList<>();
Workflow workflow1 = new ApplicationCreationWorkflow(null, null, null);
workflow1.setStatus(WorkflowStatus.APPROVED);
LocalDateTime date1 = LocalDateTime.now();
workflow1.setCreatedTime(date1);
workflow1.setWorkflowDescription("Description 1");
workflow1.setWorkflowType(WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION);
String ref1 = UUID.randomUUID().toString();
workflow1.setExternalWorkflowReference(ref1);
wfList.add(workflow1);
Workflow workflow2 = new SubscriptionCreationWorkflow(null, null, null);
workflow2.setStatus(WorkflowStatus.APPROVED);
LocalDateTime date2 = LocalDateTime.now();
workflow2.setCreatedTime(date2);
workflow2.setWorkflowDescription("Description 2");
workflow2.setWorkflowType(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION);
String ref2 = UUID.randomUUID().toString();
workflow2.setExternalWorkflowReference(ref2);
wfList.add(workflow2);
WorkflowListDTO wfDtoList = WorkflowMappingUtil.toWorkflowListDTO(wfList);
int count = wfDtoList.getCount();
Assert.assertEquals(count, 2, "Mismatch in the workflow list item count");
Assert.assertEquals(wfDtoList.getList().get(0).getDescription(), "Description 1", "Invalid description for workflow item 1");
Assert.assertEquals(wfDtoList.getList().get(0).getType(), WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION, "Invalid type for workflow item 1");
Assert.assertEquals(wfDtoList.getList().get(0).getWorkflowStatus(), WorkflowStatus.APPROVED.toString(), "Invalid status for workflow item 1");
Assert.assertEquals(wfDtoList.getList().get(0).getReferenceId(), ref1, "Invalid reference id for workflow item 1");
Assert.assertEquals(wfDtoList.getList().get(1).getDescription(), "Description 2", "Invalid description for workflow item 2");
Assert.assertEquals(wfDtoList.getList().get(1).getType(), WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION, "Invalid type for workflow item 2");
Assert.assertEquals(wfDtoList.getList().get(1).getWorkflowStatus(), WorkflowStatus.APPROVED.toString(), "Invalid status for workflow item 2");
Assert.assertEquals(wfDtoList.getList().get(1).getReferenceId(), ref2, "Invalid reference id for workflow item 2");
}
use of org.wso2.carbon.apimgt.core.workflow.Workflow in project carbon-apimgt by wso2.
the class WorkflowMappingUtil method toWorkflowDTO.
/**
* Map Workflow to WorkflowDTO
* @param response WorkflowResponse object
* @return WorkflowResponseDTO mapped WorkflowResponseDTO
*/
public static WorkflowDTO toWorkflowDTO(Workflow response) {
WorkflowDTO workflowDTO = new WorkflowDTO();
if (response != null) {
workflowDTO.setCreatedTime(response.getCreatedTime().toString());
workflowDTO.setDescription(response.getWorkflowDescription());
workflowDTO.setType(response.getWorkflowType());
workflowDTO.setReferenceId(response.getExternalWorkflowReference());
workflowDTO.setWorkflowStatus(response.getStatus().toString());
}
return workflowDTO;
}
use of org.wso2.carbon.apimgt.core.workflow.Workflow in project carbon-apimgt by wso2.
the class APIStoreImpl method deleteAPISubscription.
/**
* @see APIStore#deleteAPISubscription(String)
*/
@Override
public WorkflowResponse deleteAPISubscription(String subscriptionId) throws APIManagementException {
try {
WorkflowExecutor removeSubscriptionWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_DELETION);
// check for pending subscription creation
if (subscriptionId == null) {
String errorMsg = "Subscription Id is not provided";
log.error(errorMsg);
throw new APIManagementException(errorMsg, ExceptionCodes.PARAMETER_NOT_PROVIDED);
}
Subscription subscription = getApiSubscriptionDAO().getAPISubscription(subscriptionId);
if (subscription == null) {
String errorMsg = "Subscription not found for the id - " + subscriptionId;
log.error(errorMsg);
throw new APIManagementException(errorMsg, ExceptionCodes.SUBSCRIPTION_NOT_FOUND);
} else {
// remove pending tasks for subscription creation first
cleanupPendingTaskForSubscriptionDeletion(subscription);
SubscriptionDeletionWorkflow workflow = new SubscriptionDeletionWorkflow(getApiSubscriptionDAO(), getWorkflowDAO(), getApiGateway());
workflow.setWorkflowReference(subscriptionId);
workflow.setSubscription(subscription);
workflow.setWorkflowType(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_DELETION);
workflow.setStatus(WorkflowStatus.CREATED);
workflow.setCreatedTime(LocalDateTime.now());
workflow.setExternalWorkflowReference(UUID.randomUUID().toString());
workflow.setSubscriber(getUsername());
String workflowDescription = "API [ " + subscription.getApi().getName() + " - " + subscription.getApi().getVersion() + " ] subscription deletion request from subscriber - " + getUsername() + " for the application - " + subscription.getApplication().getName() + "";
workflow.setWorkflowDescription(workflowDescription);
WorkflowResponse response = removeSubscriptionWFExecutor.execute(workflow);
workflow.setStatus(response.getWorkflowStatus());
if (WorkflowStatus.CREATED != response.getWorkflowStatus()) {
completeWorkflow(removeSubscriptionWFExecutor, workflow);
} else {
// add entry to workflow table if it is only in pending state
// haven't changed the subscription's state to allow to use it till approval
addWorkflowEntries(workflow);
}
return response;
}
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while deleting api subscription - " + subscriptionId;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
}
}
use of org.wso2.carbon.apimgt.core.workflow.Workflow in project carbon-apimgt by wso2.
the class APIStoreImpl method updateCompositeApi.
/**
* {@inheritDoc}
*/
@Override
public void updateCompositeApi(CompositeAPI.Builder apiBuilder) throws APIManagementException {
apiBuilder.provider(getUsername());
apiBuilder.updatedBy(getUsername());
CompositeAPI originalAPI = getApiDAO().getCompositeAPI(apiBuilder.getId());
apiBuilder.createdTime(originalAPI.getCreatedTime());
// workflow status is an internal property and shouldn't be allowed to update externally
apiBuilder.workflowStatus(originalAPI.getWorkflowStatus());
APIUtils.verifyValidityOfApiUpdate(apiBuilder, originalAPI);
try {
String updatedSwagger = apiDefinitionFromSwagger20.generateSwaggerFromResources(apiBuilder);
InputStream gatewayConfig = getApiDAO().getCompositeAPIGatewayConfig(apiBuilder.getId());
GatewaySourceGenerator gatewaySourceGenerator = getGatewaySourceGenerator();
APIConfigContext apiConfigContext = new APIConfigContext(apiBuilder.build(), config.getGatewayPackageName());
gatewaySourceGenerator.setApiConfigContext(apiConfigContext);
String updatedGatewayConfig = gatewaySourceGenerator.getGatewayConfigFromSwagger(IOUtils.toString(gatewayConfig, StandardCharsets.UTF_8), updatedSwagger);
CompositeAPI api = apiBuilder.build();
if (originalAPI.getContext() != null && !originalAPI.getContext().equals(apiBuilder.getContext())) {
if (isContextExist(api.getContext())) {
throw new APIManagementException("Context already Exist", ExceptionCodes.API_ALREADY_EXISTS);
}
}
// publishing config to gateway
gateway.addCompositeAPI(api);
getApiDAO().updateApiDefinition(api.getId(), updatedSwagger, api.getUpdatedBy());
getApiDAO().updateCompositeAPIGatewayConfig(api.getId(), new ByteArrayInputStream(updatedGatewayConfig.getBytes(StandardCharsets.UTF_8)), api.getUpdatedBy());
if (log.isDebugEnabled()) {
log.debug("API " + api.getName() + "-" + api.getVersion() + " was updated successfully.");
}
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while updating the API - " + apiBuilder.getName();
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
} catch (IOException e) {
String errorMsg = "Error occurred while reading gateway configuration the API - " + apiBuilder.getName();
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, ExceptionCodes.APIMGT_DAO_EXCEPTION);
}
}
Aggregations