use of org.wso2.carbon.apimgt.core.api.WorkflowResponse in project carbon-apimgt by wso2.
the class MiscMappingUtil method fromWorkflowResponseToDTO.
/**
* Map WorkflowResponse to WorkflowResponseDTO
*
* @param response WorkflowResponse object
* @return WorkflowResponseDTO mapped WorkflowResponseDTO
*/
public static WorkflowResponseDTO fromWorkflowResponseToDTO(WorkflowResponse response) {
WorkflowResponseDTO responseDTO = new WorkflowResponseDTO();
responseDTO.setWorkflowStatus(WorkflowStatusEnum.valueOf(response.getWorkflowStatus().toString()));
responseDTO.setJsonPayload(response.getJSONPayload());
return responseDTO;
}
use of org.wso2.carbon.apimgt.core.api.WorkflowResponse in project carbon-apimgt by wso2.
the class ApplicationsApiServiceImplTestCase method testApplicationsApplicationIdPutErrorCase.
@Test
public void testApplicationsApplicationIdPutErrorCase() throws APIManagementException, NotFoundException {
TestUtil.printTestMethodName();
String applicationId = UUID.randomUUID().toString();
String accessToken = UUID.randomUUID().toString();
String clientID = UUID.randomUUID().toString();
String clientSecret = UUID.randomUUID().toString();
ApplicationsApiServiceImpl applicationsApiService = new ApplicationsApiServiceImpl();
APIStore apiStore = Mockito.mock(APIStoreImpl.class);
PowerMockito.mockStatic(RestApiUtil.class);
PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
Request request = getRequest();
PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
ApplicationTokenDTO applicationTokenDTO = new ApplicationTokenDTO();
applicationTokenDTO.setAccessToken(accessToken);
applicationTokenDTO.setTokenScopes("SCOPE1");
applicationTokenDTO.setValidityTime((long) 100000);
List<String> grantTypes = new ArrayList<>();
grantTypes.add("password");
grantTypes.add("jwt");
ApplicationKeysDTO applicationKeysDTO = new ApplicationKeysDTO();
applicationKeysDTO.setConsumerKey(clientID);
applicationKeysDTO.setConsumerSecret(clientSecret);
applicationKeysDTO.setKeyType(ApplicationKeysDTO.KeyTypeEnum.PRODUCTION);
applicationKeysDTO.setCallbackUrl(null);
applicationKeysDTO.setSupportedGrantTypes(grantTypes);
List<ApplicationKeysDTO> applicationKeysDTOList = new ArrayList<>();
applicationKeysDTOList.add(applicationKeysDTO);
ApplicationDTO applicationDTO = new ApplicationDTO();
applicationDTO.setApplicationId(applicationId);
applicationDTO.setDescription("sample application");
applicationDTO.setName("app1");
applicationDTO.setSubscriber("subscriber");
applicationDTO.setPermission("permission");
applicationDTO.setLifeCycleStatus("APPROVED");
applicationDTO.setThrottlingTier("UNLIMITED");
applicationDTO.setToken(applicationTokenDTO);
applicationDTO.setKeys(applicationKeysDTOList);
WorkflowResponse workflowResponse = new GeneralWorkflowResponse();
workflowResponse.setWorkflowStatus(WorkflowStatus.APPROVED);
Mockito.when(apiStore.getApplication(applicationId, USER)).thenReturn(getSampleApplication(applicationId));
Mockito.doThrow(new APIManagementException("Error Occurred", ExceptionCodes.INTERNAL_ERROR)).when(apiStore).updateApplication(applicationId, getSampleApplication(applicationId));
Response response = applicationsApiService.applicationsApplicationIdPut(applicationId, applicationDTO, null, null, request);
Assert.assertEquals(500, response.getStatus());
}
use of org.wso2.carbon.apimgt.core.api.WorkflowResponse in project carbon-apimgt by wso2.
the class ApplicationsApiServiceImplTestCase method testApplicationsApplicationIdDelete.
@Test
public void testApplicationsApplicationIdDelete() throws APIManagementException, NotFoundException {
TestUtil.printTestMethodName();
String applicationId = UUID.randomUUID().toString();
ApplicationsApiServiceImpl applicationsApiService = new ApplicationsApiServiceImpl();
APIStore apiStore = Mockito.mock(APIStoreImpl.class);
PowerMockito.mockStatic(RestApiUtil.class);
PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
Request request = getRequest();
PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
WorkflowResponse workflowResponse = new GeneralWorkflowResponse();
workflowResponse.setWorkflowStatus(WorkflowStatus.APPROVED);
Mockito.when(apiStore.deleteApplication(applicationId)).thenReturn(workflowResponse);
Response response = applicationsApiService.applicationsApplicationIdDelete(applicationId, null, null, request);
Assert.assertEquals(200, response.getStatus());
}
use of org.wso2.carbon.apimgt.core.api.WorkflowResponse 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.api.WorkflowResponse in project carbon-apimgt by wso2.
the class APIStoreImpl method deleteApplication.
/**
* @see APIStore#deleteApplication(String)
*/
@Override
public WorkflowResponse deleteApplication(String appId) throws APIManagementException {
try {
if (appId == null) {
String message = "Application Id is not provided";
throw new APIManagementException(message, ExceptionCodes.PARAMETER_NOT_PROVIDED);
}
// get app info
Application application = getApplicationDAO().getApplication(appId);
if (application == null) {
String message = "Application cannot be found for id :" + appId;
throw new APIManagementException(message, ExceptionCodes.APPLICATION_NOT_FOUND);
}
// delete application creation pending tasks
cleanupPendingTaskForApplicationDeletion(application);
WorkflowExecutor removeApplicationWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_APPLICATION_DELETION);
ApplicationDeletionWorkflow workflow = new ApplicationDeletionWorkflow(getApplicationDAO(), getWorkflowDAO(), getApiGateway());
workflow.setApplication(application);
workflow.setWorkflowType(APIMgtConstants.WorkflowConstants.WF_TYPE_AM_APPLICATION_DELETION);
workflow.setWorkflowReference(application.getId());
workflow.setExternalWorkflowReference(UUID.randomUUID().toString());
workflow.setCreatedTime(LocalDateTime.now());
String workflowDescription = "Application [ " + application.getName() + " ] deletion request from - " + application.getName();
workflow.setWorkflowDescription(workflowDescription);
WorkflowResponse response = removeApplicationWFExecutor.execute(workflow);
workflow.setStatus(response.getWorkflowStatus());
if (WorkflowStatus.CREATED != response.getWorkflowStatus()) {
completeWorkflow(removeApplicationWFExecutor, workflow);
} else {
// add entry to workflow table if it is only in pending state
addWorkflowEntries(workflow);
}
return response;
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while deleting the application - " + appId;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
}
}
Aggregations