Search in sources :

Example 16 with WorkflowExecutor

use of org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor in project carbon-apimgt by wso2.

the class APIProviderImplTest method prepareForChangeLifeCycleStatus.

/**
 * This method can be used when invoking changeLifeCycleStatus()
 */
private void prepareForChangeLifeCycleStatus(APIProviderImplWrapper apiProvider, ApiMgtDAO apimgtDAO, APIIdentifier apiId, GenericArtifact apiArtifact) throws GovernanceException, APIManagementException, FaultGatewaysException, WorkflowException, XMLStreamException {
    Mockito.when(APIUtil.getAPIArtifact(apiId, apiProvider.registry)).thenReturn(apiArtifact);
    Mockito.when(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER)).thenReturn("admin");
    Mockito.when(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME)).thenReturn("API1");
    Mockito.when(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION)).thenReturn("1.0.0");
    Mockito.when(apiArtifact.getLifecycleState()).thenReturn("CREATED");
    Mockito.when(apimgtDAO.getAPIID(apiUUID)).thenReturn(1);
    // Workflow has not started, this will trigger the executor
    WorkflowDTO wfDTO1 = Mockito.mock(WorkflowDTO.class);
    Mockito.when(wfDTO1.getStatus()).thenReturn(null);
    WorkflowDTO wfDTO2 = Mockito.mock(WorkflowDTO.class);
    Mockito.when(wfDTO2.getStatus()).thenReturn(WorkflowStatus.APPROVED);
    Mockito.when(apimgtDAO.retrieveWorkflowFromInternalReference("1", WorkflowConstants.WF_TYPE_AM_API_STATE)).thenReturn(wfDTO1, wfDTO2);
    ServiceReferenceHolder sh = TestUtils.getServiceReferenceHolder();
    APIManagerConfigurationService amConfigService = Mockito.mock(APIManagerConfigurationService.class);
    APIManagerConfiguration amConfig = Mockito.mock(APIManagerConfiguration.class);
    PowerMockito.when(sh.getAPIManagerConfigurationService()).thenReturn(amConfigService);
    PowerMockito.when(amConfigService.getAPIManagerConfiguration()).thenReturn(amConfig);
    WorkflowProperties workflowProperties = Mockito.mock(WorkflowProperties.class);
    Mockito.when(workflowProperties.getWorkflowCallbackAPI()).thenReturn("");
    Mockito.when(amConfig.getWorkflowProperties()).thenReturn(workflowProperties);
    WorkflowExecutorFactory wfe = PowerMockito.mock(WorkflowExecutorFactory.class);
    Mockito.when(WorkflowExecutorFactory.getInstance()).thenReturn(wfe);
    WorkflowExecutor apiStateWFExecutor = new APIStateChangeSimpleWorkflowExecutor();
    Mockito.when(wfe.getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_API_STATE)).thenReturn(apiStateWFExecutor);
    Mockito.when(APIUtil.isAnalyticsEnabled()).thenReturn(false);
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) WorkflowDTO(org.wso2.carbon.apimgt.impl.dto.WorkflowDTO) WorkflowExecutorFactory(org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutorFactory) APIStateChangeSimpleWorkflowExecutor(org.wso2.carbon.apimgt.impl.workflow.APIStateChangeSimpleWorkflowExecutor) WorkflowExecutor(org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor) APIStateChangeSimpleWorkflowExecutor(org.wso2.carbon.apimgt.impl.workflow.APIStateChangeSimpleWorkflowExecutor) WorkflowProperties(org.wso2.carbon.apimgt.impl.dto.WorkflowProperties)

Example 17 with WorkflowExecutor

use of org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor in project carbon-apimgt by wso2.

the class APIProviderImplTest method testDeleteWorkflowTask.

@Test
public void testDeleteWorkflowTask() throws APIManagementException, WorkflowException {
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    Mockito.when(apimgtDAO.getAPIID(apiUUID)).thenReturn(1111);
    WorkflowExecutorFactory wfe = PowerMockito.mock(WorkflowExecutorFactory.class);
    Mockito.when(WorkflowExecutorFactory.getInstance()).thenReturn(wfe);
    WorkflowExecutor apiStateChangeWFExecutor = Mockito.mock(WorkflowExecutor.class);
    Mockito.when(wfe.getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_API_STATE)).thenReturn(apiStateChangeWFExecutor);
    WorkflowDTO workflowDTO = Mockito.mock(WorkflowDTO.class);
    Mockito.when(apimgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(1111), WorkflowConstants.WF_TYPE_AM_API_STATE)).thenReturn(workflowDTO);
    APIIdentifier identifier = new APIIdentifier("admin", "API1", "1.0.0", apiUUID);
    apiProvider.deleteWorkflowTask(identifier);
    Mockito.verify(apimgtDAO, Mockito.times(1)).getAPIID(apiUUID);
}
Also used : WorkflowDTO(org.wso2.carbon.apimgt.impl.dto.WorkflowDTO) WorkflowExecutorFactory(org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutorFactory) APIStateChangeSimpleWorkflowExecutor(org.wso2.carbon.apimgt.impl.workflow.APIStateChangeSimpleWorkflowExecutor) WorkflowExecutor(org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 18 with WorkflowExecutor

use of org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor in project carbon-apimgt by wso2.

the class APIProviderImpl method setAPIStateWorkflowDTOParameters.

/**
 * Set API or API Product state change workflow parameters
 *
 * @param currentStatus Current state of the API or API Product
 * @param action        LC state change action
 * @param name          Name of the API or API Product
 * @param context       Context of the API or API Product
 * @param apiType       API or API Product
 * @param version       Version of API or API Product
 * @param providerName  Owner of the API or API Product
 * @param apiOrApiProductId Unique ID of the API or API Product
 * @param uuid              Unique UUID of the API or API Product
 * @param gatewayVendor     Gateway Vendor
 * @param workflowType      Workflow Type
 * @param apiStateWFExecutor    WorkflowExecutor
 * @return APIStateWorkflowDTO Object
 */
private APIStateWorkflowDTO setAPIStateWorkflowDTOParameters(String currentStatus, String action, String name, String context, String apiType, String version, String providerName, int apiOrApiProductId, String uuid, String gatewayVendor, String workflowType, WorkflowExecutor apiStateWFExecutor) {
    WorkflowProperties workflowProperties = getAPIManagerConfiguration().getWorkflowProperties();
    APIStateWorkflowDTO stateWorkflowDTO = new APIStateWorkflowDTO();
    stateWorkflowDTO.setApiCurrentState(currentStatus);
    stateWorkflowDTO.setApiLCAction(action);
    stateWorkflowDTO.setApiName(name);
    stateWorkflowDTO.setApiContext(context);
    stateWorkflowDTO.setApiType(apiType);
    stateWorkflowDTO.setApiVersion(version);
    stateWorkflowDTO.setApiProvider(providerName);
    stateWorkflowDTO.setGatewayVendor(gatewayVendor);
    stateWorkflowDTO.setCallbackUrl(workflowProperties.getWorkflowCallbackAPI());
    stateWorkflowDTO.setExternalWorkflowReference(apiStateWFExecutor.generateUUID());
    stateWorkflowDTO.setTenantId(tenantId);
    stateWorkflowDTO.setTenantDomain(this.tenantDomain);
    stateWorkflowDTO.setWorkflowType(workflowType);
    stateWorkflowDTO.setStatus(WorkflowStatus.CREATED);
    stateWorkflowDTO.setCreatedTime(System.currentTimeMillis());
    stateWorkflowDTO.setWorkflowReference(Integer.toString(apiOrApiProductId));
    stateWorkflowDTO.setInvoker(this.username);
    stateWorkflowDTO.setApiUUID(uuid);
    String workflowDescription = "Pending lifecycle state change action: " + action;
    stateWorkflowDTO.setWorkflowDescription(workflowDescription);
    return stateWorkflowDTO;
}
Also used : APIStateWorkflowDTO(org.wso2.carbon.apimgt.impl.workflow.APIStateWorkflowDTO) WorkflowProperties(org.wso2.carbon.apimgt.impl.dto.WorkflowProperties)

Example 19 with WorkflowExecutor

use of org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor in project carbon-apimgt by wso2.

the class APIProviderImpl method cleanUpPendingSubscriptionCreationProcessesByAPI.

/**
 * Clean-up pending subscriptions of a given API
 *
 * @param uuid API uuid
 * @throws APIManagementException
 */
private void cleanUpPendingSubscriptionCreationProcessesByAPI(String uuid) throws APIManagementException {
    WorkflowExecutor createSubscriptionWFExecutor = getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION);
    Set<Integer> pendingSubscriptions = apiMgtDAO.getPendingSubscriptionsByAPIId(uuid);
    String workflowExtRef = null;
    for (int subscription : pendingSubscriptions) {
        try {
            workflowExtRef = apiMgtDAO.getExternalWorkflowReferenceForSubscription(subscription);
            createSubscriptionWFExecutor.cleanUpPendingTask(workflowExtRef);
        } catch (APIManagementException ex) {
            // failed clean-up processes are ignored to prevent failures in API state change flow
            log.warn("Failed to retrieve external workflow reference for subscription for subscription ID: " + subscription);
        } catch (WorkflowException ex) {
            // failed clean-up processes are ignored to prevent failures in API state change flow
            log.warn("Failed to clean-up pending subscription approval task for subscription ID: " + subscription);
        }
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) WorkflowException(org.wso2.carbon.apimgt.impl.workflow.WorkflowException) WorkflowExecutor(org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor)

Example 20 with WorkflowExecutor

use of org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor in project carbon-apimgt by wso2.

the class UserPostSelfRegistrationHandler method executeUserRegistrationWorkflow.

/**
 * This method adds new role to the existing user roles
 * @param tenantDomain tenant domain extracted from the event
 * @param userName username extracted from the event
 * @throws org.wso2.carbon.identity.recovery.IdentityRecoveryServerException when unable to retrieve
 * userStoreManager instance
 */
private void executeUserRegistrationWorkflow(String tenantDomain, String userName) throws org.wso2.carbon.identity.recovery.IdentityRecoveryServerException {
    try {
        // Realm service is used for user management tasks
        RealmService realmService = ServiceReferenceHolder.getInstance().getRealmService();
        UserStoreManager userStoreManager;
        try {
            userStoreManager = realmService.getTenantUserRealm(IdentityTenantUtil.getTenantId(tenantDomain)).getUserStoreManager();
        } catch (UserStoreException e) {
            throw Utils.handleServerException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_UNEXPECTED, userName, e);
        }
        // Start a tenant flow
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
        carbonContext.setTenantId(IdentityTenantUtil.getTenantId(tenantDomain));
        carbonContext.setTenantDomain(tenantDomain);
        if (userStoreManager.isExistingUser(userName)) {
            List<String> roleList = asList(userStoreManager.getRoleListOfUser(userName));
            // User should have selfSignup role. Checking whether the user is in the new role
            if (roleList.contains(SELF_SIGNUP_ROLE) && !roleList.contains(SUBSCRIBER_ROLE)) {
                WorkflowExecutor userSignUpWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_USER_SIGNUP);
                // initiate a new signup workflow
                WorkflowDTO signUpWFDto = new WorkflowDTO();
                signUpWFDto.setWorkflowReference(userName);
                signUpWFDto.setStatus(WorkflowStatus.CREATED);
                signUpWFDto.setCreatedTime(System.currentTimeMillis());
                signUpWFDto.setTenantDomain(tenantDomain);
                signUpWFDto.setTenantId(IdentityTenantUtil.getTenantId(tenantDomain));
                signUpWFDto.setExternalWorkflowReference(userSignUpWFExecutor.generateUUID());
                signUpWFDto.setWorkflowType(WorkflowConstants.WF_TYPE_AM_USER_SIGNUP);
                signUpWFDto.setCallbackUrl(userSignUpWFExecutor.getCallbackURL());
                userSignUpWFExecutor.execute(signUpWFDto);
            }
        }
    } catch (UserStoreException | WorkflowException e) {
        throw Utils.handleServerException(IdentityRecoveryConstants.ErrorMessages.ERROR_CODE_UNEXPECTED, userName, e);
    } finally {
        Utils.clearArbitraryProperties();
        PrivilegedCarbonContext.endTenantFlow();
    }
}
Also used : WorkflowDTO(org.wso2.carbon.apimgt.impl.dto.WorkflowDTO) RealmService(org.wso2.carbon.user.core.service.RealmService) WorkflowException(org.wso2.carbon.apimgt.impl.workflow.WorkflowException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) WorkflowExecutor(org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor) UserStoreManager(org.wso2.carbon.user.api.UserStoreManager)

Aggregations

WorkflowExecutor (org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor)17 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)15 WorkflowExecutor (org.wso2.carbon.apimgt.core.api.WorkflowExecutor)13 WorkflowException (org.wso2.carbon.apimgt.impl.workflow.WorkflowException)13 WorkflowDTO (org.wso2.carbon.apimgt.impl.dto.WorkflowDTO)12 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)10 JSONObject (org.json.simple.JSONObject)9 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)8 SubscriptionWorkflowDTO (org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO)7 HashMap (java.util.HashMap)6 ApplicationRegistrationWorkflowDTO (org.wso2.carbon.apimgt.impl.dto.ApplicationRegistrationWorkflowDTO)6 ApplicationWorkflowDTO (org.wso2.carbon.apimgt.impl.dto.ApplicationWorkflowDTO)6 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)5 WorkflowProperties (org.wso2.carbon.apimgt.impl.dto.WorkflowProperties)5 ParseException (org.json.simple.parser.ParseException)4 WorkflowResponse (org.wso2.carbon.apimgt.api.WorkflowResponse)4 Application (org.wso2.carbon.apimgt.core.models.Application)4 Iterator (java.util.Iterator)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3